-
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.
- Loading branch information
Showing
36 changed files
with
359 additions
and
3,647 deletions.
There are no files selected for viewing
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
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
File renamed without changes.
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,5 @@ | ||
export class ResourceNotFound extends Error { | ||
constructor(file: string) { | ||
super(`Resource ${file} not found`); | ||
} | ||
} |
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,2 +1,3 @@ | ||
export * from "./Resource"; | ||
export * from "./ResourceStore"; | ||
export * from "./ResourceNotFound"; |
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,53 @@ | ||
import {Disposable, IDisposable} from "@bunt/unit"; | ||
import {AsyncCallback, Fn, Promisify} from "@bunt/util"; | ||
import {IPubSubTransport, ISubscriber} from "./interfaces"; | ||
|
||
export abstract class PubSubAbstract<S extends Record<string, any>, T extends IPubSubTransport> | ||
implements IDisposable { | ||
readonly #transport: T; | ||
readonly #subscriptions = new Map<string, ISubscriber>(); | ||
readonly #iterables = new Set<AsyncCallback<any>>(); | ||
|
||
public constructor(transport: T) { | ||
this.#transport = transport; | ||
} | ||
|
||
public async publish<K extends keyof S>(channel: Extract<K, string>, message: S[K]): Promise<void> { | ||
await this.#transport.publish(channel, this.serialize(message)); | ||
} | ||
|
||
public async subscribe<K extends keyof S>(channel: Extract<K, string>): Promise<AsyncIterable<S[K]>>; | ||
public async subscribe<K extends keyof S>(channel: Extract<K, string>, | ||
listener: Fn<[S[K]], unknown>): Promise<() => void>; | ||
public async subscribe<K extends keyof S>( | ||
channel: Extract<K, string>, listener?: Fn<[S[K]], unknown>): Promise<AsyncIterable<S[K]> | (() => void)> { | ||
const subscription = this.#subscriptions.get(channel) ?? await this.#transport.subscribe(channel); | ||
if (!this.#subscriptions.has(channel)) { | ||
await subscription.subscribe(); | ||
this.#subscriptions.set(channel, subscription); | ||
} | ||
|
||
if (listener) { | ||
return subscription.listen((message) => listener(this.parse(message))); | ||
} | ||
|
||
const iterable = new AsyncCallback<S[K]>((emit) => { | ||
return subscription.listen((message) => emit(this.parse(message))); | ||
}); | ||
|
||
this.#iterables.add(iterable); | ||
return iterable; | ||
} | ||
|
||
public dispose(): Promisify<Disposable | Disposable[] | void> { | ||
for (const iterable of this.#iterables.values()) { | ||
iterable.dispose(); | ||
} | ||
|
||
return this.#transport.dispose(); | ||
} | ||
|
||
protected abstract serialize<K extends keyof S>(message: S[K]): string; | ||
|
||
protected abstract parse<K extends keyof S>(message: string): S[K]; | ||
} |
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,12 @@ | ||
import {IPubSubTransport} from "./interfaces"; | ||
import {PubSubAbstract} from "./PubSubAbstract"; | ||
|
||
export class PubSubSimple<S extends Record<string, any>, T extends IPubSubTransport> extends PubSubAbstract<S, T> { | ||
protected serialize<K extends keyof S>(message: S[K]): string { | ||
return JSON.stringify(message); | ||
} | ||
|
||
protected parse<K extends keyof S>(message: string): S[K] { | ||
return JSON.parse(message); | ||
} | ||
} |
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,19 @@ | ||
import {Fn} from "@bunt/util"; | ||
import {ITransport} from "../interfaces"; | ||
|
||
export interface ISubscriber { | ||
readonly channel: string; | ||
|
||
unsubscribe(): Promise<void>; | ||
subscribe(): Promise<void>; | ||
|
||
listen(listener: Fn<[string], unknown>): Fn; | ||
|
||
close(): void; | ||
} | ||
|
||
export interface IPubSubTransport extends ITransport { | ||
publish(channel: string, message: string): Promise<number>; | ||
|
||
subscribe(channel: string): Promise<ISubscriber>; | ||
} |
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,6 @@ | ||
import {Message} from "./interfaces"; | ||
import {QueueListAbstract} from "./QueueListAbstract"; | ||
|
||
export class QueueList<M extends Message> extends QueueListAbstract<M> { | ||
|
||
} |
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
2 changes: 1 addition & 1 deletion
2
packages/queue/src/Queue/ReaderAbstract.ts → ...es/queue/src/Queue/QueueReaderAbstract.ts
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
export * from "./fn"; | ||
export * from "./interfaces"; | ||
export * from "./ReadOperation"; | ||
export * from "./ReaderAbstract"; | ||
export * from "./SubscriptionAbstract"; | ||
export * from "./Subscription"; | ||
export * from "./Message"; | ||
export * from "./QueueListAbstract"; | ||
export * from "./QueueList"; | ||
export * from "./QueueAbstract"; | ||
export * from "./Queue"; | ||
export * from "./Message"; | ||
export * from "./QueueReaderAbstract"; | ||
export * from "./ReadOperation"; |
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,5 @@ | ||
import {Message, QueueListAbstract} from "../Queue"; | ||
|
||
export class RedisQueueList<M extends Message> extends QueueListAbstract<M> { | ||
|
||
} |
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
Oops, something went wrong.