Skip to content

Commit dcc23d4

Browse files
committed
✨ save streaming promises to map in controller
1 parent c696686 commit dcc23d4

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/models/controller.model.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class Controller {
5151
private ajv: Ajv;
5252
private pubSubClient: PubSubClient | null = null;
5353
private integrationName: string = "UNKNOWN";
54+
private streamingPromises = new Map<string, Promise<void>>();
5455

5556
constructor(adapter: Adapter, contactCache: ContactCache | null) {
5657
this.adapter = adapter;
@@ -189,6 +190,12 @@ export class Controller {
189190
throw new ServerError(400, "Missing parameters");
190191
}
191192

193+
const { userId } = providerConfig;
194+
195+
if (!userId) {
196+
throw new ServerError(400, "Missing user ID");
197+
}
198+
192199
const timestamp = Date.now();
193200

194201
try {
@@ -216,7 +223,7 @@ export class Controller {
216223
}
217224

218225
const message: PubSubContactsMessage = {
219-
userId: providerConfig.userId,
226+
userId,
220227
timestamp,
221228
contacts: contacts.map((contact) =>
222229
sanitizeContact(contact, providerConfig.locale)
@@ -239,7 +246,7 @@ export class Controller {
239246
}
240247
};
241248

242-
streamContacts()
249+
const streamingPromise = streamContacts()
243250
.catch((error) =>
244251
errorLogger(
245252
"streamContacts",
@@ -248,15 +255,18 @@ export class Controller {
248255
error
249256
)
250257
)
251-
.finally(() =>
258+
.finally(() => {
252259
this.pubSubClient?.publishMessage({
253260
userId: providerConfig.userId,
254261
timestamp,
255262
contacts: [],
256263
state: PubSubContactsState.COMPLETE,
257264
integrationName: this.integrationName,
258-
})
259-
);
265+
});
266+
this.streamingPromises.delete(`${userId}:${timestamp}`);
267+
});
268+
269+
this.streamingPromises.set(`${userId}:${timestamp}`, streamingPromise);
260270

261271
if (this.adapter.getToken && req.providerConfig) {
262272
const { apiKey } = await this.adapter.getToken(req.providerConfig);

0 commit comments

Comments
 (0)