Repository navigation
feat(streaming-data): load large collections in batches outside the data model - #968
Conversation
…ata model Add @player-ui/streaming-data-plugin. A flow declares a collection with a `source` on its schema entry: the name of a source the app registers, a status binding, and params. Content starts a load with loadStreamingData(binding). The plugin appends each batch to one frozen array, serves it by reference at the collection's binding through a data middleware, keeps the binding read-only and out of serialize(), and writes progress to the status binding. Change notices name only what changed, so a view resolves again only the nodes that read it. Saves run in `commit-records` EXTERNAL states through a handler the app adds to its ExternalStatePlugin. The handler collects the changes (eachRow expressions over selected records, a draft to create or update, keys to remove), calls the source's commit, merges the returned records by key, and transitions with saved, partial, or failed. While the save runs, it keeps the view that started it current, so the view can show the save's progress.
Benchmark ResultsComparison against baseline from
|
| Benchmark | Current | Baseline | Change |
|---|---|---|---|
core/player/src/binding/__tests__/parser.bench.ts > parser benchmarks > Resolving binding: foo.bar |
1.18M ops/s | 925.06K ops/s | +27.6% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > parser benchmarks > Resolving binding: foo.pets.1.name |
717.39K ops/s | 521.10K ops/s | +37.7% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > parser benchmarks > Resolving binding: foo.pets.01.name |
686.00K ops/s | 503.37K ops/s | +36.3% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > parser benchmarks > Resolving binding: foo.pets['01'].name |
574.60K ops/s | 501.47K ops/s | +14.6% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > parser benchmarks > Resolving binding: foo.pets[01].name |
627.14K ops/s | 492.83K ops/s | +27.3% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > parser benchmarks > Resolving binding: foo.pets[name = "frodo"].type |
362.38K ops/s | 279.80K ops/s | +29.5% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > parser benchmarks > Resolving binding: foo.pets["name" = "sprinkles"].type |
292.87K ops/s | 235.69K ops/s | +24.3% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > parser benchmarks > Resolving binding: foo.pets["isDog" = false].type |
364.21K ops/s | 308.07K ops/s | +18.2% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > parser benchmarks > Resolving binding: foo.pets["isDog" = true].type |
374.12K ops/s | 318.65K ops/s | +17.4% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > binding creation benchmarks > Resolving binding: foo.bar |
724.43K ops/s | 605.13K ops/s | +19.7% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > binding creation benchmarks > Resolving binding: foo.pets.1.name |
490.25K ops/s | 389.77K ops/s | +25.8% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > binding creation benchmarks > Resolving binding: foo.pets.01.name |
480.12K ops/s | 392.62K ops/s | +22.3% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > binding creation benchmarks > Resolving binding: foo.pets['01'].name |
414.37K ops/s | 358.56K ops/s | +15.6% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > binding creation benchmarks > Resolving binding: foo.pets[01].name |
466.83K ops/s | 393.40K ops/s | +18.7% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > binding creation benchmarks > Resolving binding: foo.pets[name = "frodo"].type |
302.50K ops/s | 250.49K ops/s | +20.8% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > binding creation benchmarks > Resolving binding: foo.pets["name" = "sprinkles"].type |
258.47K ops/s | 200.40K ops/s | +29.0% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > binding creation benchmarks > Resolving binding: foo.pets["isDog" = false].type |
305.00K ops/s | 243.54K ops/s | +25.2% ✅ |
core/player/src/binding/__tests__/parser.bench.ts > binding creation benchmarks > Resolving binding: foo.pets["isDog" = true].type |
311.08K ops/s | 256.20K ops/s | +21.4% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = 1 + 3 (sync) |
568.51K ops/s | 490.09K ops/s | +16.0% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = 1 + 3 (async) |
435.17K ops/s | 398.74K ops/s | +9.1% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: conditional(true, true, false) (sync) |
573.99K ops/s | 501.45K ops/s | +14.5% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: conditional(true, true, false) (async) |
499.11K ops/s | 441.52K ops/s | +13.0% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = conditional({{bar}} > 0, true, false) (sync) |
251.92K ops/s | 221.46K ops/s | +13.8% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = conditional({{bar}} > 0, true, false) (async) |
223.51K ops/s | 206.12K ops/s | +8.4% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = conditional(conditional(true = false, false, true), conditional(false = false, true, false), conditional(true = true, false, true)) (sync) |
200.45K ops/s | 153.99K ops/s | +30.2% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = conditional(conditional(true = false, false, true), conditional(false = false, true, false), conditional(true = true, false, true)) (async) |
176.09K ops/s | 158.90K ops/s | +10.8% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = await(asyncTestFunction(1)) (sync) |
N/A | N/A | N/A |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = await(asyncTestFunction(1)) (async) |
367.45K ops/s | 298.85K ops/s | +23.0% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = asyncTestFunction(1) (sync) |
409.48K ops/s | 344.27K ops/s | +18.9% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = asyncTestFunction(1) (async) |
360.75K ops/s | 314.43K ops/s | +14.7% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: asyncTestFunction(1) (sync) |
1.05M ops/s | 942.19K ops/s | +11.2% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: asyncTestFunction(1) (async) |
814.65K ops/s | 742.09K ops/s | +9.8% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = conditional(!{{bar}} == false, await(asyncTestFunction(1)), false) (sync) |
217.76K ops/s | 193.03K ops/s | +12.8% ✅ |
core/player/src/expressions/__tests__/performance.bench.ts > Expression Parsing/Execution Benchmark > Parsing: {{foo}} = conditional(!{{bar}} == false, await(asyncTestFunction(1)), false) (async) |
199.34K ops/s | 177.43K ops/s | +12.3% ✅ |
core/player/src/view/resolver/__tests__/index.bench.ts > resolver benchmarks > initial resolve |
885.24 ops/s | 695.31 ops/s | +27.3% ✅ |
core/player/src/view/resolver/__tests__/index.bench.ts > resolver benchmarks > Resolving from cache |
33.33K ops/s | 21.96K ops/s | +51.8% ✅ |
core/player/src/view/resolver/__tests__/index.bench.ts > resolver benchmarks > data changes |
4.61K ops/s | 3.14K ops/s | +46.6% ✅ |
core/player/src/view/resolver/__tests__/index.bench.ts > resolver benchmarks > data changes slow |
886.25 ops/s | 660.60 ops/s | +34.2% ✅ |
plugins/async-node/core
| Benchmark | Current | Baseline | Change |
|---|---|---|---|
plugins/async-node/core/src/__tests__/index.bench.ts > async node benchmarks > Resolve Async Node 1 times |
23.29K ops/s | 13.80K ops/s | +68.8% ✅ |
plugins/async-node/core/src/__tests__/index.bench.ts > async node benchmarks > Resolve Async Node 5 times |
20.23K ops/s | 11.81K ops/s | +71.3% ✅ |
plugins/async-node/core/src/__tests__/index.bench.ts > async node benchmarks > Resolve Async Node 10 times |
15.67K ops/s | 10.29K ops/s | +52.3% ✅ |
plugins/async-node/core/src/__tests__/index.bench.ts > async node benchmarks > Resolve Async Node 50 times |
5.58K ops/s | 3.26K ops/s | +71.0% ✅ |
plugins/async-node/core/src/__tests__/index.bench.ts > async node benchmarks > Resolve Async Node 100 times |
2.85K ops/s | 1.29K ops/s | +120.7% ✅ |
plugins/async-node/core/src/__tests__/transform.bench.ts > async transform benchmarks > Resolve Async Node 1 times |
11.48K ops/s | 7.74K ops/s | +48.4% ✅ |
plugins/async-node/core/src/__tests__/transform.bench.ts > async transform benchmarks > Resolve Async Node 5 times |
11.14K ops/s | 8.61K ops/s | +29.4% ✅ |
plugins/async-node/core/src/__tests__/transform.bench.ts > async transform benchmarks > Resolve Async Node 10 times |
9.24K ops/s | 7.56K ops/s | +22.1% ✅ |
plugins/async-node/core/src/__tests__/transform.bench.ts > async transform benchmarks > Resolve Async Node 50 times |
4.28K ops/s | 2.84K ops/s | +50.6% ✅ |
plugins/async-node/core/src/__tests__/transform.bench.ts > async transform benchmarks > Resolve Async Node 100 times |
2.56K ops/s | 1.48K ops/s | +72.8% ✅ |
react/player
| Benchmark | Current | Baseline | Change |
|---|---|---|---|
react/player/src/asset/__tests__/index.bench.tsx > ReactAsset benchmarks > Render asset nested in 1 ReactAssets |
698.38 ops/s | 581.40 ops/s | +20.1% ✅ |
react/player/src/asset/__tests__/index.bench.tsx > ReactAsset benchmarks > Bubble errors nested in 1 ReactAssets |
1.63K ops/s | 887.17 ops/s | +84.1% ✅ |
react/player/src/asset/__tests__/index.bench.tsx > ReactAsset benchmarks > Render asset nested in 5 ReactAssets |
710.30 ops/s | 601.54 ops/s | +18.1% ✅ |
react/player/src/asset/__tests__/index.bench.tsx > ReactAsset benchmarks > Bubble errors nested in 5 ReactAssets |
1.61K ops/s | 996.91 ops/s | +61.9% ✅ |
react/player/src/asset/__tests__/index.bench.tsx > ReactAsset benchmarks > Render asset nested in 10 ReactAssets |
724.40 ops/s | 600.54 ops/s | +20.6% ✅ |
react/player/src/asset/__tests__/index.bench.tsx > ReactAsset benchmarks > Bubble errors nested in 10 ReactAssets |
1.28K ops/s | 550.59 ops/s | +132.0% ✅ |
react/player/src/asset/__tests__/index.bench.tsx > ReactAsset benchmarks > Render asset nested in 50 ReactAssets |
657.74 ops/s | 425.02 ops/s | +54.8% ✅ |
react/player/src/asset/__tests__/index.bench.tsx > ReactAsset benchmarks > Bubble errors nested in 50 ReactAssets |
384.34 ops/s | 192.67 ops/s | +99.5% ✅ |
react/player/src/asset/__tests__/index.bench.tsx > ReactAsset benchmarks > Render asset nested in 100 ReactAssets |
485.63 ops/s | 346.42 ops/s | +40.2% ✅ |
react/player/src/asset/__tests__/index.bench.tsx > ReactAsset benchmarks > Bubble errors nested in 100 ReactAssets |
169.03 ops/s | 95.61 ops/s | +76.8% ✅ |
The plugin walked the flow's schema itself to find `source` entries and used `in` on every entry, which throws when an entry isn't an object. SchemaController already walks the schema when the flow starts and keeps every entry by binding, so read the declarations from its map instead. A declaration inside an array or record is now logged and ignored, as the docs describe, instead of skipped silently.
A save's `remove` now lists each removed record with its key, as an update does, so a source can address a delete with more than the key. Keys the collection doesn't hold are skipped. The collection status and `lastCommit` gain an optional `errorKind`. When a load or save fails, the plugin copies a string `kind` from the error the source threw, without interpreting it, and clears it whenever `error` clears, so content can branch on a stable value instead of the message.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## fix/late-resolve-data-stages #968 +/- ##
===================================================
===================================================
☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Bundle ReportChanges will increase total bundle size by 86.97kB (1.45%) ⬆️. This is within the configured threshold ✅ Detailed changes
|
…ler is missing The plugin created an AbortController at the start of every flow, so in a JavaScript engine without one, such as Player's iOS and Android engines, every flow failed to start, including flows that declare no collection. Create it only when the flow declares a collection, and document which AbortController members the plugin uses.
| vitest_config(name = "vitest_config") | ||
|
|
||
| js_pipeline( | ||
| package_name = "@player-ui/streaming-data-plugin", |
There was a problem hiding this comment.
I think by convention this should be @player-ui/streaming-data-plugin-core
There was a problem hiding this comment.
Renamed to @player-ui/streaming-data-plugin-core, matching @player-ui/{description}-plugin-{platform} in DEV_GUIDE.md. FYI, the other core plugins on main (@player-ui/async-node-plugin, @player-ui/context-plugin, and so on) don't use the -core suffix, so let me know if you'd rather this one matched them instead.
| | ------------------------------------------------ | ------------------------------------------------------------------------------------------------ | | ||
| | `started`, `loading`, `completed` | Where the load is | | ||
| | `loadedBatches`, `totalBatches`, `loadedRecords` | How much has arrived | | ||
| | `progress` | 0 to 1 when the source reports `totalBatches`, otherwise 0 until loading completes | |
There was a problem hiding this comment.
Little confused by this, is it always 0-1 or is it 0-totalBatches. If its the former can't the same info be gleaned from the starting/loading/completed value?
There was a problem hiding this comment.
It was 0 to 1, loadedBatches / totalBatches capped at 1, so the three flags couldn't give it, but loadedBatches and totalBatches already did, and without totalBatches it only repeated completed. I've removed it. The docs now show the expression that computes it: {{transactionsStatus.loadedBatches}} / {{transactionsStatus.totalBatches}}.
| | `error` | Why the load failed, if it did | | ||
| | `errorKind` | A machine-readable reason for `error`, if the source gave one | |
There was a problem hiding this comment.
Can these be merged into one value? Do we need both?
There was a problem hiding this comment.
Merged. error on the status and on lastCommit is now one object, { message, kind? }, so a kind can't outlive its message. I kept both parts: the message says what happened, and the kind is a stable value the source defines, so content can branch on it without matching message text. isNotEmpty({{transactionsStatus.error}}) still works as the failure check.
… error kind
- Name the package @player-ui/streaming-data-plugin-core, following the
@player-ui/{description}-plugin-{platform} convention in DEV_GUIDE.md.
- Drop the status's progress field: loadedBatches and totalBatches already
give it, and without totalBatches it only repeated completed. The docs
show the expression that computes it.
- Replace error and errorKind, on the status and on lastCommit, with one
error object, { message, kind? }, so a kind can't outlive its message.
| const { viewController, lastView, lastViewState, backgroundState } = this; | ||
| if (!viewController || viewController.currentView) return; | ||
| if (lastViewState && this.isOverlay(lastViewState)) { | ||
| if (backgroundState) viewController.onView(backgroundState); |
There was a problem hiding this comment.
Maybe a minor issue but I think this would cause a new view to be rendered, not reuse an existing one. Not sure if thats intended.
There was a problem hiding this comment.
This is actually an open question on the PR description, but I should have commented it inline too:
Open question: keeping a view on screen during a save
When the flow enters a state that isn't a
VIEW, Player setsViewController.currentViewtoundefined, so nothing is on screen while a save'sEXTERNALstate runs. So that the view that started the save can show its progress, the handler puts it back:if (lastViewState && this.isOverlay(lastViewState)) { if (backgroundState) viewController.onView(backgroundState); } else if (lastView) { viewController.currentView = lastView; }For a save started from an overlay, such as a modal that closes when the flow leaves it, the handler shows the last view that isn't an overlay instead (the
isOverlayoption says which views are). This writes a public field and works with how Player behaves today, but it isn't a supported API. A core way to keep a view current through anEXTERNALstate would replace it. I'd rather settle that API separately than hold this PR for it, and I'm happy to hear other ideas.
This doesn't cause any issues, performance or otherwise for my use case, and it's only scoped to this plugin's external state. I can leave a comment so we can replace it with a better core approach later.
There was a problem hiding this comment.
Added that comment on showViewDuringSave in 4377fe7.
There was a problem hiding this comment.
Want to make an issue for it?
| const record = collection.get(key); | ||
| if (!record) return; | ||
| context.set(rowBinding, record); | ||
| eachRow.forEach((expression) => context.evaluate(expression)); |
There was a problem hiding this comment.
If this throws then I don't think anything would be deleted. Probably a minor issue but just wanted to flag it.
There was a problem hiding this comment.
Good catch, and it went a bit further: with Player's default error handling, a failing expression doesn't throw here. Player fails the flow, the expression returns, and the save still sent whatever the earlier expressions changed.
In 4377fe7 the handler evaluates eachRow with throwErrors, so a failing expression fails only the save: nothing is sent, including removals, the row binding is cleared, and lastCommit.failedKeys lists every record the save names. I kept it all or nothing so a bulk edit is never half sent.
A failing eachRow expression used to fail the whole flow while the save still sent what the earlier expressions changed. Evaluating eachRow with throwErrors fails just the save: nothing is sent, the row binding is cleared, and lastCommit lists every record the request names as failed. Also notes on showViewDuringSave that it works around a missing core API.
| model.delete(binding); | ||
| }, | ||
| evaluate: (expression) => { | ||
| if (expression === "fail()") throw new Error("Broken"); |
There was a problem hiding this comment.
Probably want a better error message here but we can tweak it later
The merge-base changed after approval.
Adds
@player-ui/streaming-data-plugin-core, a core plugin for collections too large to keep in the data model: thousands of records that arrive in pages from a backend, are shown in something like a table, and are edited in place.It builds on the idea in #943, with a declarative API: a flow declares its collections in the schema, content decides when loading starts, and saves go through
EXTERNALstates. It would supersede #943.Stacked on #967, which is stacked on #966. It depends on #967, which lets the plugin's data middleware join the pipeline whatever order plugins are registered in. This PR's diff shows only the plugin.
How it works
source: { ref, status, params }declares a collection.refnames a source the app registers with the plugin,statusis where the plugin writes the collection's status (by default<binding>Status), andparamsare templates resolved when loading starts.loadStreamingData(binding), usually in a navigationonStart; reads never start one. A source'sloadreturns an async iterable of batches. Loading again with the same params does nothing, new params start over,retryStreamingData(binding)retries after a failure, and loads abort when the flow ends.serialize(). Change notices are narrow: appends announce<binding>.lengthand a replaced record its own index, so a view resolves again only the nodes that read what changed. Code that keeps derived state, such as a table's sort keys, cansubscribeto changes throughgetCollection(binding).commit-recordsEXTERNALstates save throughcommitRecordsHandler, which the app adds to itsExternalStatePlugin. The handler collects the changes (expressions ineachRowrun on each selected record, a draft to create or update, and the records to remove, each with its key), calls the source'scommit, merges the returned records by key, and transitions withsaved,partial, orfailed. It never throws. If aneachRowexpression fails, the handler sends nothing and fails the save, not the flow.eachRowsits besideparamrather than in it, because Player resolves the templates inparamwhen the state starts.erroron the status and onlastCommitsays why a load or save failed: amessage, and akindwhen the error the source threw carries a stringkind. The plugin copies the kind without interpreting it, so content can branch on a stable value the source defines instead of the message.Open question: keeping a view on screen during a save
When the flow enters a state that isn't a
VIEW, Player setsViewController.currentViewtoundefined, so nothing is on screen while a save'sEXTERNALstate runs. So that the view that started the save can show its progress, the handler puts it back:For a save started from an overlay, such as a modal that closes when the flow leaves it, the handler shows the last view that isn't an overlay instead (the
isOverlayoption says which views are). This writes a public field and works with how Player behaves today, but it isn't a supported API. A core way to keep a view current through anEXTERNALstate would replace it. I'd rather settle that API separately than hold this PR for it, and I'm happy to hear other ideas.Tests
52 tests in five files:
StreamingDataPlugin.test.tsruns the plugin on a plainPlayerwith a source the test feeds one batch at a time. It covers loading only on request with resolved params, serving by reference, ignored writes and deletes, declaration errors, narrow change notices, reloading with new params, retrying after a failure, cancelling when the flow ends, and saves:eachRowupdates, removals, nothing to save, partial, rejected, and a source withoutcommit, and a failed load's or save'skindreachingerror.kindand clearing with the error. It also covers the view kept during a save (including from an overlay), a misplacedeachRow, a failingeachRowexpression, and a flow that declares no collection running whereAbortControllerdoesn't exist. One test registers a plugin that reads data before this one taps the pipeline; it fails without fix(core): run data stages tapped after the pipeline is built #967.RecordCollection,collectChanges,findCollectionDeclarations, andtoCommitRequest.The plugin's vitest, eslint, typecheck, and build targets pass. The lockfile change adds only the new package's importer entry, and
pnpm install --frozen-lockfileaccepts it.Change Type (required)
Indicate the type of change your pull request is:
patchminormajorN/ADoes your PR have any documentation updates?
Adds a docs page at
plugins/core/streaming-data.Release Notes
New
@player-ui/streaming-data-plugin-coreloads large collections in batches outside the data model and saves changes to them throughcommit-recordsEXTERNALstates. See the Streaming Data plugin docs.