-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathutils.ts
33 lines (30 loc) · 1.01 KB
/
utils.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
// TODO: Remove once global `EventSource` makes it out of experimental
// in nodejs LTS.
import EventSource from "eventsource";
import type { Json } from "@skipruntime/core";
import { SkipServiceBroker } from "@skipruntime/helpers";
export async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export async function subscribe(
service: SkipServiceBroker,
resource: string,
streaming_port: number,
params: Json = {},
): Promise<{ close: () => void }> {
return await service.getStreamUUID(resource, params).then((uuid) => {
const evSource = new EventSource(
`http://localhost:${streaming_port}/v1/streams/${uuid}`,
);
evSource.addEventListener("init", (e: MessageEvent<string>) => {
console.log("Init", e.data);
});
evSource.addEventListener("update", (e: MessageEvent<string>) => {
console.log("Update", e.data);
});
evSource.onerror = (e: MessageEvent<string>) => {
console.log("Error", e);
};
return evSource;
});
}