@dxos/functions 0.5.3-main.d3c5e1f → 0.5.3-main.d7fe7b5

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.
Files changed (56) hide show
  1. package/dist/lib/browser/index.mjs +301 -282
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node/index.cjs +303 -286
  5. package/dist/lib/node/index.cjs.map +4 -4
  6. package/dist/lib/node/meta.json +1 -1
  7. package/dist/types/src/index.d.ts +1 -1
  8. package/dist/types/src/index.d.ts.map +1 -1
  9. package/dist/types/src/{function → registry}/function-registry.d.ts +4 -4
  10. package/dist/types/src/registry/function-registry.d.ts.map +1 -0
  11. package/dist/types/src/registry/function-registry.test.d.ts.map +1 -0
  12. package/dist/types/src/registry/index.d.ts.map +1 -0
  13. package/dist/types/src/runtime/dev-server.d.ts +1 -1
  14. package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
  15. package/dist/types/src/runtime/scheduler.d.ts +1 -2
  16. package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
  17. package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -1
  18. package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +1 -1
  19. package/dist/types/src/types.d.ts +19 -33
  20. package/dist/types/src/types.d.ts.map +1 -1
  21. package/package.json +18 -31
  22. package/schema/functions.json +9 -18
  23. package/src/index.ts +1 -1
  24. package/src/{function → registry}/function-registry.test.ts +10 -10
  25. package/src/registry/function-registry.ts +84 -0
  26. package/src/runtime/dev-server.test.ts +2 -2
  27. package/src/runtime/dev-server.ts +6 -5
  28. package/src/runtime/scheduler.test.ts +1 -1
  29. package/src/runtime/scheduler.ts +8 -16
  30. package/src/testing/functions-integration.test.ts +1 -1
  31. package/src/testing/setup.ts +1 -1
  32. package/src/trigger/trigger-registry.test.ts +34 -60
  33. package/src/trigger/trigger-registry.ts +6 -19
  34. package/src/trigger/type/subscription-trigger.ts +10 -17
  35. package/src/types.ts +10 -12
  36. package/dist/lib/browser/chunk-366QG6IX.mjs +0 -81
  37. package/dist/lib/browser/chunk-366QG6IX.mjs.map +0 -7
  38. package/dist/lib/browser/types.mjs +0 -12
  39. package/dist/lib/browser/types.mjs.map +0 -7
  40. package/dist/lib/node/chunk-3VSJ57ZZ.cjs +0 -97
  41. package/dist/lib/node/chunk-3VSJ57ZZ.cjs.map +0 -7
  42. package/dist/lib/node/types.cjs +0 -33
  43. package/dist/lib/node/types.cjs.map +0 -7
  44. package/dist/types/src/function/function-registry.d.ts.map +0 -1
  45. package/dist/types/src/function/function-registry.test.d.ts.map +0 -1
  46. package/dist/types/src/function/index.d.ts.map +0 -1
  47. package/dist/types/src/util.d.ts +0 -15
  48. package/dist/types/src/util.d.ts.map +0 -1
  49. package/dist/types/src/util.test.d.ts +0 -2
  50. package/dist/types/src/util.test.d.ts.map +0 -1
  51. package/src/function/function-registry.ts +0 -90
  52. package/src/util.test.ts +0 -43
  53. package/src/util.ts +0 -48
  54. /package/dist/types/src/{function → registry}/function-registry.test.d.ts +0 -0
  55. /package/dist/types/src/{function → registry}/index.d.ts +0 -0
  56. /package/src/{function → registry}/index.ts +0 -0
@@ -1,33 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var types_exports = {};
20
- __export(types_exports, {
21
- FunctionDef: () => import_chunk_3VSJ57ZZ.FunctionDef,
22
- FunctionManifestSchema: () => import_chunk_3VSJ57ZZ.FunctionManifestSchema,
23
- FunctionTrigger: () => import_chunk_3VSJ57ZZ.FunctionTrigger
24
- });
25
- module.exports = __toCommonJS(types_exports);
26
- var import_chunk_3VSJ57ZZ = require("./chunk-3VSJ57ZZ.cjs");
27
- // Annotate the CommonJS export names for ESM import in node:
28
- 0 && (module.exports = {
29
- FunctionDef,
30
- FunctionManifestSchema,
31
- FunctionTrigger
32
- });
33
- //# sourceMappingURL=types.cjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["types.cjs"],
4
- "sourcesContent": ["import {\n FunctionDef,\n FunctionManifestSchema,\n FunctionTrigger\n} from \"./chunk-3VSJ57ZZ.cjs\";\nexport {\n FunctionDef,\n FunctionManifestSchema,\n FunctionTrigger\n};\n//# sourceMappingURL=types.cjs.map\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAIO;",
6
- "names": []
7
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"function-registry.d.ts","sourceRoot":"","sources":["../../../../src/function/function-registry.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAkB,KAAK,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,KAAK,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAKvD,OAAO,EAAE,WAAW,EAAE,KAAK,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAG9D,MAAM,MAAM,wBAAwB,GAAG;IACrC,KAAK,EAAE,KAAK,CAAC;IACb,KAAK,EAAE,WAAW,EAAE,CAAC;CACtB,CAAC;AAEF,qBAAa,gBAAiB,SAAQ,QAAQ;IAKhC,OAAO,CAAC,QAAQ,CAAC,OAAO;IAJpC,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA4D;IAEhG,SAAgB,UAAU,kCAAyC;gBAEtC,OAAO,EAAE,MAAM;IAIrC,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,WAAW,EAAE;IAIhD;;;OAGG;IACU,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,gBAAgB,CAAC,WAAW,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;cAiBnE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;cAgCtB,MAAM,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAG3D"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"function-registry.test.d.ts","sourceRoot":"","sources":["../../../../src/function/function-registry.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/function/index.ts"],"names":[],"mappings":"AAIA,cAAc,qBAAqB,CAAC"}
@@ -1,15 +0,0 @@
1
- export type Comparator<A, B = A> = (a: A, b: B) => boolean;
2
- export type DiffResult<A, B = A> = {
3
- added: B[];
4
- updated: A[];
5
- removed: A[];
6
- };
7
- /**
8
- *
9
- * @param previous
10
- * @param next
11
- * @param comparator
12
- */
13
- export declare const diff: <A, B = A>(previous: readonly A[], next: readonly B[], comparator: Comparator<A, B>) => DiffResult<A, B>;
14
- export declare const intersection: <A, B = A>(a: A[], b: B[], comparator: Comparator<A, B>) => A[];
15
- //# sourceMappingURL=util.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/util.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC;AAE3D,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI;IACjC,KAAK,EAAE,CAAC,EAAE,CAAC;IACX,OAAO,EAAE,CAAC,EAAE,CAAC;IACb,OAAO,EAAE,CAAC,EAAE,CAAC;CACd,CAAC;AAEF;;;;;GAKG;AAEH,eAAO,MAAM,IAAI,0GAwBhB,CAAC;AAGF,eAAO,MAAM,YAAY,iEACuC,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=util.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.test.d.ts","sourceRoot":"","sources":["../../../src/util.test.ts"],"names":[],"mappings":""}
@@ -1,90 +0,0 @@
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 { log } from '@dxos/log';
11
- import { ComplexMap } from '@dxos/util';
12
-
13
- import { FunctionDef, type FunctionManifest } from '../types';
14
- import { diff } from '../util';
15
-
16
- export type FunctionsRegisteredEvent = {
17
- space: Space;
18
- added: FunctionDef[];
19
- };
20
-
21
- export class FunctionRegistry extends Resource {
22
- private readonly _functionBySpaceKey = new ComplexMap<PublicKey, FunctionDef[]>(PublicKey.hash);
23
-
24
- public readonly registered = new Event<FunctionsRegisteredEvent>();
25
-
26
- constructor(private readonly _client: Client) {
27
- super();
28
- }
29
-
30
- public getFunctions(space: Space): FunctionDef[] {
31
- return this._functionBySpaceKey.get(space.key) ?? [];
32
- }
33
-
34
- /**
35
- * Loads function definitions from the manifest into the space.
36
- * We first load all the definitions from the space to deduplicate by functionId.
37
- */
38
- public async register(space: Space, functions: FunctionManifest['functions']): Promise<void> {
39
- log('register', { space: space.key, functions: functions?.length ?? 0 });
40
- if (!functions?.length) {
41
- return;
42
- }
43
- if (!space.db.graph.runtimeSchemaRegistry.hasSchema(FunctionDef)) {
44
- space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionDef);
45
- }
46
-
47
- // Sync definitions.
48
- const { objects: existing } = await space.db.query(Filter.schema(FunctionDef)).run();
49
- const { added, removed } = diff(existing, functions, (a, b) => a.uri === b.uri);
50
- added.forEach((def) => space.db.add(create(FunctionDef, def)));
51
- // TODO(burdon): Update existing templates.
52
- removed.forEach((def) => space.db.remove(def));
53
- }
54
-
55
- protected override async _open(): Promise<void> {
56
- const spacesSubscription = this._client.spaces.subscribe(async (spaces) => {
57
- for (const space of spaces) {
58
- if (this._functionBySpaceKey.has(space.key)) {
59
- continue;
60
- }
61
-
62
- const registered: FunctionDef[] = [];
63
- this._functionBySpaceKey.set(space.key, registered);
64
- await space.waitUntilReady();
65
- if (this._ctx.disposed) {
66
- break;
67
- }
68
-
69
- // Subscribe to updates.
70
- this._ctx.onDispose(
71
- space.db.query(Filter.schema(FunctionDef)).subscribe(({ objects }) => {
72
- const { added } = diff(registered, objects, (a, b) => a.uri === b.uri);
73
- // TODO(burdon): Update and remove.
74
- if (added.length > 0) {
75
- registered.push(...added);
76
- this.registered.emit({ space, added });
77
- }
78
- }),
79
- );
80
- }
81
- });
82
-
83
- // TODO(burdon): API: Normalize unsubscribe methods.
84
- this._ctx.onDispose(() => spacesSubscription.unsubscribe());
85
- }
86
-
87
- protected override async _close(_: Context): Promise<void> {
88
- this._functionBySpaceKey.clear();
89
- }
90
- }
package/src/util.test.ts DELETED
@@ -1,43 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { expect } from 'chai';
6
-
7
- import { describe, test } from '@dxos/test';
8
-
9
- import { diff, intersection } from './util';
10
-
11
- describe('diff', () => {
12
- test('returns the difference between two sets', () => {
13
- {
14
- const { added, updated, removed } = diff<number>([], [], (a, b) => a === b);
15
- expect(added).to.deep.eq([]);
16
- expect(updated).to.deep.eq([]);
17
- expect(removed).to.deep.eq([]);
18
- }
19
- {
20
- const previous = [1, 2, 3];
21
- const next = [2, 3, 4];
22
- const { added, updated, removed } = diff(previous, next, (a, b) => a === b);
23
- expect(added).to.deep.eq([4]);
24
- expect(updated).to.deep.eq([2, 3]);
25
- expect(removed).to.deep.eq([1]);
26
- }
27
- {
28
- const previous = [{ x: 1 }, { x: 2 }, { x: 3 }];
29
- const next = [{ x: 2 }, { x: 3 }, { x: 4 }];
30
- const { added, updated, removed } = diff(previous, next, (a, b) => a.x === b.x);
31
- expect(added).to.deep.eq([{ x: 4 }]);
32
- expect(updated).to.deep.eq([{ x: 2 }, { x: 3 }]);
33
- expect(removed).to.deep.eq([{ x: 1 }]);
34
- }
35
- });
36
-
37
- test('intersection', () => {
38
- expect(intersection([1, 2, 3], [2, 3, 4], (a, b) => a === b)).to.deep.eq([2, 3]);
39
- expect(
40
- intersection([{ x: 1 }, { x: 2 }, { x: 3 }], [{ x: 2 }, { x: 3 }, { x: 4 }], (a, b) => a.x === b.x),
41
- ).to.deep.eq([{ x: 2 }, { x: 3 }]);
42
- });
43
- });
package/src/util.ts DELETED
@@ -1,48 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- export type Comparator<A, B = A> = (a: A, b: B) => boolean;
6
-
7
- export type DiffResult<A, B = A> = {
8
- added: B[];
9
- updated: A[];
10
- removed: A[];
11
- };
12
-
13
- /**
14
- *
15
- * @param previous
16
- * @param next
17
- * @param comparator
18
- */
19
- // TODO(burdon): Factor out.
20
- export const diff = <A, B = A>(
21
- previous: readonly A[],
22
- next: readonly B[],
23
- comparator: Comparator<A, B>,
24
- ): DiffResult<A, B> => {
25
- const remaining = [...previous];
26
- const result: DiffResult<A, B> = {
27
- added: [],
28
- updated: [],
29
- removed: remaining,
30
- };
31
-
32
- // TODO(burdon): Mark and sweep.
33
- for (const object of next) {
34
- const index = remaining.findIndex((item) => comparator(item, object));
35
- if (index === -1) {
36
- result.added.push(object);
37
- } else {
38
- result.updated.push(remaining[index]);
39
- remaining.splice(index, 1);
40
- }
41
- }
42
-
43
- return result;
44
- };
45
-
46
- // TODO(burdon): Factor out.
47
- export const intersection = <A, B = A>(a: A[], b: B[], comparator: Comparator<A, B>): A[] =>
48
- a.filter((a) => b.find((b) => comparator(a, b)) !== undefined);
File without changes