|
1 |
| -import { reactive, toRaw, watch } from 'vue' |
| 1 | +import { reactive, ref, toRaw, watch } from 'vue' |
| 2 | + |
| 3 | +export type SyncWrite<State extends object> = (state: State, updating?: boolean) => void |
| 4 | + |
| 5 | +export interface Sync { |
| 6 | + enabled?: boolean |
| 7 | + init: <State extends object>(channelKey: string, onUpdate: (data: Partial<State>) => void, state: State, persist?: boolean) => SyncWrite<State> | undefined |
| 8 | +} |
| 9 | + |
| 10 | +interface SlidevSync extends Sync { |
| 11 | + channels: BroadcastChannel[] |
| 12 | + disable: () => void |
| 13 | + listener?: (event: StorageEvent) => void |
| 14 | +} |
| 15 | + |
| 16 | +const slidevSync: SlidevSync = { |
| 17 | + channels: [], |
| 18 | + enabled: true, |
| 19 | + init<State extends object>(channelKey: string, onUpdate: (data: Partial<State>) => void, state: State, persist = false) { |
| 20 | + let stateChannel: BroadcastChannel |
| 21 | + if (!__SLIDEV_HAS_SERVER__ && !persist) { |
| 22 | + stateChannel = new BroadcastChannel(channelKey) |
| 23 | + stateChannel.addEventListener('message', (event: MessageEvent<Partial<State>>) => onUpdate(event.data)) |
| 24 | + this.channels.push(stateChannel) |
| 25 | + } |
| 26 | + else if (!__SLIDEV_HAS_SERVER__ && persist) { |
| 27 | + this.listener = function (event: StorageEvent) { |
| 28 | + if (event && event.key === channelKey && event.newValue) |
| 29 | + onUpdate(JSON.parse(event.newValue) as Partial<State>) |
| 30 | + } |
| 31 | + window.addEventListener('storage', this.listener) |
| 32 | + const serializedState = window.localStorage.getItem(channelKey) |
| 33 | + if (serializedState) |
| 34 | + onUpdate(JSON.parse(serializedState) as Partial<State>) |
| 35 | + } |
| 36 | + return (state: State, updating = false) => { |
| 37 | + if (this.enabled) { |
| 38 | + if (!persist && stateChannel && !updating) |
| 39 | + stateChannel.postMessage(toRaw(state)) |
| 40 | + if (persist && !updating) |
| 41 | + window.localStorage.setItem(channelKey, JSON.stringify(state)) |
| 42 | + } |
| 43 | + } |
| 44 | + }, |
| 45 | + disable() { |
| 46 | + this.enabled = false |
| 47 | + this.channels.forEach(channel => channel.close()) |
| 48 | + if (this.listener) { |
| 49 | + window.removeEventListener('storage', this.listener) |
| 50 | + } |
| 51 | + }, |
| 52 | +} |
| 53 | +const syncInterfaces: Sync[] = reactive([slidevSync]) |
| 54 | +const channels: Map<string, { onUpdate: (data: Partial<object>) => void, persist?: boolean, state: object }> = new Map() |
| 55 | +const syncWrites = ref<Record<string, SyncWrite<object>[]>>({}) |
| 56 | + |
| 57 | +export function disableSlidevSync() { |
| 58 | + slidevSync.disable() |
| 59 | +} |
| 60 | + |
| 61 | +export function addSyncMethod(sync: Sync) { |
| 62 | + syncInterfaces.push(sync) |
| 63 | + for (const [channelKey, { onUpdate, persist, state }] of channels.entries()) { |
| 64 | + const write = sync.init(channelKey, onUpdate, state, persist) |
| 65 | + if (write) { |
| 66 | + syncWrites.value[channelKey].push(write) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
2 | 70 |
|
3 | 71 | export function createSyncState<State extends object>(serverState: State, defaultState: State, persist = false) {
|
4 | 72 | const onPatchCallbacks: ((state: State) => void)[] = []
|
@@ -36,35 +104,19 @@ export function createSyncState<State extends object>(serverState: State, defaul
|
36 | 104 | }
|
37 | 105 |
|
38 | 106 | function init(channelKey: string) {
|
39 |
| - let stateChannel: BroadcastChannel |
40 |
| - if (!__SLIDEV_HAS_SERVER__ && !persist) { |
41 |
| - stateChannel = new BroadcastChannel(channelKey) |
42 |
| - stateChannel.addEventListener('message', (event: MessageEvent<Partial<State>>) => onUpdate(event.data)) |
43 |
| - } |
44 |
| - else if (!__SLIDEV_HAS_SERVER__ && persist) { |
45 |
| - window.addEventListener('storage', (event) => { |
46 |
| - if (event && event.key === channelKey && event.newValue) |
47 |
| - onUpdate(JSON.parse(event.newValue) as Partial<State>) |
48 |
| - }) |
49 |
| - } |
| 107 | + channels.set(channelKey, { onUpdate, persist, state }) |
| 108 | + syncWrites.value[channelKey] = syncInterfaces |
| 109 | + .map(sync => sync.init<State>(channelKey, onUpdate, state, persist)) |
| 110 | + .filter((x): x is SyncWrite<object> => Boolean(x)) |
50 | 111 |
|
51 | 112 | function onStateChanged() {
|
52 |
| - if (!persist && stateChannel && !updating) |
53 |
| - stateChannel.postMessage(toRaw(state)) |
54 |
| - else if (persist && !updating) |
55 |
| - window.localStorage.setItem(channelKey, JSON.stringify(state)) |
| 113 | + syncWrites.value[channelKey].forEach(write => write?.(toRaw(state), updating)) |
56 | 114 | if (!patching)
|
57 | 115 | onPatchCallbacks.forEach((fn: (state: State) => void) => fn(state))
|
58 | 116 | }
|
59 | 117 |
|
60 |
| - watch(state, onStateChanged, { deep: true, flush: 'sync' }) |
61 |
| - |
62 |
| - if (!__SLIDEV_HAS_SERVER__ && persist) { |
63 |
| - const serialzedState = window.localStorage.getItem(channelKey) |
64 |
| - if (serialzedState) |
65 |
| - onUpdate(JSON.parse(serialzedState) as Partial<State>) |
66 |
| - } |
| 118 | + watch(state, onStateChanged, { deep: true }) |
67 | 119 | }
|
68 | 120 |
|
69 |
| - return { init, onPatch, patch, state } |
| 121 | + return { init, onPatch, onUpdate, patch, state } |
70 | 122 | }
|
0 commit comments