nmtjs 0.15.3 → 0.16.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,106 +0,0 @@
1
- import EventEmitter, { on } from 'node:events'
2
-
3
- import { isAbortError } from '@nmtjs/common'
4
- import { createFactoryInjectable, provision } from '@nmtjs/core'
5
-
6
- import type { RuntimePlugin } from '../plugin.ts'
7
- import type { Store } from '../types.ts'
8
- import type { PubSubAdapterEvent, PubSubAdapterType } from './manager.ts'
9
- import { pubSubAdapter, storeConfig } from '../injectables.ts'
10
- import { createStoreClient } from '../store/index.ts'
11
-
12
- export class RedisPubSubAdapter implements PubSubAdapterType {
13
- protected readonly events = new EventEmitter()
14
- protected readonly listeners = new Map<string, number>()
15
- protected subscriberClient?: Store
16
-
17
- constructor(protected readonly client: Store) {}
18
-
19
- async initialize() {
20
- // Create a dedicated subscriber client (Redis requires separate clients for pub/sub)
21
- this.subscriberClient = this.client.duplicate()
22
-
23
- // Set up message handler
24
- this.subscriberClient.on('message', (channel: string, message: string) => {
25
- try {
26
- const parsed = JSON.parse(message)
27
- this.events.emit(channel, parsed)
28
- } catch {}
29
- })
30
- }
31
-
32
- async dispose() {
33
- if (this.subscriberClient) {
34
- await this.subscriberClient.quit()
35
- this.subscriberClient = undefined
36
- }
37
- }
38
-
39
- async publish(channel: string, payload: any): Promise<boolean> {
40
- try {
41
- await this.client.publish(channel, JSON.stringify(payload))
42
- return true
43
- } catch {
44
- return false
45
- }
46
- }
47
-
48
- async *subscribe(
49
- channel: string,
50
- signal?: AbortSignal,
51
- ): AsyncGenerator<PubSubAdapterEvent> {
52
- if (!this.subscriberClient) {
53
- throw new Error('RedisPubSubAdapter not initialized')
54
- }
55
-
56
- if (!this.listeners.has(channel)) {
57
- await this.subscriberClient.subscribe(channel)
58
- this.listeners.set(channel, 1)
59
- } else {
60
- this.listeners.set(channel, this.listeners.get(channel)! + 1)
61
- }
62
-
63
- try {
64
- signal?.throwIfAborted()
65
- for await (const [payload] of on(this.events, channel, { signal })) {
66
- yield { channel, payload }
67
- }
68
- } catch (error: any) {
69
- if (isAbortError(error)) throw error
70
- } finally {
71
- const count = this.listeners.get(channel)
72
- if (count !== undefined) {
73
- if (count > 1) {
74
- this.listeners.set(channel, count - 1)
75
- } else {
76
- await this.subscriberClient?.unsubscribe(channel)
77
- this.listeners.delete(channel)
78
- }
79
- }
80
- }
81
- }
82
- }
83
-
84
- export const RedisPubSubAdapterPlugin = (): RuntimePlugin => {
85
- return {
86
- name: 'pubsub-redis-adapter',
87
- hooks: {
88
- 'lifecycle:beforeInitialize': async (ctx) => {
89
- const adapter = await ctx.container.resolve(
90
- createFactoryInjectable({
91
- dependencies: { config: storeConfig },
92
- factory: async ({ config }) => {
93
- const connection = await createStoreClient(config)
94
- const adapter = new RedisPubSubAdapter(connection)
95
- await adapter.initialize()
96
- return { adapter, connection }
97
- },
98
- pick: ({ adapter }) => adapter,
99
- dispose: ({ connection }) => connection.quit(),
100
- }),
101
- )
102
- ctx.container.provide(pubSubAdapter, adapter)
103
- },
104
- },
105
- }
106
- }