foldkit-sync 0.7.0 → 0.9.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,231 @@
1
+ import { Option, Schema } from "effect";
2
+ //#region src/entity.d.ts
3
+ /** Who committed an edit: the actor, shown in words, and the replica (a tab), which says whose it is. */
4
+ export declare const Author: Schema.Struct<{
5
+ readonly actor: Schema.String;
6
+ readonly replica: Schema.String;
7
+ }>;
8
+ export type Author = typeof Author.Type;
9
+ /** What `make` reads of an Entity: each member's schema, `id` among them. */
10
+ export interface EntityFields {
11
+ readonly fields: {
12
+ readonly id: {
13
+ readonly schema: Schema.Top;
14
+ };
15
+ } & {
16
+ readonly [member: string]: {
17
+ readonly schema: Schema.Top;
18
+ };
19
+ };
20
+ }
21
+ /** The members an edit can change: every field but `id`. */
22
+ export type Member<E extends EntityFields> = Exclude<keyof E['fields'] & string, 'id'>;
23
+ type ValueOf<E extends EntityFields, K extends Member<E>> = E['fields'][K]['schema']['Type'];
24
+ type IdOf<E extends EntityFields> = E['fields']['id']['schema']['Type'];
25
+ /** One cell's new value: the member and a value of that member's type, together. */
26
+ export type Change<E extends EntityFields, Ms extends Member<E>> = { readonly [K in Ms]: {
27
+ readonly id: IdOf<E>;
28
+ readonly member: K;
29
+ readonly value: ValueOf<E, K>;
30
+ }; }[Ms];
31
+ /** A cell's edit as the slice keeps it: committed `at` by `by`, or, with neither, pending. */
32
+ export type CellEdit<E extends EntityFields, Ms extends Member<E>> = { readonly [K in Ms]: {
33
+ readonly id: IdOf<E>;
34
+ readonly member: K;
35
+ readonly value: ValueOf<E, K>;
36
+ readonly at: Option.Option<number>;
37
+ readonly by: Option.Option<Author>;
38
+ }; }[Ms];
39
+ /** A row an overlay draws on: its id, the revision the table read it at, and its members. */
40
+ export type RowOf<E extends EntityFields, Ms extends Member<E>> = {
41
+ readonly id: IdOf<E>;
42
+ readonly revision: number;
43
+ } & { readonly [K in Ms]: ValueOf<E, K>; };
44
+ /** A cell this replica last wrote that a later edit replaced: its edit, and what the cell holds now. */
45
+ export interface Replaced<Edit> {
46
+ readonly edit: Edit;
47
+ /** The later edit's author, when the slice still says; none once the journal absorbed it. */
48
+ readonly by: Option.Option<Author>;
49
+ }
50
+ declare const make: <E extends EntityFields, const Ms extends ReadonlyArray<Member<E>>>(entity: E, options: {
51
+ readonly members: Ms;
52
+ }) => {
53
+ members: Ms;
54
+ /** The members, as a schema: what names a cell. */
55
+ Member: Schema.Literals<Ms>;
56
+ /** One cell's change, on the wire: its member's value, by that member's schema. */
57
+ Change: Schema.Codec<Change<E, Ms[number]>, unknown, never, never>;
58
+ /** One cell's edit as the slice keeps it; `null` on the wire where it has no `at` or `by`. */
59
+ Edit: Schema.Codec<CellEdit<E, Ms[number]>, unknown, never, never>;
60
+ /** The durable Message that edits cells: its fields, for the application's own tag. */
61
+ edited: {
62
+ changes: Schema.$Array<Schema.Codec<Change<E, Ms[number]>, unknown, never, never>>;
63
+ /** The journal's: absent on what a replica sends, stamped at commit. */
64
+ at: Schema.optionalKey<Schema.Number>;
65
+ by: Schema.optionalKey<Schema.Struct<{
66
+ readonly actor: Schema.String;
67
+ readonly replica: Schema.String;
68
+ }>>;
69
+ };
70
+ /** The server's durable Message: the table holds every edit committed through `through`. */
71
+ absorbed: {
72
+ through: Schema.Number;
73
+ };
74
+ /** What the stamp writes into an edit: the sequence and the author. */
75
+ stamped: (commit: {
76
+ readonly sequence: number;
77
+ readonly actorId: string;
78
+ readonly replicaId: string;
79
+ }) => {
80
+ at: number;
81
+ by: {
82
+ actor: string;
83
+ replica: string;
84
+ };
85
+ };
86
+ /** The edits with `changes` laid over them, each cell's latest kept. */
87
+ merge: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, changes: ReadonlyArray<Change<E, Ms[number]>>, at: Option.Option<number>, by: Option.Option<Author>) => ReadonlyArray<CellEdit<E, Ms[number]>>;
88
+ /** The edits less those committed through `through`; the same array when none was. */
89
+ absorb: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, through: number) => ReadonlyArray<CellEdit<E, Ms[number]>>;
90
+ /** Whether any edit is committed through `through`: whether an absorb would drop one. */
91
+ holdsThrough: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, through: number) => boolean;
92
+ /** Whether the row, read at `revision`, does not hold the edit yet: shown over it. */
93
+ shows: (edit: CellEdit<E, Ms[number]>, revision: number) => boolean;
94
+ /**
95
+ * The rows as shown: each with the edits it has not absorbed laid over it.
96
+ * Built once per `edits`, for `RowModel.map`, which caches by its identity.
97
+ */
98
+ overlay: <R extends RowOf<E, Ms[number]>>(edits: ReadonlyArray<CellEdit<E, Ms[number]>>) => ((row: R) => R);
99
+ /**
100
+ * Of `edits`, the committed ones that `next` no longer holds, whose row is
101
+ * cached below them: what the row would otherwise show stale. A row not
102
+ * cached keeps nothing; its next read has the table's value.
103
+ */
104
+ held: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, next: ReadonlyArray<CellEdit<E, Ms[number]>>, revisionOf: (id: IdOf<E>) => Option.Option<number>) => ReadonlyArray<CellEdit<E, Ms[number]>>;
105
+ /** Whether `after` holds a committed cell `before` did not: one just taken from the slice. */
106
+ newlyHeld: (before: ReadonlyArray<CellEdit<E, Ms[number]>>, after: ReadonlyArray<CellEdit<E, Ms[number]>>) => boolean;
107
+ /**
108
+ * The held edits a read of their rows has reached, let go; of those,
109
+ * `replica`'s own that the row shows another value for, as replaced: the
110
+ * table applies in order, so a row at or past an edit that holds another
111
+ * value took a later one, whose author the journal has absorbed. With
112
+ * nothing reached, `held` comes back as it was given.
113
+ */
114
+ settled: (held: ReadonlyArray<CellEdit<E, Ms[number]>>, rowOf: (id: IdOf<E>) => Option.Option<RowOf<E, Ms[number]>>, replica: string) => {
115
+ readonly held: ReadonlyArray<CellEdit<E, Ms[number]>>;
116
+ readonly replaced: ReadonlyArray<Replaced<CellEdit<E, Ms[number]>>>;
117
+ };
118
+ /**
119
+ * What the slice replaced that `replica` had written: each cell whose
120
+ * edit was its own, and now holds another replica's commit with another
121
+ * value, though it be the same person's in another tab.
122
+ */
123
+ replaced: (previous: ReadonlyArray<CellEdit<E, Ms[number]>>, next: ReadonlyArray<CellEdit<E, Ms[number]>>, replica: string) => ReadonlyArray<Replaced<CellEdit<E, Ms[number]>>>;
124
+ /** The cells a set of changes names: what a refusal of them undid. */
125
+ cellsOf: (changes: ReadonlyArray<Change<E, Ms[number]>>) => ReadonlyArray<{
126
+ readonly id: IdOf<E>;
127
+ readonly member: Ms[number];
128
+ }>;
129
+ /**
130
+ * What a row holds in one member, as the change that would set it: the
131
+ * value an undo puts back.
132
+ */
133
+ changeAt: (row: RowOf<E, Ms[number]>, member: Ms[number]) => Change<E, Ms[number]>;
134
+ /** The edit of one cell, if the edits hold one. */
135
+ editOf: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, row: IdOf<E>, member: Ms[number]) => Option.Option<CellEdit<E, Ms[number]>>;
136
+ mine: (edit: CellEdit<E, Ms[number]>, replica: string) => boolean;
137
+ };
138
+ /** Edits to an Entity's members, kept per cell in a replicated slice and laid over its rows. */
139
+ export declare const EditableEntity: {
140
+ make: <E extends EntityFields, const Ms extends ReadonlyArray<Member<E>>>(entity: E, options: {
141
+ readonly members: Ms;
142
+ }) => {
143
+ members: Ms;
144
+ /** The members, as a schema: what names a cell. */
145
+ Member: Schema.Literals<Ms>;
146
+ /** One cell's change, on the wire: its member's value, by that member's schema. */
147
+ Change: Schema.Codec<Change<E, Ms[number]>, unknown, never, never>;
148
+ /** One cell's edit as the slice keeps it; `null` on the wire where it has no `at` or `by`. */
149
+ Edit: Schema.Codec<CellEdit<E, Ms[number]>, unknown, never, never>;
150
+ /** The durable Message that edits cells: its fields, for the application's own tag. */
151
+ edited: {
152
+ changes: Schema.$Array<Schema.Codec<Change<E, Ms[number]>, unknown, never, never>>;
153
+ /** The journal's: absent on what a replica sends, stamped at commit. */
154
+ at: Schema.optionalKey<Schema.Number>;
155
+ by: Schema.optionalKey<Schema.Struct<{
156
+ readonly actor: Schema.String;
157
+ readonly replica: Schema.String;
158
+ }>>;
159
+ };
160
+ /** The server's durable Message: the table holds every edit committed through `through`. */
161
+ absorbed: {
162
+ through: Schema.Number;
163
+ };
164
+ /** What the stamp writes into an edit: the sequence and the author. */
165
+ stamped: (commit: {
166
+ readonly sequence: number;
167
+ readonly actorId: string;
168
+ readonly replicaId: string;
169
+ }) => {
170
+ at: number;
171
+ by: {
172
+ actor: string;
173
+ replica: string;
174
+ };
175
+ };
176
+ /** The edits with `changes` laid over them, each cell's latest kept. */
177
+ merge: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, changes: ReadonlyArray<Change<E, Ms[number]>>, at: Option.Option<number>, by: Option.Option<Author>) => ReadonlyArray<CellEdit<E, Ms[number]>>;
178
+ /** The edits less those committed through `through`; the same array when none was. */
179
+ absorb: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, through: number) => ReadonlyArray<CellEdit<E, Ms[number]>>;
180
+ /** Whether any edit is committed through `through`: whether an absorb would drop one. */
181
+ holdsThrough: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, through: number) => boolean;
182
+ /** Whether the row, read at `revision`, does not hold the edit yet: shown over it. */
183
+ shows: (edit: CellEdit<E, Ms[number]>, revision: number) => boolean;
184
+ /**
185
+ * The rows as shown: each with the edits it has not absorbed laid over it.
186
+ * Built once per `edits`, for `RowModel.map`, which caches by its identity.
187
+ */
188
+ overlay: <R extends RowOf<E, Ms[number]>>(edits: ReadonlyArray<CellEdit<E, Ms[number]>>) => ((row: R) => R);
189
+ /**
190
+ * Of `edits`, the committed ones that `next` no longer holds, whose row is
191
+ * cached below them: what the row would otherwise show stale. A row not
192
+ * cached keeps nothing; its next read has the table's value.
193
+ */
194
+ held: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, next: ReadonlyArray<CellEdit<E, Ms[number]>>, revisionOf: (id: IdOf<E>) => Option.Option<number>) => ReadonlyArray<CellEdit<E, Ms[number]>>;
195
+ /** Whether `after` holds a committed cell `before` did not: one just taken from the slice. */
196
+ newlyHeld: (before: ReadonlyArray<CellEdit<E, Ms[number]>>, after: ReadonlyArray<CellEdit<E, Ms[number]>>) => boolean;
197
+ /**
198
+ * The held edits a read of their rows has reached, let go; of those,
199
+ * `replica`'s own that the row shows another value for, as replaced: the
200
+ * table applies in order, so a row at or past an edit that holds another
201
+ * value took a later one, whose author the journal has absorbed. With
202
+ * nothing reached, `held` comes back as it was given.
203
+ */
204
+ settled: (held: ReadonlyArray<CellEdit<E, Ms[number]>>, rowOf: (id: IdOf<E>) => Option.Option<RowOf<E, Ms[number]>>, replica: string) => {
205
+ readonly held: ReadonlyArray<CellEdit<E, Ms[number]>>;
206
+ readonly replaced: ReadonlyArray<Replaced<CellEdit<E, Ms[number]>>>;
207
+ };
208
+ /**
209
+ * What the slice replaced that `replica` had written: each cell whose
210
+ * edit was its own, and now holds another replica's commit with another
211
+ * value, though it be the same person's in another tab.
212
+ */
213
+ replaced: (previous: ReadonlyArray<CellEdit<E, Ms[number]>>, next: ReadonlyArray<CellEdit<E, Ms[number]>>, replica: string) => ReadonlyArray<Replaced<CellEdit<E, Ms[number]>>>;
214
+ /** The cells a set of changes names: what a refusal of them undid. */
215
+ cellsOf: (changes: ReadonlyArray<Change<E, Ms[number]>>) => ReadonlyArray<{
216
+ readonly id: IdOf<E>;
217
+ readonly member: Ms[number];
218
+ }>;
219
+ /**
220
+ * What a row holds in one member, as the change that would set it: the
221
+ * value an undo puts back.
222
+ */
223
+ changeAt: (row: RowOf<E, Ms[number]>, member: Ms[number]) => Change<E, Ms[number]>;
224
+ /** The edit of one cell, if the edits hold one. */
225
+ editOf: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, row: IdOf<E>, member: Ms[number]) => Option.Option<CellEdit<E, Ms[number]>>;
226
+ mine: (edit: CellEdit<E, Ms[number]>, replica: string) => boolean;
227
+ };
228
+ };
229
+ export type EditableEntity<E extends EntityFields, Ms extends ReadonlyArray<Member<E>>> = ReturnType<typ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M;;IAIX,QAAA,MAAA,SAAA,GAAA,aAAA;;;;;IAMpB,UAAA,UAAC,MAAA,GAAA,aAAA,OAAqB,cAAa,SAAA,GAAA,mBAAW,KAAK,MAAM;;;;;;IAmB1D,OAAA,OAAA,cAAa,SAAA,GAAA,cAAM,MACpB,cAAa,SAAA,GAAA,cAAM,aACZ,IAAI,KAAK,OAAO,OAAO,mBACnC,cAAa,SAAA,GAAA;;IAWI,YAAA,QAAA,cAAa,SAAA,GAAA,cAAM,OAAS,cAAa,SAAA,GAAA;;;;;;;;IAarD,UAAA,MAAA,cAAa,SAAA,GAAA,cAAM,QACjB,IAAI,KAAK,OAAO,OAAO,OAAM,MAAA,GAAA,cAAK;eAE9B,MAAM,cAAa,SAAA,GAAA;eAAiB,UAAU,cAAc,SAAQ,SAAA,GAAA;;;;;;;IAuBtE,WAAA,UAAA,cAAa,SAAA,GAAA,cAAM,MACvB,cAAa,SAAA,GAAA,cAAM,oBAExB,cAAc,SAAQ,SAAA,GAAA;;IAiBd,UAAA,SAAA,cAAc,OAAO,GAAC,iBAC9B;eAAyB,IAAI,KAAK;eAAa,QAAM;;;;;;IAOvB,WAAA,KAAA,MAAA,GAAA,aAAA,QAAA,eAAA,OAAO,GAAC;;IAKzB,SAAA,OAAA,cAAa,SAAA,GAAA,cAAM,KAAO,KAAK,IAAE,QAAA,eAAc,OAAO,OAAM,SAAA,GAAA;IApK3C,OAAA,MAAA,SAAA,GAAA,aAAA;;;YA+KzB,eACV,UAAU,cACV,WAAW,cAAc,OAAO,OAC9B,kBAAkB,KAAK,GAAG"}
@@ -0,0 +1,204 @@
1
+ import { Equal, Option, Schema } from "effect";
2
+ //#region src/entity.ts
3
+ /**
4
+ * `foldkit-sync/entity`: edits to rows a server owns, kept in a replicated
5
+ * slice that the journal owns, and laid over the rows Remote reads.
6
+ *
7
+ * ```text
8
+ * cell shown = the row's value, unless an edit of that cell is pending,
9
+ * or committed after the row's revision (at > row.revision)
10
+ * ```
11
+ *
12
+ * The journal orders the edits and stamps each with the sequence it
13
+ * committed at (`at`) and who committed it from where (`by`); the table is
14
+ * the journal's read model, each row carrying the highest sequence it has
15
+ * applied (`revision`). An edit the table holds is absorbed: the server says
16
+ * so (`absorbed`), every replica drops it, and the journal compacts. A page
17
+ * keeps a dropped edit (`held`) while a row it has cached is below it, so the
18
+ * row never shows the stale read, and lets it go once a read of the row is at
19
+ * or past it (`settled`), noting when the row then holds another value, a
20
+ * later edit's.
21
+ *
22
+ * Everything here is pure: it owns no state, does no I/O, and sends nothing.
23
+ * The application keeps the edits in its Model and calls these from
24
+ * `update`, the stamp, and `Sync.mount`'s `onReinstall`.
25
+ */
26
+ /** Who committed an edit: the actor, shown in words, and the replica (a tab), which says whose it is. */
27
+ const Author = Schema.Struct({
28
+ actor: Schema.String,
29
+ replica: Schema.String
30
+ });
31
+ const make = (entity, options) => {
32
+ const { members } = options;
33
+ const id = entity.fields.id.schema;
34
+ if (members.length === 0) throw new Error("EditableEntity.make: name at least one member");
35
+ for (const member of members) if (member === "id" || !Object.hasOwn(entity.fields, member)) throw new Error(`EditableEntity.make: ${member} is not a member of the entity`);
36
+ const schemaOf = (member) => entity.fields[member].schema;
37
+ const Change = Schema.Union(members.map((member) => Schema.Struct({
38
+ id,
39
+ member: Schema.Literal(member),
40
+ value: schemaOf(member)
41
+ })));
42
+ const Edit = Schema.Union(members.map((member) => Schema.Struct({
43
+ id,
44
+ member: Schema.Literal(member),
45
+ value: schemaOf(member),
46
+ at: Schema.OptionFromNullOr(Schema.Number),
47
+ by: Schema.OptionFromNullOr(Author)
48
+ })));
49
+ /** The edits by row, then member: no key is a string built from an id. */
50
+ const indexOf = (edits) => {
51
+ const index = /* @__PURE__ */ new Map();
52
+ for (const edit of edits) {
53
+ const row = index.get(edit.id) ?? /* @__PURE__ */ new Map();
54
+ row.set(edit.member, edit);
55
+ index.set(edit.id, row);
56
+ }
57
+ return index;
58
+ };
59
+ const sameCell = (a, b) => Equal.equals(a.id, b.id) && a.member === b.member;
60
+ /** Whether a row read at `revision` has the edit: the table applies in the journal's order. */
61
+ const reached = (edit, revision) => Option.exists(edit.at, (at) => at <= revision);
62
+ /** Whether `replica` wrote the edit: pending here, or committed from it. */
63
+ const mine = (edit, replica) => Option.match(edit.at, {
64
+ onNone: () => true,
65
+ onSome: () => Option.exists(edit.by, (by) => by.replica === replica)
66
+ });
67
+ return {
68
+ members,
69
+ /** The members, as a schema: what names a cell. */
70
+ Member: Schema.Literals(members),
71
+ /** One cell's change, on the wire: its member's value, by that member's schema. */
72
+ Change,
73
+ /** One cell's edit as the slice keeps it; `null` on the wire where it has no `at` or `by`. */
74
+ Edit,
75
+ /** The durable Message that edits cells: its fields, for the application's own tag. */
76
+ edited: {
77
+ changes: Schema.Array(Change),
78
+ /** The journal's: absent on what a replica sends, stamped at commit. */
79
+ at: Schema.optionalKey(Schema.Number),
80
+ by: Schema.optionalKey(Author)
81
+ },
82
+ /** The server's durable Message: the table holds every edit committed through `through`. */
83
+ absorbed: { through: Schema.Number },
84
+ /** What the stamp writes into an edit: the sequence and the author. */
85
+ stamped: (commit) => ({
86
+ at: commit.sequence,
87
+ by: {
88
+ actor: commit.actorId,
89
+ replica: commit.replicaId
90
+ }
91
+ }),
92
+ /** The edits with `changes` laid over them, each cell's latest kept. */
93
+ merge: (edits, changes, at, by) => {
94
+ if (changes.length === 0) return edits;
95
+ const added = changes.map((change) => ({
96
+ ...change,
97
+ at,
98
+ by
99
+ }));
100
+ return [...edits.filter((edit) => !added.some((next) => sameCell(edit, next))), ...added];
101
+ },
102
+ /** The edits less those committed through `through`; the same array when none was. */
103
+ absorb: (edits, through) => edits.some((edit) => reached(edit, through)) ? edits.filter((edit) => !reached(edit, through)) : edits,
104
+ /** Whether any edit is committed through `through`: whether an absorb would drop one. */
105
+ holdsThrough: (edits, through) => edits.some((edit) => reached(edit, through)),
106
+ /** Whether the row, read at `revision`, does not hold the edit yet: shown over it. */
107
+ shows: (edit, revision) => !reached(edit, revision),
108
+ /**
109
+ * The rows as shown: each with the edits it has not absorbed laid over it.
110
+ * Built once per `edits`, for `RowModel.map`, which caches by its identity.
111
+ */
112
+ overlay: (edits) => {
113
+ const index = indexOf(edits);
114
+ return (row) => {
115
+ const cells = index.get(row.id);
116
+ if (cells === void 0) return row;
117
+ let shown = row;
118
+ for (const edit of cells.values()) if (!reached(edit, row.revision)) shown = {
119
+ ...shown,
120
+ [edit.member]: edit.value
121
+ };
122
+ return shown;
123
+ };
124
+ },
125
+ /**
126
+ * Of `edits`, the committed ones that `next` no longer holds, whose row is
127
+ * cached below them: what the row would otherwise show stale. A row not
128
+ * cached keeps nothing; its next read has the table's value.
129
+ */
130
+ held: (edits, next, revisionOf) => {
131
+ const current = indexOf(next);
132
+ return edits.filter((edit) => Option.isSome(edit.at) && !current.get(edit.id)?.has(edit.member) && Option.exists(revisionOf(edit.id), (revision) => !reached(edit, revision)));
133
+ },
134
+ /** Whether `after` holds a committed cell `before` did not: one just taken from the slice. */
135
+ newlyHeld: (before, after) => after.some((edit) => !before.some((kept) => sameCell(kept, edit) && Equal.equals(kept.at, edit.at))),
136
+ /**
137
+ * The held edits a read of their rows has reached, let go; of those,
138
+ * `replica`'s own that the row shows another value for, as replaced: the
139
+ * table applies in order, so a row at or past an edit that holds another
140
+ * value took a later one, whose author the journal has absorbed. With
141
+ * nothing reached, `held` comes back as it was given.
142
+ */
143
+ settled: (held, rowOf, replica) => {
144
+ const replaced = [];
145
+ const kept = held.filter((edit) => Option.match(rowOf(edit.id), {
146
+ onNone: () => true,
147
+ onSome: (row) => {
148
+ if (!reached(edit, row.revision)) return true;
149
+ if (mine(edit, replica) && !Equal.equals(row[edit.member], edit.value)) replaced.push({
150
+ edit,
151
+ by: Option.none()
152
+ });
153
+ return false;
154
+ }
155
+ }));
156
+ return {
157
+ held: kept.length === held.length ? held : kept,
158
+ replaced
159
+ };
160
+ },
161
+ /**
162
+ * What the slice replaced that `replica` had written: each cell whose
163
+ * edit was its own, and now holds another replica's commit with another
164
+ * value, though it be the same person's in another tab.
165
+ */
166
+ replaced: (previous, next, replica) => {
167
+ const before = indexOf(previous);
168
+ return next.flatMap((edit) => {
169
+ const prior = before.get(edit.id)?.get(edit.member);
170
+ if (prior === void 0 || !mine(prior, replica)) return [];
171
+ return Option.match(edit.by, {
172
+ onNone: () => [],
173
+ onSome: (by) => by.replica !== replica && !Equal.equals(edit.value, prior.value) ? [{
174
+ edit: prior,
175
+ by: Option.some(by)
176
+ }] : []
177
+ });
178
+ });
179
+ },
180
+ /** The cells a set of changes names: what a refusal of them undid. */
181
+ cellsOf: (changes) => changes.map(({ id: row, member }) => ({
182
+ id: row,
183
+ member
184
+ })),
185
+ /**
186
+ * What a row holds in one member, as the change that would set it: the
187
+ * value an undo puts back.
188
+ */
189
+ changeAt: (row, member) => ({
190
+ id: row.id,
191
+ member,
192
+ value: row[member]
193
+ }),
194
+ /** The edit of one cell, if the edits hold one. */
195
+ editOf: (edits, row, member) => Option.fromUndefinedOr(edits.find((edit) => Equal.equals(edit.id, row) && edit.member === member)),
196
+ mine
197
+ };
198
+ };
199
+ /** Edits to an Entity's members, kept per cell in a replicated slice and laid over its rows. */
200
+ const EditableEntity = { make };
201
+ //#endregion
202
+ export { Author, EditableEntity };
203
+
204
+ //# sourceMappingURL=entity.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity.mjs","names":[],"sources":["../src/entity.ts"],"sourcesContent":["/**\n * `foldkit-sync/entity`: edits to rows a server owns, kept in a replicated\n * slice that the journal owns, and laid over the rows Remote reads.\n *\n * ```text\n * cell shown = the row's value, unless an edit of that cell is pending,\n * or committed after the row's revision (at > row.revision)\n * ```\n *\n * The journal orders the edits and stamps each with the sequence it\n * committed at (`at`) and who committed it from where (`by`); the table is\n * the journal's read model, each row carrying the highest sequence it has\n * applied (`revision`). An edit the table holds is absorbed: the server says\n * so (`absorbed`), every replica drops it, and the journal compacts. A page\n * keeps a dropped edit (`held`) while a row it has cached is below it, so the\n * row never shows the stale read, and lets it go once a read of the row is at\n * or past it (`settled`), noting when the row then holds another value, a\n * later edit's.\n *\n * Everything here is pure: it owns no state, does no I/O, and sends nothing.\n * The application keeps the edits in its Model and calls these from\n * `update`, the stamp, and `Sync.mount`'s `onReinstall`.\n */\nimport { Equal, Option, Schema } from 'effect'\n\n/** Who committed an edit: the actor, shown in words, and the replica (a tab), which says whose it is. */\nexport const Author = Schema.Struct({ actor: Schema.String, replica: Schema.String })\nexport type Author = typeof Author.Type\n\n/** What `make` reads of an Entity: each member's schema, `id` among them. */\nexport interface EntityFields {\n readonly fields: { readonly id: { readonly schema: Schema.Top } } & {\n readonly [member: string]: { readonly schema: Schema.Top }\n }\n}\n\n/** The members an edit can change: every field but `id`. */\nexport type Member<E extends EntityFields> = Exclude<keyof E['fields'] & string, 'id'>\ntype ValueOf<E extends EntityFields, K extends Member<E>> = E['fields'][K]['schema']['Type']\ntype IdOf<E extends EntityFields> = E['fields']['id']['schema']['Type']\n\n/** One cell's new value: the member and a value of that member's type, together. */\nexport type Change<E extends EntityFields, Ms extends Member<E>> = {\n readonly [K in Ms]: { readonly id: IdOf<E>; readonly member: K; readonly value: ValueOf<E, K> }\n}[Ms]\n\n/** A cell's edit as the slice keeps it: committed `at` by `by`, or, with neither, pending. */\nexport type CellEdit<E extends EntityFields, Ms extends Member<E>> = {\n readonly [K in Ms]: {\n readonly id: IdOf<E>\n readonly member: K\n readonly value: ValueOf<E, K>\n readonly at: Option.Option<number>\n readonly by: Option.Option<Author>\n }\n}[Ms]\n\n/** A row an overlay draws on: its id, the revision the table read it at, and its members. */\nexport type RowOf<E extends EntityFields, Ms extends Member<E>> = {\n readonly id: IdOf<E>\n readonly revision: number\n} & { readonly [K in Ms]: ValueOf<E, K> }\n\n/** A cell this replica last wrote that a later edit replaced: its edit, and what the cell holds now. */\nexport interface Replaced<Edit> {\n readonly edit: Edit\n /** The later edit's author, when the slice still says; none once the journal absorbed it. */\n readonly by: Option.Option<Author>\n}\n\nconst make = <E extends EntityFields, const Ms extends ReadonlyArray<Member<E>>>(\n entity: E,\n options: { readonly members: Ms },\n) => {\n type M = Ms[number]\n type Edit = CellEdit<E, M>\n type Row = RowOf<E, M>\n const { members } = options\n const id = entity.fields.id.schema\n if (members.length === 0) throw new Error('EditableEntity.make: name at least one member')\n for (const member of members) {\n if ((member as string) === 'id' || !Object.hasOwn(entity.fields, member)) {\n throw new Error(`EditableEntity.make: ${member} is not a member of the entity`)\n }\n }\n const schemaOf = (member: M) => entity.fields[member]!.schema\n // One struct per member, so a value is decoded by its own member's schema:\n // the union is exactly `Change`, member and value paired.\n const Change = Schema.Union(\n members.map(member =>\n Schema.Struct({ id, member: Schema.Literal(member), value: schemaOf(member) }),\n ),\n ) as unknown as Schema.Codec<Change<E, M>, unknown>\n const Edit = Schema.Union(\n members.map(member =>\n Schema.Struct({\n id,\n member: Schema.Literal(member),\n value: schemaOf(member),\n at: Schema.OptionFromNullOr(Schema.Number),\n by: Schema.OptionFromNullOr(Author),\n }),\n ),\n ) as unknown as Schema.Codec<Edit, unknown>\n\n /** The edits by row, then member: no key is a string built from an id. */\n const indexOf = (edits: ReadonlyArray<Edit>) => {\n const index = new Map<IdOf<E>, Map<M, Edit>>()\n for (const edit of edits) {\n const row = index.get(edit.id) ?? new Map<M, Edit>()\n row.set(edit.member, edit)\n index.set(edit.id, row)\n }\n return index\n }\n const sameCell = (a: { readonly id: IdOf<E>; readonly member: M }, b: typeof a) =>\n Equal.equals(a.id, b.id) && a.member === b.member\n\n /** Whether a row read at `revision` has the edit: the table applies in the journal's order. */\n const reached = (edit: Edit, revision: number) => Option.exists(edit.at, at => at <= revision)\n\n /** Whether `replica` wrote the edit: pending here, or committed from it. */\n const mine = (edit: Edit, replica: string): boolean =>\n Option.match(edit.at, {\n onNone: () => true,\n onSome: () => Option.exists(edit.by, by => by.replica === replica),\n })\n\n return {\n members,\n /** The members, as a schema: what names a cell. */\n Member: Schema.Literals(members),\n /** One cell's change, on the wire: its member's value, by that member's schema. */\n Change,\n /** One cell's edit as the slice keeps it; `null` on the wire where it has no `at` or `by`. */\n Edit,\n /** The durable Message that edits cells: its fields, for the application's own tag. */\n edited: {\n changes: Schema.Array(Change),\n /** The journal's: absent on what a replica sends, stamped at commit. */\n at: Schema.optionalKey(Schema.Number),\n by: Schema.optionalKey(Author),\n },\n /** The server's durable Message: the table holds every edit committed through `through`. */\n absorbed: { through: Schema.Number },\n /** What the stamp writes into an edit: the sequence and the author. */\n stamped: (commit: {\n readonly sequence: number\n readonly actorId: string\n readonly replicaId: string\n }) => ({ at: commit.sequence, by: { actor: commit.actorId, replica: commit.replicaId } }),\n\n /** The edits with `changes` laid over them, each cell's latest kept. */\n merge: (\n edits: ReadonlyArray<Edit>,\n changes: ReadonlyArray<Change<E, M>>,\n at: Option.Option<number>,\n by: Option.Option<Author>,\n ): ReadonlyArray<Edit> => {\n if (changes.length === 0) return edits\n const added = changes.map(change => ({ ...change, at, by }) as Edit)\n return [...edits.filter(edit => !added.some(next => sameCell(edit, next))), ...added]\n },\n\n /** The edits less those committed through `through`; the same array when none was. */\n absorb: (edits: ReadonlyArray<Edit>, through: number): ReadonlyArray<Edit> =>\n edits.some(edit => reached(edit, through))\n ? edits.filter(edit => !reached(edit, through))\n : edits,\n\n /** Whether any edit is committed through `through`: whether an absorb would drop one. */\n holdsThrough: (edits: ReadonlyArray<Edit>, through: number): boolean =>\n edits.some(edit => reached(edit, through)),\n\n /** Whether the row, read at `revision`, does not hold the edit yet: shown over it. */\n shows: (edit: Edit, revision: number): boolean => !reached(edit, revision),\n\n /**\n * The rows as shown: each with the edits it has not absorbed laid over it.\n * Built once per `edits`, for `RowModel.map`, which caches by its identity.\n */\n overlay: <R extends Row>(edits: ReadonlyArray<Edit>): ((row: R) => R) => {\n const index = indexOf(edits)\n return (row: R): R => {\n const cells = index.get(row.id)\n if (cells === undefined) return row\n let shown = row\n for (const edit of cells.values()) {\n if (!reached(edit, row.revision)) shown = { ...shown, [edit.member]: edit.value }\n }\n return shown\n }\n },\n\n /**\n * Of `edits`, the committed ones that `next` no longer holds, whose row is\n * cached below them: what the row would otherwise show stale. A row not\n * cached keeps nothing; its next read has the table's value.\n */\n held: (\n edits: ReadonlyArray<Edit>,\n next: ReadonlyArray<Edit>,\n revisionOf: (id: IdOf<E>) => Option.Option<number>,\n ): ReadonlyArray<Edit> => {\n const current = indexOf(next)\n return edits.filter(\n edit =>\n Option.isSome(edit.at) &&\n !current.get(edit.id)?.has(edit.member) &&\n Option.exists(revisionOf(edit.id), revision => !reached(edit, revision)),\n )\n },\n\n /** Whether `after` holds a committed cell `before` did not: one just taken from the slice. */\n newlyHeld: (before: ReadonlyArray<Edit>, after: ReadonlyArray<Edit>): boolean =>\n after.some(\n edit => !before.some(kept => sameCell(kept, edit) && Equal.equals(kept.at, edit.at)),\n ),\n\n /**\n * The held edits a read of their rows has reached, let go; of those,\n * `replica`'s own that the row shows another value for, as replaced: the\n * table applies in order, so a row at or past an edit that holds another\n * value took a later one, whose author the journal has absorbed. With\n * nothing reached, `held` comes back as it was given.\n */\n settled: (\n held: ReadonlyArray<Edit>,\n rowOf: (id: IdOf<E>) => Option.Option<Row>,\n replica: string,\n ): { readonly held: ReadonlyArray<Edit>; readonly replaced: ReadonlyArray<Replaced<Edit>> } => {\n const replaced: Array<Replaced<Edit>> = []\n const kept = held.filter(edit =>\n Option.match(rowOf(edit.id), {\n onNone: () => true,\n onSome: row => {\n if (!reached(edit, row.revision)) return true\n if (mine(edit, replica) && !Equal.equals(row[edit.member], edit.value)) {\n replaced.push({ edit, by: Option.none() })\n }\n return false\n },\n }),\n )\n return { held: kept.length === held.length ? held : kept, replaced }\n },\n\n /**\n * What the slice replaced that `replica` had written: each cell whose\n * edit was its own, and now holds another replica's commit with another\n * value, though it be the same person's in another tab.\n */\n replaced: (\n previous: ReadonlyArray<Edit>,\n next: ReadonlyArray<Edit>,\n replica: string,\n ): ReadonlyArray<Replaced<Edit>> => {\n const before = indexOf(previous)\n return next.flatMap(edit => {\n const prior = before.get(edit.id)?.get(edit.member)\n if (prior === undefined || !mine(prior, replica)) return []\n return Option.match(edit.by, {\n onNone: () => [],\n onSome: by =>\n by.replica !== replica && !Equal.equals(edit.value, prior.value)\n ? [{ edit: prior, by: Option.some(by) }]\n : [],\n })\n })\n },\n\n /** The cells a set of changes names: what a refusal of them undid. */\n cellsOf: (\n changes: ReadonlyArray<Change<E, M>>,\n ): ReadonlyArray<{ readonly id: IdOf<E>; readonly member: M }> =>\n changes.map(({ id: row, member }) => ({ id: row, member })),\n\n /**\n * What a row holds in one member, as the change that would set it: the\n * value an undo puts back.\n */\n changeAt: (row: Row, member: M): Change<E, M> =>\n // The member and its value are read together, so they pair as `Change` does.\n ({ id: row.id, member, value: row[member] }) as Change<E, M>,\n\n /** The edit of one cell, if the edits hold one. */\n editOf: (edits: ReadonlyArray<Edit>, row: IdOf<E>, member: M): Option.Option<Edit> =>\n Option.fromUndefinedOr(\n edits.find(edit => Equal.equals(edit.id, row) && edit.member === member),\n ),\n\n mine,\n }\n}\n\n/** Edits to an Entity's members, kept per cell in a replicated slice and laid over its rows. */\nexport const EditableEntity = { make }\nexport type EditableEntity<\n E extends EntityFields,\n Ms extends ReadonlyArray<Member<E>>,\n> = ReturnType<typeof make<E, Ms>>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,MAAa,SAAS,OAAO,OAAO;CAAE,OAAO,OAAO;CAAQ,SAAS,OAAO;AAAO,CAAC;AA4CpF,MAAM,QACJ,QACA,YACG;CAIH,MAAM,EAAE,YAAY;CACpB,MAAM,KAAK,OAAO,OAAO,GAAG;CAC5B,IAAI,QAAQ,WAAW,GAAG,MAAM,IAAI,MAAM,+CAA+C;CACzF,KAAK,MAAM,UAAU,SACnB,IAAK,WAAsB,QAAQ,CAAC,OAAO,OAAO,OAAO,QAAQ,MAAM,GACrE,MAAM,IAAI,MAAM,wBAAwB,OAAO,+BAA+B;CAGlF,MAAM,YAAY,WAAc,OAAO,OAAO,OAAO,CAAE;CAGvD,MAAM,SAAS,OAAO,MACpB,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