@nuxtjs/ionic
Version:
Ionic integration for Nuxt
83 lines (82 loc) • 2.55 kB
JavaScript
import { onIonViewDidEnter, onIonViewDidLeave } from "@ionic/vue";
import { onBeforeUnmount } from "vue";
import { useRoute, useRouter } from "vue-router";
import { injectHead } from "#imports";
const headMap = /* @__PURE__ */ new Map();
let beforeHook;
let afterHook;
let currPath;
let prevPath;
export function useHead(obj) {
const currentPath = useRoute().path;
const activeHead = injectHead();
const { currentRoute } = useRouter();
const router = useRouter();
let hasReallyLeft = false;
let innerObj = obj;
const __returned = {
dispose() {
const headArr = [...headMap.get(currentPath)];
const headArrIndex = headArr.findIndex((headVal) => headVal[0] === innerObj);
if (headArrIndex === -1) return;
const headToDispose = headArr[headArrIndex][1];
headToDispose?.dispose();
headArr.splice(headArrIndex, 1);
headMap.set(currentPath, headArr);
},
patch(newObj) {
const headArr = [...headMap.get(currentPath)];
const headArrIndex = headArr.findIndex((headVal) => headVal[0] === innerObj);
if (headArrIndex === -1) return;
const [, headToPatch] = headArr[headArrIndex];
innerObj = newObj;
headToPatch?.patch(innerObj);
headArr.splice(headArrIndex, 1, [innerObj, headToPatch]);
headMap.set(currentPath, headArr);
}
};
if (!headMap.has(currentPath)) {
const headObj = activeHead?.push(obj);
headMap.set(currentPath, [[obj, headObj]]);
} else {
const headObj = activeHead?.push(obj);
const metaArr = headMap.get(currentPath) || [];
headMap.set(currentPath, [...metaArr, [obj, headObj]]);
}
onBeforeUnmount(__returned.dispose);
if (!beforeHook) {
beforeHook = router.beforeEach(() => {
prevPath = currentRoute.value.path;
});
}
if (!afterHook) {
afterHook = router.afterEach(() => {
currPath = currentRoute.value.path;
});
}
onIonViewDidLeave(() => {
let headArr = headMap.get(prevPath);
if (headArr) {
headArr = headArr.map(([obj2, head]) => {
head?.dispose();
return [obj2, head];
});
headMap.set(prevPath, headArr);
}
hasReallyLeft = true;
});
onIonViewDidEnter(() => {
if (hasReallyLeft) {
let headArr = headMap.get(currPath);
if (headArr) {
headArr = headArr.map(([obj2, head]) => {
head?.dispose();
const newHead = activeHead?.push(obj2);
return [obj2, newHead];
});
headMap.set(currPath, headArr);
}
}
});
return __returned;
}