@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
@@ -29,116 +29,189 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  var node_exports = {};
30
30
  __export(node_exports, {
31
31
  DevServer: () => DevServer,
32
- FunctionDef: () => import_chunk_3VSJ57ZZ.FunctionDef,
33
- FunctionManifestSchema: () => import_chunk_3VSJ57ZZ.FunctionManifestSchema,
32
+ FunctionDef: () => FunctionDef,
33
+ FunctionManifestSchema: () => FunctionManifestSchema,
34
34
  FunctionRegistry: () => FunctionRegistry,
35
- FunctionTrigger: () => import_chunk_3VSJ57ZZ.FunctionTrigger,
35
+ FunctionTrigger: () => FunctionTrigger,
36
36
  Scheduler: () => Scheduler,
37
37
  TriggerRegistry: () => TriggerRegistry,
38
38
  subscriptionHandler: () => subscriptionHandler
39
39
  });
40
40
  module.exports = __toCommonJS(node_exports);
41
- var import_chunk_3VSJ57ZZ = require("./chunk-3VSJ57ZZ.cjs");
41
+ var import_client = require("@dxos/client");
42
+ var import_log = require("@dxos/log");
43
+ var import_util = require("@dxos/util");
42
44
  var import_async = require("@dxos/async");
43
45
  var import_echo = require("@dxos/client/echo");
44
46
  var import_context = require("@dxos/context");
45
47
  var import_keys = require("@dxos/keys");
46
- var import_log = require("@dxos/log");
47
- var import_util = require("@dxos/util");
48
- var import_client = require("@dxos/client");
49
- var import_log2 = require("@dxos/log");
50
48
  var import_util2 = require("@dxos/util");
49
+ var import_echo_schema = require("@dxos/echo-schema");
51
50
  var import_express = __toESM(require("express"));
52
51
  var import_get_port_please = require("get-port-please");
53
52
  var import_node_path = require("node:path");
54
53
  var import_async2 = require("@dxos/async");
55
54
  var import_context2 = require("@dxos/context");
56
55
  var import_invariant = require("@dxos/invariant");
57
- var import_log3 = require("@dxos/log");
56
+ var import_log2 = require("@dxos/log");
58
57
  var import_node_path2 = __toESM(require("node:path"));
59
- var import_async3 = require("@dxos/async");
60
58
  var import_context3 = require("@dxos/context");
61
- var import_log4 = require("@dxos/log");
62
- var import_async4 = require("@dxos/async");
59
+ var import_log3 = require("@dxos/log");
60
+ var import_async3 = require("@dxos/async");
63
61
  var import_echo2 = require("@dxos/client/echo");
64
62
  var import_context4 = require("@dxos/context");
65
- var import_echo_schema = require("@dxos/echo-schema");
66
63
  var import_invariant2 = require("@dxos/invariant");
67
64
  var import_keys2 = require("@dxos/keys");
68
- var import_log5 = require("@dxos/log");
65
+ var import_log4 = require("@dxos/log");
69
66
  var import_util3 = require("@dxos/util");
70
67
  var import_types = require("@braneframe/types");
71
- var import_async5 = require("@dxos/async");
68
+ var import_async4 = require("@dxos/async");
72
69
  var import_echo_db = require("@dxos/echo-db");
73
- var import_log6 = require("@dxos/log");
70
+ var import_log5 = require("@dxos/log");
74
71
  var import_cron = require("cron");
75
- var import_async6 = require("@dxos/async");
76
- var import_log7 = require("@dxos/log");
72
+ var import_async5 = require("@dxos/async");
73
+ var import_log6 = require("@dxos/log");
77
74
  var import_get_port_please2 = require("get-port-please");
78
75
  var import_node_http = __toESM(require("node:http"));
79
- var import_log8 = require("@dxos/log");
76
+ var import_log7 = require("@dxos/log");
80
77
  var import_ws = __toESM(require("ws"));
81
- var import_async7 = require("@dxos/async");
82
- var import_log9 = require("@dxos/log");
83
- var diff = (previous, next, comparator) => {
84
- const remaining = [
85
- ...previous
86
- ];
87
- const result = {
88
- added: [],
89
- updated: [],
90
- removed: remaining
91
- };
92
- for (const object of next) {
93
- const index = remaining.findIndex((item) => comparator(item, object));
94
- if (index === -1) {
95
- result.added.push(object);
78
+ var import_async6 = require("@dxos/async");
79
+ var import_log8 = require("@dxos/log");
80
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
81
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
82
+ }) : x)(function(x) {
83
+ if (typeof require !== "undefined")
84
+ return require.apply(this, arguments);
85
+ throw Error('Dynamic require of "' + x + '" is not supported');
86
+ });
87
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/handler.ts";
88
+ var subscriptionHandler = (handler) => {
89
+ return ({ event: { data }, context, ...rest }) => {
90
+ const { client } = context;
91
+ const space = data.spaceKey ? client.spaces.get(import_client.PublicKey.from(data.spaceKey)) : void 0;
92
+ const objects = space ? data.objects?.map((id) => space.db.getObjectById(id)).filter(import_util.nonNullable) : [];
93
+ if (!!data.spaceKey && !space) {
94
+ import_log.log.warn("invalid space", {
95
+ data
96
+ }, {
97
+ F: __dxlog_file,
98
+ L: 91,
99
+ S: void 0,
100
+ C: (f, a) => f(...a)
101
+ });
96
102
  } else {
97
- result.updated.push(remaining[index]);
98
- remaining.splice(index, 1);
103
+ import_log.log.info("handler", {
104
+ space: space?.key.truncate(),
105
+ objects: objects?.length
106
+ }, {
107
+ F: __dxlog_file,
108
+ L: 93,
109
+ S: void 0,
110
+ C: (f, a) => f(...a)
111
+ });
99
112
  }
100
- }
101
- return result;
113
+ return handler({
114
+ event: {
115
+ data: {
116
+ ...data,
117
+ space,
118
+ objects
119
+ }
120
+ },
121
+ context,
122
+ ...rest
123
+ });
124
+ };
125
+ };
126
+ var omitEchoId = (schema) => import_echo_schema.S.make(import_echo_schema.AST.omit(schema.ast, [
127
+ "id"
128
+ ]));
129
+ var SubscriptionTriggerSchema = import_echo_schema.S.struct({
130
+ type: import_echo_schema.S.literal("subscription"),
131
+ // TODO(burdon): Define query DSL.
132
+ filter: import_echo_schema.S.array(import_echo_schema.S.struct({
133
+ type: import_echo_schema.S.string,
134
+ props: import_echo_schema.S.optional(import_echo_schema.S.record(import_echo_schema.S.string, import_echo_schema.S.any))
135
+ })),
136
+ options: import_echo_schema.S.optional(import_echo_schema.S.struct({
137
+ // Watch changes to object (not just creation).
138
+ deep: import_echo_schema.S.optional(import_echo_schema.S.boolean),
139
+ // Debounce changes (delay in ms).
140
+ delay: import_echo_schema.S.optional(import_echo_schema.S.number)
141
+ }))
142
+ });
143
+ var TimerTriggerSchema = import_echo_schema.S.struct({
144
+ type: import_echo_schema.S.literal("timer"),
145
+ cron: import_echo_schema.S.string
146
+ });
147
+ var WebhookTriggerSchema = import_echo_schema.S.mutable(import_echo_schema.S.struct({
148
+ type: import_echo_schema.S.literal("webhook"),
149
+ method: import_echo_schema.S.string,
150
+ // Assigned port.
151
+ port: import_echo_schema.S.optional(import_echo_schema.S.number)
152
+ }));
153
+ var WebsocketTriggerSchema = import_echo_schema.S.struct({
154
+ type: import_echo_schema.S.literal("websocket"),
155
+ url: import_echo_schema.S.string,
156
+ init: import_echo_schema.S.optional(import_echo_schema.S.record(import_echo_schema.S.string, import_echo_schema.S.any))
157
+ });
158
+ var TriggerSpecSchema = import_echo_schema.S.union(TimerTriggerSchema, WebhookTriggerSchema, WebsocketTriggerSchema, SubscriptionTriggerSchema);
159
+ var FunctionDef = class extends (0, import_echo_schema.TypedObject)({
160
+ typename: "dxos.org/type/FunctionDef",
161
+ version: "0.1.0"
162
+ })({
163
+ uri: import_echo_schema.S.string,
164
+ description: import_echo_schema.S.optional(import_echo_schema.S.string),
165
+ route: import_echo_schema.S.string,
166
+ // TODO(burdon): NPM/GitHub/Docker/CF URL?
167
+ handler: import_echo_schema.S.string
168
+ }) {
169
+ };
170
+ var FunctionTrigger = class extends (0, import_echo_schema.TypedObject)({
171
+ typename: "dxos.org/type/FunctionTrigger",
172
+ version: "0.1.0"
173
+ })({
174
+ function: import_echo_schema.S.string.pipe(import_echo_schema.S.description("Function ID/URI.")),
175
+ // Context passed to a function.
176
+ meta: import_echo_schema.S.optional(import_echo_schema.S.record(import_echo_schema.S.string, import_echo_schema.S.any)),
177
+ spec: TriggerSpecSchema
178
+ }) {
102
179
  };
103
- var intersection = (a, b, comparator) => a.filter((a2) => b.find((b2) => comparator(a2, b2)) !== void 0);
104
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/function/function-registry.ts";
180
+ var FunctionManifestSchema = import_echo_schema.S.struct({
181
+ functions: import_echo_schema.S.optional(import_echo_schema.S.mutable(import_echo_schema.S.array(omitEchoId(FunctionDef)))),
182
+ triggers: import_echo_schema.S.optional(import_echo_schema.S.mutable(import_echo_schema.S.array(omitEchoId(FunctionTrigger))))
183
+ });
105
184
  var FunctionRegistry = class extends import_context.Resource {
106
185
  constructor(_client) {
107
186
  super();
108
187
  this._client = _client;
109
- this._functionBySpaceKey = new import_util.ComplexMap(import_keys.PublicKey.hash);
110
- this.registered = new import_async.Event();
188
+ this._functionBySpaceKey = new import_util2.ComplexMap(import_keys.PublicKey.hash);
189
+ this.onFunctionsRegistered = new import_async.Event();
111
190
  }
112
191
  getFunctions(space) {
113
192
  return this._functionBySpaceKey.get(space.key) ?? [];
114
193
  }
115
194
  /**
116
- * Loads function definitions from the manifest into the space.
195
+ * The method loads function definitions from the manifest into the space.
117
196
  * We first load all the definitions from the space to deduplicate by functionId.
118
197
  */
119
- async register(space, functions) {
120
- (0, import_log.log)("register", {
121
- space: space.key,
122
- functions: functions?.length ?? 0
123
- }, {
124
- F: __dxlog_file,
125
- L: 39,
126
- S: this,
127
- C: (f, a) => f(...a)
128
- });
129
- if (!functions?.length) {
198
+ // TODO(burdon): This should not be space specific (they are static for the agent).
199
+ async register(space, manifest) {
200
+ if (!manifest.functions?.length) {
130
201
  return;
131
202
  }
132
- if (!space.db.graph.runtimeSchemaRegistry.hasSchema(import_chunk_3VSJ57ZZ.FunctionDef)) {
133
- space.db.graph.runtimeSchemaRegistry.registerSchema(import_chunk_3VSJ57ZZ.FunctionDef);
203
+ if (!space.db.graph.runtimeSchemaRegistry.isSchemaRegistered(FunctionDef)) {
204
+ space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionDef);
134
205
  }
135
- const { objects: existing } = await space.db.query(import_echo.Filter.schema(import_chunk_3VSJ57ZZ.FunctionDef)).run();
136
- const { added, removed } = diff(existing, functions, (a, b) => a.uri === b.uri);
137
- added.forEach((def) => space.db.add((0, import_echo.create)(import_chunk_3VSJ57ZZ.FunctionDef, def)));
138
- removed.forEach((def) => space.db.remove(def));
206
+ const { objects: existingDefinitions } = await space.db.query(import_echo.Filter.schema(FunctionDef)).run();
207
+ const newDefinitions = getNewDefinitions(manifest.functions, existingDefinitions);
208
+ const reactiveObjects = newDefinitions.map((template) => (0, import_echo.create)(FunctionDef, {
209
+ ...template
210
+ }));
211
+ reactiveObjects.forEach((obj) => space.db.add(obj));
139
212
  }
140
213
  async _open() {
141
- const spacesSubscription = this._client.spaces.subscribe(async (spaces) => {
214
+ const spaceListSubscription = this._client.spaces.subscribe(async (spaces) => {
142
215
  for (const space of spaces) {
143
216
  if (this._functionBySpaceKey.has(space.key)) {
144
217
  continue;
@@ -149,65 +222,31 @@ var FunctionRegistry = class extends import_context.Resource {
149
222
  if (this._ctx.disposed) {
150
223
  break;
151
224
  }
152
- this._ctx.onDispose(space.db.query(import_echo.Filter.schema(import_chunk_3VSJ57ZZ.FunctionDef)).subscribe(({ objects }) => {
153
- const { added } = diff(registered, objects, (a, b) => a.uri === b.uri);
154
- if (added.length > 0) {
155
- registered.push(...added);
156
- this.registered.emit({
225
+ const functionsSubscription = space.db.query(import_echo.Filter.schema(FunctionDef)).subscribe((definitions) => {
226
+ const newFunctions = getNewDefinitions(definitions.objects, registered);
227
+ if (newFunctions.length > 0) {
228
+ registered.push(...newFunctions);
229
+ this.onFunctionsRegistered.emit({
157
230
  space,
158
- added
231
+ newFunctions
159
232
  });
160
233
  }
161
- }));
234
+ });
235
+ this._ctx.onDispose(functionsSubscription);
162
236
  }
163
237
  });
164
- this._ctx.onDispose(() => spacesSubscription.unsubscribe());
238
+ this._ctx.onDispose(() => spaceListSubscription.unsubscribe());
165
239
  }
166
240
  async _close(_) {
167
241
  this._functionBySpaceKey.clear();
168
242
  }
169
243
  };
170
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/functions/src/handler.ts";
171
- var subscriptionHandler = (handler) => {
172
- return ({ event: { data }, context, ...rest }) => {
173
- const { client } = context;
174
- const space = data.spaceKey ? client.spaces.get(import_client.PublicKey.from(data.spaceKey)) : void 0;
175
- const objects = space ? data.objects?.map((id) => space.db.getObjectById(id)).filter(import_util2.nonNullable) : [];
176
- if (!!data.spaceKey && !space) {
177
- import_log2.log.warn("invalid space", {
178
- data
179
- }, {
180
- F: __dxlog_file2,
181
- L: 91,
182
- S: void 0,
183
- C: (f, a) => f(...a)
184
- });
185
- } else {
186
- import_log2.log.info("handler", {
187
- space: space?.key.truncate(),
188
- objects: objects?.length
189
- }, {
190
- F: __dxlog_file2,
191
- L: 93,
192
- S: void 0,
193
- C: (f, a) => f(...a)
194
- });
195
- }
196
- return handler({
197
- event: {
198
- data: {
199
- ...data,
200
- space,
201
- objects
202
- }
203
- },
204
- context,
205
- ...rest
206
- });
207
- };
244
+ var getNewDefinitions = (candidateList, existing) => {
245
+ return candidateList.filter((candidate) => existing.find((def) => def.uri === candidate.uri) == null);
208
246
  };
209
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/dev-server.ts";
247
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/dev-server.ts";
210
248
  var DevServer = class {
249
+ // prettier-ignore
211
250
  constructor(_client, _functionsRegistry, _options) {
212
251
  this._client = _client;
213
252
  this._functionsRegistry = _functionsRegistry;
@@ -216,14 +255,14 @@ var DevServer = class {
216
255
  this._handlers = {};
217
256
  this._seq = 0;
218
257
  this.update = new import_async2.Event();
219
- this._functionsRegistry.registered.on(async ({ added }) => {
220
- added.forEach((def) => this._load(def));
258
+ this._functionsRegistry.onFunctionsRegistered.on(async ({ newFunctions }) => {
259
+ newFunctions.forEach((def) => this._load(def));
221
260
  await this._safeUpdateRegistration();
222
- (0, import_log3.log)("new functions loaded", {
223
- added
261
+ (0, import_log2.log)("new functions loaded", {
262
+ newFunctions
224
263
  }, {
225
- F: __dxlog_file3,
226
- L: 52,
264
+ F: __dxlog_file2,
265
+ L: 53,
227
266
  S: this,
228
267
  C: (f, a) => f(...a)
229
268
  });
@@ -236,8 +275,8 @@ var DevServer = class {
236
275
  }
237
276
  get endpoint() {
238
277
  (0, import_invariant.invariant)(this._port, void 0, {
239
- F: __dxlog_file3,
240
- L: 63,
278
+ F: __dxlog_file2,
279
+ L: 64,
241
280
  S: this,
242
281
  A: [
243
282
  "this._port",
@@ -254,17 +293,17 @@ var DevServer = class {
254
293
  }
255
294
  async start() {
256
295
  (0, import_invariant.invariant)(!this._server, void 0, {
257
- F: __dxlog_file3,
258
- L: 76,
296
+ F: __dxlog_file2,
297
+ L: 77,
259
298
  S: this,
260
299
  A: [
261
300
  "!this._server",
262
301
  ""
263
302
  ]
264
303
  });
265
- import_log3.log.info("starting...", void 0, {
266
- F: __dxlog_file3,
267
- L: 77,
304
+ import_log2.log.info("starting...", void 0, {
305
+ F: __dxlog_file2,
306
+ L: 78,
268
307
  S: this,
269
308
  C: (f, a) => f(...a)
270
309
  });
@@ -274,11 +313,11 @@ var DevServer = class {
274
313
  app.post("/:path", async (req, res) => {
275
314
  const { path: path2 } = req.params;
276
315
  try {
277
- import_log3.log.info("calling", {
316
+ import_log2.log.info("calling", {
278
317
  path: path2
279
318
  }, {
280
- F: __dxlog_file3,
281
- L: 87,
319
+ F: __dxlog_file2,
320
+ L: 88,
282
321
  S: this,
283
322
  C: (f, a) => f(...a)
284
323
  });
@@ -289,9 +328,9 @@ var DevServer = class {
289
328
  res.statusCode = await this.invoke("/" + path2, req.body);
290
329
  res.end();
291
330
  } catch (err) {
292
- import_log3.log.catch(err, void 0, {
293
- F: __dxlog_file3,
294
- L: 97,
331
+ import_log2.log.catch(err, void 0, {
332
+ F: __dxlog_file2,
333
+ L: 98,
295
334
  S: this,
296
335
  C: (f, a) => f(...a)
297
336
  });
@@ -312,11 +351,11 @@ var DevServer = class {
312
351
  const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService.register({
313
352
  endpoint: this.endpoint
314
353
  });
315
- import_log3.log.info("registered", {
354
+ import_log2.log.info("registered", {
316
355
  endpoint
317
356
  }, {
318
- F: __dxlog_file3,
319
- L: 112,
357
+ F: __dxlog_file2,
358
+ L: 113,
320
359
  S: this,
321
360
  C: (f, a) => f(...a)
322
361
  });
@@ -327,44 +366,44 @@ var DevServer = class {
327
366
  await this.stop();
328
367
  throw new Error("FunctionRegistryService not available (check plugin is configured).");
329
368
  }
330
- import_log3.log.info("started", {
369
+ import_log2.log.info("started", {
331
370
  port: this._port
332
371
  }, {
333
- F: __dxlog_file3,
334
- L: 123,
372
+ F: __dxlog_file2,
373
+ L: 124,
335
374
  S: this,
336
375
  C: (f, a) => f(...a)
337
376
  });
338
377
  }
339
378
  async stop() {
340
379
  (0, import_invariant.invariant)(this._server, void 0, {
341
- F: __dxlog_file3,
342
- L: 127,
380
+ F: __dxlog_file2,
381
+ L: 128,
343
382
  S: this,
344
383
  A: [
345
384
  "this._server",
346
385
  ""
347
386
  ]
348
387
  });
349
- import_log3.log.info("stopping...", void 0, {
350
- F: __dxlog_file3,
351
- L: 128,
388
+ import_log2.log.info("stopping...", void 0, {
389
+ F: __dxlog_file2,
390
+ L: 129,
352
391
  S: this,
353
392
  C: (f, a) => f(...a)
354
393
  });
355
394
  const trigger = new import_async2.Trigger();
356
395
  this._server.close(async () => {
357
- import_log3.log.info("server stopped", void 0, {
358
- F: __dxlog_file3,
359
- L: 132,
396
+ import_log2.log.info("server stopped", void 0, {
397
+ F: __dxlog_file2,
398
+ L: 133,
360
399
  S: this,
361
400
  C: (f, a) => f(...a)
362
401
  });
363
402
  try {
364
403
  if (this._functionServiceRegistration) {
365
404
  (0, import_invariant.invariant)(this._client.services.services.FunctionRegistryService, void 0, {
366
- F: __dxlog_file3,
367
- L: 135,
405
+ F: __dxlog_file2,
406
+ L: 136,
368
407
  S: this,
369
408
  A: [
370
409
  "this._client.services.services.FunctionRegistryService",
@@ -374,11 +413,11 @@ var DevServer = class {
374
413
  await this._client.services.services.FunctionRegistryService.unregister({
375
414
  registrationId: this._functionServiceRegistration
376
415
  });
377
- import_log3.log.info("unregistered", {
416
+ import_log2.log.info("unregistered", {
378
417
  registrationId: this._functionServiceRegistration
379
418
  }, {
380
- F: __dxlog_file3,
381
- L: 140,
419
+ F: __dxlog_file2,
420
+ L: 141,
382
421
  S: this,
383
422
  C: (f, a) => f(...a)
384
423
  });
@@ -393,9 +432,9 @@ var DevServer = class {
393
432
  await trigger.wait();
394
433
  this._port = void 0;
395
434
  this._server = void 0;
396
- import_log3.log.info("stopped", void 0, {
397
- F: __dxlog_file3,
398
- L: 154,
435
+ import_log2.log.info("stopped", void 0, {
436
+ F: __dxlog_file2,
437
+ L: 155,
399
438
  S: this,
400
439
  C: (f, a) => f(...a)
401
440
  });
@@ -403,24 +442,24 @@ var DevServer = class {
403
442
  /**
404
443
  * Load function.
405
444
  */
406
- async _load(def, force) {
445
+ async _load(def, force = false) {
407
446
  const { uri, route, handler } = def;
408
447
  const filePath = (0, import_node_path.join)(this._options.baseDir, handler);
409
- import_log3.log.info("loading", {
448
+ import_log2.log.info("loading", {
410
449
  uri,
411
450
  force
412
451
  }, {
413
- F: __dxlog_file3,
414
- L: 163,
452
+ F: __dxlog_file2,
453
+ L: 164,
415
454
  S: this,
416
455
  C: (f, a) => f(...a)
417
456
  });
418
457
  if (force) {
419
- Object.keys(import_chunk_3VSJ57ZZ.__require.cache).filter((key) => key.startsWith(filePath)).forEach((key) => {
420
- delete import_chunk_3VSJ57ZZ.__require.cache[key];
458
+ Object.keys(__require.cache).filter((key) => key.startsWith(filePath)).forEach((key) => {
459
+ delete __require.cache[key];
421
460
  });
422
461
  }
423
- const module2 = (0, import_chunk_3VSJ57ZZ.__require)(filePath);
462
+ const module2 = __require(filePath);
424
463
  if (typeof module2.default !== "function") {
425
464
  throw new Error(`Handler must export default function: ${uri}`);
426
465
  }
@@ -431,8 +470,8 @@ var DevServer = class {
431
470
  }
432
471
  async _safeUpdateRegistration() {
433
472
  (0, import_invariant.invariant)(this._functionServiceRegistration, void 0, {
434
- F: __dxlog_file3,
435
- L: 185,
473
+ F: __dxlog_file2,
474
+ L: 186,
436
475
  S: this,
437
476
  A: [
438
477
  "this._functionServiceRegistration",
@@ -448,9 +487,9 @@ var DevServer = class {
448
487
  }))
449
488
  });
450
489
  } catch (e) {
451
- import_log3.log.catch(e, void 0, {
452
- F: __dxlog_file3,
453
- L: 192,
490
+ import_log2.log.catch(e, void 0, {
491
+ F: __dxlog_file2,
492
+ L: 193,
454
493
  S: this,
455
494
  C: (f, a) => f(...a)
456
495
  });
@@ -462,26 +501,26 @@ var DevServer = class {
462
501
  async invoke(path2, data) {
463
502
  const seq = ++this._seq;
464
503
  const now = Date.now();
465
- import_log3.log.info("req", {
504
+ import_log2.log.info("req", {
466
505
  seq,
467
506
  path: path2
468
507
  }, {
469
- F: __dxlog_file3,
470
- L: 203,
508
+ F: __dxlog_file2,
509
+ L: 204,
471
510
  S: this,
472
511
  C: (f, a) => f(...a)
473
512
  });
474
513
  const statusCode = await this._invoke(path2, {
475
514
  data
476
515
  });
477
- import_log3.log.info("res", {
516
+ import_log2.log.info("res", {
478
517
  seq,
479
518
  path: path2,
480
519
  statusCode,
481
520
  duration: Date.now() - now
482
521
  }, {
483
- F: __dxlog_file3,
484
- L: 206,
522
+ F: __dxlog_file2,
523
+ L: 207,
485
524
  S: this,
486
525
  C: (f, a) => f(...a)
487
526
  });
@@ -491,8 +530,8 @@ var DevServer = class {
491
530
  async _invoke(path2, event) {
492
531
  const { handler } = this._handlers[path2] ?? {};
493
532
  (0, import_invariant.invariant)(handler, `invalid path: ${path2}`, {
494
- F: __dxlog_file3,
495
- L: 213,
533
+ F: __dxlog_file2,
534
+ L: 214,
496
535
  S: this,
497
536
  A: [
498
537
  "handler",
@@ -521,16 +560,15 @@ var DevServer = class {
521
560
  var createContext = () => new import_context2.Context({
522
561
  name: "DevServer"
523
562
  });
524
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/scheduler.ts";
563
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/scheduler.ts";
525
564
  var Scheduler = class {
526
565
  constructor(functions, triggers, _options = {}) {
527
566
  this.functions = functions;
528
567
  this.triggers = triggers;
529
568
  this._options = _options;
530
569
  this._ctx = createContext2();
531
- this._callMutex = new import_async3.Mutex();
532
- this.functions.registered.on(async ({ space, added }) => {
533
- await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space), added);
570
+ this.functions.onFunctionsRegistered.on(async ({ space, newFunctions }) => {
571
+ await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space), newFunctions);
534
572
  });
535
573
  this.triggers.registered.on(async ({ space, triggers: triggers2 }) => {
536
574
  await this._safeActivateTriggers(space, triggers2, this.functions.getFunctions(space));
@@ -547,25 +585,24 @@ var Scheduler = class {
547
585
  await this.functions.close();
548
586
  await this.triggers.close();
549
587
  }
550
- // TODO(burdon): Remove and update registries directly.
551
588
  async register(space, manifest) {
552
- await this.functions.register(space, manifest.functions);
589
+ await this.functions.register(space, manifest);
553
590
  await this.triggers.register(space, manifest);
554
591
  }
555
592
  async _safeActivateTriggers(space, triggers, functions) {
556
593
  const mountTasks = triggers.map((trigger) => {
557
594
  return this.activate(space, functions, trigger);
558
595
  });
559
- await Promise.all(mountTasks).catch(import_log4.log.catch);
596
+ await Promise.all(mountTasks).catch(import_log3.log.catch);
560
597
  }
561
598
  async activate(space, functions, fnTrigger) {
562
599
  const definition = functions.find((def) => def.uri === fnTrigger.function);
563
600
  if (!definition) {
564
- import_log4.log.info("function is not found for trigger", {
601
+ import_log3.log.info("function is not found for trigger", {
565
602
  fnTrigger
566
603
  }, {
567
- F: __dxlog_file4,
568
- L: 78,
604
+ F: __dxlog_file3,
605
+ L: 74,
569
606
  S: this,
570
607
  C: (f, a) => f(...a)
571
608
  });
@@ -574,27 +611,25 @@ var Scheduler = class {
574
611
  await this.triggers.activate({
575
612
  space
576
613
  }, fnTrigger, async (args) => {
577
- return this._callMutex.executeSynchronized(() => {
578
- return this._execFunction(definition, fnTrigger, {
579
- meta: fnTrigger.meta,
580
- data: {
581
- ...args,
582
- spaceKey: space.key
583
- }
584
- });
614
+ return this._execFunction(definition, {
615
+ meta: fnTrigger.meta,
616
+ data: {
617
+ ...args,
618
+ spaceKey: space.key
619
+ }
585
620
  });
586
621
  });
587
- (0, import_log4.log)("activated trigger", {
622
+ (0, import_log3.log)("activated trigger", {
588
623
  space: space.key,
589
624
  trigger: fnTrigger
590
625
  }, {
591
- F: __dxlog_file4,
592
- L: 91,
626
+ F: __dxlog_file3,
627
+ L: 84,
593
628
  S: this,
594
629
  C: (f, a) => f(...a)
595
630
  });
596
631
  }
597
- async _execFunction(def, trigger, { data, meta }) {
632
+ async _execFunction(def, { data, meta }) {
598
633
  let status = 0;
599
634
  try {
600
635
  const payload = Object.assign({}, meta && {
@@ -603,13 +638,12 @@ var Scheduler = class {
603
638
  const { endpoint, callback } = this._options;
604
639
  if (endpoint) {
605
640
  const url = import_node_path2.default.join(endpoint, def.route);
606
- import_log4.log.info("exec", {
641
+ import_log3.log.info("exec", {
607
642
  function: def.uri,
608
- url,
609
- triggerType: trigger.spec.type
643
+ url
610
644
  }, {
611
- F: __dxlog_file4,
612
- L: 108,
645
+ F: __dxlog_file3,
646
+ L: 100,
613
647
  S: this,
614
648
  C: (f, a) => f(...a)
615
649
  });
@@ -622,11 +656,11 @@ var Scheduler = class {
622
656
  });
623
657
  status = response.status;
624
658
  } else if (callback) {
625
- import_log4.log.info("exec", {
659
+ import_log3.log.info("exec", {
626
660
  function: def.uri
627
661
  }, {
628
- F: __dxlog_file4,
629
- L: 119,
662
+ F: __dxlog_file3,
663
+ L: 111,
630
664
  S: this,
631
665
  C: (f, a) => f(...a)
632
666
  });
@@ -635,22 +669,22 @@ var Scheduler = class {
635
669
  if (status && status >= 400) {
636
670
  throw new Error(`Response: ${status}`);
637
671
  }
638
- import_log4.log.info("done", {
672
+ import_log3.log.info("done", {
639
673
  function: def.uri,
640
674
  status
641
675
  }, {
642
- F: __dxlog_file4,
643
- L: 129,
676
+ F: __dxlog_file3,
677
+ L: 121,
644
678
  S: this,
645
679
  C: (f, a) => f(...a)
646
680
  });
647
681
  } catch (err) {
648
- import_log4.log.error("error", {
682
+ import_log3.log.error("error", {
649
683
  function: def.uri,
650
684
  error: err.message
651
685
  }, {
652
- F: __dxlog_file4,
653
- L: 131,
686
+ F: __dxlog_file3,
687
+ L: 123,
654
688
  S: this,
655
689
  C: (f, a) => f(...a)
656
690
  });
@@ -662,59 +696,53 @@ var Scheduler = class {
662
696
  var createContext2 = () => new import_context3.Context({
663
697
  name: "FunctionScheduler"
664
698
  });
665
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/subscription-trigger.ts";
699
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/subscription-trigger.ts";
666
700
  var createSubscriptionTrigger = async (ctx, triggerCtx, spec, callback) => {
667
701
  const objectIds = /* @__PURE__ */ new Set();
668
- const task = new import_async5.UpdateScheduler(ctx, async () => {
702
+ const task = new import_async4.DeferredTask(ctx, async () => {
669
703
  if (objectIds.size > 0) {
670
- const objects = Array.from(objectIds);
671
- objectIds.clear();
672
704
  await callback({
673
- objects
705
+ objects: Array.from(objectIds)
674
706
  });
707
+ objectIds.clear();
675
708
  }
676
- }, {
677
- maxFrequency: 4
678
709
  });
679
710
  const subscriptions = [];
680
711
  const subscription = (0, import_echo_db.createSubscription)(({ added, updated }) => {
681
- const sizeBefore = objectIds.size;
712
+ import_log5.log.info("updated", {
713
+ added: added.length,
714
+ updated: updated.length
715
+ }, {
716
+ F: __dxlog_file4,
717
+ L: 32,
718
+ S: void 0,
719
+ C: (f, a) => f(...a)
720
+ });
682
721
  for (const object of added) {
683
722
  objectIds.add(object.id);
684
723
  }
685
724
  for (const object of updated) {
686
725
  objectIds.add(object.id);
687
726
  }
688
- if (objectIds.size > sizeBefore) {
689
- import_log6.log.info("updated", {
690
- added: added.length,
691
- updated: updated.length
692
- }, {
693
- F: __dxlog_file5,
694
- L: 45,
695
- S: void 0,
696
- C: (f, a) => f(...a)
697
- });
698
- task.trigger();
699
- }
727
+ task.schedule();
700
728
  });
701
729
  subscriptions.push(() => subscription.unsubscribe());
702
730
  const { filter, options: { deep, delay } = {} } = spec;
703
731
  const update = ({ objects }) => {
704
732
  subscription.update(objects);
705
733
  if (deep) {
706
- import_log6.log.info("update", {
734
+ import_log5.log.info("update", {
707
735
  objects: objects.length
708
736
  }, {
709
- F: __dxlog_file5,
710
- L: 59,
737
+ F: __dxlog_file4,
738
+ L: 52,
711
739
  S: void 0,
712
740
  C: (f, a) => f(...a)
713
741
  });
714
742
  for (const object of objects) {
715
743
  const content = object.content;
716
744
  if (content instanceof import_types.TextV0Type) {
717
- subscriptions.push((0, import_echo_db.getAutomergeObjectCore)(content).updates.on((0, import_async5.debounce)(() => subscription.update([
745
+ subscriptions.push((0, import_echo_db.getAutomergeObjectCore)(content).updates.on((0, import_async4.debounce)(() => subscription.update([
718
746
  object
719
747
  ]), 1e3)));
720
748
  }
@@ -722,14 +750,14 @@ var createSubscriptionTrigger = async (ctx, triggerCtx, spec, callback) => {
722
750
  }
723
751
  };
724
752
  const query = triggerCtx.space.db.query(import_echo_db.Filter.or(filter.map(({ type, props }) => import_echo_db.Filter.typename(type, props))));
725
- subscriptions.push(query.subscribe(delay ? (0, import_async5.debounce)(update, delay) : update));
753
+ subscriptions.push(query.subscribe(delay ? (0, import_async4.debounce)(update, delay) : update));
726
754
  ctx.onDispose(() => {
727
755
  subscriptions.forEach((unsubscribe) => unsubscribe());
728
756
  });
729
757
  };
730
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/timer-trigger.ts";
758
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/timer-trigger.ts";
731
759
  var createTimerTrigger = async (ctx, triggerContext, spec, callback) => {
732
- const task = new import_async6.DeferredTask(ctx, async () => {
760
+ const task = new import_async5.DeferredTask(ctx, async () => {
733
761
  await callback({});
734
762
  });
735
763
  let last = 0;
@@ -742,12 +770,12 @@ var createTimerTrigger = async (ctx, triggerContext, spec, callback) => {
742
770
  const delta = last ? now - last : 0;
743
771
  last = now;
744
772
  run++;
745
- import_log7.log.info("tick", {
773
+ import_log6.log.info("tick", {
746
774
  space: triggerContext.space.key.truncate(),
747
775
  count: run,
748
776
  delta
749
777
  }, {
750
- F: __dxlog_file6,
778
+ F: __dxlog_file5,
751
779
  L: 37,
752
780
  S: void 0,
753
781
  C: (f, a) => f(...a)
@@ -758,7 +786,7 @@ var createTimerTrigger = async (ctx, triggerContext, spec, callback) => {
758
786
  job.start();
759
787
  ctx.onDispose(() => job.stop());
760
788
  };
761
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/webhook-trigger.ts";
789
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/webhook-trigger.ts";
762
790
  var createWebhookTrigger = async (ctx, _, spec, callback) => {
763
791
  const server = import_node_http.default.createServer(async (req, res) => {
764
792
  if (req.method !== spec.method) {
@@ -772,10 +800,10 @@ var createWebhookTrigger = async (ctx, _, spec, callback) => {
772
800
  random: true
773
801
  });
774
802
  server.listen(port, () => {
775
- import_log8.log.info("started webhook", {
803
+ import_log7.log.info("started webhook", {
776
804
  port
777
805
  }, {
778
- F: __dxlog_file7,
806
+ F: __dxlog_file6,
779
807
  L: 40,
780
808
  S: void 0,
781
809
  C: (f, a) => f(...a)
@@ -786,7 +814,7 @@ var createWebhookTrigger = async (ctx, _, spec, callback) => {
786
814
  server.close();
787
815
  });
788
816
  };
789
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/websocket-trigger.ts";
817
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/websocket-trigger.ts";
790
818
  var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
791
819
  retryDelay: 2,
792
820
  maxAttempts: 5
@@ -794,14 +822,14 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
794
822
  const { url, init } = spec;
795
823
  let ws;
796
824
  for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
797
- const open = new import_async7.Trigger();
825
+ const open = new import_async6.Trigger();
798
826
  ws = new import_ws.default(url);
799
827
  Object.assign(ws, {
800
828
  onopen: () => {
801
- import_log9.log.info("opened", {
829
+ import_log8.log.info("opened", {
802
830
  url
803
831
  }, {
804
- F: __dxlog_file8,
832
+ F: __dxlog_file7,
805
833
  L: 39,
806
834
  S: void 0,
807
835
  C: (f, a) => f(...a)
@@ -812,21 +840,21 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
812
840
  open.wake(true);
813
841
  },
814
842
  onclose: (event) => {
815
- import_log9.log.info("closed", {
843
+ import_log8.log.info("closed", {
816
844
  url,
817
845
  code: event.code
818
846
  }, {
819
- F: __dxlog_file8,
847
+ F: __dxlog_file7,
820
848
  L: 48,
821
849
  S: void 0,
822
850
  C: (f, a) => f(...a)
823
851
  });
824
852
  if (event.code === 1006) {
825
853
  setTimeout(async () => {
826
- import_log9.log.info(`reconnecting in ${options.retryDelay}s...`, {
854
+ import_log8.log.info(`reconnecting in ${options.retryDelay}s...`, {
827
855
  url
828
856
  }, {
829
- F: __dxlog_file8,
857
+ F: __dxlog_file7,
830
858
  L: 53,
831
859
  S: void 0,
832
860
  C: (f, a) => f(...a)
@@ -837,10 +865,10 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
837
865
  open.wake(false);
838
866
  },
839
867
  onerror: (event) => {
840
- import_log9.log.catch(event.error, {
868
+ import_log8.log.catch(event.error, {
841
869
  url
842
870
  }, {
843
- F: __dxlog_file8,
871
+ F: __dxlog_file7,
844
872
  L: 62,
845
873
  S: void 0,
846
874
  C: (f, a) => f(...a)
@@ -848,8 +876,8 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
848
876
  },
849
877
  onmessage: async (event) => {
850
878
  try {
851
- import_log9.log.info("message", void 0, {
852
- F: __dxlog_file8,
879
+ import_log8.log.info("message", void 0, {
880
+ F: __dxlog_file7,
853
881
  L: 67,
854
882
  S: void 0,
855
883
  C: (f, a) => f(...a)
@@ -859,10 +887,10 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
859
887
  data
860
888
  });
861
889
  } catch (err) {
862
- import_log9.log.catch(err, {
890
+ import_log8.log.catch(err, {
863
891
  url
864
892
  }, {
865
- F: __dxlog_file8,
893
+ F: __dxlog_file7,
866
894
  L: 71,
867
895
  S: void 0,
868
896
  C: (f, a) => f(...a)
@@ -876,15 +904,15 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
876
904
  } else {
877
905
  const wait = Math.pow(attempt, 2) * options.retryDelay;
878
906
  if (attempt < options.maxAttempts) {
879
- import_log9.log.warn(`failed to connect; trying again in ${wait}s`, {
907
+ import_log8.log.warn(`failed to connect; trying again in ${wait}s`, {
880
908
  attempt
881
909
  }, {
882
- F: __dxlog_file8,
910
+ F: __dxlog_file7,
883
911
  L: 82,
884
912
  S: void 0,
885
913
  C: (f, a) => f(...a)
886
914
  });
887
- await (0, import_async7.sleep)(wait * 1e3);
915
+ await (0, import_async6.sleep)(wait * 1e3);
888
916
  }
889
917
  }
890
918
  }
@@ -892,7 +920,7 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
892
920
  ws?.close();
893
921
  });
894
922
  };
895
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/trigger-registry.ts";
923
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/trigger-registry.ts";
896
924
  var triggerHandlers = {
897
925
  subscription: createSubscriptionTrigger,
898
926
  timer: createTimerTrigger,
@@ -905,8 +933,8 @@ var TriggerRegistry = class extends import_context4.Resource {
905
933
  this._client = _client;
906
934
  this._options = _options;
907
935
  this._triggersBySpaceKey = new import_util3.ComplexMap(import_keys2.PublicKey.hash);
908
- this.registered = new import_async4.Event();
909
- this.removed = new import_async4.Event();
936
+ this.registered = new import_async3.Event();
937
+ this.removed = new import_async3.Event();
910
938
  }
911
939
  getActiveTriggers(space) {
912
940
  return this._getTriggers(space, (t) => t.activationCtx != null);
@@ -915,12 +943,12 @@ var TriggerRegistry = class extends import_context4.Resource {
915
943
  return this._getTriggers(space, (t) => t.activationCtx == null);
916
944
  }
917
945
  async activate(triggerCtx, trigger, callback) {
918
- (0, import_log5.log)("activate", {
946
+ (0, import_log4.log)("activate", {
919
947
  space: triggerCtx.space.key,
920
948
  trigger
921
949
  }, {
922
- F: __dxlog_file9,
923
- L: 75,
950
+ F: __dxlog_file8,
951
+ L: 73,
924
952
  S: this,
925
953
  C: (f, a) => f(...a)
926
954
  });
@@ -930,8 +958,8 @@ var TriggerRegistry = class extends import_context4.Resource {
930
958
  this._ctx.onDispose(() => activationCtx.dispose());
931
959
  const registeredTrigger = this._triggersBySpaceKey.get(triggerCtx.space.key)?.find((reg) => reg.trigger.id === trigger.id);
932
960
  (0, import_invariant2.invariant)(registeredTrigger, `Trigger is not registered: ${trigger.function}`, {
933
- F: __dxlog_file9,
934
- L: 81,
961
+ F: __dxlog_file8,
962
+ L: 79,
935
963
  S: this,
936
964
  A: [
937
965
  "registeredTrigger",
@@ -951,35 +979,24 @@ var TriggerRegistry = class extends import_context4.Resource {
951
979
  * Loads triggers from the manifest into the space.
952
980
  */
953
981
  async register(space, manifest) {
954
- (0, import_log5.log)("register", {
982
+ (0, import_log4.log)("register", {
955
983
  space: space.key
956
984
  }, {
957
- F: __dxlog_file9,
958
- L: 97,
985
+ F: __dxlog_file8,
986
+ L: 95,
959
987
  S: this,
960
988
  C: (f, a) => f(...a)
961
989
  });
962
990
  if (!manifest.triggers?.length) {
963
991
  return;
964
992
  }
965
- if (!space.db.graph.runtimeSchemaRegistry.hasSchema(import_chunk_3VSJ57ZZ.FunctionTrigger)) {
966
- space.db.graph.runtimeSchemaRegistry.registerSchema(import_chunk_3VSJ57ZZ.FunctionTrigger);
993
+ if (!space.db.graph.runtimeSchemaRegistry.isSchemaRegistered(FunctionTrigger)) {
994
+ space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionTrigger);
967
995
  }
968
- const { objects: existing } = await space.db.query(import_echo2.Filter.schema(import_chunk_3VSJ57ZZ.FunctionTrigger)).run();
969
- const { added, removed } = diff(existing, manifest.triggers, (a, b) => {
970
- const keys = b[import_echo_schema.ECHO_ATTR_META]?.keys ?? [
971
- (0, import_echo_schema.foreignKey)("manifest", [
972
- b.function,
973
- b.spec.type
974
- ].join("-"))
975
- ];
976
- return intersection((0, import_echo2.getMeta)(a)?.keys ?? [], keys, import_echo_schema.foreignKeyEquals).length > 0;
977
- });
978
- added.forEach((trigger) => {
979
- const { meta, object } = (0, import_echo_schema.splitMeta)(trigger);
980
- space.db.add((0, import_echo2.create)(import_chunk_3VSJ57ZZ.FunctionTrigger, object, meta));
981
- });
982
- removed.forEach((trigger) => space.db.remove(trigger));
996
+ const reactiveObjects = manifest.triggers.map((template) => (0, import_echo2.create)(FunctionTrigger, {
997
+ ...template
998
+ }));
999
+ reactiveObjects.forEach((obj) => space.db.add(obj));
983
1000
  }
984
1001
  async _open() {
985
1002
  const spaceListSubscription = this._client.spaces.subscribe(async (spaces) => {
@@ -993,7 +1010,7 @@ var TriggerRegistry = class extends import_context4.Resource {
993
1010
  if (this._ctx.disposed) {
994
1011
  break;
995
1012
  }
996
- const functionsSubscription = space.db.query(import_echo2.Filter.schema(import_chunk_3VSJ57ZZ.FunctionTrigger)).subscribe(async (triggers) => {
1013
+ const functionsSubscription = space.db.query(import_echo2.Filter.schema(FunctionTrigger)).subscribe(async (triggers) => {
997
1014
  await this._handleRemovedTriggers(space, triggers.objects, registered);
998
1015
  this._handleNewTriggers(space, triggers.objects, registered);
999
1016
  });
@@ -1014,12 +1031,12 @@ var TriggerRegistry = class extends import_context4.Resource {
1014
1031
  trigger
1015
1032
  }));
1016
1033
  registered.push(...newRegisteredTriggers);
1017
- (0, import_log5.log)("registered new triggers", () => ({
1034
+ (0, import_log4.log)("registered new triggers", () => ({
1018
1035
  spaceKey: space.key,
1019
1036
  functions: newTriggers.map((t) => t.function)
1020
1037
  }), {
1021
- F: __dxlog_file9,
1022
- L: 159,
1038
+ F: __dxlog_file8,
1039
+ L: 146,
1023
1040
  S: this,
1024
1041
  C: (f, a) => f(...a)
1025
1042
  });