From 52840faa85ed5c2262301a0e4b851d34e4bebe64 Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 5 Jan 2022 00:55:30 +0900 Subject: [PATCH] wip --- packages/client/src/pizzax.ts | 38 +++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/client/src/pizzax.ts b/packages/client/src/pizzax.ts index 867d2f9bd..51b5cd40d 100644 --- a/packages/client/src/pizzax.ts +++ b/packages/client/src/pizzax.ts @@ -4,6 +4,8 @@ import { api } from './os'; import { get, set } from './scripts/idb-proxy'; import { defaultStore } from './store'; import { stream } from './stream'; +// SafariがBroadcastChannel未実装なのでライブラリを使う +import { BroadcastChannel } from 'broadcast-channel'; type StateDef = Record = { [K in keyof T]: Ref; type ArrayElement = A extends readonly (infer T)[] ? T : never; +type PizzaxChannelMessage = { + where: 'device' | 'deviceAccount'; + key: keyof T; + value: T[keyof T]; + userId?: string; +}; + const connection = $i && stream.useChannel('main'); export class Storage { @@ -22,9 +31,9 @@ export class Storage { public readonly loaded: Promise; public readonly key: string; - public readonly deviceStateKeyName: string; - public readonly deviceAccountStateKeyName: string; - public readonly registryCacheKeyName: string; + public readonly deviceStateKeyName: `pizzax::${this['key']}`; + public readonly deviceAccountStateKeyName: `pizzax::${this['key']}::${string}` | ''; + public readonly registryCacheKeyName: `pizzax::${this['key']}::cache::${string}` | ''; public readonly def: T; @@ -32,6 +41,8 @@ export class Storage { public readonly state = {} as State; public readonly reactiveState = {} as ReactiveState; + private pizzaxChannel: BroadcastChannel>; + // 簡易的にキューイングして占有ロックとする private currentIdbJob: Promise = Promise.resolve(); private addIdbSetJob(job: () => Promise) { @@ -50,6 +61,8 @@ export class Storage { this.registryCacheKeyName = $i ? `pizzax::${key}::cache::${$i.id}` : ''; this.def = def; + this.pizzaxChannel = new BroadcastChannel(`pizzax::${key}`); + this.ready = this.init(); this.loaded = this.ready.then(() => this.load()); } @@ -77,11 +90,17 @@ export class Storage { this.reactiveState[k] = ref(v); } + this.pizzaxChannel.addEventListener('message', ({ where, key, value, userId }) => { + if (where === 'deviceAccount' && !($i && userId !== $i.id)) return; + this.state[key] = value; + this.reactiveState[key].value = value; + }); + if ($i) { // streamingのuser storage updateイベントを監視して更新 connection?.on('registryUpdated', ({ scope, key, value }: { scope?: string[], key: keyof T, value: T[typeof key]['default'] }) => { if (!scope || scope.length !== 2 || scope[0] !== 'client' || scope[1] !== this.key || this.state[key] === value) return; - + this.state[key] = value; this.reactiveState[key].value = value; @@ -141,6 +160,11 @@ export class Storage { if (_DEV_) console.log(`set ${key} start`); switch (this.def[key].where) { case 'device': { + this.pizzaxChannel.postMessage({ + where: 'device', + key, + value: rawValue, + }); const deviceState = await get(this.deviceStateKeyName) || {}; deviceState[key] = rawValue; await set(this.deviceStateKeyName, deviceState); @@ -148,6 +172,12 @@ export class Storage { } case 'deviceAccount': { if ($i == null) break; + this.pizzaxChannel.postMessage({ + where: 'deviceAccount', + key, + value: rawValue, + userId: $i.id, + }); const deviceAccountState = await get(this.deviceAccountStateKeyName) || {}; deviceAccountState[key] = rawValue; await set(this.deviceAccountStateKeyName, deviceAccountState);