-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(domain): use BroadcastChannel API
- Loading branch information
1 parent
4fe3d64
commit 7abbdf2
Showing
6 changed files
with
109 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# Changelog | ||
|
||
## 0.11.0 | ||
|
||
- Use BroadcastChannel API. | ||
|
||
## 0.10.2 | ||
|
||
- Fix transaction processing. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { localStorage, events } from '../../infrastructure/webstorage/api'; | ||
import { BroadcastChannel as BC } from '../webstorage/api'; | ||
|
||
export class Channel<M extends string> { | ||
constructor( | ||
public name: string | ||
) { | ||
return typeof BroadcastChannel === 'function' | ||
? new Broadcast(name) | ||
: new Storage(name); | ||
} | ||
public listen(_listener: (ev: MessageEvent | StorageEvent) => void): () => void { | ||
return () => void 0; | ||
} | ||
public post(_message: M): void { | ||
} | ||
public close(): void { | ||
} | ||
} | ||
export namespace Channel { | ||
export import Event = BC.Event; | ||
} | ||
|
||
class Broadcast<M extends string> implements Channel<M> { | ||
constructor( | ||
public name: string | ||
) { | ||
} | ||
private channel = new BroadcastChannel(this.name); | ||
private listeners = new Set<(_: MessageEvent) => void>(); | ||
public listen(listener: (ev: MessageEvent) => void): () => void { | ||
void this.listeners.add(listener); | ||
void this.channel.addEventListener('message', listener); | ||
return () => ( | ||
void this.listeners.delete(listener), | ||
void this.channel.removeEventListener('message', listener)); | ||
} | ||
public post(message: M): void { | ||
void this.channel.postMessage(message); | ||
} | ||
public close(): void { | ||
void this.listeners | ||
.forEach(listener => | ||
void this.channel.removeEventListener('message', listener)); | ||
} | ||
} | ||
|
||
class Storage<M extends string> implements Channel<M> { | ||
constructor( | ||
public name: string | ||
) { | ||
} | ||
private storage = localStorage!; | ||
private listeners = new Set<(_: StorageEvent) => void>(); | ||
public listen(listener: (ev: StorageEvent) => void): () => void { | ||
void this.listeners.add(listener); | ||
void events.localStorage.on(['storage'], listener); | ||
return () => ( | ||
void this.listeners.delete(listener), | ||
void events.localStorage.off(['storage'], listener)); | ||
} | ||
public post(message: M): void { | ||
void this.storage.removeItem(this.name); | ||
void this.storage.setItem(this.name, message); | ||
} | ||
public close(): void { | ||
void this.listeners | ||
.forEach(listener => | ||
void events.localStorage.off(['storage'], listener)); | ||
void this.storage.removeItem(this.name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters