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.
- package/README.md +238 -26
- package/dist/do.d.mts +60 -0
- package/dist/do.d.mts.map +1 -0
- package/dist/do.mjs +59 -0
- package/dist/do.mjs.map +1 -0
- package/dist/entity.d.mts +183 -0
- package/dist/entity.d.mts.map +1 -0
- package/dist/entity.mjs +143 -0
- package/dist/entity.mjs.map +1 -0
- package/dist/index.d.mts +45 -622
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +168 -340
- package/dist/index.mjs.map +1 -1
- package/dist/journal.d.mts +142 -0
- package/dist/journal.d.mts.map +1 -0
- package/dist/journal.mjs +212 -0
- package/dist/journal.mjs.map +1 -0
- package/dist/make-h5yD8STj.d.mts +714 -0
- package/dist/make-h5yD8STj.d.mts.map +1 -0
- package/dist/remote.d.mts +123 -0
- package/dist/remote.d.mts.map +1 -0
- package/dist/remote.mjs +163 -0
- package/dist/remote.mjs.map +1 -0
- package/dist/transport-DiKXy5zx.mjs +412 -0
- package/dist/transport-DiKXy5zx.mjs.map +1 -0
- package/package.json +38 -4
|
@@ -0,0 +1,183 @@
|
|
|
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 as the table holds it: its id, the revision it has applied, 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
|
+
/** Carried so `foldkit-sync/remote` can name the rows it overlays by the Entity's own name. */
|
|
54
|
+
entity: E;
|
|
55
|
+
members: Ms;
|
|
56
|
+
/** The members, as a schema: what names a cell. */
|
|
57
|
+
Member: Schema.Literals<Ms>;
|
|
58
|
+
/** One cell's change, on the wire: its member's value, by that member's schema. */
|
|
59
|
+
Change: Schema.Codec<Change<E, Ms[number]>, unknown, never, never>;
|
|
60
|
+
/** One cell's edit as the slice keeps it; `null` on the wire where it has no `at` or `by`. */
|
|
61
|
+
Edit: Schema.Codec<CellEdit<E, Ms[number]>, unknown, never, never>;
|
|
62
|
+
/** The durable Message that edits cells: its fields, for the application's own tag. */
|
|
63
|
+
edited: {
|
|
64
|
+
changes: Schema.$Array<Schema.Codec<Change<E, Ms[number]>, unknown, never, never>>;
|
|
65
|
+
/** The journal's: absent on what a replica sends, stamped at commit. */
|
|
66
|
+
at: Schema.optionalKey<Schema.Number>;
|
|
67
|
+
by: Schema.optionalKey<Schema.Struct<{
|
|
68
|
+
readonly actor: Schema.String;
|
|
69
|
+
readonly replica: Schema.String;
|
|
70
|
+
}>>;
|
|
71
|
+
};
|
|
72
|
+
/** The server's durable Message: the table holds every edit committed through `through`. */
|
|
73
|
+
absorbed: {
|
|
74
|
+
through: Schema.Number;
|
|
75
|
+
};
|
|
76
|
+
/** What the stamp writes into an edit: the sequence and the author. */
|
|
77
|
+
stamped: (commit: {
|
|
78
|
+
readonly sequence: number;
|
|
79
|
+
readonly actorId: string;
|
|
80
|
+
readonly replicaId: string;
|
|
81
|
+
}) => {
|
|
82
|
+
at: number;
|
|
83
|
+
by: {
|
|
84
|
+
actor: string;
|
|
85
|
+
replica: string;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
/** The edits with `changes` laid over them, each cell's latest kept. */
|
|
89
|
+
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]>>;
|
|
90
|
+
/** The edits less those committed through `through`; the same array when none was. */
|
|
91
|
+
absorb: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, through: number) => ReadonlyArray<CellEdit<E, Ms[number]>>;
|
|
92
|
+
/** Whether any edit is committed through `through`: whether an absorb would drop one. */
|
|
93
|
+
holdsThrough: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, through: number) => boolean;
|
|
94
|
+
/**
|
|
95
|
+
* What the slice replaced that `replica` had written: each cell whose
|
|
96
|
+
* edit was its own, and now holds another replica's commit with another
|
|
97
|
+
* value, though it be the same person's in another tab.
|
|
98
|
+
*/
|
|
99
|
+
replaced: (previous: ReadonlyArray<CellEdit<E, Ms[number]>>, next: ReadonlyArray<CellEdit<E, Ms[number]>>, replica: string) => ReadonlyArray<Replaced<CellEdit<E, Ms[number]>>>;
|
|
100
|
+
/** The cells a set of changes names: what a refusal of them undid. */
|
|
101
|
+
cellsOf: (changes: ReadonlyArray<Change<E, Ms[number]>>) => ReadonlyArray<{
|
|
102
|
+
readonly id: IdOf<E>;
|
|
103
|
+
readonly member: Ms[number];
|
|
104
|
+
}>;
|
|
105
|
+
/**
|
|
106
|
+
* What a row holds in one member, as the change that would set it: the
|
|
107
|
+
* value an undo puts back.
|
|
108
|
+
*/
|
|
109
|
+
changeAt: (row: RowOf<E, Ms[number]>, member: Ms[number]) => Change<E, Ms[number]>;
|
|
110
|
+
/** The edit of one cell, if the edits hold one. */
|
|
111
|
+
editOf: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, row: IdOf<E>, member: Ms[number]) => Option.Option<CellEdit<E, Ms[number]>>;
|
|
112
|
+
mine: (edit: CellEdit<E, Ms[number]>, replica: string) => boolean;
|
|
113
|
+
};
|
|
114
|
+
/** Edits to an Entity's members, kept per cell in a replicated slice and laid over its rows. */
|
|
115
|
+
export declare const EditableEntity: {
|
|
116
|
+
make: <E extends EntityFields, const Ms extends ReadonlyArray<Member<E>>>(entity: E, options: {
|
|
117
|
+
readonly members: Ms;
|
|
118
|
+
}) => {
|
|
119
|
+
/** Carried so `foldkit-sync/remote` can name the rows it overlays by the Entity's own name. */
|
|
120
|
+
entity: E;
|
|
121
|
+
members: Ms;
|
|
122
|
+
/** The members, as a schema: what names a cell. */
|
|
123
|
+
Member: Schema.Literals<Ms>;
|
|
124
|
+
/** One cell's change, on the wire: its member's value, by that member's schema. */
|
|
125
|
+
Change: Schema.Codec<Change<E, Ms[number]>, unknown, never, never>;
|
|
126
|
+
/** One cell's edit as the slice keeps it; `null` on the wire where it has no `at` or `by`. */
|
|
127
|
+
Edit: Schema.Codec<CellEdit<E, Ms[number]>, unknown, never, never>;
|
|
128
|
+
/** The durable Message that edits cells: its fields, for the application's own tag. */
|
|
129
|
+
edited: {
|
|
130
|
+
changes: Schema.$Array<Schema.Codec<Change<E, Ms[number]>, unknown, never, never>>;
|
|
131
|
+
/** The journal's: absent on what a replica sends, stamped at commit. */
|
|
132
|
+
at: Schema.optionalKey<Schema.Number>;
|
|
133
|
+
by: Schema.optionalKey<Schema.Struct<{
|
|
134
|
+
readonly actor: Schema.String;
|
|
135
|
+
readonly replica: Schema.String;
|
|
136
|
+
}>>;
|
|
137
|
+
};
|
|
138
|
+
/** The server's durable Message: the table holds every edit committed through `through`. */
|
|
139
|
+
absorbed: {
|
|
140
|
+
through: Schema.Number;
|
|
141
|
+
};
|
|
142
|
+
/** What the stamp writes into an edit: the sequence and the author. */
|
|
143
|
+
stamped: (commit: {
|
|
144
|
+
readonly sequence: number;
|
|
145
|
+
readonly actorId: string;
|
|
146
|
+
readonly replicaId: string;
|
|
147
|
+
}) => {
|
|
148
|
+
at: number;
|
|
149
|
+
by: {
|
|
150
|
+
actor: string;
|
|
151
|
+
replica: string;
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
/** The edits with `changes` laid over them, each cell's latest kept. */
|
|
155
|
+
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]>>;
|
|
156
|
+
/** The edits less those committed through `through`; the same array when none was. */
|
|
157
|
+
absorb: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, through: number) => ReadonlyArray<CellEdit<E, Ms[number]>>;
|
|
158
|
+
/** Whether any edit is committed through `through`: whether an absorb would drop one. */
|
|
159
|
+
holdsThrough: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, through: number) => boolean;
|
|
160
|
+
/**
|
|
161
|
+
* What the slice replaced that `replica` had written: each cell whose
|
|
162
|
+
* edit was its own, and now holds another replica's commit with another
|
|
163
|
+
* value, though it be the same person's in another tab.
|
|
164
|
+
*/
|
|
165
|
+
replaced: (previous: ReadonlyArray<CellEdit<E, Ms[number]>>, next: ReadonlyArray<CellEdit<E, Ms[number]>>, replica: string) => ReadonlyArray<Replaced<CellEdit<E, Ms[number]>>>;
|
|
166
|
+
/** The cells a set of changes names: what a refusal of them undid. */
|
|
167
|
+
cellsOf: (changes: ReadonlyArray<Change<E, Ms[number]>>) => ReadonlyArray<{
|
|
168
|
+
readonly id: IdOf<E>;
|
|
169
|
+
readonly member: Ms[number];
|
|
170
|
+
}>;
|
|
171
|
+
/**
|
|
172
|
+
* What a row holds in one member, as the change that would set it: the
|
|
173
|
+
* value an undo puts back.
|
|
174
|
+
*/
|
|
175
|
+
changeAt: (row: RowOf<E, Ms[number]>, member: Ms[number]) => Change<E, Ms[number]>;
|
|
176
|
+
/** The edit of one cell, if the edits hold one. */
|
|
177
|
+
editOf: (edits: ReadonlyArray<CellEdit<E, Ms[number]>>, row: IdOf<E>, member: Ms[number]) => Option.Option<CellEdit<E, Ms[number]>>;
|
|
178
|
+
mine: (edit: CellEdit<E, Ms[number]>, replica: string) => boolean;
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
export type EditableEntity<E extends EntityFields, Ms extends ReadonlyArray<Member<E>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AAK,GAAG"}
|
package/dist/entity.mjs
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
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. `foldkit-sync/remote` shows them over the rows
|
|
6
|
+
* Remote reads.
|
|
7
|
+
*
|
|
8
|
+
* The journal orders the edits and stamps each with the sequence it
|
|
9
|
+
* committed at (`at`) and who committed it from where (`by`); the table is
|
|
10
|
+
* the journal's read model, each row carrying the highest sequence it has
|
|
11
|
+
* applied (`revision`). An edit the table holds is absorbed: the server says
|
|
12
|
+
* so (`absorbed`), every replica drops it, and the journal compacts.
|
|
13
|
+
*
|
|
14
|
+
* Everything here is pure: it owns no state, does no I/O, and sends nothing.
|
|
15
|
+
* The application keeps the edits in its Model and calls these from
|
|
16
|
+
* `update`, the stamp, and `Sync.mount`'s `onReinstall`.
|
|
17
|
+
*/
|
|
18
|
+
/** Who committed an edit: the actor, shown in words, and the replica (a tab), which says whose it is. */
|
|
19
|
+
const Author = Schema.Struct({
|
|
20
|
+
actor: Schema.String,
|
|
21
|
+
replica: Schema.String
|
|
22
|
+
});
|
|
23
|
+
const make = (entity, options) => {
|
|
24
|
+
const { members } = options;
|
|
25
|
+
const id = entity.fields.id.schema;
|
|
26
|
+
if (members.length === 0) throw new Error("EditableEntity.make: name at least one member");
|
|
27
|
+
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`);
|
|
28
|
+
const schemaOf = (member) => entity.fields[member].schema;
|
|
29
|
+
const Change = Schema.Union(members.map((member) => Schema.Struct({
|
|
30
|
+
id,
|
|
31
|
+
member: Schema.Literal(member),
|
|
32
|
+
value: schemaOf(member)
|
|
33
|
+
})));
|
|
34
|
+
const Edit = Schema.Union(members.map((member) => Schema.Struct({
|
|
35
|
+
id,
|
|
36
|
+
member: Schema.Literal(member),
|
|
37
|
+
value: schemaOf(member),
|
|
38
|
+
at: Schema.OptionFromNullOr(Schema.Number),
|
|
39
|
+
by: Schema.OptionFromNullOr(Author)
|
|
40
|
+
})));
|
|
41
|
+
/** The edits by row, then member: no key is a string built from an id. */
|
|
42
|
+
const indexOf = (edits) => {
|
|
43
|
+
const index = /* @__PURE__ */ new Map();
|
|
44
|
+
for (const edit of edits) {
|
|
45
|
+
const row = index.get(edit.id) ?? /* @__PURE__ */ new Map();
|
|
46
|
+
row.set(edit.member, edit);
|
|
47
|
+
index.set(edit.id, row);
|
|
48
|
+
}
|
|
49
|
+
return index;
|
|
50
|
+
};
|
|
51
|
+
const sameCell = (a, b) => Equal.equals(a.id, b.id) && a.member === b.member;
|
|
52
|
+
/** Whether a row read at `revision` has the edit: the table applies in the journal's order. */
|
|
53
|
+
const reached = (edit, revision) => Option.exists(edit.at, (at) => at <= revision);
|
|
54
|
+
/** Whether `replica` wrote the edit: pending here, or committed from it. */
|
|
55
|
+
const mine = (edit, replica) => Option.match(edit.at, {
|
|
56
|
+
onNone: () => true,
|
|
57
|
+
onSome: () => Option.exists(edit.by, (by) => by.replica === replica)
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
/** Carried so `foldkit-sync/remote` can name the rows it overlays by the Entity's own name. */
|
|
61
|
+
entity,
|
|
62
|
+
members,
|
|
63
|
+
/** The members, as a schema: what names a cell. */
|
|
64
|
+
Member: Schema.Literals(members),
|
|
65
|
+
/** One cell's change, on the wire: its member's value, by that member's schema. */
|
|
66
|
+
Change,
|
|
67
|
+
/** One cell's edit as the slice keeps it; `null` on the wire where it has no `at` or `by`. */
|
|
68
|
+
Edit,
|
|
69
|
+
/** The durable Message that edits cells: its fields, for the application's own tag. */
|
|
70
|
+
edited: {
|
|
71
|
+
changes: Schema.Array(Change),
|
|
72
|
+
/** The journal's: absent on what a replica sends, stamped at commit. */
|
|
73
|
+
at: Schema.optionalKey(Schema.Number),
|
|
74
|
+
by: Schema.optionalKey(Author)
|
|
75
|
+
},
|
|
76
|
+
/** The server's durable Message: the table holds every edit committed through `through`. */
|
|
77
|
+
absorbed: { through: Schema.Number },
|
|
78
|
+
/** What the stamp writes into an edit: the sequence and the author. */
|
|
79
|
+
stamped: (commit) => ({
|
|
80
|
+
at: commit.sequence,
|
|
81
|
+
by: {
|
|
82
|
+
actor: commit.actorId,
|
|
83
|
+
replica: commit.replicaId
|
|
84
|
+
}
|
|
85
|
+
}),
|
|
86
|
+
/** The edits with `changes` laid over them, each cell's latest kept. */
|
|
87
|
+
merge: (edits, changes, at, by) => {
|
|
88
|
+
if (changes.length === 0) return edits;
|
|
89
|
+
const added = changes.map((change) => ({
|
|
90
|
+
...change,
|
|
91
|
+
at,
|
|
92
|
+
by
|
|
93
|
+
}));
|
|
94
|
+
return [...edits.filter((edit) => !added.some((next) => sameCell(edit, next))), ...added];
|
|
95
|
+
},
|
|
96
|
+
/** The edits less those committed through `through`; the same array when none was. */
|
|
97
|
+
absorb: (edits, through) => edits.some((edit) => reached(edit, through)) ? edits.filter((edit) => !reached(edit, through)) : edits,
|
|
98
|
+
/** Whether any edit is committed through `through`: whether an absorb would drop one. */
|
|
99
|
+
holdsThrough: (edits, through) => edits.some((edit) => reached(edit, through)),
|
|
100
|
+
/**
|
|
101
|
+
* What the slice replaced that `replica` had written: each cell whose
|
|
102
|
+
* edit was its own, and now holds another replica's commit with another
|
|
103
|
+
* value, though it be the same person's in another tab.
|
|
104
|
+
*/
|
|
105
|
+
replaced: (previous, next, replica) => {
|
|
106
|
+
const before = indexOf(previous);
|
|
107
|
+
return next.flatMap((edit) => {
|
|
108
|
+
const prior = before.get(edit.id)?.get(edit.member);
|
|
109
|
+
if (prior === void 0 || !mine(prior, replica)) return [];
|
|
110
|
+
return Option.match(edit.by, {
|
|
111
|
+
onNone: () => [],
|
|
112
|
+
onSome: (by) => by.replica !== replica && !Equal.equals(edit.value, prior.value) ? [{
|
|
113
|
+
edit: prior,
|
|
114
|
+
by: Option.some(by)
|
|
115
|
+
}] : []
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
},
|
|
119
|
+
/** The cells a set of changes names: what a refusal of them undid. */
|
|
120
|
+
cellsOf: (changes) => changes.map(({ id: row, member }) => ({
|
|
121
|
+
id: row,
|
|
122
|
+
member
|
|
123
|
+
})),
|
|
124
|
+
/**
|
|
125
|
+
* What a row holds in one member, as the change that would set it: the
|
|
126
|
+
* value an undo puts back.
|
|
127
|
+
*/
|
|
128
|
+
changeAt: (row, member) => ({
|
|
129
|
+
id: row.id,
|
|
130
|
+
member,
|
|
131
|
+
value: row[member]
|
|
132
|
+
}),
|
|
133
|
+
/** The edit of one cell, if the edits hold one. */
|
|
134
|
+
editOf: (edits, row, member) => Option.fromUndefinedOr(edits.find((edit) => Equal.equals(edit.id, row) && edit.member === member)),
|
|
135
|
+
mine
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
/** Edits to an Entity's members, kept per cell in a replicated slice and laid over its rows. */
|
|
139
|
+
const EditableEntity = { make };
|
|
140
|
+
//#endregion
|
|
141
|
+
export { Author, EditableEntity };
|
|
142
|
+
|
|
143
|
+
//# 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. `foldkit-sync/remote` shows them over the rows\n * Remote reads.\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.\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 as the table holds it: its id, the revision it has applied, 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 /** Carried so `foldkit-sync/remote` can name the rows it overlays by the Entity's own name. */\n entity,\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 /**\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":";;;;;;;;;;;;;;;;;;AAkBA,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,QAAQ,KAAI,WACV,OAAO,OAAO;EAAE;EAAI,QAAQ,OAAO,QAAQ,MAAM;EAAG,OAAO,SAAS,MAAM;CAAE,CAAC,CAC/E,CACF;CACA,MAAM,OAAO,OAAO,MAClB,QAAQ,KAAI,WACV,OAAO,OAAO;EACZ;EACA,QAAQ,OAAO,QAAQ,MAAM;EAC7B,OAAO,SAAS,MAAM;EACtB,IAAI,OAAO,iBAAiB,OAAO,MAAM;EACzC,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