function injectHook(url, type = '') {
const hookScript = document.createElement("script");
if (type !== '') hookScript.type = "module";
hookScript.src = url;
hookScript.onload = function () {
this.remove();
};
(document.head || document.body || document.documentElement).appendChild(hookScript);
}
// injectHook(chrome.extension.getURL('/lib/emoji.js'));
// chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
// if (request.message === 'TabUpdated' && document.location.href.includes('https://www.facebook.com/stories')) {
// // injectHook(chrome.extension.getURL(`/lib/story.js?v=${getRandom(1, 100)}`), 'module');
// // injectHook(chrome.extension.getURL(`/lib/story.js`), 'module');
// // chrome.tabs.executeScript(null, {file: 'lib/story.js'});
// }
// })
function getRandom(min, max) {
return Math.random() * (max - min) + min;
}
import { currentInstance, setCurrentInstance } from "./index";
import { pauseTracking, resumeTracking } from "./effect";
import { isArray } from "./utils";
export const LifecycleHooks = {
CREATED: "c",
MOUNTED: "m",
UPDATED: "u"
};
export function onCreate(fn, app = currentInstance) {
injectHook(LifecycleHooks.CREATED, fn, app);
}
export function onMount(fn, app = currentInstance) {
injectHook(LifecycleHooks.MOUNTED, fn, app);
}
export function onUpdate(fn, app = currentInstance) {
injectHook(LifecycleHooks.UPDATED, fn, app);
}
export function injectHook(type, hook, target) {
if (target) {
(target[type] || (target[type] = [])).push((...args) => {
pauseTracking();
setCurrentInstance(target);
const res = hook(...args);
setCurrentInstance(null);
resumeTracking();
return res;
});
}
}
export function callHook(target, type, ...args) {
if (target && isArray(target[type])) {
target[type].forEach(hook => {
hook(...args);
});
}
}
export function callMount(target = currentInstance) {
return callHook(target, LifecycleHooks.MOUNTED);
}
export function callCreate(target = currentInstance) {
return callHook(target, LifecycleHooks.CREATED);
}
export function callUpdate(target = currentInstance) {
return callHook(target, LifecycleHooks.UPDATED);
}
function injectHook(type, hook, target = currentInstance, prepend = false) {
if (target) {
const hooks = target[type] || (target[type] = []);
// cache the error handling wrapper for injected hooks so the same hook
// can be properly deduped by the scheduler. "__weh" stands for "with error
// handling".
const wrappedHook = hook.__weh ||
(hook.__weh = (...args) => {
if (target.isUnmounted) {
return;
}
// disable tracking inside all lifecycle hooks
// since they can potentially be called inside effects.
pauseTracking();
// Set currentInstance during hook invocation.
// This assumes the hook does not synchronously trigger other hooks, which
// can only be false when the user does something really funky.
setCurrentInstance(target);
const res = callWithAsyncErrorHandling(hook, target, type, args);
setCurrentInstance(null);
resetTracking();
return res;
});
if (prepend) {
hooks.unshift(wrappedHook);
}
else {
hooks.push(wrappedHook);
}
return wrappedHook;
}
else {
const apiName = `on${capitalize(ErrorTypeStrings[type].replace(/ hook$/, ''))}`;
warn(`${apiName} is called when there is no active component instance to be ` +
`associated with. ` +
`Lifecycle injection APIs can only be used during execution of setup().` +
( ` If you are using async setup(), make sure to register lifecycle ` +
`hooks before the first await statement.`
));
}
}
const createHook = (lifecycle) => (hook, target = currentInstance) =>
// post-create lifecycle registrations are noops during SSR
!isInSSRComponentSetup && injectHook(lifecycle, hook, target);
const onBeforeMount = createHook("bm" /* BEFORE_MOUNT */);
const onMounted = createHook("m" /* MOUNTED */);
const onBeforeUpdate = createHook("bu" /* BEFORE_UPDATE */);
const onUpdated = createHook("u" /* UPDATED */);
const onBeforeUnmount = createHook("bum" /* BEFORE_UNMOUNT */);
const onUnmounted = createHook("um" /* UNMOUNTED */);
const onRenderTriggered = createHook("rtg" /* RENDER_TRIGGERED */);
const onRenderTracked = createHook("rtc" /* RENDER_TRACKED */);
const onErrorCaptured = (hook, target = currentInstance) => {
injectHook("ec" /* ERROR_CAPTURED */, hook, target);
};
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.