@heswell/vuu-data-engine-local 2.0.1 → 3.0.0

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 (39) hide show
  1. package/README.md +167 -25
  2. package/index.js +3771 -3318
  3. package/legacy.js +8 -0
  4. package/package.json +12 -8
  5. package/src/EngineDataSource.d.ts +13 -3
  6. package/src/ModuleDataSource.d.ts +3 -3
  7. package/src/UpdateGenerator.d.ts +12 -0
  8. package/src/basket/BasketModule.d.ts +12 -2
  9. package/src/basket/index.d.ts +1 -1
  10. package/src/core/engine/LocalEngine.d.ts +62 -0
  11. package/src/core/filter/PermissionFilter.d.ts +2 -2
  12. package/src/core/module/VuuModule.d.ts +68 -1
  13. package/src/core/module/edit-hooks.d.ts +135 -0
  14. package/src/core/table/TableContainer.d.ts +35 -11
  15. package/src/core/table/defaultTableContainer.d.ts +6 -0
  16. package/src/data-utils.d.ts +7 -0
  17. package/src/demo-modules.d.ts +18 -0
  18. package/src/index.d.ts +7 -2
  19. package/src/legacy.d.ts +11 -0
  20. package/src/local-datasource-provider/LocalDatasourceProvider.d.ts +18 -7
  21. package/src/local-datasource-provider/createDataSourceProvider.d.ts +12 -0
  22. package/src/module-admin/ModuleAdminModule.d.ts +0 -1
  23. package/src/module-admin/index.d.ts +1 -1
  24. package/src/notifications/NotificationModule.d.ts +1 -0
  25. package/src/notifications/index.d.ts +0 -1
  26. package/src/simul/SimulModule.d.ts +9 -1
  27. package/src/simul/index.d.ts +1 -1
  28. package/src/simul/reference-data/instrument-prices.d.ts +2 -2
  29. package/src/simul/reference-data/instruments-extended.d.ts +1 -1
  30. package/src/simul/reference-data/instruments.d.ts +2 -3
  31. package/src/simul/reference-data/orders.d.ts +2 -3
  32. package/src/simul/reference-data/parent-child-orders.d.ts +15 -4
  33. package/src/simul/reference-data/prices.d.ts +18 -6
  34. package/src/test/TestModule.d.ts +2 -3
  35. package/src/user-admin/UserAdminModule.d.ts +0 -1
  36. package/src/user-admin/index.d.ts +1 -1
  37. package/src/basket/reference-data/prices.d.ts +0 -24
  38. package/src/core/module/ModuleContainer.d.ts +0 -15
  39. package/src/notifications/simulated/notificationsModule.d.ts +0 -5
package/README.md CHANGED
@@ -9,17 +9,158 @@ The package runs alongside `@vuu-ui/vuu-data-test` and has the same public API.
9
9
  meant to replace the `VuuModule` / `TickingArrayDataSource` core in that package. The
10
10
  two packages don't depend on each other.
11
11
 
12
+ ## Peer dependencies
13
+
14
+ All `@vuu-ui` packages are peers declared as `^4.0.0-alpha.20`, so the engine uses the host
15
+ application's single copy of each (a second `@vuu-ui/vuu-utils` or `@vuu-ui/core` would mean
16
+ a second set of React contexts). They are `@vuu-ui/vuu-utils` and `@vuu-ui/vuu-filter-parser`
17
+ (runtime), `@vuu-ui/core` (React provider), and the type-only `vuu-data-types`,
18
+ `vuu-filter-types`, `vuu-protocol-types` and `vuu-table-types`.
19
+
12
20
  ## Architecture
13
21
 
14
- | Component | Role |
15
- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
16
- | `Table` | A façade over the engine `Table` that keeps the existing `Table` API. Inserts, updates and deletes go straight to the engine. Every change notifies subscribed viewports. |
17
- | `TableContainer` | A registry of tables, including join tables. |
22
+ | Component | Role |
23
+ | -------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
24
+ | `Table` | A façade over the engine `Table` that keeps the existing `Table` API. Inserts, updates and deletes go straight to the engine. Every change notifies subscribed viewports. |
25
+ | `LocalEngine` | An isolated in-browser Vuu server: the registered `VuuModule`s and a `TableContainer` of their tables. No React or DOM dependency. |
26
+ | `TableContainer` | A registry of tables, including join tables, keyed by `{module, table}`. Each engine has its own. |
18
27
  | `EngineDataSource` | A `DataSource` implementation backed by an engine `Viewport`. It handles windowing, sort, filter, groupBy, selection (including select-all), freeze, base filter, link filter and permission filter. Row updates are batched per frame. |
19
- | `ModuleDataSource` | Extends `EngineDataSource` with the module features: RPC and menu services, edit sessions, and visual links. `TickingArrayDataSource` is a deprecated alias. |
20
- | `VuuModule` | Creates `ModuleDataSource` instances. Row permissions are passed to the engine as a `permissionFilter` predicate. |
21
- | `RuntimeVisualLink` | When the parent's selection changes, sets the child's engine link filter. |
22
- | `UpdateGenerator` / `TickingGenerator` | Simulated ticking data that writes into the engine tables. |
28
+ | `ModuleDataSource` | Extends `EngineDataSource` with the module features: RPC and menu services, edit sessions, and visual links. `TickingArrayDataSource` is a deprecated alias. |
29
+ | `VuuModule` | Creates `ModuleDataSource` instances. Row permissions are passed to the engine as a `permissionFilter` predicate. Edit sessions can be customised per table with edit hooks. |
30
+ | `RuntimeVisualLink` | When the parent's selection changes, sets the child's engine link filter. |
31
+ | `UpdateGenerator` / `TickingGenerator` | Simulated ticking data that writes into the engine tables. |
32
+
33
+ ## Usage
34
+
35
+ Importing the package has no side effects: no modules, tables or timers are created
36
+ until you ask for them.
37
+
38
+ ```ts
39
+ import {
40
+ createLocalEngine,
41
+ createLocalVuuServer,
42
+ registerDemoModules,
43
+ } from "@heswell/vuu-data-engine-local";
44
+
45
+ // Your own modules, in an isolated engine
46
+ const engine = createLocalEngine({ modules: [new OrdersModule()] });
47
+ const dataSource = engine.createDataSource({
48
+ module: "ORDERS",
49
+ table: "parentOrders",
50
+ });
51
+ await dataSource.subscribe({ range: { from: 0, to: 100 } }, callback);
52
+ engine.dispose(); // disposes its modules and stops their timers
53
+
54
+ // A local Vuu server for a local-mode AuthenticationProvider (new engine per server)
55
+ const server = createLocalVuuServer({
56
+ connectionId: "orders",
57
+ modules: [new OrdersModule()],
58
+ });
59
+ // server.DataSourceProvider, server.engine
60
+
61
+ // The demo SIMUL, BASKET and TEST modules
62
+ const { simulModule } = registerDemoModules(createLocalEngine());
63
+ ```
64
+
65
+ ### Engines
66
+
67
+ | API | Description |
68
+ | ---------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
69
+ | `createLocalEngine({ modules? })` | A new, isolated `LocalEngine`. Two engines, or two modules in one engine, can use the same table names. |
70
+ | `engine.register(module)` / `unregister(name)` | Add or remove a module and its tables. A module is attached to the first engine it is registered with. |
71
+ | `engine.getModule(name)`, `hasModule`, `modules` | Look up modules. |
72
+ | `engine.getTableList()` | `VuuTable[]` (`{module, table}`) for every registered module. |
73
+ | `engine.getTableSchema(vuuTable)` | A table's schema. |
74
+ | `engine.createDataSource(vuuTable, vp?, cfg?)` | A `ModuleDataSource` for a table. |
75
+ | `engine.tableContainer` | The engine's `TableContainer`. Tables are keyed by `{module, table}`; a bare table name is accepted only if exactly one module has a table with that name. |
76
+ | `engine.dispose()` | Unregisters every module and disposes those attached to this engine (stopping ticking data and timers). |
77
+ | `createDataSourceProvider(engine)` | A React provider installing the legacy and core data contexts for the engine. |
78
+ | `createLocalVuuServer({ connectionId, modules, engine? })` | A `LocalVuuServer` (`{connectionId, DataSourceProvider, engine}`). Uses a new engine unless one is passed. |
79
+ | `registerDemoModules(engine?)` | Creates the SIMUL, BASKET and TEST demo modules and registers them with `engine` (default: the default engine). |
80
+ | `getDefaultEngine()` / `resetDefaultEngine()` | The shared engine behind the deprecated globals below. |
81
+
82
+ ### Edit sessions: hooks and service overrides
83
+
84
+ The built-in edit-session RPC services (`createSessionTable`, `editCell`, `addRow`, `deleteRow`,
85
+ `undoRowChange`, `endEditSession`, …) can be overridden by `rpcName`, per module with
86
+ `moduleServices` or per table with `services`. Precedence: table services, then module
87
+ services, then the built-ins. Services registered for a source table also apply to its
88
+ session tables.
89
+
90
+ Most customisation needs only edit hooks, which mirror vuu-server's. Implement them per
91
+ table with `editHooks` (these win), or for every table as protected methods of the module.
92
+
93
+ | Hook | Default |
94
+ | ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
95
+ | `canEditSource(column, ctx)` | `true`: editCell may write directly to a source table (outside a session). |
96
+ | `isEditableColumn(table, column, ctx)` | Any column except the key and the system columns (`vuuCreatedTimestamp`, `vuuUpdatedTimestamp`, `vuuMsg`, `vuuAction`). The key of a row added in the session may be edited. |
97
+ | `validateCellEdit(edit)` | Accept. Return an error message to refuse the edit. |
98
+ | `getStaleColumns(sourceTable, sourceRow, change)` | `defaultGetStaleColumns`: for editCell, the edited columns that changed in the source since the row was copied into the session; for deleteRow, any column. |
99
+ | `canSaveSession(prepared, ctx)` | `true`. Return `false` or a reason to refuse the save. May be async. |
100
+ | `applySessionSave(sessionTable, prepared, ctx)` | `defaultApplySessionSave`: writes the changes to the source table atomically. May be async. |
101
+
102
+ ```ts
103
+ class OrdersModule extends VuuModule<"parentOrders" | "childOrders"> {
104
+ protected editHooks = {
105
+ parentOrders: {
106
+ canSaveSession: ({ changes }) =>
107
+ changes.every(({ cellUpdates }) => Number(cellUpdates.qty) > 0) ||
108
+ "qty must be positive",
109
+ // amend orders through the order service instead of writing the table
110
+ applySessionSave: (_sessionTable, { changes }) =>
111
+ orderService.amend(changes),
112
+ },
113
+ };
114
+ // ...
115
+ }
116
+ ```
117
+
118
+ `endEditSession({ save: true })`:
119
+
120
+ 1. Rows already handled by `beforeSessionSave` (with a `vuuMsg`) are skipped.
121
+ 2. Changes are prepared. A row added with an existing key fails with `"duplicate key"`; a
122
+ source row that is missing or stale fails with exactly `"stale update"` (vuu-ui's
123
+ `VuuDataSource` matches this string and throws `StaleUpdateError`). Setting
124
+ `params.force` skips the stale check. The affected session rows get a `vuuMsg`
125
+ (`stale:<key>:<columns>`, `<action>:<key>:source row missing`,
126
+ `addRow:<key>:key already exists`).
127
+ 3. `canSaveSession`, then `applySessionSave`. A refusal or failure (`false`, an
128
+ `ERROR_RESULT`, or a thrown error) leaves the session open, so the user can retry or
129
+ cancel. Exported messages: `STALE_UPDATE`, `DUPLICATE_KEY`, `SAVE_NOT_PERMITTED`,
130
+ `SAVE_FAILED`.
131
+ 4. `afterSessionSave`, then the session rows are reset (deleted rows removed, `vuuAction`
132
+ and `vuuMsg` cleared, snapshots refreshed).
133
+
134
+ ### Ranges
135
+
136
+ `subscribe({ range })`, `dataSource.range = …` and `setRange` accept a vuu-utils `Range` or
137
+ a plain `{from, to}`.
138
+
139
+ ### Deprecated globals and the `/legacy` entry
140
+
141
+ `tableContainer`, `LocalDataSourceProvider` and `ensureVuuModule` use the default engine
142
+ (`getDefaultEngine()`). `@heswell/vuu-data-engine-local/legacy` re-exports the main entry and,
143
+ on import, registers the demo modules with the default engine, exporting the instances
144
+ `simulModule`, `basketModule`, `testModule`, `moduleAdminModule`, `userAdminModule` and
145
+ `notificationsModule` as 2.x did. It is the only entry point with side effects
146
+ (`"sideEffects": ["./src/legacy.ts"]`).
147
+
148
+ ## Migrating from 2.x
149
+
150
+ These are breaking changes; the next release should be 3.0.0.
151
+
152
+ | 2.x | 3.x |
153
+ | --------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
154
+ | Importing the package created and registered SIMUL, BASKET, TEST, MODULE_DISCOVERY, USER_ADMIN and NOTIFICATIONS, and exported the instances. | Nothing is created on import. Call `registerDemoModules()` or create the modules yourself, or import `/legacy`. |
155
+ | `new SomeModule()` registered itself in a global module container. | Register modules with an engine: `createLocalEngine({ modules })`, `engine.register()`, or `createLocalVuuServer`. |
156
+ | `createLocalVuuServer` registered its modules globally, so `LocalDataSourceProvider` saw them. | Each server has its own engine (`server.engine`). Pass `engine: getDefaultEngine()` to share the default engine. |
157
+ | `tableContainer` was global and keyed by bare table name. | Each engine has its own, keyed by `{module, table}`. The global `tableContainer` is a deprecated alias of the default engine's; a bare name throws if two modules have a table with that name. |
158
+ | `ModuleContainer` / `moduleContainer`. | Removed: use `LocalEngine`. |
159
+ | Module and table `services` were added alongside the built-in edit services. | They override built-in services with the same `rpcName`. |
160
+ | editCell could change the key and system columns, and edit missing or deleted rows. | Refused by default (see `isEditableColumn`). Editing a missing row returns an error. |
161
+ | `endEditSession` saved without checking for concurrent source changes or duplicate keys. | Fails with `"stale update"` or `"duplicate key"` (unless `force`). After a successful save the session rows are reset. |
162
+ | Demo prices and notifications ticked from import. | They start when a data source is created, and stop on `engine.dispose()`. |
163
+ | Reference data (`instrumentsTable`, `pricesTable`, `instrumentsData`, …) was created on import. | Lazy getters and factories (`getInstrumentsData()`, `createPricesTable()`, …). |
23
164
 
24
165
  ## Differences from `@vuu-ui/vuu-data-test`
25
166
 
@@ -38,7 +179,8 @@ npm run showcase:engine # equivalent to: VUU_DATA_ENGINE=local npm run showcas
38
179
  ```
39
180
 
40
181
  This aliases `@vuu-ui/vuu-data-test` to `@heswell/vuu-data-engine-local` in the showcase
41
- rsbuild config. The examples themselves don't change.
182
+ rsbuild config. The examples themselves don't change. The examples use the eagerly created demo
183
+ modules, so from 3.0 the alias should point to `@heswell/vuu-data-engine-local/legacy`.
42
184
 
43
185
  The showcase uses the published package. To publish it from vuu-websocket:
44
186
 
@@ -64,22 +206,22 @@ results for the sort, filter and groupBy scenarios.
64
206
 
65
207
  Mean time per operation, 100k rows, Node 24, Apple Silicon:
66
208
 
67
- | Scenario | legacy | engine | speed-up |
68
- | --------------------------------------------- | -------- | ------- | -------- |
69
- | create + subscribe | 24.1 ms | 8.7 ms | 2.8x |
70
- | sort numeric column | 62.1 ms | 14.1 ms | 4.4x |
71
- | sort two columns | 64.6 ms | 17.1 ms | 3.8x |
72
- | filter | 5.7 ms | 1.3 ms | 4.3x |
73
- | groupBy ccy, set/clear | 1.6 ms | 1.2 ms | 1.3x |
74
- | groupBy ccy + exchange, set/clear | 2.2 ms | 2.0 ms | 1.1x |
75
- | scroll, 100 range changes | 0.82 ms | 0.45 ms | 1.8x |
76
- | 1000 ticks, random rows | 1543 ms | 0.35 ms | ~4400x |
77
- | 1000 ticks, rows in viewport | 0.63 ms | 0.10 ms | 6.6x |
78
- | 1000 ticks, sorted column | 3012 ms | 1.9 ms | ~1600x |
79
- | 1000 ticks, filtered column | 1903 ms | 0.55 ms | ~3400x |
80
- | 1000 ticks, grouped | 1205 ms | 0.39 ms | ~3100x |
81
- | 500 deletes + 500 inserts | 1628 ms | 0.60 ms | ~2700x |
82
- | 500 deletes + 500 inserts, sorted | 2564 ms | 1.57 ms | ~1600x |
209
+ | Scenario | legacy | engine | speed-up |
210
+ | --------------------------------- | ------- | ------- | -------- |
211
+ | create + subscribe | 24.1 ms | 8.7 ms | 2.8x |
212
+ | sort numeric column | 62.1 ms | 14.1 ms | 4.4x |
213
+ | sort two columns | 64.6 ms | 17.1 ms | 3.8x |
214
+ | filter | 5.7 ms | 1.3 ms | 4.3x |
215
+ | groupBy ccy, set/clear | 1.6 ms | 1.2 ms | 1.3x |
216
+ | groupBy ccy + exchange, set/clear | 2.2 ms | 2.0 ms | 1.1x |
217
+ | scroll, 100 range changes | 0.82 ms | 0.45 ms | 1.8x |
218
+ | 1000 ticks, random rows | 1543 ms | 0.35 ms | ~4400x |
219
+ | 1000 ticks, rows in viewport | 0.63 ms | 0.10 ms | 6.6x |
220
+ | 1000 ticks, sorted column | 3012 ms | 1.9 ms | ~1600x |
221
+ | 1000 ticks, filtered column | 1903 ms | 0.55 ms | ~3400x |
222
+ | 1000 ticks, grouped | 1205 ms | 0.39 ms | ~3100x |
223
+ | 500 deletes + 500 inserts | 1628 ms | 0.60 ms | ~2700x |
224
+ | 500 deletes + 500 inserts, sorted | 2564 ms | 1.57 ms | ~1600x |
83
225
 
84
226
  Notes:
85
227