UNPKG

@vime/core

Version:

Customizable, extensible, accessible and framework agnostic media player.

34 lines (33 loc) 1.07 kB
/* eslint-disable func-names */ import { getElement } from '@stencil/core'; import { createStencilHook } from '../../../utils/stencil'; import { firePlayerEvent } from './PlayerEvents'; import { initialState } from './PlayerProps'; export function withPlayerEvents(player) { const el = getElement(player); const cache = new Map(); function initCache() { Object.keys(initialState).forEach(prop => { cache.set(prop, player[prop]); }); } createStencilHook(player, () => { initCache(); }, () => { cache.clear(); }); const { componentDidRender } = player; player.componentDidRender = function () { componentDidRender === null || componentDidRender === void 0 ? void 0 : componentDidRender(); const props = Array.from(cache.keys()); for (let i = 0; i < props.length; i += 1) { const prop = props[i]; const oldValue = cache.get(prop); const newValue = player[prop]; if (oldValue !== newValue) { firePlayerEvent(el, prop, newValue, oldValue); cache.set(prop, newValue); } } }; }