UNPKG

@channel-state/svelte

Version:

Svelte stores for channel-state, providing seamless integration with Svelte applications for cross-context state management.

1 lines 4.11 kB
{"version":3,"sources":["../src/index.ts"],"names":["writable","readable"],"mappings":";;;;;AAiDO,SAAS,gBAAmB,YAAA,EAA+B;AAChE,EAAA,MAAM,cAAcA,cAAA,CAAY,YAAA,CAAa,GAAA,EAAI,EAAG,CAAC,GAAA,KAAQ;AAC3D,IAAA,MAAM,uBAAA,GAA0B,YAAA,CAAa,SAAA,CAAU,CAAC,KAAA,KAAU;AAChE,MAAA,GAAA,CAAI,KAAK,CAAA;AAAA,KACV,CAAA;AAED,IAAA,OAAO,MAAM;AACX,MAAA,uBAAA,EAAwB;AAAA,KAC1B;AAAA,GACD,CAAA;AAED,EAAA,OAAO;AAAA,IACL,WAAW,WAAA,CAAY,SAAA;AAAA,IACvB,GAAA,EAAK,CAAC,KAAA,KAAa;AACjB,MAAA,YAAA,CAAa,IAAI,KAAK,CAAA;AAAA,KACxB;AAAA,IACA,QAAQ,WAAA,CAAY;AAAA,GACtB;AACF;AAmBO,SAAS,iBAAiB,YAAA,EAAqC;AACpE,EAAA,MAAM,MAAA,GAASC,cAAA,CAAsB,YAAA,CAAa,MAAA,EAAQ,CAAC,GAAA,KAAQ;AACjE,IAAA,MAAM,WAAA,GAAc,YAAA,CAAa,eAAA,CAAgB,CAAC,SAAA,KAAc;AAC9D,MAAA,GAAA,CAAI,SAAS,CAAA;AAAA,KACd,CAAA;AACD,IAAA,OAAO,MAAM;AACX,MAAA,WAAA,EAAY;AAAA,KACd;AAAA,GACD,CAAA;AAED,EAAA,OAAO,MAAA;AACT","file":"index.cjs","sourcesContent":["/*\n * Copyright 2025 ronny1020\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ChannelStore, StoreStatus } from '@channel-state/core'\nimport { writable, readable } from 'svelte/store'\n\n/**\n * @module @channel-state/svelte\n * @description Provides a Svelte store for integrating with ChannelStore.\n */\n\n/**\n * A Svelte store that provides access to a ChannelStore's state.\n * The returned store is both readable and writable. Updates to the store\n * will update the underlying ChannelStore, and changes in the ChannelStore\n * (from other tabs/windows or local updates) will update the Svelte store.\n * @template T The type of the state managed by the ChannelStore.\n * @param channelStore The ChannelStore instance to connect to.\n * @returns A Svelte `Writable` store that represents the current state of the ChannelStore.\n * @example\n * ```svelte\n * <script lang=\"ts\">\n * import { useChannelState } from '@channel-state/svelte';\n * import { ChannelStore } from '@channel-state/core';\n *\n * const countStore = new ChannelStore<number>({ name: 'count', initial: 0 });\n * const count = useChannelState(countStore);\n *\n * function increment() {\n * $count++;\n * }\n * </script>\n *\n * <button on:click={increment}>Count: {$count}</button>\n * ```\n */\nexport function useChannelState<T>(channelStore: ChannelStore<T>) {\n const svelteStore = writable<T>(channelStore.get(), (set) => {\n const unsubscribeChannelStore = channelStore.subscribe((value) => {\n set(value)\n })\n\n return () => {\n unsubscribeChannelStore()\n }\n })\n\n return {\n subscribe: svelteStore.subscribe,\n set: (value: T) => {\n channelStore.set(value)\n },\n update: svelteStore.update,\n }\n}\n\n/**\n * A Svelte store that provides access to a ChannelStore's status.\n * @param channelStore The ChannelStore instance to connect to.\n * @returns A Svelte `Readable` store that represents the current status of the ChannelStore.\n * @example\n * ```svelte\n * <script lang=\"ts\">\n * import { useChannelStatus } from '@channel-state/svelte';\n * import { ChannelStore } from '@channel-state/core';\n *\n * const countStore = new ChannelStore<number>({ name: 'count', initial: 0 });\n * const status = useChannelStatus(countStore);\n * </script>\n *\n * <p>Status: {$status}</p>\n * ```\n */\nexport function useChannelStatus(channelStore: ChannelStore<unknown>) {\n const status = readable<StoreStatus>(channelStore.status, (set) => {\n const unsubscribe = channelStore.subscribeStatus((newStatus) => {\n set(newStatus)\n })\n return () => {\n unsubscribe()\n }\n })\n\n return status\n}\n"]}