{"version":3,"file":"activated-watch-CTx2V_bf.js","sources":["../../Vue/src/functions/activated-watch.ts"],"sourcesContent":["import { WatchWithFilterOptions } from \"@vueuse/core\";\r\n\r\n/**\r\n * activatedWatch is meant to detect changes in the destination route\r\n * and remove watching variables when the component is no longer active, but still present/cached\r\n * because of the keep-alive feature on the router.\r\n *\r\n * @param varsToWatch Same as first variable passed to the normal watch command\r\n * @param fn Same as the 2nd variable passed to the normal watch command\r\n */\r\nexport function activatedWatch(\r\n varsToWatch: any,\r\n fn: () => void,\r\n options?: WatchWithFilterOptions,\r\n) {\r\n const route = useRoute();\r\n const router = useRouter();\r\n const debug = useDebug();\r\n let activated = true;\r\n\r\n const initialPath = route.path;\r\n debug.log(\"activatedWatch: intial path\", initialPath);\r\n\r\n //when path is reactivated, go ahead and check for var changes\r\n // since the last time and rerun fn if necessary\r\n onActivated(async () => {\r\n debug.log(\"activatedWatch: path activated\");\r\n activated = true;\r\n const activatedValues = JSON.stringify(varsToWatch());\r\n if (lastActivatedValues !== activatedValues) {\r\n debug.log(\"activatedWatch: found changes, rerunning fn\", activatedValues);\r\n await fn();\r\n }\r\n resume();\r\n });\r\n\r\n let lastActivatedValues: string = \"\";\r\n\r\n //capture values before router changes\r\n router.beforeEach((from, to, next) => {\r\n if (activated) {\r\n lastActivatedValues = JSON.stringify(varsToWatch());\r\n }\r\n next();\r\n });\r\n\r\n //stop watching for changes since path is no longer active\r\n onDeactivated(() => {\r\n activated = false;\r\n pause();\r\n debug.log(\"activatedWatch: path deactivated\", lastActivatedValues);\r\n });\r\n\r\n //while path is active, watch for changes in vars and run fn if there are changes\r\n const { pause, resume } = watchPausable(\r\n varsToWatch,\r\n async () => {\r\n await nextTick();\r\n\r\n if (route.path !== initialPath) {\r\n return;\r\n }\r\n\r\n if (!activated) {\r\n return;\r\n }\r\n\r\n debug.log(\"activatedWatch: running watch command\");\r\n\r\n fn();\r\n },\r\n options,\r\n );\r\n}\r\n"],"names":["activatedWatch","varsToWatch","fn","options","route","useRoute","router","useRouter","debug","useDebug","activated","initialPath","onActivated","activatedValues","lastActivatedValues","resume","from","to","next","onDeactivated","pause","watchPausable","nextTick"],"mappings":"uHAUgB,SAAAA,EACdC,EACAC,EACAC,EACA,CACA,MAAMC,EAAQC,IACRC,EAASC,IACTC,EAAQC,IACd,IAAIC,EAAY,GAEhB,MAAMC,EAAcP,EAAM,KACpBI,EAAA,IAAI,8BAA+BG,CAAW,EAIpDC,EAAY,SAAY,CACtBJ,EAAM,IAAI,gCAAgC,EAC9BE,EAAA,GACZ,MAAMG,EAAkB,KAAK,UAAUZ,EAAa,CAAA,EAChDa,IAAwBD,IACpBL,EAAA,IAAI,8CAA+CK,CAAe,EACxE,MAAMX,EAAG,GAEJa,GAAA,CACR,EAED,IAAID,EAA8B,GAGlCR,EAAO,WAAW,CAACU,EAAMC,EAAIC,IAAS,CAChCR,IACoBI,EAAA,KAAK,UAAUb,EAAa,CAAA,GAE/CiB,GAAA,CACN,EAGDC,EAAc,IAAM,CACNT,EAAA,GACNU,IACAZ,EAAA,IAAI,mCAAoCM,CAAmB,CAAA,CAClE,EAGK,KAAA,CAAE,MAAAM,EAAO,OAAAL,CAAA,EAAWM,EACxBpB,EACA,SAAY,CACV,MAAMqB,EAAS,EAEXlB,EAAM,OAASO,GAIdD,IAILF,EAAM,IAAI,uCAAuC,EAE9CN,IACL,EACAC,CAAA,CAEJ"}