mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 01:09:54 +08:00
fix(core): fix event timing in storage (#11628)
This commit is contained in:
@@ -45,6 +45,7 @@
|
|||||||
"cmdk": "^1.0.4",
|
"cmdk": "^1.0.4",
|
||||||
"core-js": "^3.39.0",
|
"core-js": "^3.39.0",
|
||||||
"dayjs": "^1.11.13",
|
"dayjs": "^1.11.13",
|
||||||
|
"eventemitter2": "^6.4.9",
|
||||||
"file-type": "^20.0.0",
|
"file-type": "^20.0.0",
|
||||||
"filesize": "^10.1.6",
|
"filesize": "^10.1.6",
|
||||||
"foxact": "^0.2.43",
|
"foxact": "^0.2.43",
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { Memento } from '@toeverything/infra';
|
import type { Memento } from '@toeverything/infra';
|
||||||
|
import EventEmitter2 from 'eventemitter2';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
@@ -8,6 +9,10 @@ import type {
|
|||||||
} from '../providers/global';
|
} from '../providers/global';
|
||||||
|
|
||||||
export class StorageMemento implements Memento {
|
export class StorageMemento implements Memento {
|
||||||
|
// eventEmitter is used for same tab event
|
||||||
|
private readonly eventEmitter = new EventEmitter2();
|
||||||
|
// channel is used for cross-tab event
|
||||||
|
private readonly channel = new BroadcastChannel(this.prefix);
|
||||||
constructor(
|
constructor(
|
||||||
private readonly storage: Storage,
|
private readonly storage: Storage,
|
||||||
private readonly prefix: string
|
private readonly prefix: string
|
||||||
@@ -34,20 +39,27 @@ export class StorageMemento implements Memento {
|
|||||||
const first = json ? JSON.parse(json) : undefined;
|
const first = json ? JSON.parse(json) : undefined;
|
||||||
subscriber.next(first);
|
subscriber.next(first);
|
||||||
|
|
||||||
const channel = new BroadcastChannel(this.prefix + key);
|
const eventEmitterCb = (value: T) => {
|
||||||
channel.addEventListener('message', event => {
|
subscriber.next(value);
|
||||||
subscriber.next(event.data);
|
};
|
||||||
});
|
this.eventEmitter.on(key, eventEmitterCb);
|
||||||
|
|
||||||
|
const channelCb = (event: MessageEvent) => {
|
||||||
|
if (event.data.key === key) {
|
||||||
|
subscriber.next(event.data.value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.channel.addEventListener('message', channelCb);
|
||||||
return () => {
|
return () => {
|
||||||
channel.close();
|
this.eventEmitter.off(key, eventEmitterCb);
|
||||||
|
this.channel.removeEventListener('message', channelCb);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
set<T>(key: string, value: T): void {
|
set<T>(key: string, value: T): void {
|
||||||
this.storage.setItem(this.prefix + key, JSON.stringify(value));
|
this.storage.setItem(this.prefix + key, JSON.stringify(value));
|
||||||
const channel = new BroadcastChannel(this.prefix + key);
|
this.eventEmitter.emit(key, value);
|
||||||
channel.postMessage(value);
|
this.channel.postMessage({ key, value });
|
||||||
channel.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
del(key: string): void {
|
del(key: string): void {
|
||||||
|
|||||||
@@ -430,6 +430,7 @@ __metadata:
|
|||||||
cmdk: "npm:^1.0.4"
|
cmdk: "npm:^1.0.4"
|
||||||
core-js: "npm:^3.39.0"
|
core-js: "npm:^3.39.0"
|
||||||
dayjs: "npm:^1.11.13"
|
dayjs: "npm:^1.11.13"
|
||||||
|
eventemitter2: "npm:^6.4.9"
|
||||||
fake-indexeddb: "npm:^6.0.0"
|
fake-indexeddb: "npm:^6.0.0"
|
||||||
file-type: "npm:^20.0.0"
|
file-type: "npm:^20.0.0"
|
||||||
filesize: "npm:^10.1.6"
|
filesize: "npm:^10.1.6"
|
||||||
|
|||||||
Reference in New Issue
Block a user