-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathremote.ts
81 lines (73 loc) · 1.96 KB
/
remote.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
import type {
Context,
EagerCollection,
Mapper,
NamedCollections,
Resource,
Values,
} from "@skipruntime/core";
import { SkipExternalService } from "@skipruntime/helpers";
import { runService } from "@skipruntime/server";
const platform: "wasm" | "native" =
process.env["SKIP_PLATFORM"] == "native" ? "native" : "wasm";
class AddIndex implements Mapper<string, number, string, [number, number]> {
constructor(private index: number) {}
mapEntry(
key: string,
values: Values<number>,
): Iterable<[string, [number, number]]> {
return [[key, [this.index, values.getUnique()]]];
}
}
class Mult implements Mapper<string, [number, number], string, number> {
mapEntry(
key: string,
values: Values<[number, number]>,
): Iterable<[string, number]> {
const arr = values.toArray();
return [[key, arr.length == 2 ? arr[0]![1] * arr[1]![1] : 0]];
}
}
class MultResource implements Resource {
instantiate(
_collections: NamedCollections,
context: Context,
): EagerCollection<string, number> {
const sub = context
.useExternalResource<string, number>({
service: "sumexample",
identifier: "sub",
})
.map(AddIndex, 0);
const add = context
.useExternalResource<string, number>({
service: "sumexample",
identifier: "add",
})
.map(AddIndex, 1);
return sub.merge(add).map(Mult);
}
}
const service = {
resources: { data: MultResource },
externalServices: {
sumexample: SkipExternalService.direct({
host: "localhost",
streaming_port: 3587,
control_port: 3588,
}),
},
createGraph(inputCollections: NamedCollections) {
return inputCollections;
},
};
const server = await runService(service, {
streaming_port: 3589,
control_port: 3590,
platform,
});
async function shutdown() {
await server.close();
}
// eslint-disable-next-line @typescript-eslint/no-misused-promises
["SIGTERM", "SIGINT"].map((sig) => process.on(sig, shutdown));