UNPKG

kr-observable

Version:
53 lines (52 loc) 1.26 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { PureComponent } from 'react'; import { executor } from 'kr-observable'; function noop() { } class Rss { active = false; debug = false; read; deps; name; runId = 1; onStoreChange = noop; run; constructor(rc, debug) { this.name = rc.name; this.debug = debug; this.run = rc; } getSnapshot = () => this.runId; subscriber(changes) { if (this.debug) { console.info(`[${this.name}] will re-render. Changes:`, changes); } this.onStoreChange(); } subscribe = (onStoreChange) => { this.onStoreChange = onStoreChange; return () => { executor.dispose(this); if (this.debug) { console.info(`[${this.name}] was unmounted`); } }; }; } export function observer(Rc, debug = false) { return class extends PureComponent { debug = debug; active = false; deps; runId = 1; run() { return _jsx(Rc, { ...this.props }); } render() { return executor.execute(this); } subscriber() { this.forceUpdate(); } }; }