@dxos/plugin-sheet 0.7.4 → 0.7.5-main.937ce75

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 (188) hide show
  1. package/dist/lib/browser/{SheetContainer-KCLT6PEO.mjs → SheetContainer-S4NCLUYL.mjs} +16 -22
  2. package/dist/lib/browser/SheetContainer-S4NCLUYL.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-E5WQ7U7G.mjs → chunk-A374JPWV.mjs} +72 -705
  4. package/dist/lib/browser/chunk-A374JPWV.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-Q4XS4YWF.mjs +900 -0
  6. package/dist/lib/browser/chunk-Q4XS4YWF.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +74 -180
  8. package/dist/lib/browser/index.mjs.map +3 -3
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/types/index.mjs +62 -0
  11. package/dist/lib/node/{SheetContainer-VVVRYTQG.cjs → SheetContainer-TP4GYXZB.cjs} +41 -43
  12. package/dist/lib/node/SheetContainer-TP4GYXZB.cjs.map +7 -0
  13. package/dist/lib/node/{chunk-45YW2DX2.cjs → chunk-FDEQ2PGJ.cjs} +115 -726
  14. package/dist/lib/node/chunk-FDEQ2PGJ.cjs.map +7 -0
  15. package/dist/lib/node/chunk-TQOJ7DG2.cjs +935 -0
  16. package/dist/lib/node/chunk-TQOJ7DG2.cjs.map +7 -0
  17. package/dist/lib/node/index.cjs +104 -207
  18. package/dist/lib/node/index.cjs.map +3 -3
  19. package/dist/lib/node/meta.json +1 -1
  20. package/dist/lib/node/types/index.cjs +84 -0
  21. package/dist/lib/node/types/index.cjs.map +7 -0
  22. package/dist/lib/node-esm/{SheetContainer-LSBE6Q4X.mjs → SheetContainer-YB3JBVPZ.mjs} +16 -22
  23. package/dist/lib/node-esm/SheetContainer-YB3JBVPZ.mjs.map +7 -0
  24. package/dist/lib/node-esm/{chunk-NYDNXI7L.mjs → chunk-L5PQHVTX.mjs} +72 -705
  25. package/dist/lib/node-esm/chunk-L5PQHVTX.mjs.map +7 -0
  26. package/dist/lib/node-esm/chunk-NYYIDVR7.mjs +901 -0
  27. package/dist/lib/node-esm/chunk-NYYIDVR7.mjs.map +7 -0
  28. package/dist/lib/node-esm/index.mjs +74 -180
  29. package/dist/lib/node-esm/index.mjs.map +3 -3
  30. package/dist/lib/node-esm/meta.json +1 -1
  31. package/dist/lib/node-esm/types/index.mjs +63 -0
  32. package/dist/types/src/SheetPlugin.d.ts.map +1 -1
  33. package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts +1 -1
  34. package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts.map +1 -1
  35. package/dist/types/src/components/ComputeGraph/compute-graph.stories.d.ts.map +1 -0
  36. package/dist/types/src/components/FunctionEditor/FunctionEditor.d.ts.map +1 -1
  37. package/dist/types/src/components/GridSheet/GridSheet.d.ts.map +1 -1
  38. package/dist/types/src/components/GridSheet/SheetCellEditor.stories.d.ts.map +1 -1
  39. package/dist/types/src/components/GridSheet/util.d.ts.map +1 -1
  40. package/dist/types/src/components/RangeList/RangeList.d.ts.map +1 -1
  41. package/dist/types/src/components/SheetContainer/SheetContainer.stories.d.ts.map +1 -1
  42. package/dist/types/src/components/SheetContext/SheetContext.d.ts +1 -2
  43. package/dist/types/src/components/SheetContext/SheetContext.d.ts.map +1 -1
  44. package/dist/types/src/components/Toolbar/Toolbar.d.ts +1 -1
  45. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -1
  46. package/dist/types/src/components/index.d.ts +1 -1
  47. package/dist/types/src/extensions/compute.d.ts +1 -1
  48. package/dist/types/src/extensions/compute.d.ts.map +1 -1
  49. package/dist/types/src/extensions/editor/extension.d.ts +1 -1
  50. package/dist/types/src/extensions/editor/extension.d.ts.map +1 -1
  51. package/dist/types/src/index.d.ts +1 -1
  52. package/dist/types/src/index.d.ts.map +1 -1
  53. package/dist/types/src/integrations/thread-ranges.d.ts +1 -1
  54. package/dist/types/src/integrations/thread-ranges.d.ts.map +1 -1
  55. package/dist/types/src/model/sheet-model.d.ts +6 -8
  56. package/dist/types/src/model/sheet-model.d.ts.map +1 -1
  57. package/dist/types/src/model/testing.d.ts +1 -1
  58. package/dist/types/src/model/testing.d.ts.map +1 -1
  59. package/dist/types/src/model/useSheetModel.d.ts +1 -1
  60. package/dist/types/src/model/useSheetModel.d.ts.map +1 -1
  61. package/dist/types/src/testing/testing.d.ts +2 -2
  62. package/dist/types/src/testing/testing.d.ts.map +1 -1
  63. package/dist/types/src/{defs → types}/index.d.ts +1 -0
  64. package/dist/types/src/types/index.d.ts.map +1 -0
  65. package/dist/types/src/types/schema.d.ts +105 -0
  66. package/dist/types/src/types/schema.d.ts.map +1 -0
  67. package/dist/types/src/types/sheet-range-types.d.ts.map +1 -0
  68. package/dist/types/src/types/types.d.ts +82 -0
  69. package/dist/types/src/types/types.d.ts.map +1 -0
  70. package/dist/types/src/{defs → types}/util.d.ts +5 -1
  71. package/dist/types/src/types/util.d.ts.map +1 -0
  72. package/dist/types/tsconfig.tsbuildinfo +1 -0
  73. package/package.json +46 -53
  74. package/src/SheetPlugin.tsx +44 -115
  75. package/src/components/ComputeGraph/ComputeGraphContextProvider.tsx +1 -2
  76. package/src/{compute-graph → components/ComputeGraph}/compute-graph.stories.tsx +6 -7
  77. package/src/components/FunctionEditor/FunctionEditor.tsx +2 -1
  78. package/src/components/GridSheet/GridSheet.tsx +15 -18
  79. package/src/components/GridSheet/SheetCellEditor.stories.tsx +6 -7
  80. package/src/components/GridSheet/util.ts +3 -2
  81. package/src/components/RangeList/RangeList.tsx +2 -1
  82. package/src/components/SheetContainer/SheetContainer.stories.tsx +11 -12
  83. package/src/components/SheetContext/SheetContext.tsx +1 -2
  84. package/src/components/Toolbar/Toolbar.tsx +18 -20
  85. package/src/extensions/compute.ts +7 -4
  86. package/src/extensions/editor/extension.test.ts +2 -1
  87. package/src/extensions/editor/extension.ts +2 -3
  88. package/src/index.ts +1 -3
  89. package/src/integrations/thread-ranges.ts +28 -24
  90. package/src/model/sheet-model.test.ts +4 -10
  91. package/src/model/sheet-model.ts +30 -22
  92. package/src/model/testing.ts +3 -2
  93. package/src/model/useSheetModel.ts +2 -1
  94. package/src/sanity.test.ts +1 -1
  95. package/src/testing/testing.tsx +2 -3
  96. package/src/{defs → types}/index.ts +1 -0
  97. package/src/types/schema.ts +56 -0
  98. package/src/types/types.ts +93 -0
  99. package/src/{defs → types}/util.ts +13 -13
  100. package/dist/lib/browser/SheetContainer-KCLT6PEO.mjs.map +0 -7
  101. package/dist/lib/browser/chunk-E5WQ7U7G.mjs.map +0 -7
  102. package/dist/lib/browser/chunk-F3HE6D3J.mjs +0 -3269
  103. package/dist/lib/browser/chunk-F3HE6D3J.mjs.map +0 -7
  104. package/dist/lib/browser/chunk-JXFPOYNA.mjs +0 -67
  105. package/dist/lib/browser/chunk-JXFPOYNA.mjs.map +0 -7
  106. package/dist/lib/browser/compute-graph-SNUS7HOH.mjs +0 -35
  107. package/dist/lib/browser/types.mjs +0 -16
  108. package/dist/lib/node/SheetContainer-VVVRYTQG.cjs.map +0 -7
  109. package/dist/lib/node/chunk-45YW2DX2.cjs.map +0 -7
  110. package/dist/lib/node/chunk-KSEEI5VC.cjs +0 -3323
  111. package/dist/lib/node/chunk-KSEEI5VC.cjs.map +0 -7
  112. package/dist/lib/node/chunk-OWH2EUHZ.cjs +0 -90
  113. package/dist/lib/node/chunk-OWH2EUHZ.cjs.map +0 -7
  114. package/dist/lib/node/compute-graph-WILPHO4A.cjs +0 -57
  115. package/dist/lib/node/compute-graph-WILPHO4A.cjs.map +0 -7
  116. package/dist/lib/node/types.cjs +0 -38
  117. package/dist/lib/node/types.cjs.map +0 -7
  118. package/dist/lib/node-esm/SheetContainer-LSBE6Q4X.mjs.map +0 -7
  119. package/dist/lib/node-esm/chunk-6JF2AHKO.mjs +0 -3270
  120. package/dist/lib/node-esm/chunk-6JF2AHKO.mjs.map +0 -7
  121. package/dist/lib/node-esm/chunk-BVS2IQRO.mjs +0 -68
  122. package/dist/lib/node-esm/chunk-BVS2IQRO.mjs.map +0 -7
  123. package/dist/lib/node-esm/chunk-NYDNXI7L.mjs.map +0 -7
  124. package/dist/lib/node-esm/compute-graph-S6CVN7RS.mjs +0 -36
  125. package/dist/lib/node-esm/compute-graph-S6CVN7RS.mjs.map +0 -7
  126. package/dist/lib/node-esm/types.mjs +0 -17
  127. package/dist/lib/node-esm/types.mjs.map +0 -7
  128. package/dist/types/src/compute-graph/compute-graph-registry.d.ts +0 -34
  129. package/dist/types/src/compute-graph/compute-graph-registry.d.ts.map +0 -1
  130. package/dist/types/src/compute-graph/compute-graph.d.ts +0 -64
  131. package/dist/types/src/compute-graph/compute-graph.d.ts.map +0 -1
  132. package/dist/types/src/compute-graph/compute-graph.stories.d.ts.map +0 -1
  133. package/dist/types/src/compute-graph/compute-graph.test.d.ts +0 -2
  134. package/dist/types/src/compute-graph/compute-graph.test.d.ts.map +0 -1
  135. package/dist/types/src/compute-graph/compute-node.d.ts +0 -26
  136. package/dist/types/src/compute-graph/compute-node.d.ts.map +0 -1
  137. package/dist/types/src/compute-graph/functions/async-function.d.ts +0 -66
  138. package/dist/types/src/compute-graph/functions/async-function.d.ts.map +0 -1
  139. package/dist/types/src/compute-graph/functions/edge-function.d.ts +0 -21
  140. package/dist/types/src/compute-graph/functions/edge-function.d.ts.map +0 -1
  141. package/dist/types/src/compute-graph/functions/function-defs.d.ts +0 -11
  142. package/dist/types/src/compute-graph/functions/function-defs.d.ts.map +0 -1
  143. package/dist/types/src/compute-graph/functions/index.d.ts +0 -4
  144. package/dist/types/src/compute-graph/functions/index.d.ts.map +0 -1
  145. package/dist/types/src/compute-graph/hyperformula.test.d.ts +0 -2
  146. package/dist/types/src/compute-graph/hyperformula.test.d.ts.map +0 -1
  147. package/dist/types/src/compute-graph/index.d.ts +0 -5
  148. package/dist/types/src/compute-graph/index.d.ts.map +0 -1
  149. package/dist/types/src/compute-graph/testing/index.d.ts +0 -3
  150. package/dist/types/src/compute-graph/testing/index.d.ts.map +0 -1
  151. package/dist/types/src/compute-graph/testing/test-builder.d.ts +0 -15
  152. package/dist/types/src/compute-graph/testing/test-builder.d.ts.map +0 -1
  153. package/dist/types/src/compute-graph/testing/test-plugin.d.ts +0 -36
  154. package/dist/types/src/compute-graph/testing/test-plugin.d.ts.map +0 -1
  155. package/dist/types/src/compute-graph/util.d.ts +0 -2
  156. package/dist/types/src/compute-graph/util.d.ts.map +0 -1
  157. package/dist/types/src/defs/index.d.ts.map +0 -1
  158. package/dist/types/src/defs/sheet-range-types.d.ts.map +0 -1
  159. package/dist/types/src/defs/types.d.ts +0 -26
  160. package/dist/types/src/defs/types.d.ts.map +0 -1
  161. package/dist/types/src/defs/types.test.d.ts +0 -2
  162. package/dist/types/src/defs/types.test.d.ts.map +0 -1
  163. package/dist/types/src/defs/util.d.ts.map +0 -1
  164. package/dist/types/src/types.d.ts +0 -182
  165. package/dist/types/src/types.d.ts.map +0 -1
  166. package/dist/vendor/hyperformula.mjs +0 -37145
  167. package/src/compute-graph/compute-graph-registry.ts +0 -90
  168. package/src/compute-graph/compute-graph.test.ts +0 -87
  169. package/src/compute-graph/compute-graph.ts +0 -260
  170. package/src/compute-graph/compute-node.ts +0 -62
  171. package/src/compute-graph/functions/async-function.ts +0 -179
  172. package/src/compute-graph/functions/edge-function.ts +0 -102
  173. package/src/compute-graph/functions/function-defs.ts +0 -2427
  174. package/src/compute-graph/functions/index.ts +0 -7
  175. package/src/compute-graph/hyperformula.test.ts +0 -14
  176. package/src/compute-graph/index.ts +0 -8
  177. package/src/compute-graph/testing/index.ts +0 -6
  178. package/src/compute-graph/testing/test-builder.ts +0 -54
  179. package/src/compute-graph/testing/test-plugin.ts +0 -100
  180. package/src/compute-graph/util.ts +0 -8
  181. package/src/defs/types.test.ts +0 -91
  182. package/src/defs/types.ts +0 -88
  183. package/src/types.ts +0 -125
  184. /package/dist/lib/browser/{compute-graph-SNUS7HOH.mjs.map → types/index.mjs.map} +0 -0
  185. /package/dist/lib/{browser/types.mjs.map → node-esm/types/index.mjs.map} +0 -0
  186. /package/dist/types/src/{compute-graph → components/ComputeGraph}/compute-graph.stories.d.ts +0 -0
  187. /package/dist/types/src/{defs → types}/sheet-range-types.d.ts +0 -0
  188. /package/src/{defs → types}/sheet-range-types.ts +0 -0
@@ -1,90 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import type { FunctionPluginDefinition } from 'hyperformula';
6
- import type { ConfigParams } from 'hyperformula/typings/ConfigParams';
7
- import type { FunctionTranslationsPackage } from 'hyperformula/typings/interpreter';
8
- import defaultsDeep from 'lodash.defaultsdeep';
9
-
10
- import { type SpaceId, type Space } from '@dxos/client/echo';
11
- import { Resource } from '@dxos/context';
12
- import { invariant } from '@dxos/invariant';
13
- import { log } from '@dxos/log';
14
-
15
- import { HyperFormula } from '#hyperformula';
16
- import { ComputeGraph } from './compute-graph';
17
- import { EdgeFunctionPlugin, EdgeFunctionPluginTranslations, type FunctionContextOptions } from './functions';
18
-
19
- export type ComputeGraphPlugin = {
20
- plugin: FunctionPluginDefinition;
21
- translations: FunctionTranslationsPackage;
22
- };
23
-
24
- export type ComputeGraphOptions = {
25
- plugins?: ComputeGraphPlugin[];
26
- } & Partial<FunctionContextOptions> &
27
- Partial<ConfigParams>;
28
-
29
- export const defaultOptions: ComputeGraphOptions = {
30
- licenseKey: 'gpl-v3',
31
- };
32
-
33
- export const defaultPlugins: ComputeGraphPlugin[] = [
34
- {
35
- plugin: EdgeFunctionPlugin,
36
- translations: EdgeFunctionPluginTranslations,
37
- },
38
- ];
39
-
40
- /**
41
- * Manages a collection of ComputeGraph instances for each space.
42
- *
43
- * [ComputePlugin] => [ComputeGraphRegistry] => [ComputeGraph(Space)] => [ComputeNode(Object)]
44
- *
45
- * NOTE: The ComputeGraphRegistry manages the hierarchy of resources via its root Context.
46
- * NOTE: The package.json file defines the packaged #hyperformula module.
47
- */
48
- // TODO(burdon): Move graph into separate plugin; isolate HF deps.
49
- export class ComputeGraphRegistry extends Resource {
50
- private readonly _graphs = new Map<SpaceId, ComputeGraph>();
51
-
52
- private readonly _options: ComputeGraphOptions;
53
-
54
- constructor(options: ComputeGraphOptions = { plugins: defaultPlugins }) {
55
- super();
56
- this._options = defaultsDeep({}, options, defaultOptions);
57
- this._options.plugins?.forEach(({ plugin, translations }) => {
58
- HyperFormula.registerFunctionPlugin(plugin, translations);
59
- });
60
- }
61
-
62
- getGraph(spaceId: SpaceId): ComputeGraph | undefined {
63
- return this._graphs.get(spaceId);
64
- }
65
-
66
- getOrCreateGraph(space: Space): ComputeGraph {
67
- let graph = this._graphs.get(space.id);
68
- if (!graph) {
69
- log('create graph', { space: space.id });
70
- graph = this.createGraph(space);
71
- }
72
-
73
- return graph;
74
- }
75
-
76
- createGraph(space: Space): ComputeGraph {
77
- invariant(!this._graphs.has(space.id), `ComputeGraph already exists for space: ${space.id}`);
78
- const hf = HyperFormula.buildEmpty(this._options);
79
- const graph = new ComputeGraph(hf, space, this._options);
80
- this._graphs.set(space.id, graph);
81
- return graph;
82
- }
83
-
84
- protected override async _close() {
85
- for (const graph of this._graphs.values()) {
86
- await graph.close();
87
- }
88
- this._graphs.clear();
89
- }
90
- }
@@ -1,87 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { type CellValue } from 'hyperformula';
6
- import { afterEach, beforeEach, describe, expect, test } from 'vitest';
7
-
8
- import { Trigger } from '@dxos/async';
9
- import { create, fullyQualifiedId } from '@dxos/client/echo';
10
- import { FunctionType } from '@dxos/plugin-script/types';
11
-
12
- import { DetailedCellError } from '#hyperformula';
13
- import { TestBuilder } from './testing';
14
-
15
- describe('ComputeGraph', () => {
16
- let testBuilder: TestBuilder;
17
- beforeEach(async () => {
18
- testBuilder = new TestBuilder({ types: [FunctionType] });
19
- await testBuilder.open();
20
- });
21
- afterEach(async () => {
22
- await testBuilder.close();
23
- });
24
-
25
- test('map functions', async () => {
26
- const space = await testBuilder.client.spaces.create();
27
- const graph = testBuilder.registry.createGraph(space);
28
- await graph.open();
29
-
30
- // Create script.
31
- const trigger = new Trigger();
32
- graph.update.once(() => trigger.wake());
33
- const functionObject = space.db.add(create(FunctionType, { name: 'test', version: '0.0.1', binding: 'TEST' }));
34
- await trigger.wait();
35
- const functions = graph.getFunctions({ echo: true });
36
- expect(functions).to.toHaveLength(1);
37
-
38
- const id = graph.mapFunctionBindingToId('TEST()');
39
- expect(id).to.eq(`${fullyQualifiedId(functionObject)}()`);
40
- });
41
-
42
- test('cross-node references', async () => {
43
- const space = await testBuilder.client.spaces.create();
44
- const graph = testBuilder.registry.createGraph(space);
45
-
46
- // Create nodes.
47
- const node1 = await graph.getOrCreateNode('node-1').open();
48
- const node2 = await graph.getOrCreateNode('node-2').open();
49
- expect(graph.hf.getSheetNames()).to.toHaveLength(2);
50
-
51
- {
52
- node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, [[100, 200, 300, '=SUM(A1:C1)']]);
53
- node2.graph.hf.setCellContents({ sheet: node2.sheetId, row: 0, col: 0 }, "='node-1'!D1");
54
- const value1 = node1.graph.hf.getCellValue({ sheet: node1.sheetId, col: 3, row: 0 });
55
- const value2 = node2.graph.hf.getCellValue({ sheet: node2.sheetId, col: 0, row: 0 });
56
- expect(value1).to.eq(value2);
57
- }
58
-
59
- // Get updated event.
60
- const trigger = new Trigger<CellValue>();
61
- node2.update.on(({ change }) => {
62
- const value = node2.graph.hf.getCellValue({ sheet: node2.sheetId, col: 0, row: 0 });
63
- expect(value).to.eq(change?.newValue);
64
- trigger.wake(value);
65
- });
66
-
67
- {
68
- node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, 400);
69
- const value1 = node1.graph.hf.getCellValue({ sheet: node1.sheetId, col: 3, row: 0 });
70
- const value2 = await trigger.wait();
71
- expect(value1).to.eq(value2);
72
- }
73
- });
74
-
75
- // TODO(burdon): Dynamically load node/model based on dependencies.
76
- // - Create dependency then close model.
77
- test('dynamic loading', async () => {
78
- const space = await testBuilder.client.spaces.create();
79
- const graph = testBuilder.registry.createGraph(space);
80
-
81
- const node1 = await graph.getOrCreateNode('node-1').open();
82
- node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, "='node-2'!A1");
83
- const value1 = node1.graph.hf.getCellValue({ sheet: node1.sheetId, col: 0, row: 0 });
84
- expect(value1).to.be.instanceof(DetailedCellError);
85
- expect((value1 as DetailedCellError).type).to.eq('REF');
86
- });
87
- });
@@ -1,260 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { type Listeners } from 'hyperformula/typings/Emitter';
6
-
7
- import { Event } from '@dxos/async';
8
- import { type Space, Filter, fullyQualifiedId } from '@dxos/client/echo';
9
- import { FQ_ID_LENGTH } from '@dxos/client/echo';
10
- import { Resource } from '@dxos/context';
11
- import { getTypename } from '@dxos/echo-schema';
12
- import { invariant } from '@dxos/invariant';
13
- import { PublicKey } from '@dxos/keys';
14
- import { log } from '@dxos/log';
15
- import { FunctionType } from '@dxos/plugin-script/types';
16
- import { nonNullable } from '@dxos/util';
17
-
18
- import { ExportedCellChange, type HyperFormula } from '#hyperformula';
19
- import { ComputeNode } from './compute-node';
20
- import {
21
- defaultFunctions,
22
- FunctionContext,
23
- type FunctionContextOptions,
24
- type FunctionDefinition,
25
- EDGE_FUNCTION_NAME,
26
- } from './functions';
27
-
28
- // TODO(burdon): Factor out compute-graph.
29
-
30
- const UNKNOWN_BINDING = '__UNKNOWN__';
31
-
32
- // TODO(burdon): Factory.
33
- // export type ComputeNodeGenerator = <T>(obj: T) => ComputeNode;
34
-
35
- type ObjectRef = { type: string; id: string };
36
-
37
- /**
38
- * Marker for sheets that are managed by an ECHO object.
39
- * Sheet ID: `dxos.org/type/SheetType@1234`
40
- */
41
- export const createSheetName = ({ type, id }: ObjectRef) => `${type}@${id}`;
42
- export const parseSheetName = (name: string): Partial<ObjectRef> => {
43
- const [type, id] = name.split('@');
44
- return id ? { type, id } : { id: type };
45
- };
46
-
47
- export type ComputeGraphEvent = 'functionsUpdated' | 'valuesUpdated';
48
-
49
- /**
50
- * Per-space compute and dependency graph.
51
- * Consists of multiple ComputeNode (corresponding to a HyperFormula sheet).
52
- * Manages the set of custom functions.
53
- * HyperFormula manages the dependency graph.
54
- */
55
- export class ComputeGraph extends Resource {
56
- public readonly id = `graph-${PublicKey.random().truncate()}`;
57
-
58
- // Map of nodes indexed by sheet number.
59
- private readonly _nodes = new Map<number, ComputeNode>();
60
-
61
- // Cached function objects.
62
- private _remoteFunctions: FunctionType[] = [];
63
-
64
- public readonly update = new Event<{ type: ComputeGraphEvent }>();
65
-
66
- // The context is passed to all functions.
67
- public readonly context: FunctionContext;
68
-
69
- constructor(
70
- private readonly _hf: HyperFormula,
71
- private readonly _space?: Space,
72
- private readonly _options?: Partial<FunctionContextOptions>,
73
- ) {
74
- super();
75
-
76
- const contextOptions = {
77
- ...this._options,
78
- onUpdate: (update) => {
79
- this._options?.onUpdate?.(update);
80
- this.update.emit({ type: 'valuesUpdated' });
81
- },
82
- } satisfies Partial<FunctionContextOptions>;
83
- this.context = new FunctionContext(this._hf, this._space, contextOptions);
84
- this._hf.updateConfig({ context: this.context });
85
-
86
- // TODO(burdon): If debounce then aggregate changes.
87
- const onValuesUpdate: Listeners['valuesUpdated'] = (changes) => {
88
- for (const change of changes) {
89
- if (change instanceof ExportedCellChange) {
90
- const { sheet } = change;
91
- const node = this._nodes.get(sheet);
92
- if (node) {
93
- node.update.emit({ type: 'valuesUpdated', change });
94
- }
95
- }
96
- }
97
- };
98
-
99
- this._hf.on('valuesUpdated', onValuesUpdate);
100
- this._ctx.onDispose(() => this._hf.off('valuesUpdated', onValuesUpdate));
101
- }
102
-
103
- get hf() {
104
- return this._hf;
105
- }
106
-
107
- getFunctions(
108
- { standard, echo }: { standard?: boolean; echo?: boolean } = { standard: true, echo: true },
109
- ): FunctionDefinition[] {
110
- return [
111
- ...(standard
112
- ? this._hf
113
- .getRegisteredFunctionNames()
114
- .map((name) => defaultFunctions.find((fn) => fn.name === name) ?? { name })
115
- : []),
116
- ...(echo ? this._remoteFunctions.map((fn) => ({ name: fn.binding! })) : []),
117
- ];
118
- }
119
-
120
- /**
121
- * Get or create cell representing a sheet.
122
- */
123
- // TODO(burdon): Async (open node).
124
- // The graph should be an extensible factory that plugins extend with model constructors.
125
- // This would enable on-the-fly instantiation of new models when then are referenced.
126
- // E.g., Cross-object reference would be stored as "ObjectId!A1"
127
- // The graph would then load the object and create a ComputeNode (model) of the appropriate type.
128
- getOrCreateNode(name: string): ComputeNode {
129
- invariant(name.length);
130
- if (!this._hf.doesSheetExist(name)) {
131
- log('created node', { space: this._space?.id, sheet: name });
132
- this._hf.addSheet(name);
133
- }
134
-
135
- const sheetId = this._hf.getSheetId(name);
136
- invariant(sheetId !== undefined);
137
-
138
- const node = new ComputeNode(this, sheetId);
139
- this._nodes.set(sheetId, node);
140
- return node;
141
- }
142
-
143
- /**
144
- * Map bound value to custom function invocation.
145
- * E.g., "HELLO(...args)" => "DX("HELLO", ...args)".
146
- */
147
- mapFormulaToNative(formula: string): string {
148
- return (
149
- formula
150
- //
151
- // Map cross-sheet references by name onto sheet stored by ECHO object/model.
152
- // Example: "Test Sheet"!A0 => "dxos.org/type/SheetType@1234"!A0
153
- // https://hyperformula.handsontable.com/guide/cell-references.html#cell-references
154
- //
155
- .replace(/['"]?([ \w]+)['"]?!/, (_match, name) => {
156
- if (name) {
157
- // TODO(burdon): Cache map.
158
- const objects = this._hf
159
- .getSheetNames()
160
- .map((name) => {
161
- const { type, id } = parseSheetName(name);
162
- return type && id ? this._space?.db.getObjectById(id) : undefined;
163
- })
164
- .filter(nonNullable);
165
-
166
- for (const obj of objects) {
167
- if (obj.name === name) {
168
- const type = getTypename(obj)!;
169
- // NOTE: Names must be single quoted.
170
- return `'${createSheetName({ type, id: obj.id })}'!`;
171
- }
172
- }
173
- }
174
-
175
- return `${name}!`;
176
- })
177
-
178
- //
179
- // Map remote function references (i.e., to remote DX function invocation).
180
- //
181
- .replace(/(\w+)\((.*)\)/g, (match, binding, args) => {
182
- const fn = this._remoteFunctions.find((fn) => fn.binding === binding);
183
- if (!fn) {
184
- return match;
185
- }
186
-
187
- if (args.trim() === '') {
188
- return `${EDGE_FUNCTION_NAME}("${binding}")`;
189
- } else {
190
- return `${EDGE_FUNCTION_NAME}("${binding}", ${args})`;
191
- }
192
- })
193
- );
194
- }
195
-
196
- /**
197
- * Map from binding to fully qualified ECHO ID (to store).
198
- * E.g., HELLO() => spaceId:objectId()
199
- */
200
- mapFunctionBindingToId(formula: string) {
201
- return formula.replace(/(\w+)\((.*)\)/g, (match, binding, args) => {
202
- if (binding === EDGE_FUNCTION_NAME || defaultFunctions.find((fn) => fn.name === binding)) {
203
- return match;
204
- }
205
-
206
- const fn = this._remoteFunctions.find((fn) => fn.binding === binding);
207
- if (fn) {
208
- const id = fullyQualifiedId(fn);
209
- return `${id}(${args})`;
210
- } else {
211
- return match;
212
- }
213
- });
214
- }
215
-
216
- /**
217
- * Map from fully qualified ECHO ID to binding (from store).
218
- * E.g., spaceId:objectId() => HELLO()
219
- */
220
- mapFunctionBindingFromId(formula: string) {
221
- const binding = formula.replace(/(\w+):([a-zA-Z0-9]+)\((.*)\)/g, (match, spaceId, objectId, args) => {
222
- const id = `${spaceId}:${objectId}`;
223
- if (id.length !== FQ_ID_LENGTH) {
224
- return match;
225
- }
226
-
227
- const fn = this._remoteFunctions.find((fn) => fullyQualifiedId(fn) === id);
228
- if (fn?.binding) {
229
- return `${fn.binding}(${args})`;
230
- } else {
231
- return UNKNOWN_BINDING;
232
- }
233
- });
234
-
235
- if (binding.startsWith(`=${UNKNOWN_BINDING}`)) {
236
- return undefined;
237
- } else {
238
- return binding;
239
- }
240
- }
241
-
242
- protected override async _open() {
243
- if (this._space) {
244
- // Subscribe to remote function definitions.
245
- const query = this._space.db.query(Filter.schema(FunctionType));
246
- const unsubscribe = query.subscribe(({ objects }) => {
247
- this._remoteFunctions = objects.filter(({ binding }) => binding);
248
- this.update.emit({ type: 'functionsUpdated' });
249
- });
250
-
251
- this._ctx.onDispose(unsubscribe);
252
- }
253
- }
254
-
255
- protected override async _close() {
256
- for (const node of this._nodes.values()) {
257
- await node.close();
258
- }
259
- }
260
- }
@@ -1,62 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { type Listeners } from 'hyperformula/typings/Emitter';
6
- import { type ExportedCellChange } from 'hyperformula/typings/Exporter';
7
-
8
- import { Event } from '@dxos/async';
9
- import { Resource } from '@dxos/context';
10
-
11
- import { DetailedCellError } from '#hyperformula';
12
- import { type ComputeGraph } from './compute-graph';
13
- import { type CellAddress, isFormula } from '../defs';
14
- import { type CellScalarValue } from '../types';
15
-
16
- export type ComputeNodeEvent = {
17
- type: keyof Listeners;
18
- change?: ExportedCellChange;
19
- };
20
-
21
- /**
22
- * Individual "sheet" (typically corresponds to an ECHO object).
23
- */
24
- // TODO(burdon): Factor out common HF wrapper from from SheetModel.
25
- export class ComputeNode extends Resource {
26
- public readonly update = new Event<ComputeNodeEvent>();
27
-
28
- constructor(
29
- private readonly _graph: ComputeGraph,
30
- public readonly sheetId: number,
31
- ) {
32
- super();
33
- }
34
-
35
- get graph() {
36
- return this._graph;
37
- }
38
-
39
- clear() {
40
- this._graph.hf.clearSheet(this.sheetId);
41
- }
42
-
43
- getValue(cell: CellAddress): CellScalarValue {
44
- const value = this._graph.hf.getCellValue({ sheet: this.sheetId, row: cell.row, col: cell.col });
45
- if (value instanceof DetailedCellError) {
46
- return null;
47
- }
48
-
49
- return value;
50
- }
51
-
52
- setValue(cell: CellAddress, value: CellScalarValue) {
53
- const mappedValue = isFormula(value) ? this._graph.mapFormulaToNative(value) : value;
54
- this._graph.hf.setCellContents({ sheet: this.sheetId, row: cell.row, col: cell.col }, [[mappedValue]]);
55
- }
56
-
57
- // TODO(burdon): Load data into sheet.
58
- protected override async _open() {
59
- // const unsubscribe = this._graph.update.on(this.update.emit);
60
- // this._ctx.onDispose(unsubscribe);
61
- }
62
- }
@@ -1,179 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { type SimpleCellAddress } from 'hyperformula/typings/Cell';
6
- import { type InterpreterState } from 'hyperformula/typings/interpreter/InterpreterState';
7
- import { type InterpreterValue } from 'hyperformula/typings/interpreter/InterpreterValue';
8
- import { type ProcedureAst } from 'hyperformula/typings/parser';
9
- import defaultsDeep from 'lodash.defaultsdeep';
10
-
11
- import { debounce, type UnsubscribeCallback } from '@dxos/async';
12
- import { type Space } from '@dxos/client/echo';
13
- import { log } from '@dxos/log';
14
-
15
- import { CellError, ErrorType, EmptyValue, FunctionPlugin, type HyperFormula } from '#hyperformula';
16
-
17
- // TODO(burdon): Create API gateways:
18
- // https://publicapis.io
19
- // https://api-ninjas.com/api/cryptoprice
20
- // https://developers.google.com/apis-explorer
21
- // https://publicapis.io/coin-desk-api
22
-
23
- export type AsyncFunction = (...args: any) => Promise<InterpreterValue>;
24
-
25
- export type FunctionUpdateEvent = {
26
- name: string;
27
- cell: SimpleCellAddress;
28
- };
29
-
30
- export type FunctionOptions = {
31
- ttl?: number;
32
- };
33
-
34
- export type FunctionContextOptions = {
35
- defaultTtl: number;
36
- debounceDelay: number;
37
- remoteFunctionUrl: string;
38
- onUpdate?: (update: FunctionUpdateEvent) => void;
39
- };
40
-
41
- export const defaultFunctionContextOptions: FunctionContextOptions = {
42
- defaultTtl: 5_000,
43
- debounceDelay: 200,
44
- remoteFunctionUrl: 'https://edge.dxos.workers.dev/functions', // TODO(burdon): Config.
45
- };
46
-
47
- /**
48
- * The context singleton for the model is passed into custom functions.
49
- *
50
- * HyperFormula does not support async functions.
51
- * - https://hyperformula.handsontable.com/guide/custom-functions.html
52
- * - https://hyperformula.handsontable.com/guide/known-limitations.html#known-limitations
53
- * - https://github.com/handsontable/hyperformula/issues/892
54
- */
55
- export class FunctionContext {
56
- // Mangle name with params.
57
- static createInvocationKey(name: string, ...args: any) {
58
- return JSON.stringify({ name, ...args });
59
- }
60
-
61
- // TODO(wittjosiah): Persist cached values.
62
- // Cached values for cell.
63
- private readonly _cache = new Map<string, { value: InterpreterValue; ts: number }>();
64
-
65
- // Active requests.
66
- private readonly _pending = new Map<string, number>();
67
-
68
- // Query subscriptions.
69
- private readonly _subscriptions = new Map<string, UnsubscribeCallback>();
70
-
71
- // Invocation count.
72
- private _invocations: Record<string, number> = {};
73
-
74
- private readonly _options: FunctionContextOptions;
75
-
76
- // Debounced update handler.
77
- private readonly _onUpdate: (update: FunctionUpdateEvent) => void;
78
-
79
- constructor(
80
- private readonly _hf: HyperFormula,
81
- private readonly _space: Space | undefined,
82
- _options?: Partial<FunctionContextOptions>,
83
- ) {
84
- this._options = defaultsDeep(_options ?? {}, defaultFunctionContextOptions);
85
- this._onUpdate = debounce((update) => {
86
- log('update', update);
87
- // TODO(burdon): Better way to trigger recalculation? (NOTE: rebuildAndRecalculate resets the undo history.)
88
- this._hf.resumeEvaluation();
89
- this._options.onUpdate?.(update);
90
- }, this._options.debounceDelay);
91
- }
92
-
93
- get space() {
94
- return this._space;
95
- }
96
-
97
- get remoteFunctionUrl() {
98
- return this._options.remoteFunctionUrl;
99
- }
100
-
101
- get info() {
102
- return { cache: this._cache.size, invocations: this._invocations };
103
- }
104
-
105
- flush() {
106
- this._cache.clear();
107
- this._invocations = {};
108
- this._subscriptions.forEach((unsubscribe) => unsubscribe());
109
- this._subscriptions.clear();
110
- }
111
-
112
- createSubscription(name: string, unsubscribe: UnsubscribeCallback) {
113
- this._subscriptions.get(name)?.();
114
- this._subscriptions.set(name, unsubscribe);
115
- }
116
-
117
- /**
118
- * Exec the function if TTL has expired.
119
- * Return the cached value.
120
- */
121
- invokeFunction(
122
- name: string,
123
- state: InterpreterState,
124
- args: any[],
125
- cb: AsyncFunction,
126
- options?: FunctionOptions,
127
- ): InterpreterValue | undefined {
128
- const ttl = options?.ttl ?? this._options.defaultTtl;
129
-
130
- const { formulaAddress: cell } = state;
131
- const invocationKey = FunctionContext.createInvocationKey(name, ...args);
132
- const { value = undefined, ts = 0 } = this._cache.get(invocationKey) ?? {};
133
-
134
- const now = Date.now();
135
- const delta = now - ts;
136
- if ((!ts || delta > ttl) && !this._pending.has(invocationKey)) {
137
- this._pending.set(invocationKey, now);
138
- setTimeout(async () => {
139
- this._invocations[name] = (this._invocations[name] ?? 0) + 1;
140
- try {
141
- // Exec function.
142
- const value = await cb(...args);
143
- this._cache.set(invocationKey, { value, ts: Date.now() });
144
- log('set', { cell, value });
145
- this._onUpdate({ name, cell });
146
- } catch (err) {
147
- // TODO(burdon): Show error to user.
148
- log.warn('failed', { cell, err });
149
- this._cache.set(invocationKey, { value: new CellError(ErrorType.ERROR, 'Function failed.'), ts: Date.now() });
150
- } finally {
151
- this._pending.delete(invocationKey);
152
- }
153
- });
154
- }
155
-
156
- log('invoke', { cell, name, args, cache: value });
157
- return value;
158
- }
159
- }
160
-
161
- /**
162
- * Base class for async functions.
163
- */
164
- export class AsyncFunctionPlugin extends FunctionPlugin {
165
- get context() {
166
- return this.config.context as FunctionContext;
167
- }
168
-
169
- /**
170
- * Immediately returns cached value then runs the async function.
171
- */
172
- protected runAsyncFunction(ast: ProcedureAst, state: InterpreterState, cb: AsyncFunction, options?: FunctionOptions) {
173
- const { procedureName } = ast;
174
- const metadata = this.metadata(procedureName);
175
- return this.runFunction(ast.args, state, metadata, (...args: any) => {
176
- return this.context.invokeFunction(procedureName, state, args, cb, options) ?? EmptyValue;
177
- });
178
- }
179
- }