@itrocks/sync 0.0.0-stage → 0.1.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/LICENSE +165 -0
- package/README.md +249 -2
- package/config.yaml +2 -0
- package/demo/src/demo.css +458 -0
- package/dist/client/author-background.d.ts +2 -0
- package/dist/client/author-background.js +28 -0
- package/dist/client/clock.d.ts +10 -0
- package/dist/client/clock.js +30 -0
- package/dist/client/collections/order.d.ts +10 -0
- package/dist/client/collections/order.js +77 -0
- package/dist/client/collections/reorder-nodes.d.ts +2 -0
- package/dist/client/collections/reorder-nodes.js +67 -0
- package/dist/client/collections/row-changes.d.ts +12 -0
- package/dist/client/collections/row-changes.js +26 -0
- package/dist/client/collections/row-history.d.ts +16 -0
- package/dist/client/collections/row-history.js +61 -0
- package/dist/client/collections/table-columns.d.ts +19 -0
- package/dist/client/collections/table-columns.js +200 -0
- package/dist/client/collections.d.ts +54 -0
- package/dist/client/collections.js +262 -0
- package/dist/client/discovery.d.ts +1 -0
- package/dist/client/discovery.js +57 -0
- package/dist/client/field-address.d.ts +38 -0
- package/dist/client/field-address.js +225 -0
- package/dist/client/field-value.d.ts +7 -0
- package/dist/client/field-value.js +105 -0
- package/dist/client/form-identity.d.ts +1 -0
- package/dist/client/form-identity.js +43 -0
- package/dist/client/form-session.d.ts +53 -0
- package/dist/client/form-session.js +218 -0
- package/dist/client/identity.d.ts +3 -0
- package/dist/client/identity.js +27 -0
- package/dist/client/index.d.ts +43 -0
- package/dist/client/index.js +109 -0
- package/dist/client/journal.d.ts +13 -0
- package/dist/client/journal.js +59 -0
- package/dist/client/lifecycle.d.ts +26 -0
- package/dist/client/lifecycle.js +112 -0
- package/dist/client/observation.d.ts +11 -0
- package/dist/client/observation.js +46 -0
- package/dist/client/outbox.d.ts +15 -0
- package/dist/client/outbox.js +71 -0
- package/dist/client/plugin.d.ts +42 -0
- package/dist/client/plugin.js +1 -0
- package/dist/client/plugins/column-order/apply.d.ts +2 -0
- package/dist/client/plugins/column-order/apply.js +30 -0
- package/dist/client/plugins/column-order/colgroups.d.ts +2 -0
- package/dist/client/plugins/column-order/colgroups.js +78 -0
- package/dist/client/plugins/column-order/index.d.ts +2 -0
- package/dist/client/plugins/column-order/index.js +98 -0
- package/dist/client/plugins/cursor/editable.d.ts +16 -0
- package/dist/client/plugins/cursor/editable.js +67 -0
- package/dist/client/plugins/cursor/geometry.d.ts +16 -0
- package/dist/client/plugins/cursor/geometry.js +122 -0
- package/dist/client/plugins/cursor/index.d.ts +4 -0
- package/dist/client/plugins/cursor/index.js +13 -0
- package/dist/client/plugins/cursor/indicators.d.ts +31 -0
- package/dist/client/plugins/cursor/indicators.js +144 -0
- package/dist/client/plugins/cursor/local-selection.d.ts +19 -0
- package/dist/client/plugins/cursor/local-selection.js +234 -0
- package/dist/client/plugins/cursor/selection.d.ts +9 -0
- package/dist/client/plugins/cursor/selection.js +49 -0
- package/dist/client/plugins/cursor/text-presence.d.ts +15 -0
- package/dist/client/plugins/cursor/text-presence.js +95 -0
- package/dist/client/plugins/fields/author.d.ts +7 -0
- package/dist/client/plugins/fields/author.js +22 -0
- package/dist/client/plugins/fields/index.d.ts +4 -0
- package/dist/client/plugins/fields/index.js +192 -0
- package/dist/client/plugins/fields/writes.d.ts +19 -0
- package/dist/client/plugins/fields/writes.js +51 -0
- package/dist/client/plugins/focus/index.d.ts +9 -0
- package/dist/client/plugins/focus/index.js +92 -0
- package/dist/client/plugins/focus/indicators.d.ts +19 -0
- package/dist/client/plugins/focus/indicators.js +163 -0
- package/dist/client/plugins/naming/algorithm.d.ts +7 -0
- package/dist/client/plugins/naming/algorithm.js +65 -0
- package/dist/client/plugins/naming/attributes.d.ts +16 -0
- package/dist/client/plugins/naming/attributes.js +195 -0
- package/dist/client/plugins/naming/index.d.ts +5 -0
- package/dist/client/plugins/naming/index.js +185 -0
- package/dist/client/plugins/range/index.d.ts +4 -0
- package/dist/client/plugins/range/index.js +15 -0
- package/dist/client/plugins/reconciliation/index.d.ts +6 -0
- package/dist/client/plugins/reconciliation/index.js +282 -0
- package/dist/client/plugins/reconciliation/matching.d.ts +12 -0
- package/dist/client/plugins/reconciliation/matching.js +37 -0
- package/dist/client/plugins/reconciliation/proposals.d.ts +11 -0
- package/dist/client/plugins/reconciliation/proposals.js +32 -0
- package/dist/client/plugins/row-order/index.d.ts +2 -0
- package/dist/client/plugins/row-order/index.js +99 -0
- package/dist/client/plugins/rows/clone.d.ts +2 -0
- package/dist/client/plugins/rows/clone.js +123 -0
- package/dist/client/plugins/rows/index.d.ts +2 -0
- package/dist/client/plugins/rows/index.js +191 -0
- package/dist/client/plugins/rows/materialize.d.ts +4 -0
- package/dist/client/plugins/rows/materialize.js +43 -0
- package/dist/client/plugins/rows/remove.d.ts +1 -0
- package/dist/client/plugins/rows/remove.js +13 -0
- package/dist/client/plugins/rows/template.d.ts +2 -0
- package/dist/client/plugins/rows/template.js +21 -0
- package/dist/client/transport.d.ts +23 -0
- package/dist/client/transport.js +128 -0
- package/dist/client/user-colour.d.ts +3 -0
- package/dist/client/user-colour.js +11 -0
- package/dist/server/acknowledgments.d.ts +3 -0
- package/dist/server/acknowledgments.js +8 -0
- package/dist/server/broadcast.d.ts +3 -0
- package/dist/server/broadcast.js +7 -0
- package/dist/server/cli.d.ts +2 -0
- package/dist/server/cli.js +44 -0
- package/dist/server/clock.d.ts +2 -0
- package/dist/server/clock.js +3 -0
- package/dist/server/configuration.d.ts +3 -0
- package/dist/server/configuration.js +7 -0
- package/dist/server/connection.d.ts +13 -0
- package/dist/server/connection.js +1 -0
- package/dist/server/express.d.ts +2 -0
- package/dist/server/express.js +1 -0
- package/dist/server/fastify.d.ts +10 -0
- package/dist/server/fastify.js +15 -0
- package/dist/server/index.d.ts +7 -0
- package/dist/server/index.js +1 -0
- package/dist/server/journal.d.ts +7 -0
- package/dist/server/journal.js +16 -0
- package/dist/server/messages.d.ts +4 -0
- package/dist/server/messages.js +106 -0
- package/dist/server/participants.d.ts +9 -0
- package/dist/server/participants.js +7 -0
- package/dist/server/receipts.d.ts +13 -0
- package/dist/server/receipts.js +28 -0
- package/dist/server/recovery.d.ts +3 -0
- package/dist/server/recovery.js +9 -0
- package/dist/server/server.d.ts +34 -0
- package/dist/server/server.js +189 -0
- package/dist/server/sessions.d.ts +16 -0
- package/dist/server/sessions.js +37 -0
- package/dist/server/standalone.d.ts +9 -0
- package/dist/server/standalone.js +12 -0
- package/dist/server/transactions.d.ts +15 -0
- package/dist/server/transactions.js +50 -0
- package/dist/server/websocket-connection.d.ts +3 -0
- package/dist/server/websocket-connection.js +38 -0
- package/dist/server/websocket.d.ts +11 -0
- package/dist/server/websocket.js +26 -0
- package/dist/shared/field-update.d.ts +12 -0
- package/dist/shared/field-update.js +18 -0
- package/dist/shared/journal-entries.d.ts +9 -0
- package/dist/shared/journal-entries.js +33 -0
- package/dist/shared/protocol.d.ts +105 -0
- package/dist/shared/protocol.js +15 -0
- package/docs/dependencies.md +130 -0
- package/docs/spec-addendum-dependencies.md +10 -0
- package/docs/spec-addendum.md +717 -0
- package/docs/spec.md +1165 -0
- package/docs/testing.md +39 -0
- package/package.json +96 -5
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { compareTransactions } from '../../../shared/protocol.js';
|
|
2
|
+
import { fieldUpdate } from '../../field-address.js';
|
|
3
|
+
import { matchRows } from './matching.js';
|
|
4
|
+
import { identityRank } from './proposals.js';
|
|
5
|
+
import { projectAssociations } from './proposals.js';
|
|
6
|
+
const associationType = 'associate-rows';
|
|
7
|
+
export function reconciliation(algorithm = matchRows) {
|
|
8
|
+
return {
|
|
9
|
+
name: 'reconciliation',
|
|
10
|
+
init(context) {
|
|
11
|
+
const state = new Reconciliation(context, algorithm);
|
|
12
|
+
return {
|
|
13
|
+
flush: source => state.flush(source),
|
|
14
|
+
observe: records => state.observe(records),
|
|
15
|
+
receive: transaction => state.receive(transaction)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function isFieldValue(value) {
|
|
21
|
+
return (value === null) || (typeof value === 'string') || (typeof value === 'boolean')
|
|
22
|
+
|| (Array.isArray(value) && value.every(item => typeof item === 'string'));
|
|
23
|
+
}
|
|
24
|
+
function pairKey(first, second) {
|
|
25
|
+
return JSON.stringify([first, second].sort());
|
|
26
|
+
}
|
|
27
|
+
class Reconciliation {
|
|
28
|
+
context;
|
|
29
|
+
algorithm;
|
|
30
|
+
#associations = new Map();
|
|
31
|
+
#attempted = new WeakMap();
|
|
32
|
+
#excluded = new Map();
|
|
33
|
+
#historical = new Map();
|
|
34
|
+
#installed = new WeakMap();
|
|
35
|
+
#remote = new Map();
|
|
36
|
+
#resolved = new Map();
|
|
37
|
+
#rescan = false;
|
|
38
|
+
#structural = false;
|
|
39
|
+
constructor(context, algorithm) {
|
|
40
|
+
this.context = context;
|
|
41
|
+
this.algorithm = algorithm;
|
|
42
|
+
}
|
|
43
|
+
flush(source) {
|
|
44
|
+
const { collections } = this.context;
|
|
45
|
+
let blocked = false;
|
|
46
|
+
for (const [id, association] of this.#associations) {
|
|
47
|
+
const collection = collections.get(id);
|
|
48
|
+
if (collection) {
|
|
49
|
+
blocked = !this.#bind(collection, association) || blocked;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
for (const [id, pending] of this.#remote) {
|
|
53
|
+
const collection = collections.get(id);
|
|
54
|
+
if (collection) {
|
|
55
|
+
this.#reconcile(collection, pending);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (source === 'remote') {
|
|
59
|
+
if ((this.#rescan && this.#remote.size) || (blocked && this.#structural)) {
|
|
60
|
+
this.context.changed();
|
|
61
|
+
}
|
|
62
|
+
this.#rescan = false;
|
|
63
|
+
this.#structural = false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
observe(records) {
|
|
67
|
+
for (const id of this.#associations.keys()) {
|
|
68
|
+
const collection = this.context.collections.get(id);
|
|
69
|
+
if (collection?.affected(records)) {
|
|
70
|
+
this.#attempted.delete(collection);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
receive(transaction) {
|
|
75
|
+
const type = transaction.type;
|
|
76
|
+
if ((type !== associationType) && (type !== 'add-row') && (type !== 'field') && (type !== 'remove-row'))
|
|
77
|
+
return;
|
|
78
|
+
if (!transaction.data || (typeof transaction.data !== 'object'))
|
|
79
|
+
return;
|
|
80
|
+
const data = transaction.data;
|
|
81
|
+
if (type === associationType) {
|
|
82
|
+
const { collection, first, second } = data;
|
|
83
|
+
if ((typeof collection === 'string') && (typeof first === 'string') && (typeof second === 'string')) {
|
|
84
|
+
this.#record(collection, { first, second, transaction });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else if (type === 'field') {
|
|
88
|
+
const update = fieldUpdate(data);
|
|
89
|
+
if (update?.address.collection && update.address.row) {
|
|
90
|
+
const { address, value } = update;
|
|
91
|
+
const remote = this.#candidate(update.address.collection, update.address.row);
|
|
92
|
+
if (remote) {
|
|
93
|
+
this.#updateValue(remote, address.field, { snapshot: false, transaction, value });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
const { collection, row, values } = data;
|
|
99
|
+
if ((typeof collection !== 'string') || (typeof row !== 'string'))
|
|
100
|
+
return;
|
|
101
|
+
this.#structural = true;
|
|
102
|
+
if (type === 'remove-row') {
|
|
103
|
+
this.#exclude(collection, row);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const historical = this.#historical.get(collection) ?? new Set();
|
|
107
|
+
if (!this.context.replaying() && !historical.has(row)) {
|
|
108
|
+
if (this.#isInitial(collection, row)) {
|
|
109
|
+
this.#rescan = true;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
this.#exclude(collection, row);
|
|
113
|
+
}
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (!historical.has(row)) {
|
|
117
|
+
historical.add(row);
|
|
118
|
+
this.#historical.set(collection, historical);
|
|
119
|
+
const state = this.context.collections.get(collection);
|
|
120
|
+
if (state) {
|
|
121
|
+
this.#attempted.delete(state);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const remote = this.#candidate(collection, row);
|
|
125
|
+
if (!remote || !values || (typeof values !== 'object') || Array.isArray(values))
|
|
126
|
+
return;
|
|
127
|
+
for (const [field, value] of Object.entries(values)) {
|
|
128
|
+
if (isFieldValue(value)) {
|
|
129
|
+
this.#updateValue(remote, field, { snapshot: true, transaction, value });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
#bind(collection, association) {
|
|
135
|
+
const { collections } = this.context;
|
|
136
|
+
const projection = association.projection ??= projectAssociations(association.proposals.values());
|
|
137
|
+
if (this.#installed.get(collection) === projection)
|
|
138
|
+
return true;
|
|
139
|
+
const attempted = this.#attempted.get(collection);
|
|
140
|
+
const revision = collections.revision;
|
|
141
|
+
if ((attempted?.projection === projection) && (attempted.revision === revision))
|
|
142
|
+
return false;
|
|
143
|
+
this.#attempted.set(collection, { projection, revision });
|
|
144
|
+
const historical = this.#historical.get(collection.id) ?? new Set();
|
|
145
|
+
let accepted = false;
|
|
146
|
+
this.context.mute(() => {
|
|
147
|
+
accepted = collections.rebind(collection.id, projection.aliases, historical);
|
|
148
|
+
});
|
|
149
|
+
if (accepted) {
|
|
150
|
+
this.#installed.set(collection, projection);
|
|
151
|
+
this.#updateCandidates(collection.id, projection.paired);
|
|
152
|
+
}
|
|
153
|
+
return accepted;
|
|
154
|
+
}
|
|
155
|
+
#candidate(collection, row) {
|
|
156
|
+
const known = this.#remote.get(collection)?.get(row) ?? this.#resolved.get(collection)?.get(row);
|
|
157
|
+
if (known)
|
|
158
|
+
return known;
|
|
159
|
+
const { collections } = this.context;
|
|
160
|
+
const state = collections.get(collection);
|
|
161
|
+
const paired = state && this.#installed.get(state)?.paired.has(row);
|
|
162
|
+
if (this.#excluded.get(collection)?.has(row) || state?.initial.includes(row)) {
|
|
163
|
+
this.#rescan ||= this.#isInitial(collection, row);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (row.startsWith('key:') || (state?.initial.length === 0)
|
|
167
|
+
|| (!paired && collections.findRow(collection, row) && !this.#historical.get(collection)?.has(row)))
|
|
168
|
+
return;
|
|
169
|
+
const created = { values: new Map() };
|
|
170
|
+
this.#store(paired ? this.#resolved : this.#remote, collection, row, created);
|
|
171
|
+
return created;
|
|
172
|
+
}
|
|
173
|
+
#exclude(collection, row) {
|
|
174
|
+
for (const store of [this.#remote, this.#resolved]) {
|
|
175
|
+
const candidates = store.get(collection);
|
|
176
|
+
if (candidates?.delete(row) && !candidates.size) {
|
|
177
|
+
store.delete(collection);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const excluded = this.#excluded.get(collection) ?? new Set();
|
|
181
|
+
excluded.add(row);
|
|
182
|
+
this.#excluded.set(collection, excluded);
|
|
183
|
+
}
|
|
184
|
+
#isInitial(collection, row) {
|
|
185
|
+
const { collections } = this.context;
|
|
186
|
+
const canonical = collections.canonical(collection, row);
|
|
187
|
+
return collections.get(collection)?.initial.some(id => collections.canonical(collection, id) === canonical) ?? false;
|
|
188
|
+
}
|
|
189
|
+
#reconcile(collection, pending) {
|
|
190
|
+
if (!collection.initial.length)
|
|
191
|
+
return;
|
|
192
|
+
const { collections } = this.context;
|
|
193
|
+
const historical = this.#historical.get(collection.id);
|
|
194
|
+
const initial = new Set(collection.initial);
|
|
195
|
+
const paired = this.#installed.get(collection)?.paired;
|
|
196
|
+
const mounted = new Map(collection.items().map(element => [
|
|
197
|
+
collections.rawIdentity(collection, element), element
|
|
198
|
+
]));
|
|
199
|
+
const remote = [];
|
|
200
|
+
for (const [id, candidate] of pending) {
|
|
201
|
+
if (!candidate.values.size || paired?.has(id) || initial.has(id))
|
|
202
|
+
continue;
|
|
203
|
+
if (!historical?.has(id) && mounted.has(id))
|
|
204
|
+
continue;
|
|
205
|
+
const values = Object.fromEntries([...candidate.values].map(([key, { value }]) => [key, value]));
|
|
206
|
+
remote.push({ id, values });
|
|
207
|
+
}
|
|
208
|
+
if (!remote.length)
|
|
209
|
+
return;
|
|
210
|
+
const local = [];
|
|
211
|
+
for (const id of initial) {
|
|
212
|
+
const element = mounted.get(id);
|
|
213
|
+
if (element && !paired?.has(id)) {
|
|
214
|
+
local.push({ id, values: this.context.snapshot(element) });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (!local.length)
|
|
218
|
+
return;
|
|
219
|
+
const localIds = new Set(local.map(row => row.id));
|
|
220
|
+
const remoteIds = new Set(remote.map(row => row.id));
|
|
221
|
+
for (const { first, second } of this.algorithm(local, remote)) {
|
|
222
|
+
if (!localIds.has(first) || !remoteIds.has(second) || (identityRank(first) === identityRank(second)))
|
|
223
|
+
continue;
|
|
224
|
+
if (this.#associations.get(collection.id)?.proposals.has(pairKey(first, second)))
|
|
225
|
+
continue;
|
|
226
|
+
const transaction = this.context.publish(associationType, { collection: collection.id, first, second });
|
|
227
|
+
this.#record(collection.id, { first, second, transaction });
|
|
228
|
+
localIds.delete(first);
|
|
229
|
+
remoteIds.delete(second);
|
|
230
|
+
}
|
|
231
|
+
const association = this.#associations.get(collection.id);
|
|
232
|
+
if (association) {
|
|
233
|
+
this.#bind(collection, association);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
#record(collection, proposal) {
|
|
237
|
+
const { first, second, transaction } = proposal;
|
|
238
|
+
if (identityRank(first) === identityRank(second))
|
|
239
|
+
return;
|
|
240
|
+
const association = this.#associations.get(collection) ?? { proposals: new Map() };
|
|
241
|
+
const key = pairKey(first, second);
|
|
242
|
+
const previous = association.proposals.get(key);
|
|
243
|
+
if (!previous || (compareTransactions(transaction, previous.transaction) < 0)) {
|
|
244
|
+
association.proposals.set(key, proposal);
|
|
245
|
+
association.projection = undefined;
|
|
246
|
+
this.#associations.set(collection, association);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
#store(store, collection, id, row) {
|
|
250
|
+
const candidates = store.get(collection) ?? new Map();
|
|
251
|
+
candidates.set(id, row);
|
|
252
|
+
store.set(collection, candidates);
|
|
253
|
+
}
|
|
254
|
+
#updateCandidates(collection, paired) {
|
|
255
|
+
const pending = this.#remote.get(collection);
|
|
256
|
+
const resolved = this.#resolved.get(collection);
|
|
257
|
+
for (const [id, row] of resolved ?? []) {
|
|
258
|
+
if (paired.has(id))
|
|
259
|
+
continue;
|
|
260
|
+
resolved?.delete(id);
|
|
261
|
+
this.#store(this.#remote, collection, id, row);
|
|
262
|
+
}
|
|
263
|
+
for (const [id, row] of pending ?? []) {
|
|
264
|
+
if (!paired.has(id))
|
|
265
|
+
continue;
|
|
266
|
+
pending?.delete(id);
|
|
267
|
+
this.#store(this.#resolved, collection, id, row);
|
|
268
|
+
}
|
|
269
|
+
if (pending && !pending.size)
|
|
270
|
+
this.#remote.delete(collection);
|
|
271
|
+
if (resolved && !resolved.size)
|
|
272
|
+
this.#resolved.delete(collection);
|
|
273
|
+
}
|
|
274
|
+
#updateValue(remote, field, update) {
|
|
275
|
+
const previous = remote.values.get(field);
|
|
276
|
+
if (!previous || (previous.snapshot && !update.snapshot)
|
|
277
|
+
|| ((previous.snapshot === update.snapshot)
|
|
278
|
+
&& (compareTransactions(update.transaction, previous.transaction) > 0))) {
|
|
279
|
+
remote.values.set(field, update);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FieldValue } from '../../field-value.js';
|
|
2
|
+
export type MatchRows = (local: readonly RowSnapshot[], remote: readonly RowSnapshot[]) => RowAssociation[];
|
|
3
|
+
export type Values = Readonly<Record<string, FieldValue>>;
|
|
4
|
+
export interface RowAssociation {
|
|
5
|
+
first: string;
|
|
6
|
+
second: string;
|
|
7
|
+
}
|
|
8
|
+
export interface RowSnapshot {
|
|
9
|
+
id: string;
|
|
10
|
+
values: Values;
|
|
11
|
+
}
|
|
12
|
+
export declare function matchRows(local: readonly RowSnapshot[], remote: readonly RowSnapshot[]): RowAssociation[];
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export function matchRows(local, remote) {
|
|
2
|
+
const available = new Map();
|
|
3
|
+
const matches = [];
|
|
4
|
+
for (const row of [...local].sort((first, second) => compareRows(second, first))) {
|
|
5
|
+
const key = signature(row.values);
|
|
6
|
+
if (!key)
|
|
7
|
+
continue;
|
|
8
|
+
const rows = available.get(key);
|
|
9
|
+
if (rows) {
|
|
10
|
+
rows.push(row.id);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
available.set(key, [row.id]);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
for (const row of [...remote].sort(compareRows)) {
|
|
17
|
+
const key = signature(row.values);
|
|
18
|
+
if (!key)
|
|
19
|
+
continue;
|
|
20
|
+
const first = available.get(key)?.pop();
|
|
21
|
+
if (first !== undefined) {
|
|
22
|
+
matches.push({ first, second: row.id });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return matches;
|
|
26
|
+
}
|
|
27
|
+
function compareRows(first, second) {
|
|
28
|
+
if (first.id === second.id)
|
|
29
|
+
return 0;
|
|
30
|
+
return (first.id < second.id) ? -1 : 1;
|
|
31
|
+
}
|
|
32
|
+
function signature(values) {
|
|
33
|
+
const keys = Object.keys(values).sort();
|
|
34
|
+
if (!keys.length)
|
|
35
|
+
return;
|
|
36
|
+
return JSON.stringify(keys.map(key => [key, values[key]]));
|
|
37
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Transaction } from '../../../shared/protocol.js';
|
|
2
|
+
import type { RowAssociation } from './matching.js';
|
|
3
|
+
export interface Projection {
|
|
4
|
+
aliases: ReadonlyMap<string, string>;
|
|
5
|
+
paired: ReadonlySet<string>;
|
|
6
|
+
}
|
|
7
|
+
export interface Proposal extends RowAssociation {
|
|
8
|
+
transaction: Transaction;
|
|
9
|
+
}
|
|
10
|
+
export declare function identityRank(id: string): number;
|
|
11
|
+
export declare function projectAssociations(proposals: Iterable<Proposal>): Projection;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { compareTransactions } from '../../../shared/protocol.js';
|
|
2
|
+
export function identityRank(id) {
|
|
3
|
+
if (id.startsWith('key:'))
|
|
4
|
+
return 0;
|
|
5
|
+
if (id.startsWith('initial:'))
|
|
6
|
+
return 2;
|
|
7
|
+
return 1;
|
|
8
|
+
}
|
|
9
|
+
export function projectAssociations(proposals) {
|
|
10
|
+
const aliases = new Map();
|
|
11
|
+
const groups = new Map();
|
|
12
|
+
const ordered = [...proposals].sort((first, second) => compareTransactions(first.transaction, second.transaction));
|
|
13
|
+
for (const proposal of ordered) {
|
|
14
|
+
const first = aliases.get(proposal.first) ?? proposal.first;
|
|
15
|
+
const second = aliases.get(proposal.second) ?? proposal.second;
|
|
16
|
+
if (first === second)
|
|
17
|
+
continue;
|
|
18
|
+
const members = [...(groups.get(first) ?? [first]), ...(groups.get(second) ?? [second])];
|
|
19
|
+
if (new Set(members.map(identityRank)).size !== members.length)
|
|
20
|
+
continue;
|
|
21
|
+
const canonical = (identityRank(first) < identityRank(second)) ? first : second;
|
|
22
|
+
for (const id of members) {
|
|
23
|
+
if (id !== canonical) {
|
|
24
|
+
aliases.set(id, canonical);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
groups.delete(first);
|
|
28
|
+
groups.delete(second);
|
|
29
|
+
groups.set(canonical, members);
|
|
30
|
+
}
|
|
31
|
+
return { aliases, paired: new Set([...groups.values()].flat()) };
|
|
32
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { compareTransactions } from '../../../shared/protocol.js';
|
|
2
|
+
import { rowHistory } from '../../collections/row-history.js';
|
|
3
|
+
import { rowPlacements } from '../../collections/row-history.js';
|
|
4
|
+
import { rowTransactions } from '../../collections/row-history.js';
|
|
5
|
+
import { mergeOrder } from '../../collections/order.js';
|
|
6
|
+
import { orderChanged } from '../../collections/order.js';
|
|
7
|
+
import { rowSequence } from '../../collections/order.js';
|
|
8
|
+
import { reorderByParent } from '../../collections/reorder-nodes.js';
|
|
9
|
+
function applyOrder(context, collection, history) {
|
|
10
|
+
const { collections } = context;
|
|
11
|
+
const canonical = (id) => collections.canonical(collection.id, id);
|
|
12
|
+
const changes = rowHistory(history, canonical);
|
|
13
|
+
const requested = latestOrder(history)?.map(canonical);
|
|
14
|
+
if (!requested && !changes.additions.size && !changes.removed.size)
|
|
15
|
+
return;
|
|
16
|
+
const present = new Map(collection.items().map(row => [collections.identify(collection, row), row]));
|
|
17
|
+
const initial = collection.initial.map(canonical);
|
|
18
|
+
const sequence = rowSequence(initial, rowPlacements(changes, initial), changes.removed, requested);
|
|
19
|
+
const expected = mergeOrder([...present.keys()], sequence);
|
|
20
|
+
reorderByParent(expected.map(id => present.get(id)));
|
|
21
|
+
}
|
|
22
|
+
function latestOrder(history) {
|
|
23
|
+
let latest;
|
|
24
|
+
let rows;
|
|
25
|
+
for (const transaction of history) {
|
|
26
|
+
const order = orderOf(transaction);
|
|
27
|
+
if (!order)
|
|
28
|
+
continue;
|
|
29
|
+
if (!latest || (compareTransactions(latest, transaction) < 0)) {
|
|
30
|
+
latest = transaction;
|
|
31
|
+
rows = order.rows;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return rows;
|
|
35
|
+
}
|
|
36
|
+
function orderOf(transaction) {
|
|
37
|
+
if ((transaction.type !== 'order-rows') || !transaction.data || (typeof transaction.data !== 'object')) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
const data = transaction.data;
|
|
41
|
+
if ((typeof data.collection === 'string') && Array.isArray(data.rows) && data.rows.every(id => typeof id === 'string')) {
|
|
42
|
+
return data;
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
export function rowOrder() {
|
|
47
|
+
return {
|
|
48
|
+
name: 'row-order',
|
|
49
|
+
init(context) {
|
|
50
|
+
const { collections } = context;
|
|
51
|
+
const observed = new Map();
|
|
52
|
+
let dirty = false;
|
|
53
|
+
let revision = collections.revision;
|
|
54
|
+
const capture = (collection) => collection.items().map(row => collections.rawIdentity(collection, row));
|
|
55
|
+
const remember = () => {
|
|
56
|
+
observed.clear();
|
|
57
|
+
for (const collection of collections.states())
|
|
58
|
+
observed.set(collection, capture(collection));
|
|
59
|
+
};
|
|
60
|
+
remember();
|
|
61
|
+
collections.managedRowOrder = true;
|
|
62
|
+
return {
|
|
63
|
+
destroy() {
|
|
64
|
+
collections.managedRowOrder = false;
|
|
65
|
+
},
|
|
66
|
+
observe(records) {
|
|
67
|
+
for (const collection of collections.states()) {
|
|
68
|
+
if (!collection.affected(records))
|
|
69
|
+
continue;
|
|
70
|
+
dirty = true;
|
|
71
|
+
const before = observed.get(collection);
|
|
72
|
+
if (!before)
|
|
73
|
+
continue;
|
|
74
|
+
const rows = capture(collection);
|
|
75
|
+
if (orderChanged(before, rows)) {
|
|
76
|
+
context.publish('order-rows', { collection: collection.id, rows });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
flush() {
|
|
81
|
+
if (!dirty && (revision === collections.revision))
|
|
82
|
+
return;
|
|
83
|
+
dirty = false;
|
|
84
|
+
revision = collections.revision;
|
|
85
|
+
const history = rowTransactions(context.history());
|
|
86
|
+
context.mute(() => {
|
|
87
|
+
for (const collection of collections.states()) {
|
|
88
|
+
applyOrder(context, collection, history.get(collection.id) ?? []);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
remember();
|
|
92
|
+
},
|
|
93
|
+
receive(transaction) {
|
|
94
|
+
dirty ||= ['add-row', 'order-rows', 'remove-row'].includes(transaction.type);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { restoreAuthorBackground } from '../../author-background.js';
|
|
2
|
+
import { keyAttribute } from '../../collections.js';
|
|
3
|
+
import { rowKeyInput } from '../../collections.js';
|
|
4
|
+
import { controlSelector } from '../../field-value.js';
|
|
5
|
+
const references = [
|
|
6
|
+
'aria-activedescendant', 'aria-controls', 'aria-describedby', 'aria-details', 'aria-errormessage',
|
|
7
|
+
'aria-flowto', 'aria-labelledby', 'aria-owns', 'for', 'headers', 'list', 'popovertarget'
|
|
8
|
+
];
|
|
9
|
+
function clearValues(row) {
|
|
10
|
+
const controls = [...row.querySelectorAll(controlSelector)];
|
|
11
|
+
if (row.matches(controlSelector))
|
|
12
|
+
controls.unshift(row);
|
|
13
|
+
for (const control of controls) {
|
|
14
|
+
if (control instanceof HTMLInputElement) {
|
|
15
|
+
if ((control.type === 'checkbox') || (control.type === 'radio')) {
|
|
16
|
+
control.defaultChecked = false;
|
|
17
|
+
control.checked = false;
|
|
18
|
+
control.indeterminate = false;
|
|
19
|
+
}
|
|
20
|
+
else if (!['button', 'image', 'reset', 'submit'].includes(control.type)) {
|
|
21
|
+
control.defaultValue = '';
|
|
22
|
+
control.value = '';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else if (control instanceof HTMLTextAreaElement) {
|
|
26
|
+
control.defaultValue = '';
|
|
27
|
+
control.value = '';
|
|
28
|
+
}
|
|
29
|
+
else if (control instanceof HTMLSelectElement) {
|
|
30
|
+
for (const option of control.options) {
|
|
31
|
+
option.defaultSelected = false;
|
|
32
|
+
option.selected = false;
|
|
33
|
+
}
|
|
34
|
+
control.selectedIndex = -1;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
control.textContent = '';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export function cloneRow(model, document = model.ownerDocument) {
|
|
42
|
+
const clone = document.importNode(model, true);
|
|
43
|
+
const source = [model, ...model.querySelectorAll('*')];
|
|
44
|
+
const elements = [clone, ...clone.querySelectorAll('*')];
|
|
45
|
+
elements.forEach((element, index) => restoreAuthorBackground(source[index], element));
|
|
46
|
+
return clone;
|
|
47
|
+
}
|
|
48
|
+
export function prepareClone(row, id, key, managedNaming = false) {
|
|
49
|
+
clearValues(row);
|
|
50
|
+
const businessId = resetIdentity(row, id, key);
|
|
51
|
+
if (!managedNaming)
|
|
52
|
+
scopeReferences(row, id, businessId);
|
|
53
|
+
return row;
|
|
54
|
+
}
|
|
55
|
+
function resetIdentity(row, id, selector) {
|
|
56
|
+
const value = id.startsWith('key:') ? id.slice(4) : '';
|
|
57
|
+
const target = selector
|
|
58
|
+
? (row.matches(selector) ? row : row.querySelector(selector))
|
|
59
|
+
: rowKeyInput(row);
|
|
60
|
+
const source = selector ? target ?? row : row;
|
|
61
|
+
const named = selector && keyAttribute(selector);
|
|
62
|
+
const key = named || (source.hasAttribute('data-key') ? 'data-key' : undefined)
|
|
63
|
+
|| (source.hasAttribute('data-id') ? 'data-id' : undefined);
|
|
64
|
+
row.removeAttribute('data-id');
|
|
65
|
+
row.removeAttribute('data-key');
|
|
66
|
+
if (key) {
|
|
67
|
+
if (value)
|
|
68
|
+
source.setAttribute(key, value);
|
|
69
|
+
else
|
|
70
|
+
source.removeAttribute(key);
|
|
71
|
+
if (selector) {
|
|
72
|
+
return (key === 'id') ? source : undefined;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) {
|
|
76
|
+
target.defaultValue = value;
|
|
77
|
+
target.value = value;
|
|
78
|
+
}
|
|
79
|
+
else if (target instanceof HTMLSelectElement) {
|
|
80
|
+
for (const option of target.options) {
|
|
81
|
+
option.defaultSelected = !!value && (option.value === value);
|
|
82
|
+
}
|
|
83
|
+
target.value = value;
|
|
84
|
+
}
|
|
85
|
+
else if (target && !target.children.length) {
|
|
86
|
+
target.textContent = value;
|
|
87
|
+
}
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
function scopeReferences(row, id, businessId) {
|
|
91
|
+
const elements = [row, ...row.querySelectorAll('*')];
|
|
92
|
+
const identifiers = new Map();
|
|
93
|
+
const used = new Set();
|
|
94
|
+
for (const element of elements) {
|
|
95
|
+
if (!element.id || (element === businessId))
|
|
96
|
+
continue;
|
|
97
|
+
const original = element.id;
|
|
98
|
+
const base = original + '-sync-' + id.replace(/[^\w-]/g, '_');
|
|
99
|
+
let suffix = 1;
|
|
100
|
+
let scoped = base;
|
|
101
|
+
while (used.has(scoped) || row.ownerDocument.getElementById(scoped)) {
|
|
102
|
+
scoped = base + '-' + suffix++;
|
|
103
|
+
}
|
|
104
|
+
if (!identifiers.has(original))
|
|
105
|
+
identifiers.set(original, scoped);
|
|
106
|
+
used.add(scoped);
|
|
107
|
+
element.id = scoped;
|
|
108
|
+
}
|
|
109
|
+
for (const element of elements) {
|
|
110
|
+
for (const attribute of references) {
|
|
111
|
+
const value = element.getAttribute(attribute);
|
|
112
|
+
if (!value)
|
|
113
|
+
continue;
|
|
114
|
+
const next = value.split(/\s+/).map(target => identifiers.get(target) ?? target).join(' ');
|
|
115
|
+
if (next !== value)
|
|
116
|
+
element.setAttribute(attribute, next);
|
|
117
|
+
}
|
|
118
|
+
const href = element.getAttribute('href');
|
|
119
|
+
const target = href?.startsWith('#') ? identifiers.get(href.slice(1)) : undefined;
|
|
120
|
+
if (target)
|
|
121
|
+
element.setAttribute('href', '#' + target);
|
|
122
|
+
}
|
|
123
|
+
}
|