fix
This commit is contained in:
		
							parent
							
								
									48a169516c
								
							
						
					
					
						commit
						8e290ba7f6
					
				packages/client/src
|  | @ -17,7 +17,8 @@ type ArrayElement<A> = A extends readonly (infer T)[] ? T : never; | ||||||
| const connection = $i && stream.useChannel('main'); | const connection = $i && stream.useChannel('main'); | ||||||
| 
 | 
 | ||||||
| export class Storage<T extends StateDef> { | export class Storage<T extends StateDef> { | ||||||
| 	public readonly ready: PromiseLike<void>; | 	public readonly ready: Promise<void>; | ||||||
|  | 	public readonly loaded: Promise<void>; | ||||||
| 
 | 
 | ||||||
| 	public readonly key: string; | 	public readonly key: string; | ||||||
| 	public readonly deviceStateKeyName: string; | 	public readonly deviceStateKeyName: string; | ||||||
|  | @ -49,10 +50,10 @@ export class Storage<T extends StateDef> { | ||||||
| 		this.def = def; | 		this.def = def; | ||||||
| 
 | 
 | ||||||
| 		this.ready = this.init(); | 		this.ready = this.init(); | ||||||
|  | 		this.loaded = this.ready.then(() => this.load()); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private init(): Promise<void> { | 	private async init(): Promise<void> { | ||||||
| 		return new Promise(async (resolve, reject) => { |  | ||||||
| 		await this.migrate(); | 		await this.migrate(); | ||||||
| 
 | 
 | ||||||
| 		const deviceState: State<T> = await get(this.deviceStateKeyName) || {}; | 		const deviceState: State<T> = await get(this.deviceStateKeyName) || {}; | ||||||
|  | @ -75,6 +76,27 @@ export class Storage<T extends StateDef> { | ||||||
| 			this.reactiveState[k] = ref(v); | 			this.reactiveState[k] = ref(v); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  | 		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; | ||||||
|  | 	 | ||||||
|  | 				this.addIdbSetJob(async () => { | ||||||
|  | 					const cache = await get(this.registryCacheKeyName); | ||||||
|  | 					if (cache[key] !== value) { | ||||||
|  | 						cache[key] = value; | ||||||
|  | 						await set(this.registryCacheKeyName, cache); | ||||||
|  | 					} | ||||||
|  | 				}); | ||||||
|  | 			}); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	private async load(): Promise<void> { | ||||||
|  | 		return new Promise((resolve, reject) => { | ||||||
| 			if ($i) { | 			if ($i) { | ||||||
| 				// なぜかsetTimeoutしないとapi関数内でエラーになる(おそらく循環参照してることに原因がありそう)
 | 				// なぜかsetTimeoutしないとapi関数内でエラーになる(おそらく循環参照してることに原因がありそう)
 | ||||||
| 				setTimeout(() => { | 				setTimeout(() => { | ||||||
|  | @ -98,22 +120,6 @@ export class Storage<T extends StateDef> { | ||||||
| 					}) | 					}) | ||||||
| 					.then(() => resolve()); | 					.then(() => resolve()); | ||||||
| 				}, 1); | 				}, 1); | ||||||
| 
 |  | ||||||
| 				// streamingのuser storage updateイベントを監視して更新
 |  | ||||||
| 				connection?.on('registryUpdated', ({ scope, key, value }: { scope: string[], key: keyof T, value: T[typeof key]['default'] }) => { |  | ||||||
| 					if (scope.length !== 2 || scope[0] !== 'client' || scope[1] !== this.key || this.state[key] === value) return; |  | ||||||
| 
 |  | ||||||
| 					this.state[key] = value; |  | ||||||
| 					this.reactiveState[key].value = value; |  | ||||||
| 
 |  | ||||||
| 					this.addIdbSetJob(async () => { |  | ||||||
| 						const cache = await get(this.registryCacheKeyName); |  | ||||||
| 						if (cache[key] !== value) { |  | ||||||
| 							cache[key] = value; |  | ||||||
| 							await set(this.registryCacheKeyName, cache); |  | ||||||
| 						} |  | ||||||
| 					} |  | ||||||
| 				}); |  | ||||||
| 			} | 			} | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue