This commit is contained in:
syuilo
2024-08-26 09:15:46 +09:00
parent a5f2163b07
commit 3143cf388e
7 changed files with 35 additions and 107 deletions

View File

@@ -32,10 +32,6 @@ type PizzaxChannelMessage<T extends StateDef> = {
userId?: string;
};
export type PizzaxConfig = {
disableMessageChannel: boolean;
};
export class Storage<T extends StateDef> {
public readonly ready: Promise<void>;
public readonly loaded: Promise<void>;
@@ -51,10 +47,6 @@ export class Storage<T extends StateDef> {
public readonly state: State<T>;
public readonly reactiveState: ReactiveState<T>;
private options: PizzaxConfig = {
disableMessageChannel: false,
};
private pizzaxChannel: BroadcastChannel<PizzaxChannelMessage<T>>;
// 簡易的にキューイングして占有ロックとする
@@ -68,13 +60,12 @@ export class Storage<T extends StateDef> {
return promise;
}
constructor(key: string, def: T, options?: Partial<PizzaxConfig>) {
constructor(key: string, def: T) {
this.key = key;
this.deviceStateKeyName = `pizzax::${key}`;
this.deviceAccountStateKeyName = $i ? `pizzax::${key}::${$i.id}` : '';
this.registryCacheKeyName = $i ? `pizzax::${key}::cache::${$i.id}` : '';
this.def = def;
this.options = Object.assign(this.options, options);
this.pizzaxChannel = new BroadcastChannel(`pizzax::${key}`);
@@ -128,7 +119,7 @@ export class Storage<T extends StateDef> {
this.pizzaxChannel.addEventListener('message', ({ where, key, value, userId }) => {
// アカウント変更すればunisonReloadが効くため、このreturnが発火することは
// まずないと思うけど一応弾いておく
if ((where === 'deviceAccount' && !($i && userId !== $i.id) || this.options.disableMessageChannel)) return;
if (where === 'deviceAccount' && !($i && userId !== $i.id)) return;
this.reactiveState[key].value = this.state[key] = value;
});
@@ -183,17 +174,6 @@ export class Storage<T extends StateDef> {
});
}
/**
* Sets the configuration options for Pizzax.
*
* 特にinitを待ったりとかはしないので、boot.jsなど、ロード初期段階で呼ぶ必要がある
*
* @param config - The partial configuration object.
*/
public setConfig(config: Partial<PizzaxConfig>) {
this.options = Object.assign(this.options, config);
}
public set<K extends keyof T>(key: K, value: T[K]['default']): Promise<void> {
// IndexedDBやBroadcastChannelで扱うために単純なオブジェクトにする
// (JSON.parse(JSON.stringify(value))の代わり)
@@ -207,13 +187,11 @@ export class Storage<T extends StateDef> {
if (_DEV_) console.log(`set ${String(key)} start`);
switch (this.def[key].where) {
case 'device': {
if (!this.options.disableMessageChannel) {
this.pizzaxChannel.postMessage({
where: 'device',
key,
value: rawValue,
});
}
this.pizzaxChannel.postMessage({
where: 'device',
key,
value: rawValue,
});
const deviceState = await get(this.deviceStateKeyName) || {};
deviceState[key] = rawValue;
await set(this.deviceStateKeyName, deviceState);
@@ -221,14 +199,12 @@ export class Storage<T extends StateDef> {
}
case 'deviceAccount': {
if ($i == null) break;
if (!this.options.disableMessageChannel) {
this.pizzaxChannel.postMessage({
where: 'deviceAccount',
key,
value: rawValue,
userId: $i.id,
});
}
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);