Skip to content

Commit

Permalink
Move BroadcastChannel initialization into afterNextRender().
Browse files Browse the repository at this point in the history
  • Loading branch information
e-oz committed Nov 2, 2024
1 parent e6bbec8 commit 32003bc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-reactive-storage",
"version": "2.0.0-next.1",
"version": "2.0.0-next.2",
"license": "MIT",
"author": {
"name": "Evgeniy OZ",
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-reactive-storage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-reactive-storage",
"version": "2.0.0-next.1",
"version": "2.0.0-next.2",
"license": "MIT",
"private": false,
"author": {
Expand Down
35 changes: 24 additions & 11 deletions projects/ngx-reactive-storage/src/lib/idb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class RxStorage implements ReactiveStorage {
private readonly dbName: string;
private readonly tableName: string;
private readonly observer = new Observer(this);
private readonly channel: BroadcastChannel;
private channel?: BroadcastChannel;
private readonly listener = (event: MessageEvent) => {
if (event.data && typeof event.data === 'object') {
const msg = event.data as KeyChange;
Expand All @@ -39,15 +39,22 @@ export class RxStorage implements ReactiveStorage {
constructor(tableName: string = 'table', dbName: string = 'db', private injector?: Injector) {
this.tableName = tableName || 'table';
this.dbName = dbName || 'db';
this.channel = new BroadcastChannel(this.dbName + '.' + this.tableName);
this.channel.addEventListener('message', this.listener, { passive: true });

if (typeof window === 'undefined' || typeof window.indexedDB === 'undefined') {
afterNextRender(() => {
this.storage = localforage.createInstance({ name: this.dbName, storeName: this.tableName });
}, { injector: this.injector });
} else {
const init = () => {
this.storage = localforage.createInstance({ name: this.dbName, storeName: this.tableName });
this.channel = new BroadcastChannel(this.dbName + '.' + this.tableName);
this.channel.addEventListener('message', this.listener, { passive: true })
};

if (typeof window === 'undefined' || typeof BroadcastChannel === 'undefined' || typeof window.indexedDB === 'undefined') {
if (!this.injector) {
if (isDevMode()) {
console.error('RxStorage:: For SSR, please provide an Injector instance in the constructor.');
}
}
afterNextRender(() => init(), { injector: this.injector });
} else {
init();
}
}

Expand Down Expand Up @@ -192,12 +199,18 @@ export class RxStorage implements ReactiveStorage {

dispose(): void {
this.observer.dispose();
this.channel.removeEventListener('message', this.listener);
this.channel.close();
if (this.channel) {
this.channel.removeEventListener('message', this.listener);
this.channel.close();
}
}

private broadcastChange(change: KeyChange) {
this.channel.postMessage(change);
this.whenStorageIsReady(() => {
if (this.channel) {
this.channel.postMessage(change);
}
});
}

private whenStorageIsReady(cb: (storage: LocalForage) => unknown): void {
Expand Down

0 comments on commit 32003bc

Please sign in to comment.