foldkit-sync 0.13.0 → 0.14.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.
- package/README.md +1 -1
- package/dist/remote.d.ts +2 -9
- package/dist/remote.d.ts.map +1 -1
- package/dist/remote.js +7 -6
- package/dist/remote.js.map +1 -1
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -629,7 +629,7 @@ undo's value). It owns no state and does no I/O.
|
|
|
629
629
|
|
|
630
630
|
`RemoteEdits.make(Data, ProductEdits)` is the bridge to a Remote domain over
|
|
631
631
|
the same Entity. Its `reconcile(model, edits, replica)` brings Remote's
|
|
632
|
-
overlays (`
|
|
632
|
+
overlays (`overlay` from `foldkit-remote/overlay`) in line with the slice, so every read of a row draws
|
|
633
633
|
its edits, a second view or a filter as well as the grid. It holds an edit the
|
|
634
634
|
journal absorbed while the cached row is below it, and lifts it once a read
|
|
635
635
|
reaches it; it returns those newly `held` (ask for their rows again) and this
|
package/dist/remote.d.ts
CHANGED
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
import { Option, Schema } from "effect";
|
|
2
|
-
import {
|
|
2
|
+
import { OverlaidDomain } from "foldkit-remote/overlay";
|
|
3
3
|
import { Author } from "./entity.js";
|
|
4
4
|
/** What `RemoteEdits.make` uses of a Remote domain: `Remote.make`'s value is one. */
|
|
5
|
-
export
|
|
6
|
-
readonly store: {
|
|
7
|
-
get(model: AppModel): RemoteModel;
|
|
8
|
-
};
|
|
9
|
-
overlay(model: AppModel, id: string, optimistic: ReadonlyArray<OptimisticOperation>): AppModel;
|
|
10
|
-
lift(model: AppModel, id: string): AppModel;
|
|
11
|
-
overlays(model: AppModel): ReadonlyArray<string>;
|
|
12
|
-
}
|
|
5
|
+
export type RemoteOverlays<AppModel> = OverlaidDomain<AppModel>;
|
|
13
6
|
/** What `RemoteEdits.make` uses of `EditableEntity.make`'s value, over a named Entity. */
|
|
14
7
|
export interface EditsOf<Edit> {
|
|
15
8
|
readonly entity: {
|
package/dist/remote.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote.d.ts","names":[],"sources":["../src/remote.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"remote.d.ts","names":[],"sources":["../src/remote.ts"],"mappings":";;;;YA0BY,eAAe,YAAY,eAAe;;iBAGrC,QAAQ;WACd;aAAmB;;WACnB,MAAM,OAAO,MAAM;EAC5B,KAAK,MAAM,MAAM;;;UAIT;WACC;WACA,IAAI,OAAO;WACX,IAAI,OAAO,OAAO;;;iBAWZ,UAAU;WAChB,MAAM;;WAEN;;;iBAIM,WAAW,UAAU;WAC3B,OAAO;;;;;;WAMP,MAAM,cAAc;;;;;;WAMpB,UAAU,cAAc;;cAG7B,OAAQ,UAAU,aAAa,SAAO,MACpC,eAAe,WAAS,OACvB,QAAQ;;;;;;;;;;;;;;;;;EA6Eb,YAAS,OACA,UAAQ,OACR,cAAc,OAAK,oBAEzB,WAAW,UAAU;;;;;;EA8DxB,QAAK,OAAU,aAAW,cAAc,UAAU;;;;;EAWlD,QAAK,OAAU,aAAW;;qBAKjB;;;;;;;;;;;;YAYD,YAAY,UAAU,aAAa,WAAW,kBAAkB,KAAK,UAAU"}
|
package/dist/remote.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Equal, Option, Schema } from "effect";
|
|
2
2
|
import { entityKey, readField } from "foldkit-remote";
|
|
3
|
+
import { lift, overlay, overlays } from "foldkit-remote/overlay";
|
|
3
4
|
// src/remote.ts
|
|
4
5
|
/**
|
|
5
6
|
* `foldkit-sync/remote`: a slice's cell edits shown through Remote, as
|
|
@@ -39,7 +40,7 @@ const make = (data, edits) => {
|
|
|
39
40
|
const rowKey = (edit) => entityKey(entity, String(encodedOf(edit).id));
|
|
40
41
|
const cellOf = (edit) => `${rowKey(edit)}\u0000${edit.member}`;
|
|
41
42
|
/** The edits this bridge shows, by overlay id, read back from Remote's overlays. */
|
|
42
|
-
const shownIn = (model) =>
|
|
43
|
+
const shownIn = (model) => overlays(data, model).flatMap((id) => {
|
|
43
44
|
const held = id.startsWith(HELD);
|
|
44
45
|
if (!held && !id.startsWith(SHOWN)) return [];
|
|
45
46
|
const key = id.slice((held ? HELD : SHOWN).length);
|
|
@@ -102,19 +103,19 @@ const make = (data, edits) => {
|
|
|
102
103
|
if (!shown.held && inSlice.has(shown.key)) {
|
|
103
104
|
inSlice.delete(shown.key);
|
|
104
105
|
if (!reachedNow || !differs && edits.mine(shown.edit, replica)) continue;
|
|
105
|
-
next =
|
|
106
|
+
next = lift(data, next, shown.id);
|
|
106
107
|
if (differs && edits.mine(shown.edit, replica)) replaced.push(shown.edit);
|
|
107
108
|
continue;
|
|
108
109
|
}
|
|
109
110
|
const holds = !edited.has(cellOf(shown.edit)) && Option.isSome(shown.edit.at) && Option.isSome(revisionOf(remote, shown.edit)) && !reached(remote, shown.edit);
|
|
110
111
|
if (holds && shown.held) continue;
|
|
111
|
-
next =
|
|
112
|
+
next = lift(data, next, shown.id);
|
|
112
113
|
if (holds) {
|
|
113
|
-
next =
|
|
114
|
+
next = overlay(data, next, `${HELD}${shown.key}`, [patchOf(shown.edit)]);
|
|
114
115
|
held.push(shown.edit);
|
|
115
116
|
} else if (differs && edits.mine(shown.edit, replica)) replaced.push(shown.edit);
|
|
116
117
|
}
|
|
117
|
-
for (const [key, edit] of inSlice) if (!reached(remote, edit)) next =
|
|
118
|
+
for (const [key, edit] of inSlice) if (!reached(remote, edit)) next = overlay(data, next, `${SHOWN}${key}`, [patchOf(edit)]);
|
|
118
119
|
settled = {
|
|
119
120
|
entities: remote.entities,
|
|
120
121
|
optimistic: data.store.get(next).optimistic,
|
|
@@ -143,7 +144,7 @@ const make = (data, edits) => {
|
|
|
143
144
|
* Lifts every overlay this bridge shows: for a server reset, after which
|
|
144
145
|
* the rows' revisions count a history that is gone.
|
|
145
146
|
*/
|
|
146
|
-
clear: (model) => shownIn(model).reduce((next, shown) =>
|
|
147
|
+
clear: (model) => shownIn(model).reduce((next, shown) => lift(data, next, shown.id), model)
|
|
147
148
|
};
|
|
148
149
|
};
|
|
149
150
|
const RemoteEdits = {
|
package/dist/remote.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote.js","names":[],"sources":["../src/remote.ts"],"sourcesContent":["/**\n * `foldkit-sync/remote`: a slice's cell edits shown through Remote, as\n * request-less overlays every read draws, until the row Remote holds has them.\n *\n * ```text\n * cell shown = Remote's row, with each edit over it the row has not reached:\n * one pending, one committed after the row's revision, or one\n * the journal absorbed while the cached row is still below it\n * ```\n *\n * Remote keeps the overlays; the slice stays the only copy of the edits. An\n * overlay is not stored, so after a reload `reconcile` rebuilds them from the\n * slice the replica restored. What an edit absorbed and dropped from the slice\n * still owes a cached row is held as an overlay, not in the application's\n * Model, so every view of the row (a second list, a filter, an undo's reading\n * of the page) sees what the page shows.\n *\n * Pure, like `foldkit-sync/entity`: it reads and writes the Model it is given\n * and sends nothing.\n */\nimport { Equal, Option, Schema } from 'effect'\nimport { entityKey, readField, type OptimisticOperation, type RemoteModel } from 'foldkit-remote'\nimport type { Author } from './entity.js'\n\n/** What `RemoteEdits.make` uses of a Remote domain: `Remote.make`'s value is one. */\nexport interface RemoteOverlays<AppModel> {\n readonly store: { get(model: AppModel): RemoteModel }\n overlay(model: AppModel, id: string, optimistic: ReadonlyArray<OptimisticOperation>): AppModel\n lift(model: AppModel, id: string): AppModel\n overlays(model: AppModel): ReadonlyArray<string>\n}\n\n/** What `RemoteEdits.make` uses of `EditableEntity.make`'s value, over a named Entity. */\nexport interface EditsOf<Edit> {\n readonly entity: { readonly name: string }\n readonly Edit: Schema.Codec<Edit, unknown>\n mine(edit: Edit, replica: string): boolean\n}\n\n/** The fields every cell edit has, as `EditableEntity`'s `Edit` declares them. */\ninterface AnyEdit {\n readonly member: string\n readonly at: Option.Option<number>\n readonly by: Option.Option<Author>\n}\n\n/** An edit as the store holds it: the encoded id, member and value. */\nconst Encoded = Schema.Struct({\n id: Schema.Union([Schema.String, Schema.Number]),\n member: Schema.String,\n value: Schema.Unknown,\n})\n\n/** A cell an overlay draws, and whether its edit is still in the slice or only held. */\nexport interface ShownEdit<Edit> {\n readonly edit: Edit\n /** Absorbed from the slice, and shown until a read of its row has it. */\n readonly held: boolean\n}\n\n/** What `reconcile` did besides the Model. */\nexport interface Reconciled<AppModel, Edit> {\n readonly model: AppModel\n /**\n * Edits the journal absorbed that a cached row has not been read since. The\n * row is out of date until it is: ask for it again, unless a live stream\n * brings it.\n */\n readonly held: ReadonlyArray<Edit>\n /**\n * `replica`'s own edits a read of their row reached with another value: the\n * table applies in commit order, so a later edit replaced them. The slice\n * may say whose later, or may not yet; this is the row saying it.\n */\n readonly replaced: ReadonlyArray<Edit>\n}\n\nconst make = <AppModel, Edit extends AnyEdit>(\n data: RemoteOverlays<AppModel>,\n edits: EditsOf<Edit>,\n) => {\n const entity = edits.entity.name\n const encode = Schema.encodeSync(edits.Edit)\n const decode = Schema.decodeUnknownOption(edits.Edit)\n const decodeEncoded = Schema.decodeUnknownSync(Encoded)\n // Named by the Entity, so a bridge over another one never reads these back.\n const SHOWN = `sync-edit:${entity}:`\n const HELD = `sync-held:${entity}:`\n\n // The overlay's id is the edit itself, encoded: the same edit keeps its\n // overlay, and another edit of the cell is another overlay.\n const keyOf = (edit: Edit) => JSON.stringify(encode(edit))\n const encodedOf = (edit: Edit) => decodeEncoded(encode(edit))\n const rowKey = (edit: Edit) => entityKey(entity, String(encodedOf(edit).id))\n const cellOf = (edit: Edit) => `${rowKey(edit)}\\u0000${edit.member}`\n\n /** The edits this bridge shows, by overlay id, read back from Remote's overlays. */\n const shownIn = (model: AppModel) =>\n data.overlays(model).flatMap(id => {\n const held = id.startsWith(HELD)\n if (!held && !id.startsWith(SHOWN)) return []\n const key = id.slice((held ? HELD : SHOWN).length)\n return Option.match(Option.flatMap(Option.liftThrowable(JSON.parse)(key), decode), {\n onNone: () => [],\n onSome: edit => [{ id, key, edit, held }],\n })\n })\n\n /** The revision Remote holds for the edit's row; none when the row is not cached. */\n const revisionOf = (remote: RemoteModel, edit: Edit): Option.Option<number> =>\n Option.filter(readField(remote.entities, rowKey(edit), 'revision'), Schema.is(Schema.Number))\n const reached = (remote: RemoteModel, edit: Edit) =>\n Option.exists(edit.at, at =>\n Option.exists(revisionOf(remote, edit), revision => at <= revision),\n )\n\n // Reconciling runs after every update; what it depends on rarely changes, so\n // the inputs it last left in line are kept by identity and a repeat is free.\n let settled:\n | {\n readonly entities: RemoteModel['entities']\n readonly optimistic: RemoteModel['optimistic']\n readonly slice: ReadonlyArray<Edit>\n readonly replica: string\n }\n | undefined\n\n /** Whether the row Remote holds has the edit's value in its cell. */\n const holdsValue = (remote: RemoteModel, edit: Edit) =>\n Equal.equals(\n readField(remote.entities, rowKey(edit), edit.member),\n Option.some(encodedOf(edit).value),\n )\n\n const patchOf = (edit: Edit): OptimisticOperation => {\n const { id, member, value } = encodedOf(edit)\n return { entity, id: String(id), values: { [member]: value } }\n }\n\n return {\n /**\n * Brings Remote's overlays in line with `slice`: each edit its row has not\n * reached is shown, and each overlay whose edit has left the slice is\n * lifted, unless its row is cached below it, in which case it is held\n * until a read of the row reaches it. This replica's edit the row has\n * reached with its own value is kept, landed, until the slice drops it, so\n * a later edit reaching the row live is seen replacing it. Call it from\n * `Sync.mount`'s `afterUpdate`: a read can land in any Remote Message, and\n * a replayed durable Message may not touch Remote. With nothing to change,\n * the Model comes back as it was given.\n *\n * Only a committed edit can be held: its `at` says which revision has it.\n * One that leaves the slice straight from pending, committed and absorbed\n * before this page installed the commit, is lifted, and its row shows the\n * cached read until it is read again.\n */\n reconcile: (\n model: AppModel,\n slice: ReadonlyArray<Edit>,\n replica: string,\n ): Reconciled<AppModel, Edit> => {\n const remote = data.store.get(model)\n if (\n settled !== undefined &&\n settled.entities === remote.entities &&\n settled.optimistic === remote.optimistic &&\n settled.slice === slice &&\n settled.replica === replica\n ) {\n return { model, held: [], replaced: [] }\n }\n const inSlice = new Map(slice.map(edit => [keyOf(edit), edit]))\n const edited = new Set(slice.map(cellOf))\n const held: Array<Edit> = []\n const replaced: Array<Edit> = []\n let next = model\n for (const shown of shownIn(model)) {\n const reachedNow = reached(remote, shown.edit)\n const differs = reachedNow && !holdsValue(remote, shown.edit)\n if (!shown.held && inSlice.has(shown.key)) {\n inSlice.delete(shown.key)\n // Kept while the row is below it, and, landed, while it is this\n // replica's and the row still holds it: a later edit that reaches the\n // row live is then seen replacing it, though the slice may never say.\n if (!reachedNow || (!differs && edits.mine(shown.edit, replica))) continue\n next = data.lift(next, shown.id)\n if (differs && edits.mine(shown.edit, replica)) replaced.push(shown.edit)\n continue\n }\n // Held only while nothing in the slice edits the cell and the cached\n // row is below the commit; a later edit of the cell takes its place.\n const holds =\n !edited.has(cellOf(shown.edit)) &&\n Option.isSome(shown.edit.at) &&\n Option.isSome(revisionOf(remote, shown.edit)) &&\n !reached(remote, shown.edit)\n if (holds && shown.held) continue\n next = data.lift(next, shown.id)\n if (holds) {\n next = data.overlay(next, `${HELD}${shown.key}`, [patchOf(shown.edit)])\n held.push(shown.edit)\n } else if (differs && edits.mine(shown.edit, replica)) {\n replaced.push(shown.edit)\n }\n }\n for (const [key, edit] of inSlice) {\n if (!reached(remote, edit)) next = data.overlay(next, `${SHOWN}${key}`, [patchOf(edit)])\n }\n settled = {\n entities: remote.entities,\n optimistic: data.store.get(next).optimistic,\n slice,\n replica,\n }\n return { model: next, held, replaced }\n },\n\n /**\n * The edits the overlays show over their rows now, in the order they are\n * drawn: for a cell's mark. One the row has reached, kept only to see a\n * later edit replace it, is not among them.\n */\n shown: (model: AppModel): ReadonlyArray<ShownEdit<Edit>> => {\n const remote = data.store.get(model)\n return shownIn(model).flatMap(({ edit, held }) =>\n reached(remote, edit) ? [] : [{ edit, held }],\n )\n },\n\n /**\n * Lifts every overlay this bridge shows: for a server reset, after which\n * the rows' revisions count a history that is gone.\n */\n clear: (model: AppModel): AppModel =>\n shownIn(model).reduce((next, shown) => data.lift(next, shown.id), model),\n }\n}\n\nexport const RemoteEdits = {\n /**\n * The bridge between a slice of `EditableEntity` edits and a Remote domain\n * that reads the same Entity, with its `revision`.\n *\n * ```ts\n * const Shown = RemoteEdits.make(Data, ProductEdits)\n * const { model: next } = Shown.reconcile(model, model.edits, model.replica)\n * ```\n */\n make,\n}\nexport type RemoteEdits<AppModel, Edit extends AnyEdit> = ReturnType<typeof make<AppModel, Edit>>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA+CA,MAAM,UAAU,OAAO,OAAO;CAC5B,IAAI,OAAO,MAAM,CAAC,OAAO,QAAQ,OAAO,MAAM,CAAC;CAC/C,QAAQ,OAAO;CACf,OAAO,OAAO;AAChB,CAAC;AA0BD,MAAM,QACJ,MACA,UACG;CACH,MAAM,SAAS,MAAM,OAAO;CAC5B,MAAM,SAAS,OAAO,WAAW,MAAM,IAAI;CAC3C,MAAM,SAAS,OAAO,oBAAoB,MAAM,IAAI;CACpD,MAAM,gBAAgB,OAAO,kBAAkB,OAAO;CAEtD,MAAM,QAAQ,aAAa,OAAO;CAClC,MAAM,OAAO,aAAa,OAAO;CAIjC,MAAM,SAAS,SAAe,KAAK,UAAU,OAAO,IAAI,CAAC;CACzD,MAAM,aAAa,SAAe,cAAc,OAAO,IAAI,CAAC;CAC5D,MAAM,UAAU,SAAe,UAAU,QAAQ,OAAO,UAAU,IAAI,CAAC,CAAC,EAAE,CAAC;CAC3E,MAAM,UAAU,SAAe,GAAG,OAAO,IAAI,EAAE,QAAQ,KAAK;;CAG5D,MAAM,WAAW,UACf,KAAK,SAAS,KAAK,CAAC,CAAC,SAAQ,OAAM;EACjC,MAAM,OAAO,GAAG,WAAW,IAAI;EAC/B,IAAI,CAAC,QAAQ,CAAC,GAAG,WAAW,KAAK,GAAG,OAAO,CAAC;EAC5C,MAAM,MAAM,GAAG,OAAO,OAAO,OAAO,MAAA,CAAO,MAAM;EACjD,OAAO,OAAO,MAAM,OAAO,QAAQ,OAAO,cAAc,KA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QAAQ,UAAoD;GAC1D,MAAM,SAAS,KAAK,MAAM,IAAI,KAAK;GACnC,OAAO,QAAQ,KAAK,CAAC,CAAC,SAAS,EAAE,MAAM,WACrC,QAAQ,QAAQ,IAAI,IAAI,CAAC,IAAI,CAAC;IAAE;IAAM;GAAK,CAAC,CAC9C;EACF;;;;;EAMA,QAAQ,UACN,QAAQ,KAAK,CAAC,CAAC,QAAQ,MAAM,UAAU,KAAK,KAAK,MAAM,MAAM,EAAE,GAAG,KAAK;CAC3E;AACF;AAEA,MAAa,cAAc;;;;;;;;;;AAUzB,KACF"}
|
|
1
|
+
{"version":3,"file":"remote.js","names":[],"sources":["../src/remote.ts"],"sourcesContent":["/**\n * `foldkit-sync/remote`: a slice's cell edits shown through Remote, as\n * request-less overlays every read draws, until the row Remote holds has them.\n *\n * ```text\n * cell shown = Remote's row, with each edit over it the row has not reached:\n * one pending, one committed after the row's revision, or one\n * the journal absorbed while the cached row is still below it\n * ```\n *\n * Remote keeps the overlays; the slice stays the only copy of the edits. An\n * overlay is not stored, so after a reload `reconcile` rebuilds them from the\n * slice the replica restored. What an edit absorbed and dropped from the slice\n * still owes a cached row is held as an overlay, not in the application's\n * Model, so every view of the row (a second list, a filter, an undo's reading\n * of the page) sees what the page shows.\n *\n * Pure, like `foldkit-sync/entity`: it reads and writes the Model it is given\n * and sends nothing.\n */\nimport { Equal, Option, Schema } from 'effect'\nimport { entityKey, readField, type OptimisticOperation, type RemoteModel } from 'foldkit-remote'\nimport { lift, overlay, overlays, type OverlaidDomain } from 'foldkit-remote/overlay'\nimport type { Author } from './entity.js'\n\n/** What `RemoteEdits.make` uses of a Remote domain: `Remote.make`'s value is one. */\nexport type RemoteOverlays<AppModel> = OverlaidDomain<AppModel>\n\n/** What `RemoteEdits.make` uses of `EditableEntity.make`'s value, over a named Entity. */\nexport interface EditsOf<Edit> {\n readonly entity: { readonly name: string }\n readonly Edit: Schema.Codec<Edit, unknown>\n mine(edit: Edit, replica: string): boolean\n}\n\n/** The fields every cell edit has, as `EditableEntity`'s `Edit` declares them. */\ninterface AnyEdit {\n readonly member: string\n readonly at: Option.Option<number>\n readonly by: Option.Option<Author>\n}\n\n/** An edit as the store holds it: the encoded id, member and value. */\nconst Encoded = Schema.Struct({\n id: Schema.Union([Schema.String, Schema.Number]),\n member: Schema.String,\n value: Schema.Unknown,\n})\n\n/** A cell an overlay draws, and whether its edit is still in the slice or only held. */\nexport interface ShownEdit<Edit> {\n readonly edit: Edit\n /** Absorbed from the slice, and shown until a read of its row has it. */\n readonly held: boolean\n}\n\n/** What `reconcile` did besides the Model. */\nexport interface Reconciled<AppModel, Edit> {\n readonly model: AppModel\n /**\n * Edits the journal absorbed that a cached row has not been read since. The\n * row is out of date until it is: ask for it again, unless a live stream\n * brings it.\n */\n readonly held: ReadonlyArray<Edit>\n /**\n * `replica`'s own edits a read of their row reached with another value: the\n * table applies in commit order, so a later edit replaced them. The slice\n * may say whose later, or may not yet; this is the row saying it.\n */\n readonly replaced: ReadonlyArray<Edit>\n}\n\nconst make = <AppModel, Edit extends AnyEdit>(\n data: RemoteOverlays<AppModel>,\n edits: EditsOf<Edit>,\n) => {\n const entity = edits.entity.name\n const encode = Schema.encodeSync(edits.Edit)\n const decode = Schema.decodeUnknownOption(edits.Edit)\n const decodeEncoded = Schema.decodeUnknownSync(Encoded)\n // Named by the Entity, so a bridge over another one never reads these back.\n const SHOWN = `sync-edit:${entity}:`\n const HELD = `sync-held:${entity}:`\n\n // The overlay's id is the edit itself, encoded: the same edit keeps its\n // overlay, and another edit of the cell is another overlay.\n const keyOf = (edit: Edit) => JSON.stringify(encode(edit))\n const encodedOf = (edit: Edit) => decodeEncoded(encode(edit))\n const rowKey = (edit: Edit) => entityKey(entity, String(encodedOf(edit).id))\n const cellOf = (edit: Edit) => `${rowKey(edit)}\\u0000${edit.member}`\n\n /** The edits this bridge shows, by overlay id, read back from Remote's overlays. */\n const shownIn = (model: AppModel) =>\n overlays(data, model).flatMap(id => {\n const held = id.startsWith(HELD)\n if (!held && !id.startsWith(SHOWN)) return []\n const key = id.slice((held ? HELD : SHOWN).length)\n return Option.match(Option.flatMap(Option.liftThrowable(JSON.parse)(key), decode), {\n onNone: () => [],\n onSome: edit => [{ id, key, edit, held }],\n })\n })\n\n /** The revision Remote holds for the edit's row; none when the row is not cached. */\n const revisionOf = (remote: RemoteModel, edit: Edit): Option.Option<number> =>\n Option.filter(readField(remote.entities, rowKey(edit), 'revision'), Schema.is(Schema.Number))\n const reached = (remote: RemoteModel, edit: Edit) =>\n Option.exists(edit.at, at =>\n Option.exists(revisionOf(remote, edit), revision => at <= revision),\n )\n\n // Reconciling runs after every update; what it depends on rarely changes, so\n // the inputs it last left in line are kept by identity and a repeat is free.\n let settled:\n | {\n readonly entities: RemoteModel['entities']\n readonly optimistic: RemoteModel['optimistic']\n readonly slice: ReadonlyArray<Edit>\n readonly replica: string\n }\n | undefined\n\n /** Whether the row Remote holds has the edit's value in its cell. */\n const holdsValue = (remote: RemoteModel, edit: Edit) =>\n Equal.equals(\n readField(remote.entities, rowKey(edit), edit.member),\n Option.some(encodedOf(edit).value),\n )\n\n const patchOf = (edit: Edit): OptimisticOperation => {\n const { id, member, value } = encodedOf(edit)\n return { entity, id: String(id), values: { [member]: value } }\n }\n\n return {\n /**\n * Brings Remote's overlays in line with `slice`: each edit its row has not\n * reached is shown, and each overlay whose edit has left the slice is\n * lifted, unless its row is cached below it, in which case it is held\n * until a read of the row reaches it. This replica's edit the row has\n * reached with its own value is kept, landed, until the slice drops it, so\n * a later edit reaching the row live is seen replacing it. Call it from\n * `Sync.mount`'s `afterUpdate`: a read can land in any Remote Message, and\n * a replayed durable Message may not touch Remote. With nothing to change,\n * the Model comes back as it was given.\n *\n * Only a committed edit can be held: its `at` says which revision has it.\n * One that leaves the slice straight from pending, committed and absorbed\n * before this page installed the commit, is lifted, and its row shows the\n * cached read until it is read again.\n */\n reconcile: (\n model: AppModel,\n slice: ReadonlyArray<Edit>,\n replica: string,\n ): Reconciled<AppModel, Edit> => {\n const remote = data.store.get(model)\n if (\n settled !== undefined &&\n settled.entities === remote.entities &&\n settled.optimistic === remote.optimistic &&\n settled.slice === slice &&\n settled.replica === replica\n ) {\n return { model, held: [], replaced: [] }\n }\n const inSlice = new Map(slice.map(edit => [keyOf(edit), edit]))\n const edited = new Set(slice.map(cellOf))\n const held: Array<Edit> = []\n const replaced: Array<Edit> = []\n let next = model\n for (const shown of shownIn(model)) {\n const reachedNow = reached(remote, shown.edit)\n const differs = reachedNow && !holdsValue(remote, shown.edit)\n if (!shown.held && inSlice.has(shown.key)) {\n inSlice.delete(shown.key)\n // Kept while the row is below it, and, landed, while it is this\n // replica's and the row still holds it: a later edit that reaches the\n // row live is then seen replacing it, though the slice may never say.\n if (!reachedNow || (!differs && edits.mine(shown.edit, replica))) continue\n next = lift(data, next, shown.id)\n if (differs && edits.mine(shown.edit, replica)) replaced.push(shown.edit)\n continue\n }\n // Held only while nothing in the slice edits the cell and the cached\n // row is below the commit; a later edit of the cell takes its place.\n const holds =\n !edited.has(cellOf(shown.edit)) &&\n Option.isSome(shown.edit.at) &&\n Option.isSome(revisionOf(remote, shown.edit)) &&\n !reached(remote, shown.edit)\n if (holds && shown.held) continue\n next = lift(data, next, shown.id)\n if (holds) {\n next = overlay(data, next, `${HELD}${shown.key}`, [patchOf(shown.edit)])\n held.push(shown.edit)\n } else if (differs && edits.mine(shown.edit, replica)) {\n replaced.push(shown.edit)\n }\n }\n for (const [key, edit] of inSlice) {\n if (!reached(remote, edit)) next = overlay(data, next, `${SHOWN}${key}`, [patchOf(edit)])\n }\n settled = {\n entities: remote.entities,\n optimistic: data.store.get(next).optimistic,\n slice,\n replica,\n }\n return { model: next, held, replaced }\n },\n\n /**\n * The edits the overlays show over their rows now, in the order they are\n * drawn: for a cell's mark. One the row has reached, kept only to see a\n * later edit replace it, is not among them.\n */\n shown: (model: AppModel): ReadonlyArray<ShownEdit<Edit>> => {\n const remote = data.store.get(model)\n return shownIn(model).flatMap(({ edit, held }) =>\n reached(remote, edit) ? [] : [{ edit, held }],\n )\n },\n\n /**\n * Lifts every overlay this bridge shows: for a server reset, after which\n * the rows' revisions count a history that is gone.\n */\n clear: (model: AppModel): AppModel =>\n shownIn(model).reduce((next, shown) => lift(data, next, shown.id), model),\n }\n}\n\nexport const RemoteEdits = {\n /**\n * The bridge between a slice of `EditableEntity` edits and a Remote domain\n * that reads the same Entity, with its `revision`.\n *\n * ```ts\n * const Shown = RemoteEdits.make(Data, ProductEdits)\n * const { model: next } = Shown.reconcile(model, model.edits, model.replica)\n * ```\n */\n make,\n}\nexport type RemoteEdits<AppModel, Edit extends AnyEdit> = ReturnType<typeof make<AppModel, Edit>>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,MAAM,UAAU,OAAO,OAAO;CAC5B,IAAI,OAAO,MAAM,CAAC,OAAO,QAAQ,OAAO,MAAM,CAAC;CAC/C,QAAQ,OAAO;CACf,OAAO,OAAO;AAChB,CAAC;AA0BD,MAAM,QACJ,MACA,UACG;CACH,MAAM,SAAS,MAAM,OAAO;CAC5B,MAAM,SAAS,OAAO,WAAW,MAAM,IAAI;CAC3C,MAAM,SAAS,OAAO,oBAAoB,MAAM,IAAI;CACpD,MAAM,gBAAgB,OAAO,kBAAkB,OAAO;CAEtD,MAAM,QAAQ,aAAa,OAAO;CAClC,MAAM,OAAO,aAAa,OAAO;CAIjC,MAAM,SAAS,SAAe,KAAK,UAAU,OAAO,IAAI,CAAC;CACzD,MAAM,aAAa,SAAe,cAAc,OAAO,IAAI,CAAC;CAC5D,MAAM,UAAU,SAAe,UAAU,QAAQ,OAAO,UAAU,IAAI,CAAC,CAAC,EAAE,CAAC;CAC3E,MAAM,UAAU,SAAe,GAAG,OAAO,IAAI,EAAE,QAAQ,KAAK;;CAG5D,MAAM,WAAW,UACf,SAAS,MAAM,KAAK,CAAC,CAAC,SAAQ,OAAM;EAClC,MAAM,OAAO,GAAG,WAAW,IAAI;EAC/B,IAAI,CAAC,QAAQ,CAA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1F,UAAU;IACR,UAAU,OAAO;IACjB,YAAY,KAAK,MAAM,IAAI,IAAI,CAAC,CAAC;IACjC;IACA;GACF;GACA,OAAO;IAAE,OAAO;IAAM;IAAM;GAAS;EACvC;;;;;;EAOA,QAAQ,UAAoD;GAC1D,MAAM,SAAS,KAAK,MAAM,IAAI,KAAK;GACnC,OAAO,QAAQ,KAAK,CAAC,CAAC,SAAS,EAAE,MAAM,WACrC,QAAQ,QAAQ,IAAI,IAAI,CAAC,IAAI,CAAC;IAAE;IAAM;GAAK,CAAC,CAC9C;EACF;;;;;EAMA,QAAQ,UACN,QAAQ,KAAK,CAAC,CAAC,QAAQ,MAAM,UAAU,KAAK,MAAM,MAAM,MAAM,EAAE,GAAG,KAAK;CAC5E;AACF;AAEA,MAAa,cAAc;;;;;;;;;;AAUzB,KACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "foldkit-sync",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "A local-first replica of a shared Foldkit projection: offline outbox, optimistic updates, and reconciliation.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"foldkit",
|
|
@@ -59,13 +59,13 @@
|
|
|
59
59
|
"LICENSE"
|
|
60
60
|
],
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"foldkit-surface": "0.
|
|
62
|
+
"foldkit-surface": "0.12.0"
|
|
63
63
|
},
|
|
64
64
|
"peerDependencies": {
|
|
65
65
|
"effect": "^4.1.0",
|
|
66
66
|
"foldkit": "^0.170.0",
|
|
67
|
-
"foldkit-
|
|
68
|
-
"foldkit-
|
|
67
|
+
"foldkit-remote": "0.23.0",
|
|
68
|
+
"foldkit-durable": "0.11.0"
|
|
69
69
|
},
|
|
70
70
|
"peerDependenciesMeta": {
|
|
71
71
|
"foldkit-durable": {
|
|
@@ -77,22 +77,22 @@
|
|
|
77
77
|
},
|
|
78
78
|
"devDependencies": {
|
|
79
79
|
"@cloudflare/workers-types": "5.20261022.1",
|
|
80
|
-
"@effect/platform-browser": "^4.1.
|
|
81
|
-
"@effect/sql-d1": "^4.1.
|
|
82
|
-
"effect": "^4.1.
|
|
80
|
+
"@effect/platform-browser": "^4.1.1",
|
|
81
|
+
"@effect/sql-d1": "^4.1.1",
|
|
82
|
+
"effect": "^4.1.1",
|
|
83
83
|
"esbuild": "0.28.2",
|
|
84
84
|
"foldkit": "0.170.0",
|
|
85
85
|
"jsdom": "30.1.3",
|
|
86
86
|
"miniflare": "4.20260730.0",
|
|
87
87
|
"tsdown": "^0.23.1",
|
|
88
88
|
"typescript": "7.0.2",
|
|
89
|
-
"foldkit-durable": "0.
|
|
90
|
-
"foldkit-
|
|
91
|
-
"foldkit-
|
|
92
|
-
"foldkit-
|
|
89
|
+
"foldkit-durable": "0.11.0",
|
|
90
|
+
"foldkit-remote": "0.23.0",
|
|
91
|
+
"foldkit-agent": "0.10.0",
|
|
92
|
+
"foldkit-entity": "0.15.0"
|
|
93
93
|
},
|
|
94
94
|
"scripts": {
|
|
95
|
-
"build": "tsdown",
|
|
95
|
+
"build": "node ../../scripts/declarations.mjs . && tsdown && node ../../scripts/declared-any.mjs .",
|
|
96
96
|
"typecheck": "tsc -b"
|
|
97
97
|
}
|
|
98
98
|
}
|