-
Notifications
You must be signed in to change notification settings - Fork 703
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
70 additions
and
114 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,23 +1,11 @@ | ||
import type { Observable } from 'rxjs'; | ||
|
||
/** Generic interface for pub-subs. */ | ||
export interface GenericPubSub<T> { | ||
// Publish an event for a match. | ||
publish(channelId: PubSubChannelId, payload: T); | ||
publish(channelId: string, payload: T); | ||
|
||
// Subscribes to events related to a single match. | ||
subscribe(channelId: PubSubChannelId): Observable<T>; | ||
subscribe(channelId: string, callback: (payload: T) => void): void; | ||
|
||
// Cleans up subscription for a given channel. | ||
unsubscribe(channelId: PubSubChannelId); | ||
} | ||
|
||
/** All possible namespaces for IDs that pubsubs might be used. */ | ||
export enum PubSubChannelIdNamespace { | ||
MATCH = 'MATCH', | ||
} | ||
|
||
export interface PubSubChannelId { | ||
namespace: PubSubChannelIdNamespace; | ||
value: string; | ||
unsubscribeAll(channelId: string); | ||
} |
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 |
---|---|---|
@@ -1,33 +1,28 @@ | ||
import { Subject } from 'rxjs'; | ||
import type { Observable } from 'rxjs'; | ||
import type { GenericPubSub, PubSubChannelId } from './generic-pub-sub'; | ||
import { globalChannelId } from './util'; | ||
import type { GenericPubSub } from './generic-pub-sub'; | ||
|
||
export class InMemoryPubSub<T> implements GenericPubSub<T> { | ||
subjects: Map<string, Subject<T>> = new Map(); | ||
callbacks: Map<string, ((payload: T) => void)[]> = new Map(); | ||
|
||
publish(channelId: PubSubChannelId, payload: T) { | ||
this.initializeSubject(channelId); | ||
const subject = this.subjects.get(globalChannelId(channelId)); | ||
subject.next(payload); | ||
} | ||
|
||
subscribe(channelId: PubSubChannelId): Observable<T> { | ||
this.initializeSubject(channelId); | ||
const subject = this.subjects.get(globalChannelId(channelId)); | ||
return subject; | ||
publish(channelId: string, payload: T) { | ||
if (!this.callbacks.has(channelId)) { | ||
return; | ||
} | ||
const allCallbacks = this.callbacks.get(channelId); | ||
for (const callback of allCallbacks) { | ||
callback(payload); | ||
} | ||
} | ||
|
||
unsubscribe(channelId: PubSubChannelId) { | ||
if (this.subjects.has(globalChannelId(channelId))) { | ||
this.subjects.get(globalChannelId(channelId)).unsubscribe(); | ||
this.subjects.delete(globalChannelId(channelId)); | ||
subscribe(channelId: string, callback: (payload: T) => void): void { | ||
if (!this.callbacks.has(channelId)) { | ||
this.callbacks.set(channelId, []); | ||
} | ||
this.callbacks.get(channelId).push(callback); | ||
} | ||
|
||
private initializeSubject(channelId: PubSubChannelId) { | ||
if (!this.subjects.has(globalChannelId(channelId))) { | ||
this.subjects.set(globalChannelId(channelId), new Subject<T>()); | ||
unsubscribeAll(channelId: string) { | ||
if (this.callbacks.has(channelId)) { | ||
this.callbacks.delete(channelId); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,41 +1,42 @@ | ||
import type redis from 'redis'; | ||
import { Observable } from 'rxjs'; | ||
import type { GenericPubSub, PubSubChannelId } from './generic-pub-sub'; | ||
import { globalChannelId } from './util'; | ||
import type { GenericPubSub } from './generic-pub-sub'; | ||
|
||
export class RedisPubSub<T> implements GenericPubSub<T> { | ||
private pubClient: redis.RedisClient; | ||
private subClient: redis.RedisClient; | ||
private subscriptions: Map<string, Observable<T>> = new Map(); | ||
callbacks: Map<string, ((payload: T) => void)[]> = new Map(); | ||
|
||
constructor(pubClient: redis.redisclient, subClient: redis.redisclient) { | ||
this.pubClient = pubClient; | ||
this.subClient = subClient; | ||
this.subClient.on('message', (redisChannelId, message) => { | ||
if (!this.callbacks.has(redisChannelId)) { | ||
return; | ||
} | ||
const allCallbacks = this.callbacks.get(redisChannelId); | ||
const parsedPayload = JSON.parse(message) as T; | ||
for (const callback of allCallbacks) { | ||
callback(parsedPayload); | ||
} | ||
}); | ||
} | ||
|
||
publish(channelId: PubSubChannelId, payload: T) { | ||
this.pubClient.publish(globalChannelId(channelId), JSON.stringify(payload)); | ||
publish(channelId: string, payload: T) { | ||
this.pubClient.publish(channelId, JSON.stringify(payload)); | ||
} | ||
|
||
subscribe(channelId: PubSubChannelId): Observable<T> { | ||
if (this.subscriptions.has(globalChannelId(channelId))) { | ||
return this.subscriptions.get(globalChannelId(channelId)); | ||
subscribe(channelId: string, callback: (payload: T) => void) { | ||
if (!this.callbacks.has(channelId)) { | ||
this.callbacks.set(channelId, []); | ||
} | ||
const observable = new Observable<T>((subscribe) => { | ||
this.subClient.on('message', (redisChannelId, message) => { | ||
if (redisChannelId !== globalChannelId(channelId)) { | ||
return; | ||
} | ||
subscribe.next(JSON.parse(message) as T); | ||
}); | ||
}); | ||
this.subClient.subscribe(globalChannelId(channelId)); | ||
this.subscriptions.set(globalChannelId(channelId), observable); | ||
return observable; | ||
this.callbacks.get(channelId).push(callback); | ||
this.subClient.subscribe(channelId); | ||
} | ||
|
||
unsubscribe(channelId: PubSubChannelId) { | ||
this.subscriptions.delete(globalChannelId(channelId)); | ||
this.subClient.unsubscribe(globalChannelId(channelId)); | ||
unsubscribeAll(channelId: string) { | ||
this.subClient.unsubscribe(channelId); | ||
if (this.callbacks.has(channelId)) { | ||
this.callbacks.delete(channelId); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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