@vime/core
Version:
Customizable, extensible, accessible and framework agnostic media player.
27 lines (26 loc) • 749 B
JavaScript
import { isUndefined } from './unit';
export function wrapStencilHook(component, lifecycle, hook) {
const prevHook = component[lifecycle];
component[lifecycle] = function () {
hook();
return prevHook ? prevHook.call(component) : undefined;
};
}
export function createStencilHook(component, onConnect, onDisconnect) {
let hasLoaded = false;
if (!isUndefined(onConnect)) {
wrapStencilHook(component, 'componentWillLoad', () => {
onConnect();
hasLoaded = true;
});
wrapStencilHook(component, 'connectedCallback', () => {
if (hasLoaded)
onConnect();
});
}
if (!isUndefined(onDisconnect)) {
wrapStencilHook(component, 'disconnectedCallback', () => {
onDisconnect();
});
}
}