@dxos/compute 0.8.4-main.5ad4a44 → 0.8.4-main.66e292d

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.
@@ -78,20 +78,13 @@ var inRange = (range, cell) => {
78
78
  import { Event } from "@dxos/async";
79
79
  import { Resource } from "@dxos/context";
80
80
  import { DetailedCellError as DetailedCellError2 } from "@dxos/vendor-hyperformula";
81
- function _define_property(obj, key, value) {
82
- if (key in obj) {
83
- Object.defineProperty(obj, key, {
84
- value,
85
- enumerable: true,
86
- configurable: true,
87
- writable: true
88
- });
89
- } else {
90
- obj[key] = value;
91
- }
92
- return obj;
93
- }
94
81
  var ComputeNode = class extends Resource {
82
+ _graph;
83
+ sheetId;
84
+ update = new Event();
85
+ constructor(_graph, sheetId) {
86
+ super(), this._graph = _graph, this.sheetId = sheetId;
87
+ }
95
88
  get graph() {
96
89
  return this._graph;
97
90
  }
@@ -124,9 +117,6 @@ var ComputeNode = class extends Resource {
124
117
  // TODO(burdon): Load data into sheet.
125
118
  async _open() {
126
119
  }
127
- constructor(_graph, sheetId) {
128
- super(), _define_property(this, "_graph", void 0), _define_property(this, "sheetId", void 0), _define_property(this, "update", void 0), this._graph = _graph, this.sheetId = sheetId, this.update = new Event();
129
- }
130
120
  };
131
121
 
132
122
  // src/functions/async-function.ts
@@ -134,25 +124,15 @@ import defaultsDeep from "lodash.defaultsdeep";
134
124
  import { debounce } from "@dxos/async";
135
125
  import { log } from "@dxos/log";
136
126
  import { CellError, EmptyValue, ErrorType, FunctionPlugin } from "@dxos/vendor-hyperformula";
137
- function _define_property2(obj, key, value) {
138
- if (key in obj) {
139
- Object.defineProperty(obj, key, {
140
- value,
141
- enumerable: true,
142
- configurable: true,
143
- writable: true
144
- });
145
- } else {
146
- obj[key] = value;
147
- }
148
- return obj;
149
- }
150
127
  var __dxlog_file2 = "/__w/dxos/dxos/packages/core/compute/src/functions/async-function.ts";
151
128
  var defaultFunctionContextOptions = {
152
129
  defaultTtl: 5e3,
153
130
  debounceDelay: 200
154
131
  };
155
132
  var FunctionContext = class _FunctionContext {
133
+ _hf;
134
+ _runtime;
135
+ _space;
156
136
  // Mangle name with params.
157
137
  static createInvocationKey(name, ...args) {
158
138
  return JSON.stringify({
@@ -160,6 +140,34 @@ var FunctionContext = class _FunctionContext {
160
140
  ...args
161
141
  });
162
142
  }
143
+ // TODO(wittjosiah): Persist cached values.
144
+ // Cached values for cell.
145
+ _cache = /* @__PURE__ */ new Map();
146
+ // Active requests.
147
+ _pending = /* @__PURE__ */ new Map();
148
+ // Query subscriptions.
149
+ _subscriptions = /* @__PURE__ */ new Map();
150
+ // Invocation count.
151
+ _invocations = {};
152
+ _options;
153
+ // Debounced update handler.
154
+ _onUpdate;
155
+ constructor(_hf, _runtime, _space, _options) {
156
+ this._hf = _hf;
157
+ this._runtime = _runtime;
158
+ this._space = _space;
159
+ this._options = defaultsDeep(_options ?? {}, defaultFunctionContextOptions);
160
+ this._onUpdate = debounce((update) => {
161
+ log("update", update, {
162
+ F: __dxlog_file2,
163
+ L: 86,
164
+ S: this,
165
+ C: (f, a) => f(...a)
166
+ });
167
+ this._hf.resumeEvaluation();
168
+ this._options.onUpdate?.(update);
169
+ }, this._options.debounceDelay);
170
+ }
163
171
  get space() {
164
172
  return this._space;
165
173
  }
@@ -248,35 +256,6 @@ var FunctionContext = class _FunctionContext {
248
256
  });
249
257
  return value;
250
258
  }
251
- constructor(_hf, _runtime, _space, _options) {
252
- _define_property2(this, "_hf", void 0);
253
- _define_property2(this, "_runtime", void 0);
254
- _define_property2(this, "_space", void 0);
255
- _define_property2(this, "_cache", void 0);
256
- _define_property2(this, "_pending", void 0);
257
- _define_property2(this, "_subscriptions", void 0);
258
- _define_property2(this, "_invocations", void 0);
259
- _define_property2(this, "_options", void 0);
260
- _define_property2(this, "_onUpdate", void 0);
261
- this._hf = _hf;
262
- this._runtime = _runtime;
263
- this._space = _space;
264
- this._cache = /* @__PURE__ */ new Map();
265
- this._pending = /* @__PURE__ */ new Map();
266
- this._subscriptions = /* @__PURE__ */ new Map();
267
- this._invocations = {};
268
- this._options = defaultsDeep(_options ?? {}, defaultFunctionContextOptions);
269
- this._onUpdate = debounce((update) => {
270
- log("update", update, {
271
- F: __dxlog_file2,
272
- L: 86,
273
- S: this,
274
- C: (f, a) => f(...a)
275
- });
276
- this._hf.resumeEvaluation();
277
- this._options.onUpdate?.(update);
278
- }, this._options.debounceDelay);
279
- }
280
259
  };
281
260
  var AsyncFunctionPlugin = class extends FunctionPlugin {
282
261
  get context() {
@@ -299,8 +278,8 @@ import { effect } from "@preact/signals-core";
299
278
  import * as Effect from "effect/Effect";
300
279
  import * as SchemaAST from "effect/SchemaAST";
301
280
  import { Filter } from "@dxos/client/echo";
302
- import { toEffectSchema } from "@dxos/echo/internal";
303
- import { ComputeEventLogger, FunctionInvocationService, FunctionType, TracingService } from "@dxos/functions";
281
+ import { JsonSchema } from "@dxos/echo";
282
+ import { Function, FunctionInvocationService, TracingService } from "@dxos/functions";
304
283
  import { FunctionDefinition } from "@dxos/functions";
305
284
  import { log as log2 } from "@dxos/log";
306
285
  import { isNonNullable } from "@dxos/util";
@@ -315,7 +294,7 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
315
294
  if (!space) {
316
295
  return new CellError2(ErrorType2.REF, "Missing space");
317
296
  }
318
- const { objects: [fn] } = await space.db.query(Filter.type(FunctionType, {
297
+ const { objects: [fn] } = await space.db.query(Filter.type(Function.Function, {
319
298
  binding
320
299
  })).run();
321
300
  if (!fn) {
@@ -350,7 +329,7 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
350
329
  const functionDef = FunctionDefinition.deserialize(fn);
351
330
  let input;
352
331
  if (fn.inputSchema) {
353
- const schema = toEffectSchema(fn.inputSchema);
332
+ const schema = JsonSchema.toEffectSchema(fn.inputSchema);
354
333
  const props = SchemaAST.getPropertySignatures(schema.ast);
355
334
  input = {};
356
335
  props.forEach(({ name }, index) => {
@@ -363,7 +342,7 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
363
342
  }
364
343
  const result = runtime.runPromise(Effect.gen(function* () {
365
344
  return yield* FunctionInvocationService.invokeFunction(functionDef, input);
366
- }).pipe(Effect.provide(ComputeEventLogger.layerFromTracing), Effect.provide(TracingService.layerNoop)));
345
+ }).pipe(Effect.provide(TracingService.layerNoop)));
367
346
  return result;
368
347
  };
369
348
  return this.runAsyncFunction(ast, state, handler(true), {
@@ -2800,30 +2779,18 @@ var defaultFunctions = [
2800
2779
  ];
2801
2780
 
2802
2781
  // src/compute-graph.ts
2782
+ import { effect as effect2 } from "@preact/signals-core";
2803
2783
  import { Event as Event2 } from "@dxos/async";
2804
- import { Filter as Filter2, fullyQualifiedId } from "@dxos/client/echo";
2784
+ import { Filter as Filter2 } from "@dxos/client/echo";
2805
2785
  import { FQ_ID_LENGTH } from "@dxos/client/echo";
2806
2786
  import { Resource as Resource2 } from "@dxos/context";
2807
- import { getTypename } from "@dxos/echo/internal";
2808
- import { FunctionType as FunctionType2 } from "@dxos/functions";
2787
+ import { Obj } from "@dxos/echo";
2788
+ import { Function as Function2 } from "@dxos/functions";
2809
2789
  import { invariant as invariant2 } from "@dxos/invariant";
2810
2790
  import { PublicKey } from "@dxos/keys";
2811
2791
  import { log as log3 } from "@dxos/log";
2812
2792
  import { isNonNullable as isNonNullable2 } from "@dxos/util";
2813
2793
  import { ExportedCellChange as ExportedCellChange2 } from "@dxos/vendor-hyperformula";
2814
- function _define_property3(obj, key, value) {
2815
- if (key in obj) {
2816
- Object.defineProperty(obj, key, {
2817
- value,
2818
- enumerable: true,
2819
- configurable: true,
2820
- writable: true
2821
- });
2822
- } else {
2823
- obj[key] = value;
2824
- }
2825
- return obj;
2826
- }
2827
2794
  var __dxlog_file4 = "/__w/dxos/dxos/packages/core/compute/src/compute-graph.ts";
2828
2795
  var UNKNOWN_BINDING = "__UNKNOWN__";
2829
2796
  var createSheetName = ({ type, id }) => `${type}@${id}`;
@@ -2837,6 +2804,49 @@ var parseSheetName = (name) => {
2837
2804
  };
2838
2805
  };
2839
2806
  var ComputeGraph = class extends Resource2 {
2807
+ _hf;
2808
+ _runtime;
2809
+ _space;
2810
+ _options;
2811
+ id = `graph-${PublicKey.random().truncate()}`;
2812
+ // Map of nodes indexed by sheet number.
2813
+ _nodes = /* @__PURE__ */ new Map();
2814
+ // Cached function objects.
2815
+ _remoteFunctions = [];
2816
+ update = new Event2();
2817
+ // The context is passed to all functions.
2818
+ context;
2819
+ constructor(_hf, _runtime, _space, _options) {
2820
+ super(), this._hf = _hf, this._runtime = _runtime, this._space = _space, this._options = _options;
2821
+ this.context = new FunctionContext(this._hf, this._runtime, this._space, {
2822
+ ...this._options,
2823
+ onUpdate: (update) => {
2824
+ this._options?.onUpdate?.(update);
2825
+ this.update.emit({
2826
+ type: "valuesUpdated"
2827
+ });
2828
+ }
2829
+ });
2830
+ this._hf.updateConfig({
2831
+ context: this.context
2832
+ });
2833
+ const onValuesUpdate = (changes) => {
2834
+ for (const change of changes) {
2835
+ if (change instanceof ExportedCellChange2) {
2836
+ const { sheet } = change;
2837
+ const node = this._nodes.get(sheet);
2838
+ if (node) {
2839
+ node.update.emit({
2840
+ type: "valuesUpdated",
2841
+ change
2842
+ });
2843
+ }
2844
+ }
2845
+ }
2846
+ };
2847
+ this._hf.on("valuesUpdated", onValuesUpdate);
2848
+ this._ctx.onDispose(() => this._hf.off("valuesUpdated", onValuesUpdate));
2849
+ }
2840
2850
  get hf() {
2841
2851
  return this._hf;
2842
2852
  }
@@ -2864,7 +2874,7 @@ var ComputeGraph = class extends Resource2 {
2864
2874
  getOrCreateNode(name) {
2865
2875
  invariant2(name.length, void 0, {
2866
2876
  F: __dxlog_file4,
2867
- L: 130,
2877
+ L: 134,
2868
2878
  S: this,
2869
2879
  A: [
2870
2880
  "name.length",
@@ -2877,7 +2887,7 @@ var ComputeGraph = class extends Resource2 {
2877
2887
  sheet: name
2878
2888
  }, {
2879
2889
  F: __dxlog_file4,
2880
- L: 132,
2890
+ L: 136,
2881
2891
  S: this,
2882
2892
  C: (f, a) => f(...a)
2883
2893
  });
@@ -2886,7 +2896,7 @@ var ComputeGraph = class extends Resource2 {
2886
2896
  const sheetId = this._hf.getSheetId(name);
2887
2897
  invariant2(sheetId !== void 0, void 0, {
2888
2898
  F: __dxlog_file4,
2889
- L: 137,
2899
+ L: 141,
2890
2900
  S: this,
2891
2901
  A: [
2892
2902
  "sheetId !== undefined",
@@ -2910,7 +2920,7 @@ var ComputeGraph = class extends Resource2 {
2910
2920
  }).filter(isNonNullable2);
2911
2921
  for (const obj of objects) {
2912
2922
  if (obj.name === name) {
2913
- const type = getTypename(obj);
2923
+ const type = Obj.getTypename(obj);
2914
2924
  return `'${createSheetName({
2915
2925
  type,
2916
2926
  id: obj.id
@@ -2942,7 +2952,7 @@ var ComputeGraph = class extends Resource2 {
2942
2952
  }
2943
2953
  const fn = this._remoteFunctions.find((fn2) => fn2.binding === binding);
2944
2954
  if (fn) {
2945
- const id = fullyQualifiedId(fn);
2955
+ const id = Obj.getDXN(fn).toString();
2946
2956
  return `${id}(${args})`;
2947
2957
  } else {
2948
2958
  return match;
@@ -2959,7 +2969,7 @@ var ComputeGraph = class extends Resource2 {
2959
2969
  if (id.length !== FQ_ID_LENGTH) {
2960
2970
  return match;
2961
2971
  }
2962
- const fn = this._remoteFunctions.find((fn2) => fullyQualifiedId(fn2) === id);
2972
+ const fn = this._remoteFunctions.find((fn2) => Obj.getDXN(fn2).toString() === id);
2963
2973
  if (fn?.binding) {
2964
2974
  return `${fn.binding}(${args})`;
2965
2975
  } else {
@@ -2974,14 +2984,18 @@ var ComputeGraph = class extends Resource2 {
2974
2984
  }
2975
2985
  async _open() {
2976
2986
  if (this._space) {
2977
- const query = this._space.db.query(Filter2.type(FunctionType2));
2978
- const unsubscribe = query.subscribe(({ objects }) => {
2979
- this._remoteFunctions = objects.filter(({ binding }) => binding);
2987
+ const query = this._space.db.query(Filter2.type(Function2.Function));
2988
+ const unsubscribe = query.subscribe();
2989
+ const dispose = effect2(() => {
2990
+ this._remoteFunctions = query.objects.filter(({ binding }) => binding);
2980
2991
  this.update.emit({
2981
2992
  type: "functionsUpdated"
2982
2993
  });
2983
2994
  });
2984
- this._ctx.onDispose(unsubscribe);
2995
+ this._ctx.onDispose(() => {
2996
+ unsubscribe();
2997
+ dispose();
2998
+ });
2985
2999
  }
2986
3000
  }
2987
3001
  async _close() {
@@ -2989,40 +3003,6 @@ var ComputeGraph = class extends Resource2 {
2989
3003
  await node.close();
2990
3004
  }
2991
3005
  }
2992
- constructor(_hf, _runtime, _space, _options) {
2993
- super(), _define_property3(this, "_hf", void 0), _define_property3(this, "_runtime", void 0), _define_property3(this, "_space", void 0), _define_property3(this, "_options", void 0), _define_property3(this, "id", void 0), // Map of nodes indexed by sheet number.
2994
- _define_property3(this, "_nodes", void 0), // Cached function objects.
2995
- _define_property3(this, "_remoteFunctions", void 0), _define_property3(this, "update", void 0), // The context is passed to all functions.
2996
- _define_property3(this, "context", void 0), this._hf = _hf, this._runtime = _runtime, this._space = _space, this._options = _options, this.id = `graph-${PublicKey.random().truncate()}`, this._nodes = /* @__PURE__ */ new Map(), this._remoteFunctions = [], this.update = new Event2();
2997
- this.context = new FunctionContext(this._hf, this._runtime, this._space, {
2998
- ...this._options,
2999
- onUpdate: (update) => {
3000
- this._options?.onUpdate?.(update);
3001
- this.update.emit({
3002
- type: "valuesUpdated"
3003
- });
3004
- }
3005
- });
3006
- this._hf.updateConfig({
3007
- context: this.context
3008
- });
3009
- const onValuesUpdate = (changes) => {
3010
- for (const change of changes) {
3011
- if (change instanceof ExportedCellChange2) {
3012
- const { sheet } = change;
3013
- const node = this._nodes.get(sheet);
3014
- if (node) {
3015
- node.update.emit({
3016
- type: "valuesUpdated",
3017
- change
3018
- });
3019
- }
3020
- }
3021
- }
3022
- };
3023
- this._hf.on("valuesUpdated", onValuesUpdate);
3024
- this._ctx.onDispose(() => this._hf.off("valuesUpdated", onValuesUpdate));
3025
- }
3026
3006
  };
3027
3007
 
3028
3008
  // src/compute-graph-registry.ts
@@ -3031,19 +3011,6 @@ import { Resource as Resource3 } from "@dxos/context";
3031
3011
  import { invariant as invariant3 } from "@dxos/invariant";
3032
3012
  import { log as log4 } from "@dxos/log";
3033
3013
  import { HyperFormula } from "@dxos/vendor-hyperformula";
3034
- function _define_property4(obj, key, value) {
3035
- if (key in obj) {
3036
- Object.defineProperty(obj, key, {
3037
- value,
3038
- enumerable: true,
3039
- configurable: true,
3040
- writable: true
3041
- });
3042
- } else {
3043
- obj[key] = value;
3044
- }
3045
- return obj;
3046
- }
3047
3014
  var __dxlog_file5 = "/__w/dxos/dxos/packages/core/compute/src/compute-graph-registry.ts";
3048
3015
  var defaultPlugins = [
3049
3016
  {
@@ -3055,6 +3022,16 @@ var defaultOptions = {
3055
3022
  licenseKey: "gpl-v3"
3056
3023
  };
3057
3024
  var ComputeGraphRegistry = class extends Resource3 {
3025
+ _graphs = /* @__PURE__ */ new Map();
3026
+ _options;
3027
+ constructor(options) {
3028
+ super();
3029
+ this._options = defaultsDeep2({}, options, defaultOptions);
3030
+ this._options.plugins = this._options.plugins ?? defaultPlugins;
3031
+ this._options.plugins?.forEach(({ plugin, translations }) => {
3032
+ HyperFormula.registerFunctionPlugin(plugin, translations);
3033
+ });
3034
+ }
3058
3035
  getGraph(spaceId) {
3059
3036
  return this._graphs.get(spaceId);
3060
3037
  }
@@ -3104,14 +3081,6 @@ var ComputeGraphRegistry = class extends Resource3 {
3104
3081
  }
3105
3082
  this._graphs.clear();
3106
3083
  }
3107
- constructor(options) {
3108
- super(), _define_property4(this, "_graphs", /* @__PURE__ */ new Map()), _define_property4(this, "_options", void 0);
3109
- this._options = defaultsDeep2({}, options, defaultOptions);
3110
- this._options.plugins = this._options.plugins ?? defaultPlugins;
3111
- this._options.plugins?.forEach(({ plugin, translations }) => {
3112
- HyperFormula.registerFunctionPlugin(plugin, translations);
3113
- });
3114
- }
3115
3084
  };
3116
3085
 
3117
3086
  export {
@@ -3141,4 +3110,4 @@ export {
3141
3110
  defaultOptions,
3142
3111
  ComputeGraphRegistry
3143
3112
  };
3144
- //# sourceMappingURL=chunk-C7KLV2IU.mjs.map
3113
+ //# sourceMappingURL=chunk-ILJV45WQ.mjs.map