UNPKG

exome

Version:

State manager for deeply nested states

8 lines (7 loc) 1.66 kB
{ "version": 3, "sources": ["../src/lit.ts"], "sourcesContent": ["/**\n * @module exome/lit\n */\nimport { type Exome, subscribe } from \"exome\";\nimport type { ReactiveController, ReactiveControllerHost } from \"lit\";\n\n/**\n * Subscribes to store instance update events and trigger updates to component accordingly.\n *\n * @example:\n * ```ts\n * import { StoreController } from \"exome/lit\"\n * import { counterStore } from \"./counter.store.ts\"\n *\n * @customElement(\"counter\")\n * class CounterComponent extends LitElement {\n * private counter = new StoreController(this, counterStore)\n *\n * render() {\n * const { count, increment } = this.counter.store\n *\n * return html`\n * <button @click=${increment}>${count}</button>\n * `\n * }\n * }\n * ```\n */\nexport class StoreController<T extends Exome> implements ReactiveController {\n\tprivate unsubscribe: undefined | (() => void);\n\n\tconstructor(\n\t\tprivate host: ReactiveControllerHost,\n\t\tpublic store: T,\n\t) {\n\t\thost.addController(this);\n\t}\n\n\thostConnected() {\n\t\tthis.unsubscribe = subscribe(this.store, () => {\n\t\t\tthis.host.requestUpdate();\n\t\t});\n\t}\n\n\thostDisconnected() {\n\t\tthis.unsubscribe?.();\n\t}\n}\n"], "mappings": ";AAGA,SAAqB,aAAAA,SAAiB;AAyB/B,IAAMC,IAAN,MAAqE;AAAA,EAG3E,YACSC,GACDC,GACN;AAFO,gBAAAD;AACD,iBAAAC;AAEP,IAAAD,EAAK,cAAc,IAAI;AAAA,EACxB;AAAA,EAEA,gBAAgB;AACf,SAAK,cAAcF,EAAU,KAAK,OAAO,MAAM;AAC9C,WAAK,KAAK,cAAc;AAAA,IACzB,CAAC;AAAA,EACF;AAAA,EAEA,mBAAmB;AA5CpB,QAAAI;AA6CE,KAAAA,IAAA,KAAK,gBAAL,QAAAA,EAAA;AAAA,EACD;AACD;", "names": ["subscribe", "StoreController", "host", "store", "_a"] }