foldkit-sync 0.8.0 → 0.10.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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"make-h5yD8STj.d.mts","names":[],"sources":["../src/ids.ts","../src/errors.ts","../src/indexedDb.ts","../src/transport.ts","../src/sync.ts","../src/make.ts"],"mappings":";;;;cAGa,YAAU,OAAA,MAAA,OAAA;KACX,oBAAoB,WAAW;;cAG9B,WAAS,OAAA,MAAA,OAAA;KACV,mBAAmB,UAAU;;cAG5B,MAAI,OAAA,MAAA,OAAA;KACL,cAAc,KAAK;;;;;cAMlB,UAAQ,OAAA,MAAA,OAAA;KAKT,kBAAkB,SAAS;;cAG1B,eAAa,OAAA,MAAA,OAAA;KAKd,uBAAuB,cAAc;;;;;;cAOpC,aAAc,kBAAgB;cAC9B,YAAa,kBAAgB;cAC7B,OAAQ,kBAAgB;cACxB,WAAY,kBAAgB;cAC5B,gBAAiB,kBAAgB;;;;;;;;cCvCjC,qBAAqB;;;;;;cAMrB,qCAAqC;;;;;;;cASrC,iCAAiC;;;;;;cAUjC,mCAAmC;;;;;;cASnC,6BAA6B;;;;;cAS7B,2BAA2B;;;;;;;cAQ3B,kCAAkC;;;;;;;cAUlC,4BAA4B;;;;;;cAU5B,8BAA8B;;;;;;;;;;cAa9B,oCAAoC;;;;;cASpC,2BAA2B;;;;;;;cAQ3B,uCAAuC;;;;;;;;;cAavC,oBAAoB;KAKrB,eACR,eACA,cACA,2BACA,6BACA,uBACA,iCACA,qBACA,4BACA,sBACA,wBACA,8BACA;;;;UClIa;;WAEN;WACA;;;;;WAKA;;WAEA;;;UAIM;;;;;EAKf,YAAY,OAAO,gBAAgB;;EAEnC,OAAO,gBAAgB,oCAAoC,OAAO,aAAa;;;;;;EAM/E,UAAU,OAAO,aAAa,6BAA6B,OAAO,aAAa;;EAE/E,OAAO,OAAO;;;cAwCH,YAAS,eAAA,OAAA,gBAAA,UAA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,OAAO,OAAO,KAAK,SAAS,4BAEtE,sBAAsB,WAAW,OAAO,OAAO,KAAK,SAAS;;;UAIlD,gBACf,UACA,UAAU,OAAO,OAAO,QACxB,cAAc,eAAe,OAAO,OAAO,SAC3C;;;;;;WAOS,gBAAgB,QAAQ,gBAAgB,UAAU,GAAG,OAAO;;WAE5D,WACP,eAAe,OAAO,OAAO,QAC7B,QACA,oBAAoB,mBAAmB,WAAW,UAAU,GAAG,WAC/D;aACS,QAAQ,mBAAmB,UAAU;aACrC,SAAS,WAAW,eAAe,QAAQ;QAChD,aAAa,UAAU,QAAQ,QAAQ;;;;;;WAMpC,gBAAgB,oBAAoB,aAAa,+BACrD,WAAW,OACX,aAAa,UAAU,eAAe,KAAK,iBAAiB,KAAK,qBAAqB;WAClF;KAEL,eAAe,OAAO,OAAO,QAC7B,QACA,oBAAoB,mBAAmB,WAAW,UAAU,GAAG,WAE/D,SAAS,YAAY,UAAU,QAAQ,QAAQ,IAAI;eACxC,WAAW,gBAAgB,WAAW,QAAQ,SAAS,QAAQ;QAEzE,YAAY,UAAU,QAAQ,WAAW,UAAU,GAAG,QAAQ,IAAI;KAEnE,eAAe,OAAO,OAAO,QAC7B,QACA,oBAAoB,mBAAmB,WAAW,UAAU,GAAG,WAE/D,SAAS,YAAY,UAAU,QAAQ,QAAQ,IAAI,aAClD,YAAY,UAAU,QAAQ,WAAW,UAAU,GAAG,QAAQ,IAAI;;;;;;;;;;;cAqF5D,iBACX,UACA,UAAU,OAAO,OAAO,QACxB,cAAc,eAAe,OAAO,OAAO,SAE3C,KAAK,oBAAoB,UAAU,GAAG,gBACrC,gBAAgB,UAAU,GAAG"}
@@ -0,0 +1,123 @@
1
+ import { Author } from "./entity.mjs";
2
+ import { Option, Schema } from "effect";
3
+ import { OptimisticOperation, RemoteModel } from "foldkit-remote";
4
+ //#region src/remote.d.ts
5
+ /** What `RemoteEdits.make` uses of a Remote domain: `Remote.make`'s value is one. */
6
+ export interface RemoteOverlays<AppModel> {
7
+ readonly store: {
8
+ get(model: AppModel): RemoteModel;
9
+ };
10
+ overlay(model: AppModel, id: string, optimistic: ReadonlyArray<OptimisticOperation>): AppModel;
11
+ lift(model: AppModel, id: string): AppModel;
12
+ overlays(model: AppModel): ReadonlyArray<string>;
13
+ }
14
+ /** What `RemoteEdits.make` uses of `EditableEntity.make`'s value, over a named Entity. */
15
+ export interface EditsOf<Edit> {
16
+ readonly entity: {
17
+ readonly name: string;
18
+ };
19
+ readonly Edit: Schema.Codec<Edit, unknown>;
20
+ mine(edit: Edit, replica: string): boolean;
21
+ }
22
+ /** The fields every cell edit has, as `EditableEntity`'s `Edit` declares them. */
23
+ interface AnyEdit {
24
+ readonly member: string;
25
+ readonly at: Option.Option<number>;
26
+ readonly by: Option.Option<Author>;
27
+ }
28
+ /** A cell an overlay draws, and whether its edit is still in the slice or only held. */
29
+ export interface ShownEdit<Edit> {
30
+ readonly edit: Edit;
31
+ /** Absorbed from the slice, and shown until a read of its row has it. */
32
+ readonly held: boolean;
33
+ }
34
+ /** What `reconcile` did besides the Model. */
35
+ export interface Reconciled<AppModel, Edit> {
36
+ readonly model: AppModel;
37
+ /**
38
+ * Edits the journal absorbed that a cached row has not been read since. The
39
+ * row is out of date until it is: ask for it again, unless a live stream
40
+ * brings it.
41
+ */
42
+ readonly held: ReadonlyArray<Edit>;
43
+ /**
44
+ * `replica`'s own edits a read of their row reached with another value: the
45
+ * table applies in commit order, so a later edit replaced them. The slice
46
+ * may say whose later, or may not yet; this is the row saying it.
47
+ */
48
+ readonly replaced: ReadonlyArray<Edit>;
49
+ }
50
+ declare const make: <AppModel, Edit extends AnyEdit>(data: RemoteOverlays<AppModel>, edits: EditsOf<Edit>) => {
51
+ /**
52
+ * Brings Remote's overlays in line with `slice`: each edit its row has not
53
+ * reached is shown, and each overlay whose edit has left the slice is
54
+ * lifted, unless its row is cached below it, in which case it is held
55
+ * until a read of the row reaches it. This replica's edit the row has
56
+ * reached with its own value is kept, landed, until the slice drops it, so
57
+ * a later edit reaching the row live is seen replacing it. Call it from
58
+ * `Sync.mount`'s `afterUpdate`: a read can land in any Remote Message, and
59
+ * a replayed durable Message may not touch Remote. With nothing to change,
60
+ * the Model comes back as it was given.
61
+ *
62
+ * Only a committed edit can be held: its `at` says which revision has it.
63
+ * One that leaves the slice straight from pending, committed and absorbed
64
+ * before this page installed the commit, is lifted, and its row shows the
65
+ * cached read until it is read again.
66
+ */
67
+ reconcile: (model: AppModel, slice: ReadonlyArray<Edit>, replica: string) => Reconciled<AppModel, Edit>;
68
+ /**
69
+ * The edits the overlays show over their rows now, in the order they are
70
+ * drawn: for a cell's mark. One the row has reached, kept only to see a
71
+ * later edit replace it, is not among them.
72
+ */
73
+ shown: (model: AppModel) => ReadonlyArray<ShownEdit<Edit>>;
74
+ /**
75
+ * Lifts every overlay this bridge shows: for a server reset, after which
76
+ * the rows' revisions count a history that is gone.
77
+ */
78
+ clear: (model: AppModel) => AppModel;
79
+ };
80
+ export declare const RemoteEdits: {
81
+ /**
82
+ * The bridge between a slice of `EditableEntity` edits and a Remote domain
83
+ * that reads the same Entity, with its `revision`.
84
+ *
85
+ * ```ts
86
+ * const Shown = RemoteEdits.make(Data, ProductEdits)
87
+ * const { model: next } = Shown.reconcile(model, model.edits, model.replica)
88
+ * ```
89
+ */
90
+ make: <AppModel, Edit extends AnyEdit>(data: RemoteOverlays<AppModel>, edits: EditsOf<Edit>) => {
91
+ /**
92
+ * Brings Remote's overlays in line with `slice`: each edit its row has not
93
+ * reached is shown, and each overlay whose edit has left the slice is
94
+ * lifted, unless its row is cached below it, in which case it is held
95
+ * until a read of the row reaches it. This replica's edit the row has
96
+ * reached with its own value is kept, landed, until the slice drops it, so
97
+ * a later edit reaching the row live is seen replacing it. Call it from
98
+ * `Sync.mount`'s `afterUpdate`: a read can land in any Remote Message, and
99
+ * a replayed durable Message may not touch Remote. With nothing to change,
100
+ * the Model comes back as it was given.
101
+ *
102
+ * Only a committed edit can be held: its `at` says which revision has it.
103
+ * One that leaves the slice straight from pending, committed and absorbed
104
+ * before this page installed the commit, is lifted, and its row shows the
105
+ * cached read until it is read again.
106
+ */
107
+ reconcile: (model: AppModel, slice: ReadonlyArray<Edit>, replica: string) => Reconciled<AppModel, Edit>;
108
+ /**
109
+ * The edits the overlays show over their rows now, in the order they are
110
+ * drawn: for a cell's mark. One the row has reached, kept only to see a
111
+ * later edit replace it, is not among them.
112
+ */
113
+ shown: (model: AppModel) => ReadonlyArray<ShownEdit<Edit>>;
114
+ /**
115
+ * Lifts every overlay this bridge shows: for a server reset, after which
116
+ * the rows' revisions count a history that is gone.
117
+ */
118
+ clear: (model: AppModel) => AppModel;
119
+ };
120
+ };
121
+ export type RemoteEdits<AppModel, Edit extends AnyEdit> = ReturnType<typeof make<AppModel, Edit>>;
122
+ //#endregion
123
+ //# sourceMappingURL=remote.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remote.d.mts","names":[],"sources":["../src/remote.ts"],"mappings":";;;;;iBAyBiB,eAAe;WACrB;IAAS,IAAI,OAAO,WAAW;;EACxC,QAAQ,OAAO,UAAU,YAAY,YAAY,cAAc,uBAAuB;EACtF,KAAK,OAAO,UAAU,aAAa;EACnC,SAAS,OAAO,WAAW;;;iBAIZ,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,SACnC,MAAM,eAAe,WACrB,OAAO,QAAQ;;;;;;;;;;;;;;;;;EA8EJ,YAAA,OAAA,UAAQ,OACR,cAAc,OAAK,oBAEzB,WAAW,UAAU;;;;;;EA8DT,QAAA,OAAA,aAAW,cAAc,UAAU;;;;;EAWnC,QAAA,OAAA,aAAW;;qBAKjB;;;;;;;;;;EAjKC,OAAA,UAAU,aAAa,SAAO,MACpC,eAAe,WAAS,OACvB,QAAQ;;;;;;;;;;;;;;;;;IA8EJ,YAAA,OAAA,UAAQ,OACR,cAAc,OAAK,oBAEzB,WAAW,UAAU;;;;;;IA8DT,QAAA,OAAA,aAAW,cAAc,UAAU;;;;;IAWnC,QAAA,OAAA,aAAW;;;YAiBlB,YAAY,UAAU,aAAa,WAAW,kBAAkB,KAAK,UAAU"}
@@ -0,0 +1,163 @@
1
+ import { Equal, Option, Schema } from "effect";
2
+ import { entityKey, readField } from "foldkit-remote";
3
+ //#region src/remote.ts
4
+ /**
5
+ * `foldkit-sync/remote`: a slice's cell edits shown through Remote, as
6
+ * request-less overlays every read draws, until the row Remote holds has them.
7
+ *
8
+ * ```text
9
+ * cell shown = Remote's row, with each edit over it the row has not reached:
10
+ * one pending, one committed after the row's revision, or one
11
+ * the journal absorbed while the cached row is still below it
12
+ * ```
13
+ *
14
+ * Remote keeps the overlays; the slice stays the only copy of the edits. An
15
+ * overlay is not stored, so after a reload `reconcile` rebuilds them from the
16
+ * slice the replica restored. What an edit absorbed and dropped from the slice
17
+ * still owes a cached row is held as an overlay, not in the application's
18
+ * Model, so every view of the row (a second list, a filter, an undo's reading
19
+ * of the page) sees what the page shows.
20
+ *
21
+ * Pure, like `foldkit-sync/entity`: it reads and writes the Model it is given
22
+ * and sends nothing.
23
+ */
24
+ /** An edit as the store holds it: the encoded id, member and value. */
25
+ const Encoded = Schema.Struct({
26
+ id: Schema.Union([Schema.String, Schema.Number]),
27
+ member: Schema.String,
28
+ value: Schema.Unknown
29
+ });
30
+ const make = (data, edits) => {
31
+ const entity = edits.entity.name;
32
+ const encode = Schema.encodeSync(edits.Edit);
33
+ const decode = Schema.decodeUnknownOption(edits.Edit);
34
+ const decodeEncoded = Schema.decodeUnknownSync(Encoded);
35
+ const SHOWN = `sync-edit:${entity}:`;
36
+ const HELD = `sync-held:${entity}:`;
37
+ const keyOf = (edit) => JSON.stringify(encode(edit));
38
+ const encodedOf = (edit) => decodeEncoded(encode(edit));
39
+ const rowKey = (edit) => entityKey(entity, String(encodedOf(edit).id));
40
+ const cellOf = (edit) => `${rowKey(edit)}\u0000${edit.member}`;
41
+ /** The edits this bridge shows, by overlay id, read back from Remote's overlays. */
42
+ const shownIn = (model) => data.overlays(model).flatMap((id) => {
43
+ const held = id.startsWith(HELD);
44
+ if (!held && !id.startsWith(SHOWN)) return [];
45
+ const key = id.slice((held ? HELD : SHOWN).length);
46
+ return Option.match(Option.flatMap(Option.liftThrowable(JSON.parse)(key), decode), {
47
+ onNone: () => [],
48
+ onSome: (edit) => [{
49
+ id,
50
+ key,
51
+ edit,
52
+ held
53
+ }]
54
+ });
55
+ });
56
+ /** The revision Remote holds for the edit's row; none when the row is not cached. */
57
+ const revisionOf = (remote, edit) => Option.filter(readField(remote.entities, rowKey(edit), "revision"), Schema.is(Schema.Number));
58
+ const reached = (remote, edit) => Option.exists(edit.at, (at) => Option.exists(revisionOf(remote, edit), (revision) => at <= revision));
59
+ let settled;
60
+ /** Whether the row Remote holds has the edit's value in its cell. */
61
+ const holdsValue = (remote, edit) => Equal.equals(readField(remote.entities, rowKey(edit), edit.member), Option.some(encodedOf(edit).value));
62
+ const patchOf = (edit) => {
63
+ const { id, member, value } = encodedOf(edit);
64
+ return {
65
+ entity,
66
+ id: String(id),
67
+ values: { [member]: value }
68
+ };
69
+ };
70
+ return {
71
+ /**
72
+ * Brings Remote's overlays in line with `slice`: each edit its row has not
73
+ * reached is shown, and each overlay whose edit has left the slice is
74
+ * lifted, unless its row is cached below it, in which case it is held
75
+ * until a read of the row reaches it. This replica's edit the row has
76
+ * reached with its own value is kept, landed, until the slice drops it, so
77
+ * a later edit reaching the row live is seen replacing it. Call it from
78
+ * `Sync.mount`'s `afterUpdate`: a read can land in any Remote Message, and
79
+ * a replayed durable Message may not touch Remote. With nothing to change,
80
+ * the Model comes back as it was given.
81
+ *
82
+ * Only a committed edit can be held: its `at` says which revision has it.
83
+ * One that leaves the slice straight from pending, committed and absorbed
84
+ * before this page installed the commit, is lifted, and its row shows the
85
+ * cached read until it is read again.
86
+ */
87
+ reconcile: (model, slice, replica) => {
88
+ const remote = data.store.get(model);
89
+ if (settled !== void 0 && settled.entities === remote.entities && settled.optimistic === remote.optimistic && settled.slice === slice && settled.replica === replica) return {
90
+ model,
91
+ held: [],
92
+ replaced: []
93
+ };
94
+ const inSlice = new Map(slice.map((edit) => [keyOf(edit), edit]));
95
+ const edited = new Set(slice.map(cellOf));
96
+ const held = [];
97
+ const replaced = [];
98
+ let next = model;
99
+ for (const shown of shownIn(model)) {
100
+ const reachedNow = reached(remote, shown.edit);
101
+ const differs = reachedNow && !holdsValue(remote, shown.edit);
102
+ if (!shown.held && inSlice.has(shown.key)) {
103
+ inSlice.delete(shown.key);
104
+ if (!reachedNow || !differs && edits.mine(shown.edit, replica)) continue;
105
+ next = data.lift(next, shown.id);
106
+ if (differs && edits.mine(shown.edit, replica)) replaced.push(shown.edit);
107
+ continue;
108
+ }
109
+ const holds = !edited.has(cellOf(shown.edit)) && Option.isSome(shown.edit.at) && Option.isSome(revisionOf(remote, shown.edit)) && !reached(remote, shown.edit);
110
+ if (holds && shown.held) continue;
111
+ next = data.lift(next, shown.id);
112
+ if (holds) {
113
+ next = data.overlay(next, `${HELD}${shown.key}`, [patchOf(shown.edit)]);
114
+ held.push(shown.edit);
115
+ } else if (differs && edits.mine(shown.edit, replica)) replaced.push(shown.edit);
116
+ }
117
+ for (const [key, edit] of inSlice) if (!reached(remote, edit)) next = data.overlay(next, `${SHOWN}${key}`, [patchOf(edit)]);
118
+ settled = {
119
+ entities: remote.entities,
120
+ optimistic: data.store.get(next).optimistic,
121
+ slice,
122
+ replica
123
+ };
124
+ return {
125
+ model: next,
126
+ held,
127
+ replaced
128
+ };
129
+ },
130
+ /**
131
+ * The edits the overlays show over their rows now, in the order they are
132
+ * drawn: for a cell's mark. One the row has reached, kept only to see a
133
+ * later edit replace it, is not among them.
134
+ */
135
+ shown: (model) => {
136
+ const remote = data.store.get(model);
137
+ return shownIn(model).flatMap(({ edit, held }) => reached(remote, edit) ? [] : [{
138
+ edit,
139
+ held
140
+ }]);
141
+ },
142
+ /**
143
+ * Lifts every overlay this bridge shows: for a server reset, after which
144
+ * the rows' revisions count a history that is gone.
145
+ */
146
+ clear: (model) => shownIn(model).reduce((next, shown) => data.lift(next, shown.id), model)
147
+ };
148
+ };
149
+ const RemoteEdits = {
150
+ /**
151
+ * The bridge between a slice of `EditableEntity` edits and a Remote domain
152
+ * that reads the same Entity, with its `revision`.
153
+ *
154
+ * ```ts
155
+ * const Shown = RemoteEdits.make(Data, ProductEdits)
156
+ * const { model: next } = Shown.reconcile(model, model.edits, model.replica)
157
+ * ```
158
+ */
159
+ make };
160
+ //#endregion
161
+ export { RemoteEdits };
162
+
163
+ //# sourceMappingURL=remote.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remote.mjs","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,MAA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