|
| 1 | +/** |
| 2 | + * The plugin that synchronizes the executor state with a browser storage item. |
| 3 | + * |
| 4 | + * ```ts |
| 5 | + * import syncBrowserStorage from 'react-executor/plugin/syncBrowserStorage'; |
| 6 | + * |
| 7 | + * const executor = useExecutor('test', 42, [syncBrowserStorage()]); |
| 8 | + * ``` |
| 9 | + * |
| 10 | + * @module plugin/syncBrowserStorage |
| 11 | + */ |
| 12 | + |
| 13 | +import type { Executor, ExecutorPlugin, ExecutorState, Serializer } from '../types.js'; |
| 14 | +import { emptyObject } from '../utils.js'; |
| 15 | +import syncExternalStore, { ExternalStore } from './syncExternalStore.js'; |
| 16 | +import { PubSub } from 'parallel-universe'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Options of the {@link syncBrowserStorage} plugin. |
| 20 | + */ |
| 21 | +export interface SyncBrowserStorageOptions { |
| 22 | + /** |
| 23 | + * The storage where executor state is persisted. |
| 24 | + * |
| 25 | + * @default "local" |
| 26 | + */ |
| 27 | + storageType?: 'local' | 'session'; |
| 28 | + |
| 29 | + /** |
| 30 | + * A storage item key, or a callback that returns a storage item key. |
| 31 | + * |
| 32 | + * By default, an {@link react-executor!ExecutorManagerOptions.keyIdGenerator executor key ID} is used. |
| 33 | + */ |
| 34 | + storageKey?: string | ((executor: Executor) => string); |
| 35 | + |
| 36 | + /** |
| 37 | + * The storage item value serializer. |
| 38 | + * |
| 39 | + * @default JSON |
| 40 | + */ |
| 41 | + serializer?: Serializer; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Synchronizes the executor state with a browser storage item. No-op in a non-browser environment. |
| 46 | + * |
| 47 | + * If an executor is detached, then the corresponding item is removed from the storage. |
| 48 | + * |
| 49 | + * @param options Storage options. |
| 50 | + */ |
| 51 | +export default function syncBrowserStorage(options: SyncBrowserStorageOptions = emptyObject): ExecutorPlugin { |
| 52 | + const { storageType = 'local', storageKey, serializer = JSON } = options; |
| 53 | + |
| 54 | + return executor => { |
| 55 | + const key = |
| 56 | + storageKey === undefined |
| 57 | + ? executor.manager.keyIdGenerator(executor.key) |
| 58 | + : typeof storageKey === 'function' |
| 59 | + ? storageKey(executor) |
| 60 | + : storageKey; |
| 61 | + |
| 62 | + if (typeof key !== 'string') { |
| 63 | + throw new Error('Cannot guess a storage key for an executor, the "storageKey" option is required'); |
| 64 | + } |
| 65 | + |
| 66 | + if (typeof window !== 'undefined') { |
| 67 | + syncExternalStore(new BrowserStore(storageType, key, serializer))(executor); |
| 68 | + } |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +const pubSub = new PubSub<StorageEvent>(); |
| 73 | + |
| 74 | +function handleStorage(event: StorageEvent): void { |
| 75 | + pubSub.publish(event); |
| 76 | +} |
| 77 | + |
| 78 | +class BrowserStore implements ExternalStore<ExecutorState> { |
| 79 | + readonly storageArea; |
| 80 | + |
| 81 | + constructor( |
| 82 | + readonly storageType: 'local' | 'session', |
| 83 | + readonly key: string, |
| 84 | + readonly serializer: Serializer |
| 85 | + ) { |
| 86 | + this.storageArea = this.storageType === 'session' ? sessionStorage : localStorage; |
| 87 | + } |
| 88 | + |
| 89 | + get(): ExecutorState | null { |
| 90 | + const json = this.storageArea.getItem(this.key); |
| 91 | + |
| 92 | + if (json === null) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + try { |
| 96 | + return this.serializer.parse(json); |
| 97 | + } catch (e) { |
| 98 | + setTimeout(() => { |
| 99 | + throw e; |
| 100 | + }); |
| 101 | + return null; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + set(state: ExecutorState): void { |
| 106 | + this.storageArea.setItem(this.key, this.serializer.stringify(state)); |
| 107 | + } |
| 108 | + |
| 109 | + delete(): void { |
| 110 | + this.storageArea.removeItem(this.key); |
| 111 | + } |
| 112 | + |
| 113 | + subscribe(listener: (state: ExecutorState | null) => void): () => void { |
| 114 | + if (pubSub.listenerCount === 0) { |
| 115 | + window.addEventListener('storage', handleStorage); |
| 116 | + } |
| 117 | + |
| 118 | + const unsubscribe = pubSub.subscribe(event => { |
| 119 | + if (event.storageArea === this.storageArea && event.key === this.key) { |
| 120 | + listener(this.get()); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + return () => { |
| 125 | + unsubscribe(); |
| 126 | + |
| 127 | + if (pubSub.listenerCount === 0) { |
| 128 | + window.removeEventListener('storage', handleStorage); |
| 129 | + } |
| 130 | + }; |
| 131 | + } |
| 132 | +} |
0 commit comments