@dxos/functions 0.5.3-main.495a683 → 0.5.3-main.4e877aa
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.
- package/dist/lib/browser/index.mjs +301 -282
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +303 -286
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/{function → registry}/function-registry.d.ts +4 -4
- package/dist/types/src/registry/function-registry.d.ts.map +1 -0
- package/dist/types/src/registry/function-registry.test.d.ts.map +1 -0
- package/dist/types/src/registry/index.d.ts.map +1 -0
- package/dist/types/src/runtime/dev-server.d.ts +1 -1
- package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
- package/dist/types/src/runtime/scheduler.d.ts +1 -2
- package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
- package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -1
- package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +19 -33
- package/dist/types/src/types.d.ts.map +1 -1
- package/package.json +18 -31
- package/schema/functions.json +9 -18
- package/src/index.ts +1 -1
- package/src/{function → registry}/function-registry.test.ts +10 -10
- package/src/registry/function-registry.ts +84 -0
- package/src/runtime/dev-server.test.ts +2 -2
- package/src/runtime/dev-server.ts +6 -5
- package/src/runtime/scheduler.test.ts +1 -1
- package/src/runtime/scheduler.ts +8 -16
- package/src/testing/functions-integration.test.ts +1 -1
- package/src/testing/setup.ts +1 -1
- package/src/trigger/trigger-registry.test.ts +34 -60
- package/src/trigger/trigger-registry.ts +6 -19
- package/src/trigger/type/subscription-trigger.ts +10 -17
- package/src/types.ts +10 -12
- package/dist/lib/browser/chunk-366QG6IX.mjs +0 -81
- package/dist/lib/browser/chunk-366QG6IX.mjs.map +0 -7
- package/dist/lib/browser/types.mjs +0 -12
- package/dist/lib/browser/types.mjs.map +0 -7
- package/dist/lib/node/chunk-3VSJ57ZZ.cjs +0 -97
- package/dist/lib/node/chunk-3VSJ57ZZ.cjs.map +0 -7
- package/dist/lib/node/types.cjs +0 -33
- package/dist/lib/node/types.cjs.map +0 -7
- package/dist/types/src/function/function-registry.d.ts.map +0 -1
- package/dist/types/src/function/function-registry.test.d.ts.map +0 -1
- package/dist/types/src/function/index.d.ts.map +0 -1
- package/dist/types/src/util.d.ts +0 -15
- package/dist/types/src/util.d.ts.map +0 -1
- package/dist/types/src/util.test.d.ts +0 -2
- package/dist/types/src/util.test.d.ts.map +0 -1
- package/src/function/function-registry.ts +0 -90
- package/src/util.test.ts +0 -43
- package/src/util.ts +0 -48
- /package/dist/types/src/{function → registry}/function-registry.test.d.ts +0 -0
- /package/dist/types/src/{function → registry}/index.d.ts +0 -0
- /package/src/{function → registry}/index.ts +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAIA,OAAO,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAIA,OAAO,EAAO,CAAC,EAAe,MAAM,mBAAmB,CAAC;AAKxD;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAAG,cAAc,GAAG,OAAO,GAAG,SAAS,GAAG,WAAW,CAAC;AAErF,QAAA,MAAM,yBAAyB;;;;;;;;;;;;;;;;;EAiB7B,CAAC;AACH,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,yBAAyB,CAAC,CAAC;AAElF,QAAA,MAAM,kBAAkB;;;EAGtB,CAAC;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAEpE,QAAA,MAAM,oBAAoB;;;;GAOzB,CAAC;AACF,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAExE,QAAA,MAAM,sBAAsB;;;;;;;;EAI1B,CAAC;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAQ5E,MAAM,MAAM,WAAW,GAAG,YAAY,GAAG,cAAc,GAAG,gBAAgB,GAAG,mBAAmB,CAAC;;;;;;;;;AAEjG;;GAEG;AACH,qBAAa,WAAY,SAAQ,gBAS/B;CAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEL,qBAAa,eAAgB,SAAQ,oBAQnC;CAAG;AAEL;;GAEG;AACH,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAGjC,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,sBAAsB,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions",
|
|
3
|
-
"version": "0.5.3-main.
|
|
4
|
-
"description": "Functions
|
|
3
|
+
"version": "0.5.3-main.4e877aa",
|
|
4
|
+
"description": "Functions SDK and runtime.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
7
|
"license": "MIT",
|
|
@@ -10,24 +10,10 @@
|
|
|
10
10
|
".": {
|
|
11
11
|
"browser": "./dist/lib/browser/index.mjs",
|
|
12
12
|
"node": "./dist/lib/node/index.cjs",
|
|
13
|
-
"default": "./dist/lib/node/index.cjs"
|
|
14
|
-
"types": "./dist/types/src/index.d.ts"
|
|
15
|
-
},
|
|
16
|
-
"./types": {
|
|
17
|
-
"browser": "./dist/lib/browser/types.mjs",
|
|
18
|
-
"node": "./dist/lib/node/types.cjs",
|
|
19
|
-
"default": "./dist/lib/node/types.cjs",
|
|
20
|
-
"types": "./dist/types/src/types.d.ts"
|
|
13
|
+
"default": "./dist/lib/node/index.cjs"
|
|
21
14
|
}
|
|
22
15
|
},
|
|
23
16
|
"types": "dist/types/src/index.d.ts",
|
|
24
|
-
"typesVersions": {
|
|
25
|
-
"*": {
|
|
26
|
-
"types": [
|
|
27
|
-
"dist/types/src/types.d.ts"
|
|
28
|
-
]
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
17
|
"files": [
|
|
32
18
|
"dist",
|
|
33
19
|
"schema",
|
|
@@ -40,28 +26,29 @@
|
|
|
40
26
|
"express": "^4.19.2",
|
|
41
27
|
"get-port-please": "^3.1.1",
|
|
42
28
|
"ws": "^8.14.2",
|
|
43
|
-
"@braneframe/types": "0.5.3-main.
|
|
44
|
-
"@dxos/
|
|
45
|
-
"@dxos/
|
|
46
|
-
"@dxos/
|
|
47
|
-
"@dxos/
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/
|
|
52
|
-
"@dxos/
|
|
53
|
-
"@dxos/
|
|
54
|
-
"@dxos/util": "0.5.3-main.
|
|
29
|
+
"@braneframe/types": "0.5.3-main.4e877aa",
|
|
30
|
+
"@dxos/async": "0.5.3-main.4e877aa",
|
|
31
|
+
"@dxos/client": "0.5.3-main.4e877aa",
|
|
32
|
+
"@dxos/echo-db": "0.5.3-main.4e877aa",
|
|
33
|
+
"@dxos/context": "0.5.3-main.4e877aa",
|
|
34
|
+
"@dxos/invariant": "0.5.3-main.4e877aa",
|
|
35
|
+
"@dxos/echo-schema": "0.5.3-main.4e877aa",
|
|
36
|
+
"@dxos/keys": "0.5.3-main.4e877aa",
|
|
37
|
+
"@dxos/log": "0.5.3-main.4e877aa",
|
|
38
|
+
"@dxos/node-std": "0.5.3-main.4e877aa",
|
|
39
|
+
"@dxos/protocols": "0.5.3-main.4e877aa",
|
|
40
|
+
"@dxos/util": "0.5.3-main.4e877aa"
|
|
55
41
|
},
|
|
56
42
|
"devDependencies": {
|
|
57
43
|
"@types/express": "^4.17.17",
|
|
58
44
|
"@types/ws": "^7.4.0",
|
|
59
|
-
"@dxos/agent": "0.5.3-main.
|
|
45
|
+
"@dxos/agent": "0.5.3-main.4e877aa"
|
|
60
46
|
},
|
|
61
47
|
"publishConfig": {
|
|
62
48
|
"access": "public"
|
|
63
49
|
},
|
|
64
50
|
"scripts": {
|
|
65
|
-
"gen-schema": "ts-node ./tools/schema.ts"
|
|
51
|
+
"gen-schema": "ts-node ./tools/schema.ts",
|
|
52
|
+
"prebuild": "pnpm gen-schema"
|
|
66
53
|
}
|
|
67
54
|
}
|
package/schema/functions.json
CHANGED
|
@@ -48,11 +48,17 @@
|
|
|
48
48
|
"properties": {
|
|
49
49
|
"function": {
|
|
50
50
|
"type": "string",
|
|
51
|
-
"description": "Function URI.",
|
|
51
|
+
"description": "Function ID/URI.",
|
|
52
52
|
"title": "string"
|
|
53
53
|
},
|
|
54
54
|
"meta": {
|
|
55
|
-
"
|
|
55
|
+
"type": "object",
|
|
56
|
+
"required": [],
|
|
57
|
+
"properties": {},
|
|
58
|
+
"additionalProperties": {
|
|
59
|
+
"$id": "/schemas/any",
|
|
60
|
+
"title": "any"
|
|
61
|
+
}
|
|
56
62
|
},
|
|
57
63
|
"spec": {
|
|
58
64
|
"anyOf": [
|
|
@@ -187,20 +193,5 @@
|
|
|
187
193
|
}
|
|
188
194
|
}
|
|
189
195
|
},
|
|
190
|
-
"additionalProperties": false
|
|
191
|
-
"$defs": {
|
|
192
|
-
"object": {
|
|
193
|
-
"$id": "/schemas/object",
|
|
194
|
-
"oneOf": [
|
|
195
|
-
{
|
|
196
|
-
"type": "object"
|
|
197
|
-
},
|
|
198
|
-
{
|
|
199
|
-
"type": "array"
|
|
200
|
-
}
|
|
201
|
-
],
|
|
202
|
-
"description": "an object in the TypeScript meaning, i.e. the `object` type",
|
|
203
|
-
"title": "object"
|
|
204
|
-
}
|
|
205
|
-
}
|
|
196
|
+
"additionalProperties": false
|
|
206
197
|
}
|
package/src/index.ts
CHANGED
|
@@ -44,7 +44,7 @@ describe('function registry', () => {
|
|
|
44
44
|
const client = (await createInitializedClients(testBuilder))[0];
|
|
45
45
|
const registry = createRegistry(client);
|
|
46
46
|
const space = await client.spaces.create();
|
|
47
|
-
await registry.register(space, testManifest
|
|
47
|
+
await registry.register(space, testManifest);
|
|
48
48
|
const { objects: definitions } = await space.db.query(Filter.schema(FunctionDef)).run();
|
|
49
49
|
expect(definitions.length).to.eq(1);
|
|
50
50
|
expect(definitions[0].uri).to.eq(testManifest.functions?.[0]?.uri);
|
|
@@ -54,8 +54,8 @@ describe('function registry', () => {
|
|
|
54
54
|
const client = (await createInitializedClients(testBuilder))[0];
|
|
55
55
|
const registry = createRegistry(client);
|
|
56
56
|
const space = await client.spaces.create();
|
|
57
|
-
const existing = space.db.add(create(FunctionDef, testManifest.functions![0]));
|
|
58
|
-
await registry.register(space, testManifest
|
|
57
|
+
const existing = space.db.add(create(FunctionDef, { ...testManifest.functions![0] }));
|
|
58
|
+
await registry.register(space, testManifest);
|
|
59
59
|
const { objects: definitions } = await space.db.query(Filter.schema(FunctionDef)).run();
|
|
60
60
|
expect(definitions.length).to.eq(1);
|
|
61
61
|
expect(definitions[0].uri).to.eq(existing.uri);
|
|
@@ -67,12 +67,12 @@ describe('function registry', () => {
|
|
|
67
67
|
const client = (await createInitializedClients(testBuilder))[0];
|
|
68
68
|
const registry = createRegistry(client);
|
|
69
69
|
const space = await client.spaces.create();
|
|
70
|
-
const definitions = range(3, () => create(FunctionDef, testManifest.functions![0]));
|
|
70
|
+
const definitions = range(3, () => create(FunctionDef, { ...testManifest.functions![0] }));
|
|
71
71
|
definitions.forEach((def) => space.db.add(def));
|
|
72
72
|
|
|
73
73
|
const functionRegistered = new Trigger<FunctionDef[]>();
|
|
74
|
-
registry.
|
|
75
|
-
functionRegistered.wake(fn.
|
|
74
|
+
registry.onFunctionsRegistered.on((fn) => {
|
|
75
|
+
functionRegistered.wake(fn.newFunctions);
|
|
76
76
|
});
|
|
77
77
|
void registry.open(ctx);
|
|
78
78
|
const functions = await functionRegistered.wait();
|
|
@@ -86,12 +86,12 @@ describe('function registry', () => {
|
|
|
86
86
|
const space = await client.spaces.create();
|
|
87
87
|
|
|
88
88
|
const functionRegistered = new Trigger<FunctionDef>();
|
|
89
|
-
registry.
|
|
90
|
-
expect(fn.
|
|
91
|
-
functionRegistered.wake(fn.
|
|
89
|
+
registry.onFunctionsRegistered.on((fn) => {
|
|
90
|
+
expect(fn.newFunctions.length).to.eq(1);
|
|
91
|
+
functionRegistered.wake(fn.newFunctions[0]);
|
|
92
92
|
});
|
|
93
93
|
await registry.open(ctx);
|
|
94
|
-
await registry.register(space, testManifest
|
|
94
|
+
await registry.register(space, testManifest);
|
|
95
95
|
const registered = await functionRegistered.wait();
|
|
96
96
|
expect(registered.uri).to.eq(testManifest.functions![0].uri);
|
|
97
97
|
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Event } from '@dxos/async';
|
|
6
|
+
import { type Client } from '@dxos/client';
|
|
7
|
+
import { create, Filter, type Space } from '@dxos/client/echo';
|
|
8
|
+
import { type Context, Resource } from '@dxos/context';
|
|
9
|
+
import { PublicKey } from '@dxos/keys';
|
|
10
|
+
import { ComplexMap } from '@dxos/util';
|
|
11
|
+
|
|
12
|
+
import { FunctionDef, type FunctionManifest } from '../types';
|
|
13
|
+
|
|
14
|
+
export type FunctionsRegisteredEvent = {
|
|
15
|
+
space: Space;
|
|
16
|
+
newFunctions: FunctionDef[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export class FunctionRegistry extends Resource {
|
|
20
|
+
private readonly _functionBySpaceKey = new ComplexMap<PublicKey, FunctionDef[]>(PublicKey.hash);
|
|
21
|
+
|
|
22
|
+
public readonly onFunctionsRegistered = new Event<FunctionsRegisteredEvent>();
|
|
23
|
+
|
|
24
|
+
constructor(private readonly _client: Client) {
|
|
25
|
+
super();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public getFunctions(space: Space): FunctionDef[] {
|
|
29
|
+
return this._functionBySpaceKey.get(space.key) ?? [];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The method loads function definitions from the manifest into the space.
|
|
34
|
+
* We first load all the definitions from the space to deduplicate by functionId.
|
|
35
|
+
*/
|
|
36
|
+
// TODO(burdon): This should not be space specific (they are static for the agent).
|
|
37
|
+
public async register(space: Space, manifest: FunctionManifest): Promise<void> {
|
|
38
|
+
if (!manifest.functions?.length) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (!space.db.graph.runtimeSchemaRegistry.isSchemaRegistered(FunctionDef)) {
|
|
42
|
+
space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionDef);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const { objects: existingDefinitions } = await space.db.query(Filter.schema(FunctionDef)).run();
|
|
46
|
+
const newDefinitions = getNewDefinitions(manifest.functions, existingDefinitions);
|
|
47
|
+
const reactiveObjects = newDefinitions.map((template) => create(FunctionDef, { ...template }));
|
|
48
|
+
reactiveObjects.forEach((obj) => space.db.add(obj));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected override async _open(): Promise<void> {
|
|
52
|
+
const spaceListSubscription = this._client.spaces.subscribe(async (spaces) => {
|
|
53
|
+
for (const space of spaces) {
|
|
54
|
+
if (this._functionBySpaceKey.has(space.key)) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const registered: FunctionDef[] = [];
|
|
58
|
+
this._functionBySpaceKey.set(space.key, registered);
|
|
59
|
+
await space.waitUntilReady();
|
|
60
|
+
if (this._ctx.disposed) {
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const functionsSubscription = space.db.query(Filter.schema(FunctionDef)).subscribe((definitions) => {
|
|
65
|
+
const newFunctions = getNewDefinitions(definitions.objects, registered);
|
|
66
|
+
if (newFunctions.length > 0) {
|
|
67
|
+
registered.push(...newFunctions);
|
|
68
|
+
this.onFunctionsRegistered.emit({ space, newFunctions });
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
this._ctx.onDispose(functionsSubscription);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
this._ctx.onDispose(() => spaceListSubscription.unsubscribe());
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
protected override async _close(_: Context): Promise<void> {
|
|
78
|
+
this._functionBySpaceKey.clear();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const getNewDefinitions = <T extends { uri: string }>(candidateList: T[], existing: FunctionDef[]): T[] => {
|
|
83
|
+
return candidateList.filter((candidate) => existing.find((def) => def.uri === candidate.uri) == null);
|
|
84
|
+
};
|
|
@@ -11,7 +11,7 @@ import { TestBuilder } from '@dxos/client/testing';
|
|
|
11
11
|
import { describe, test } from '@dxos/test';
|
|
12
12
|
|
|
13
13
|
import { DevServer } from './dev-server';
|
|
14
|
-
import { FunctionRegistry } from '../
|
|
14
|
+
import { FunctionRegistry } from '../registry';
|
|
15
15
|
import { createFunctionRuntime } from '../testing';
|
|
16
16
|
import { type FunctionManifest } from '../types';
|
|
17
17
|
|
|
@@ -44,7 +44,7 @@ describe('dev server', () => {
|
|
|
44
44
|
baseDir: path.join(__dirname, '../testing'),
|
|
45
45
|
});
|
|
46
46
|
const space = await client.spaces.create();
|
|
47
|
-
await registry.register(space, manifest
|
|
47
|
+
await registry.register(space, manifest);
|
|
48
48
|
await server.start();
|
|
49
49
|
|
|
50
50
|
// TODO(burdon): Doesn't shut down cleanly.
|
|
@@ -13,8 +13,8 @@ import { Context } from '@dxos/context';
|
|
|
13
13
|
import { invariant } from '@dxos/invariant';
|
|
14
14
|
import { log } from '@dxos/log';
|
|
15
15
|
|
|
16
|
-
import { type FunctionRegistry } from '../function';
|
|
17
16
|
import { type FunctionContext, type FunctionEvent, type FunctionHandler, type FunctionResponse } from '../handler';
|
|
17
|
+
import { type FunctionRegistry } from '../registry';
|
|
18
18
|
import { type FunctionDef } from '../types';
|
|
19
19
|
|
|
20
20
|
export type DevServerOptions = {
|
|
@@ -41,15 +41,16 @@ export class DevServer {
|
|
|
41
41
|
|
|
42
42
|
public readonly update = new Event<number>();
|
|
43
43
|
|
|
44
|
+
// prettier-ignore
|
|
44
45
|
constructor(
|
|
45
46
|
private readonly _client: Client,
|
|
46
47
|
private readonly _functionsRegistry: FunctionRegistry,
|
|
47
48
|
private readonly _options: DevServerOptions,
|
|
48
49
|
) {
|
|
49
|
-
this._functionsRegistry.
|
|
50
|
-
|
|
50
|
+
this._functionsRegistry.onFunctionsRegistered.on(async ({ newFunctions }) => {
|
|
51
|
+
newFunctions.forEach((def) => this._load(def));
|
|
51
52
|
await this._safeUpdateRegistration();
|
|
52
|
-
log('new functions loaded', {
|
|
53
|
+
log('new functions loaded', { newFunctions });
|
|
53
54
|
});
|
|
54
55
|
}
|
|
55
56
|
|
|
@@ -157,7 +158,7 @@ export class DevServer {
|
|
|
157
158
|
/**
|
|
158
159
|
* Load function.
|
|
159
160
|
*/
|
|
160
|
-
private async _load(def: FunctionDef, force
|
|
161
|
+
private async _load(def: FunctionDef, force = false) {
|
|
161
162
|
const { uri, route, handler } = def;
|
|
162
163
|
const filePath = join(this._options.baseDir, handler);
|
|
163
164
|
log.info('loading', { uri, force });
|
|
@@ -12,7 +12,7 @@ import { create } from '@dxos/echo-schema';
|
|
|
12
12
|
import { describe, test } from '@dxos/test';
|
|
13
13
|
|
|
14
14
|
import { Scheduler, type SchedulerOptions } from './scheduler';
|
|
15
|
-
import { FunctionRegistry } from '../
|
|
15
|
+
import { FunctionRegistry } from '../registry';
|
|
16
16
|
import { createInitializedClients, TestType, triggerWebhook } from '../testing';
|
|
17
17
|
import { TriggerRegistry } from '../trigger';
|
|
18
18
|
import { type FunctionManifest } from '../types';
|
package/src/runtime/scheduler.ts
CHANGED
|
@@ -4,13 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
|
|
7
|
-
import { Mutex } from '@dxos/async';
|
|
8
7
|
import { type Space } from '@dxos/client/echo';
|
|
9
8
|
import { Context } from '@dxos/context';
|
|
10
9
|
import { log } from '@dxos/log';
|
|
11
10
|
|
|
12
|
-
import { type FunctionRegistry } from '../function';
|
|
13
11
|
import { type FunctionEventMeta } from '../handler';
|
|
12
|
+
import { type FunctionRegistry } from '../registry';
|
|
14
13
|
import { type TriggerRegistry } from '../trigger';
|
|
15
14
|
import { type FunctionDef, type FunctionManifest, type FunctionTrigger } from '../types';
|
|
16
15
|
|
|
@@ -27,15 +26,13 @@ export type SchedulerOptions = {
|
|
|
27
26
|
export class Scheduler {
|
|
28
27
|
private _ctx = createContext();
|
|
29
28
|
|
|
30
|
-
private readonly _callMutex = new Mutex();
|
|
31
|
-
|
|
32
29
|
constructor(
|
|
33
30
|
public readonly functions: FunctionRegistry,
|
|
34
31
|
public readonly triggers: TriggerRegistry,
|
|
35
32
|
private readonly _options: SchedulerOptions = {},
|
|
36
33
|
) {
|
|
37
|
-
this.functions.
|
|
38
|
-
await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space),
|
|
34
|
+
this.functions.onFunctionsRegistered.on(async ({ space, newFunctions }) => {
|
|
35
|
+
await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space), newFunctions);
|
|
39
36
|
});
|
|
40
37
|
this.triggers.registered.on(async ({ space, triggers }) => {
|
|
41
38
|
await this._safeActivateTriggers(space, triggers, this.functions.getFunctions(space));
|
|
@@ -55,9 +52,8 @@ export class Scheduler {
|
|
|
55
52
|
await this.triggers.close();
|
|
56
53
|
}
|
|
57
54
|
|
|
58
|
-
// TODO(burdon): Remove and update registries directly.
|
|
59
55
|
public async register(space: Space, manifest: FunctionManifest) {
|
|
60
|
-
await this.functions.register(space, manifest
|
|
56
|
+
await this.functions.register(space, manifest);
|
|
61
57
|
await this.triggers.register(space, manifest);
|
|
62
58
|
}
|
|
63
59
|
|
|
@@ -80,20 +76,16 @@ export class Scheduler {
|
|
|
80
76
|
}
|
|
81
77
|
|
|
82
78
|
await this.triggers.activate({ space }, fnTrigger, async (args) => {
|
|
83
|
-
return this.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
data: { ...args, spaceKey: space.key },
|
|
87
|
-
});
|
|
79
|
+
return this._execFunction(definition, {
|
|
80
|
+
meta: fnTrigger.meta,
|
|
81
|
+
data: { ...args, spaceKey: space.key },
|
|
88
82
|
});
|
|
89
83
|
});
|
|
90
|
-
|
|
91
84
|
log('activated trigger', { space: space.key, trigger: fnTrigger });
|
|
92
85
|
}
|
|
93
86
|
|
|
94
87
|
private async _execFunction<TData, TMeta>(
|
|
95
88
|
def: FunctionDef,
|
|
96
|
-
trigger: FunctionTrigger,
|
|
97
89
|
{ data, meta }: { data: TData; meta?: TMeta },
|
|
98
90
|
): Promise<number> {
|
|
99
91
|
let status = 0;
|
|
@@ -105,7 +97,7 @@ export class Scheduler {
|
|
|
105
97
|
if (endpoint) {
|
|
106
98
|
// TODO(burdon): Move out of scheduler (generalize as callback).
|
|
107
99
|
const url = path.join(endpoint, def.route);
|
|
108
|
-
log.info('exec', { function: def.uri, url
|
|
100
|
+
log.info('exec', { function: def.uri, url });
|
|
109
101
|
const response = await fetch(url, {
|
|
110
102
|
method: 'POST',
|
|
111
103
|
headers: {
|
|
@@ -13,7 +13,7 @@ import { Invitation } from '@dxos/protocols/proto/dxos/client/services';
|
|
|
13
13
|
import { describe, test } from '@dxos/test';
|
|
14
14
|
|
|
15
15
|
import { setTestCallHandler } from './test/handler';
|
|
16
|
-
import { FunctionRegistry } from '../
|
|
16
|
+
import { FunctionRegistry } from '../registry';
|
|
17
17
|
import { DevServer, Scheduler } from '../runtime';
|
|
18
18
|
import { createFunctionRuntime, createInitializedClients, TestType } from '../testing';
|
|
19
19
|
import { TriggerRegistry } from '../trigger';
|
package/src/testing/setup.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { range } from '@dxos/util';
|
|
|
10
10
|
import { TestType } from './types';
|
|
11
11
|
import { FunctionDef, FunctionTrigger } from '../types';
|
|
12
12
|
|
|
13
|
-
// TODO(burdon): Create
|
|
13
|
+
// TODO(burdon): Create TestBuilder.
|
|
14
14
|
|
|
15
15
|
export const createInitializedClients = async (testBuilder: TestBuilder, count: number = 1, config?: Config) => {
|
|
16
16
|
const clients = range(count).map(() => new Client({ config, services: testBuilder.createLocalClientServices() }));
|