Skip to content

Commit

Permalink
fix(Broadcast): fixed ts errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alisahinozcelik committed Nov 25, 2023
1 parent c8351a0 commit dc05cfe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 32 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"typings": "index.d.ts",
"scripts": {
"build": "tsc -p ./tsconfig.lib.json",
"test": "jest",
"test:coverage": "jest --collectCoverage",
"lint": "tslint -p tsconfig.json && tslint -p tsconfig.spec.json",
Expand Down
27 changes: 10 additions & 17 deletions src/broadcast/message-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,28 @@ interface MessageEvent<T> {
data: T;
}

const CHANNEL = Symbol('Broadcast Channel');
const HANDLER = Symbol('Handler');

export class BroadcastMessageClient extends MessageClient {
public [RESPONSES$] = new Subject<MessageResponse>();
protected [RESPONSES$] = new Subject<MessageResponse>();

private [CHANNEL] = new BroadcastChannel(this.channelName);
#channel: BroadcastChannel;

constructor(private channelName = DEFAULT_CHANNEL_NAME) {
constructor(channelName = DEFAULT_CHANNEL_NAME) {
super();

this[CHANNEL].addEventListener('message', this[HANDLER]);
this.#channel = new BroadcastChannel(channelName);

this.#channel.addEventListener('message', this.#handler);
}

public [SEND]<T>(message: Message<T>) {
this[CHANNEL].postMessage(message);
protected [SEND]<T>(message: Message<T>) {
this.#channel.postMessage(message);
}

protected [HANDLER] = (event: MessageEvent<MessageResponse>) => {
#handler = (event: MessageEvent<MessageResponse>) => {
this[RESPONSES$].next(event.data);
}

protected [GET_NEW_ID](): string {
const key = 'Hermes/Broadcast/' + this.channelName;
const lastId = +(localStorage.getItem(key) || '0');
const newId = (lastId + 1) + '';

localStorage.setItem(key, newId);

return newId;
return crypto.randomUUID();
}
}
27 changes: 12 additions & 15 deletions src/broadcast/message-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,29 @@ interface MessageEvent<T> {
data: T;
}

const REQUESTS$ = Symbol('Requests');
const HANDLER = Symbol('Handler');
const CHANNEL = Symbol('Broadcast Channel');

export class BroadcastMessageHost extends MessageHost {
private [REQUESTS$] = new Subject<Message>();
private [CHANNEL] = new BroadcastChannel(this.channelName);
#requests$ = new Subject<Message>();
#channel: BroadcastChannel;

constructor(private channelName = DEFAULT_CHANNEL_NAME) {
constructor(channelName = DEFAULT_CHANNEL_NAME) {
super();

this[CHANNEL].addEventListener('message', this[HANDLER]);
this.#channel = new BroadcastChannel(channelName);

this.listen(this[REQUESTS$]);
this.#channel.addEventListener('message', this.#handler);
this.listen(this.#requests$);
}

protected response(message: MessageResponse) {
this[CHANNEL].postMessage(message);
this.#channel.postMessage(message);
}

public terminate() {
this[CHANNEL].removeEventListener('message', this[HANDLER]);
this[CHANNEL].close();
terminate() {
this.#channel.removeEventListener('message', this.#handler);
this.#channel.close();
}

private [HANDLER] = (event: MessageEvent<Message>) => {
this[REQUESTS$].next(event.data);
#handler = (event: MessageEvent<Message>) => {
this.#requests$.next(event.data);
}
}

0 comments on commit dc05cfe

Please sign in to comment.