Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ plugins/reference-assets/core/node_modules
plugins/reference-assets/react/node_modules
plugins/shared-constants/core/node_modules
plugins/stage-revert-data/core/node_modules
plugins/streaming-data/core/node_modules
tools/asset-testing-library/core/node_modules
docs/storybook/node_modules
plugins/reference-assets/mocks/node_modules
Expand Down
174 changes: 174 additions & 0 deletions docs/site/src/content/docs/plugins/core/streaming-data.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
---
title: Streaming Data
---

import CoreOnly from "../../../../components/CoreOnly.astro";

<CoreOnly />

The Streaming Data Plugin loads large collections into Player in batches without storing them in the data model. A flow declares each collection in its schema, the app registers the sources that load them, and content decides when loading starts. The plugin holds each record once, serves the list at the collection's binding, writes the collection's status to the data model, and saves changes through `EXTERNAL` states.

The plugin doesn't sort, search, filter, or page. Whatever shows the collection, such as a table asset, does that work on the records the plugin serves.

## Usage

Register the plugin with a source for each collection, and add its `commitRecordsHandler` to the app's [External State Plugin](/plugins/multiplatform/external-state) so flows can save changes:

```ts
import { Player } from "@player-ui/player";
import { ExternalStatePlugin } from "@player-ui/external-state-plugin";
import { StreamingDataPlugin } from "@player-ui/streaming-data-plugin-core";

const streamingData = new StreamingDataPlugin({
transactions: {
key: (transaction) => transaction.id,
async *load({ params, signal }) {
let page = 0;
let totalPages = 1;
while (page < totalPages) {
const response = await fetch(
`/accounts/${params.accountId}/transactions?page=${page}`,
{ signal },
);
const body = await response.json();
totalPages = body.totalPages;
page += 1;
yield { records: body.records, totalBatches: totalPages };
}
},
async commit(changes, { params, signal }) {
const response = await fetch(
`/accounts/${params.accountId}/transactions`,
{
method: "POST",
body: JSON.stringify(changes),
signal,
},
);
return response.json();
},
},
});

const player = new Player({
plugins: [
streamingData,
new ExternalStatePlugin([streamingData.commitRecordsHandler]),
],
});
```

A source has three parts:

- `key(record)` returns the record's unique key, or an empty string for a draft that hasn't been saved yet.
- `load({ params, signal })` returns an async iterable of batches. Each batch has `records`, and optionally `totalBatches`, which the plugin copies to the status. The `signal` aborts when the flow ends or a new load replaces this one.
- `commit(changes, { params, signal })` is optional. It receives the records to `create`, the records to `update` (each with its `key`, the changed fields in `patch`, and the whole `record` with them applied), and the records to `remove` (each with its `key` and the `record` the collection holds, so a source can address a delete with more than the key). It resolves with the saved `records` as the backend returned them, the `removed` keys, and the records that `failed`, each with a `key` and a `reason`.

The plugin creates `AbortController`s for the `signal`s, and uses only `abort()`, `signal.aborted`, and `signal.addEventListener("abort", ...)`. JavaScript engines without `AbortController`, such as the ones Player uses on iOS and Android, need one added before a flow that declares a collection starts. Flows without a collection don't need it.

## Declaring a collection

A collection is a schema entry with a `source`:

```json
{
"schema": {
"ROOT": {
"transactions": {
"type": "TransactionType",
"isArray": true,
"source": {
"ref": "transactions",
"status": "transactionsStatus",
"params": { "accountId": "{{accountId}}" }
}
}
}
}
}
```

- `ref` names the source the app registered.
- `status` is where the plugin writes the collection's status. It defaults to `<binding>Status`.
- `params` are the source's inputs. The plugin resolves their templates when loading starts.

A collection can sit inside nested types, but not inside an array or record entry. The plugin logs an error for a declaration it can't use and ignores it.

## Loading

Reading a collection never starts a load. Content starts one with the `loadStreamingData` expression, usually in a navigation `onStart`:

```json
{
"VIEW_transactions": {
"state_type": "VIEW",
"ref": "transactions-view",
"onStart": "loadStreamingData('transactions')",
"transitions": {}
}
}
```

Loading the same collection again with the same params does nothing. With different params, the plugin drops the records and loads again. After a load fails, `retryStreamingData('transactions')` starts it over.

While a load runs, the plugin writes this status to the status binding:

| Field | Meaning |
| ------------------------------------------------ | ----------------------------------------------------------------------------------- |
| `started`, `loading`, `completed` | Where the load is |
| `loadedBatches`, `totalBatches`, `loadedRecords` | How much has arrived |
| `error` | Why the load failed, if it did: a `message`, and a `kind` if the source gave one |
| `committing` | Whether a save is running |
| `lastCommit` | The latest save's `outcome`, `saved` and `failed` counts, `failedKeys`, and `error` |

To show progress when the source reports `totalBatches`, divide `loadedBatches` by it, as in `{{transactionsStatus.loadedBatches}} / {{transactionsStatus.totalBatches}}`.

When a load or save fails, `error.message` is the message of the error the source threw, and the plugin copies a string `kind` from that error to `error.kind` without interpreting it. The source defines the kinds, such as `throttled` or `unavailable`, so content can branch on a stable value instead of the message.

## Reading records

The collection's binding serves the plugin's array by reference, so reading `transactions` doesn't copy it and `transactions.0.amount` reads one field of one record. Records are frozen, the binding is read-only, and the collection stays out of `serialize()`. The plugin logs a warning for each write or delete it ignores.

When records change, the plugin tells Player as narrowly as it can, so a view only resolves again what reads the change: appended records announce `transactions.length`, and a replaced record announces its own index. Removals move records, so they announce the whole collection.

Code that keeps more than the array, such as a table's sort keys, can get the collection with `getCollection(binding)` and `subscribe` to its changes: `appended`, `updated`, `removed`, `reset`, and `committed`.

## Saving

Saves run in `EXTERNAL` states with the `commit-records` ref. The handler collects the changes, calls the source's `commit`, merges the returned records by key, and transitions with `saved`, `partial`, or `failed`. It never throws.

To change every selected record, name the keys in `param.update`, a binding to copy each record to in `param.rowBinding`, and the expressions that change it in `eachRow`:

```json
{
"EXTERNAL_categorize": {
"state_type": "EXTERNAL",
"ref": "commit-records",
"param": {
"binding": "transactions",
"update": "selectedKeys",
"rowBinding": "row"
},
"eachRow": ["{{row.category}} = 'travel'"],
"transitions": {
"saved": "VIEW_transactions",
"partial": "VIEW_transactions",
"failed": "VIEW_transactions"
}
}
}
```

`eachRow` goes on the state beside `param`, not inside it, because Player resolves the templates in `param` when the state starts. The plugin fails a save with `eachRow` inside `param` and logs why.

`param.save` names a binding holding a draft record: a draft without a key is created, and a draft with a key saves its changed fields. `param.remove` names a binding holding the key, or keys, to remove. One state can combine all three.

If an `eachRow` expression fails, the plugin sends nothing, not even the removals, and fails the save instead of the flow: `lastCommit.error` says why, and `failedKeys` lists every record that `update` and `remove` name.

Player drops the current view when an `EXTERNAL` state starts. So that the view can show the save running, the handler keeps the view that started the save current until the flow moves on. If that view is an overlay that closes when the flow leaves it, such as a modal, the handler shows the view underneath instead. Tell the plugin which views are overlays with the `isOverlay` option:

```ts
new StreamingDataPlugin(sources, {
isOverlay: (state) => state.attributes?.overlay === true,
});
```
23 changes: 23 additions & 0 deletions plugins/streaming-data/core/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@rules_player//javascript:defs.bzl", "js_pipeline")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("//tools:defs.bzl", "tsup_config", "vitest_config")

npm_link_all_packages(name = "node_modules")

tsup_config(name = "tsup_config")

vitest_config(name = "vitest_config")

js_pipeline(
package_name = "@player-ui/streaming-data-plugin-core",
peer_deps = [
":node_modules/@player-ui/external-state-plugin",
":node_modules/@player-ui/player",
],
test_deps = [
"//:vitest_config",
],
deps = [
"//:node_modules/queue-microtask",
],
)
9 changes: 9 additions & 0 deletions plugins/streaming-data/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@player-ui/streaming-data-plugin-core",
"version": "0.0.0-PLACEHOLDER",
"main": "src/index.ts",
"peerDependencies": {
"@player-ui/external-state-plugin": "workspace:^",
"@player-ui/player": "workspace:^"
}
}
84 changes: 84 additions & 0 deletions plugins/streaming-data/core/src/CollectionState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import type { BindingInstance } from "@player-ui/player";
import type { RecordCollection } from "./RecordCollection";
import type {
CollectionSourceDeclaration,
CollectionStatus,
RecordSource,
} from "./types";

/** One load of a collection: the params it resolved, and how to cancel it */
export interface ActiveLoad {
params: Readonly<Record<string, unknown>>;
paramsKey: string;
controller: AbortController;
}

export const INITIAL_STATUS: CollectionStatus = {
started: false,
loading: false,
completed: false,
loadedBatches: 0,
loadedRecords: 0,
committing: false,
};

/** The per-flow state of one declared collection */
export class CollectionState {
readonly binding: BindingInstance;

readonly declaration: CollectionSourceDeclaration;

readonly source?: RecordSource<object>;

readonly collection: RecordCollection<object>;

private currentStatus: CollectionStatus = INITIAL_STATUS;

private currentLoad?: ActiveLoad;

private revisionCount = 0;

constructor(
binding: BindingInstance,
declaration: CollectionSourceDeclaration,
collection: RecordCollection<object>,
source?: RecordSource<object>,
) {
this.binding = binding;
this.declaration = declaration;
this.collection = collection;
this.source = source;
}

get status(): CollectionStatus {
return this.currentStatus;
}

get load(): ActiveLoad | undefined {
return this.currentLoad;
}

/** Merges a status change; returns the new status */
patch(change: Partial<CollectionStatus>): CollectionStatus {
this.currentStatus = { ...this.currentStatus, ...change };
return this.currentStatus;
}

/** Starts the status over from INITIAL_STATUS, dropping the last load's error and totals */
reset(change: Partial<CollectionStatus>): CollectionStatus {
this.currentStatus = { ...INITIAL_STATUS, ...change };
return this.currentStatus;
}

/** Makes `load` the current load, cancelling the one before it */
begin(load: ActiveLoad): void {
this.currentLoad?.controller.abort();
this.currentLoad = load;
}

/** A new value for each change notice written to the data model */
nextRevision(): number {
this.revisionCount += 1;
return this.revisionCount;
}
}
Loading
Loading