deno.land / x / pothos@release-1713397530 / packages / plugin-smart-subscriptions / manager / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// @ts-nocheck/* eslint-disable @typescript-eslint/no-throw-literal *//* eslint-disable no-await-in-loop */import { RegisterOptions } from '../types.ts';type Timer = ReturnType<typeof setTimeout>;export default class SubscriptionManager implements AsyncIterator<object> { activeSubscriptions = new Set<string>(); nextSubscriptions = new Set<string>(); activeOptions = new Map<string, RegisterOptions[]>(); nextOptions = new Map<string, RegisterOptions[]>(); subscribeToName: (name: string, cb: (err: unknown, data: unknown) => void) => Promise<void> | void; unsubscribeFromName: (name: string) => Promise<void> | void; // Always trigger on first pull to push initial state to client pendingEvent = true; pendingError: unknown; pendingEvents: [ string, unknown ][] = []; value: object; resolveNext: ((done?: boolean) => void) | null = null; rejectNext: ((err: unknown) => void) | null = null; stopped = false; debounceDelay: number | null = null; debounceRef: Timer | null = null; constructor({ value, debounceDelay, subscribe, unsubscribe, }: { value: object; debounceDelay?: number | null; subscribe: (name: string, cb: (err: unknown, data: unknown) => void) => Promise<void> | void; unsubscribe: (name: string) => Promise<void> | void; }) { this.subscribeToName = subscribe; this.unsubscribeFromName = unsubscribe; this.value = value; this.debounceDelay = debounceDelay ?? null; } register<T>({ name, ...options }: RegisterOptions<T>) { if (this.stopped) { return; } this.addOptions(name, options as RegisterOptions); if (this.nextSubscriptions.has(name)) { return; } this.nextSubscriptions.add(name); if (this.activeSubscriptions.has(name)) { return; } const maybePromise = this.subscribeToName(name, (err, value) => { if (err) { this.handleError(err); } else { this.handleValue(name, value); } }); if (maybePromise) { maybePromise.catch((error) => void this.handleError(error)); } } [Symbol.asyncIterator]() { return this; } async return() { if (this.pendingError) { throw this.pendingError; } await this.stop(); return { done: true, value: this.value, }; } async throw(error: unknown) { this.handleError(error); // eslint-disable-next-line unicorn/no-useless-promise-resolve-reject return Promise.reject<IteratorResult<object>>(error as Error); } async next(): Promise<IteratorResult<object>> { if (this.pendingError) { throw this.pendingError; } if (this.stopped) { return { done: true, value: this.value, }; } for (const name of this.activeSubscriptions) { if (!this.nextSubscriptions.has(name)) { await this.unsubscribeFromName(name); } } this.activeSubscriptions = this.nextSubscriptions; this.nextSubscriptions = new Set(); this.activeOptions = this.nextOptions; this.nextOptions = new Map<string, RegisterOptions[]>(); if (this.pendingEvent) { this.pendingEvent = false; return { done: false, value: this.value, }; } return new Promise<IteratorResult<object>>((resolve, reject) => { this.resolveNext = (done = false) => { this.resolveNext = null; this.rejectNext = null; resolve({ done, value: this.value, }); }; this.rejectNext = (err) => { this.resolveNext = null; this.rejectNext = null; reject(err as Error); }; const pending = this.pendingEvents; if (pending.length > 0) { this.pendingEvents = []; for (const [name, value] of pending) { this.handleValue(name, value); } } }); } handleError(err: unknown) { this.pendingError = err; if (this.rejectNext) { this.rejectNext(err); } this.stop().catch((error) => void this.handleError(error)); } private async stop() { if (this.stopped) { return; } if (this.debounceRef) { clearTimeout(this.debounceRef); this.debounceRef = null; } this.stopped = true; const names = new Set([...this.activeSubscriptions, ...this.nextSubscriptions]); this.activeSubscriptions = new Set(); this.nextSubscriptions = new Set(); this.activeOptions = new Map<string, RegisterOptions[]>(); this.nextOptions = new Map<string, RegisterOptions[]>(); if (this.pendingError && this.rejectNext) { this.rejectNext(this.pendingError); } else if (this.resolveNext) { this.resolveNext(true); } for (const name of names) { await this.unsubscribeFromName(name); } } private addOptions(name: string, options: RegisterOptions) { if (!this.nextOptions.has(name)) { this.nextOptions.set(name, []); } this.nextOptions.get(name)!.push(options); } private filterValue(name: string, value: unknown) { const optionsList = this.activeOptions.get(name); if (!optionsList) { return { allowed: true }; } let allowed = false; const promises: Promise<void>[] = []; for (const options of optionsList) { const currentAllowed = !options.filter || options.filter(value); // eslint-disable-next-line logical-assignment-operators allowed = allowed || currentAllowed; if (currentAllowed && options.onValue) { const promise = options.onValue(value); if (promise) { promises.push(promise); } } } return { allowed, promises: Promise.all(promises) }; } private handleValue(name: string, value: unknown) { if (this.stopped) { return; } if (!this.resolveNext) { this.pendingEvents.push([name, value]); return; } const { allowed, promises } = this.filterValue(name, value); if (promises) { promises.catch((error) => void this.handleError(error)); } if (!allowed) { return; } if (this.debounceRef) { return; } if (this.debounceDelay === null) { this.pushValue(); } else { this.debounceRef = setTimeout(() => { this.debounceRef = null; this.pushValue(); }, this.debounceDelay); if (typeof this.debounceRef === "object" && "unref" in this.debounceRef) { (this.debounceRef as unknown as { unref: () => unknown; }).unref(); } } } private pushValue() { if (this.stopped) { return; } if (this.resolveNext) { this.resolveNext(); } else { this.pendingEvent = true; } }}
pothos

Version Info

Tagged at
a year ago