@yaks/journal 0.0.0-stage → 0.2.36
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 +202 -0
- package/README.md +522 -2
- package/_dist/backed.d.ts +9 -0
- package/_dist/backed.d.ts.map +1 -0
- package/_dist/batch.d.ts +45 -0
- package/_dist/batch.d.ts.map +1 -0
- package/_dist/feed.d.ts +108 -0
- package/_dist/feed.d.ts.map +1 -0
- package/_dist/graph.d.ts +104 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/kept.d.ts +63 -0
- package/_dist/kept.d.ts.map +1 -0
- package/_dist/log.d.ts +142 -0
- package/_dist/log.d.ts.map +1 -0
- package/_dist/merge.d.ts +41 -0
- package/_dist/merge.d.ts.map +1 -0
- package/_dist/mod.d.ts +101 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/service.d.ts +68 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/tools.d.ts +5 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/undo.d.ts +48 -0
- package/_dist/undo.d.ts.map +1 -0
- package/_dist/value.d.ts +6 -0
- package/_dist/value.d.ts.map +1 -0
- package/_dist/views.d.ts +25 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/backed.js +225 -0
- package/backed.js.map +1 -0
- package/backed.ts +274 -0
- package/batch.js +12 -0
- package/batch.js.map +1 -0
- package/batch.ts +84 -0
- package/deno.json +1 -0
- package/feed.js +295 -0
- package/feed.js.map +1 -0
- package/feed.ts +368 -0
- package/graph.js +224 -0
- package/graph.js.map +1 -0
- package/graph.ts +329 -0
- package/held.js +25 -0
- package/held.js.map +1 -0
- package/held.ts +24 -0
- package/kept.js +61 -0
- package/kept.js.map +1 -0
- package/kept.ts +100 -0
- package/log.js +1161 -0
- package/log.js.map +1 -0
- package/log.ts +1299 -0
- package/merge.js +158 -0
- package/merge.js.map +1 -0
- package/merge.ts +185 -0
- package/mod.js +101 -0
- package/mod.js.map +1 -0
- package/mod.ts +102 -0
- package/names.js +206 -0
- package/names.js.map +1 -0
- package/names.ts +230 -0
- package/package.json +52 -4
- package/page.js +160 -0
- package/page.js.map +1 -0
- package/page.ts +233 -0
- package/service.js +118 -0
- package/service.js.map +1 -0
- package/service.ts +148 -0
- package/tools.js +110 -0
- package/tools.js.map +1 -0
- package/tools.ts +113 -0
- package/undo.js +187 -0
- package/undo.js.map +1 -0
- package/undo.ts +190 -0
- package/value.js +26 -0
- package/value.js.map +1 -0
- package/value.ts +35 -0
- package/views.js +85 -0
- package/views.js.map +1 -0
- package/views.ts +91 -0
- package/vocab.js +20 -0
- package/vocab.js.map +1 -0
- package/vocab.json +308 -0
- package/vocab.ts +22 -0
package/merge.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Merging a branch into main (D-404): what the branch changed since its fork,
|
|
2
|
+
// as its store reads it (@yaks/graph `Branches.changes`), written to main as
|
|
3
|
+
// one batch. Each property merges three ways, against its value at the fork:
|
|
4
|
+
//
|
|
5
|
+
// only the branch changed it main takes the branch's value
|
|
6
|
+
// only main changed it main keeps its own
|
|
7
|
+
// both changed it, the same nothing to write
|
|
8
|
+
// both changed it, apart a conflict: the fork's, the branch's and
|
|
9
|
+
// main's values side by side, and a person
|
|
10
|
+
// picks one
|
|
11
|
+
//
|
|
12
|
+
// Nothing merges by itself: while any conflict has no pick, nothing is
|
|
13
|
+
// written, and the merge answers the conflicts instead. A component one side
|
|
14
|
+
// removed counts as every property of it changed on that side, so a removal
|
|
15
|
+
// against the other side's edit is a conflict too, and so is an entity one
|
|
16
|
+
// side deleted and the other changed, since a deletion removes every
|
|
17
|
+
// component.
|
|
18
|
+
//
|
|
19
|
+
// Only what a client may write is merged: a stamped property is main's to
|
|
20
|
+
// stamp on the merge's own write, and a computed one is nobody's. A component
|
|
21
|
+
// stamped whole merges only where it is a mark, whose presence is its say. Each
|
|
22
|
+
// property written carries main's value as its `$was`, so a main that moved
|
|
23
|
+
// after the merge was read refuses it whole rather than losing that change.
|
|
24
|
+
import { groupBy } from "@yaks/fp";
|
|
25
|
+
import { signed } from "@yaks/graph/provenance";
|
|
26
|
+
import { TOMBSTONE } from "@yaks/graph/bundle";
|
|
27
|
+
import { token } from "@yaks/graph/sha256";
|
|
28
|
+
import { same } from "@yaks/fp";
|
|
29
|
+
let key = (eid, comp, prop)=>JSON.stringify([
|
|
30
|
+
eid,
|
|
31
|
+
comp,
|
|
32
|
+
prop
|
|
33
|
+
]);
|
|
34
|
+
// Whether a component merges: not one the server stamps whole, as `created`
|
|
35
|
+
// and `updated` are, which main stamps on the merge's own write. A mark is
|
|
36
|
+
// stamped whole too, but its presence is what it says, so it merges.
|
|
37
|
+
let merges = (vocab, comp)=>{
|
|
38
|
+
let info = vocab.comp(comp);
|
|
39
|
+
return !info || !!info.writable.length || info.mark || !vocab.props(comp).length;
|
|
40
|
+
};
|
|
41
|
+
// What a client may write of a component: its stamped and computed
|
|
42
|
+
// properties left out.
|
|
43
|
+
let writable = (vocab, comp, c)=>{
|
|
44
|
+
if (!c) return null;
|
|
45
|
+
let mine = new Set(vocab.comp(comp)?.writable ?? []);
|
|
46
|
+
return Object.fromEntries(Object.entries(c).filter(([p])=>mine.has(p)));
|
|
47
|
+
};
|
|
48
|
+
// A value as a side holds it: absent and null are the same.
|
|
49
|
+
let held = (c, prop)=>c?.[prop] ?? null;
|
|
50
|
+
let part = (vocab, eid, changes, picks)=>{
|
|
51
|
+
let bundle = {
|
|
52
|
+
entity: {
|
|
53
|
+
eid
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
let conflicts = [];
|
|
57
|
+
let was = {};
|
|
58
|
+
let kept = false;
|
|
59
|
+
for (let c of changes){
|
|
60
|
+
if (c.comp == TOMBSTONE || !merges(vocab, c.comp)) continue;
|
|
61
|
+
let fork = writable(vocab, c.comp, c.fork);
|
|
62
|
+
let branch = writable(vocab, c.comp, c.branch);
|
|
63
|
+
let main = writable(vocab, c.comp, c.main);
|
|
64
|
+
if (same(branch, fork)) continue;
|
|
65
|
+
let cut = {
|
|
66
|
+
branch: !branch && !!fork,
|
|
67
|
+
main: !main && !!fork
|
|
68
|
+
};
|
|
69
|
+
let write = {};
|
|
70
|
+
let guard = {};
|
|
71
|
+
let keptHere = false;
|
|
72
|
+
let props = new Set([
|
|
73
|
+
fork,
|
|
74
|
+
branch,
|
|
75
|
+
main
|
|
76
|
+
].flatMap((x)=>Object.keys(x ?? {})));
|
|
77
|
+
for (let prop of props){
|
|
78
|
+
let f = held(fork, prop), b = held(branch, prop), m = held(main, prop);
|
|
79
|
+
let byBranch = cut.branch || !same(b, f);
|
|
80
|
+
let byMain = cut.main || !same(m, f);
|
|
81
|
+
if (!byBranch || same(b, m)) continue;
|
|
82
|
+
let pick = picks.get(key(eid, c.comp, prop))?.find((p)=>!('main' in p) || same(p.main ?? null, m));
|
|
83
|
+
let take = !byMain ? 'branch' : pick?.take;
|
|
84
|
+
if (!take) {
|
|
85
|
+
conflicts.push({
|
|
86
|
+
eid,
|
|
87
|
+
comp: c.comp,
|
|
88
|
+
prop,
|
|
89
|
+
fork: f,
|
|
90
|
+
branch: b,
|
|
91
|
+
main: m
|
|
92
|
+
});
|
|
93
|
+
} else if (take == 'main') keptHere = true;
|
|
94
|
+
else {
|
|
95
|
+
write[prop] = b;
|
|
96
|
+
guard[prop] = token(m);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
kept ||= keptHere && cut.branch;
|
|
100
|
+
if (!branch && !keptHere) {
|
|
101
|
+
if (main) bundle[c.comp] = null;
|
|
102
|
+
} else if (Object.keys(write).length || !main && branch && !keptHere) {
|
|
103
|
+
bundle[c.comp] = write;
|
|
104
|
+
if (main) was[c.comp] = guard;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
let doomed = changes.find((c)=>c.comp == TOMBSTONE);
|
|
108
|
+
if (doomed?.branch && !doomed.main && !kept) {
|
|
109
|
+
return {
|
|
110
|
+
bundle: {
|
|
111
|
+
entity: {
|
|
112
|
+
eid
|
|
113
|
+
},
|
|
114
|
+
$delete: true
|
|
115
|
+
},
|
|
116
|
+
conflicts,
|
|
117
|
+
kept
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
if (Object.keys(was).length) bundle.$was = was;
|
|
121
|
+
return {
|
|
122
|
+
bundle,
|
|
123
|
+
conflicts,
|
|
124
|
+
kept
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* A branch's changes merged onto main, three ways per property, with `picks`
|
|
129
|
+
* settling conflicts: the conflicts left, and the bundles that write the merge
|
|
130
|
+
* as one batch once none is.
|
|
131
|
+
*/ export let merged = (vocab, changes, picks = [])=>{
|
|
132
|
+
let taken = groupBy(picks, (p)=>key(p.eid, p.comp, p.prop));
|
|
133
|
+
let byEntity = groupBy(changes, (c)=>c.eid);
|
|
134
|
+
let parts = [
|
|
135
|
+
...byEntity
|
|
136
|
+
].map(([eid, cs])=>part(vocab, eid, cs, taken));
|
|
137
|
+
let conflicts = parts.flatMap((p)=>p.conflicts);
|
|
138
|
+
let bundles = conflicts.length ? [] : parts.map((p)=>p.bundle).filter((b)=>b.$delete || Object.keys(b).length > 1);
|
|
139
|
+
return {
|
|
140
|
+
conflicts,
|
|
141
|
+
bundles
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Merge a branch into main: `merge(g)(id, picks, actor)` reads branch `id`'s
|
|
146
|
+
* changes, merges them with `picks`, and, where no conflict is left, writes
|
|
147
|
+
* them to main through `g` as one batch, as `actor`. Answers what was worked
|
|
148
|
+
* out; its bundles are what was written, none while a conflict stands. The
|
|
149
|
+
* branch itself is left as it is.
|
|
150
|
+
*/ export let merge = (g)=>async (id, picks = [], actor)=>{
|
|
151
|
+
if (!g.branches) throw new Error('this graph keeps no branches');
|
|
152
|
+
let m = merged(g.vocab, await g.branches.changes(id), picks);
|
|
153
|
+
if (m.bundles.length) {
|
|
154
|
+
await g.apply(actor ? signed(m.bundles, actor) : m.bundles);
|
|
155
|
+
}
|
|
156
|
+
return m;
|
|
157
|
+
};
|
|
158
|
+
//# sourceMappingURL=merge.js.map
|
package/merge.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge.js","sources":["./merge.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,6EAA6E;AAC7E,6EAA6E;AAC7E,EAAE;AACF,+DAA+D;AAC/D,oDAAoD;AACpD,kDAAkD;AAClD,0EAA0E;AAC1E,0EAA0E;AAC1E,2CAA2C;AAC3C,EAAE;AACF,uEAAuE;AACvE,6EAA6E;AAC7E,4EAA4E;AAC5E,2EAA2E;AAC3E,qEAAqE;AACrE,aAAa;AACb,EAAE;AACF,0EAA0E;AAC1E,8EAA8E;AAC9E,gFAAgF;AAChF,4EAA4E;AAC5E,4EAA4E;AAE5E,SAAS,OAAO,wBAA6B;AAE7C,SAAS,MAAM,sCAA2C;AAE1D,SAAS,SAAS,kCAAuC;AACzD,SAAS,KAAK,kCAAuC;AAErD,SAAS,IAAI,wBAA6B;AA8B1C,IAAI,MAAM,CAAC,KAAU,MAAc,OACjC,KAAK,SAAS,CAAC;IAAC;IAAK;IAAM;GAAK;AAElC,4EAA4E;AAC5E,2EAA2E;AAC3E,qEAAqE;AACrE,IAAI,SAAS,CAAC,OAAc;EAC1B,IAAI,OAAO,MAAM,IAAI,CAAC;EACtB,OAAO,CAAC,QAAQ,CAAC,CAAC,KAAK,QAAQ,CAAC,MAAM,IAAI,KAAK,IAAI,IACjD,CAAC,MAAM,KAAK,CAAC,MAAM,MAAM;AAC7B;AAEA,mEAAmE;AACnE,uBAAuB;AACvB,IAAI,WAAW,CAAC,OAAc,MAAc;EAC1C,IAAI,CAAC,GAAG,OAAO;EACf,IAAI,OAAO,IAAI,IAAI,MAAM,IAAI,CAAC,OAAO,YAAY,EAAE;EACnD,OAAO,OAAO,WAAW,CAA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AAS,CAAC,IAAM,EAAE,GAAG;EAC5C,IAAI,QAAQ;OAAI;GAAS,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,GAAK,KAAK,OAAO,KAAK,IAAI;EAClE,IAAI,YAAY,MAAM,OAAO,CAAC,CAAC,IAAM,EAAE,SAAS;EAChD,IAAI,UAAU,UAAU,MAAM,GAAG,EAAE,GAAG,MAAM,GAAG,CAAC,CAAC,IAAM,EAAE,MAAM,EAC5D,MAAM,CAAC,CAAC,IAAM,EAAE,OAAO,IAAI,OAAO,IAAI,CAAC,GAAG,MAAM,GAAG;EACtD,OAAO;IAAE;IAAW;EAAQ;AAC9B,EAAC;AAED;;;;;;CAMC,GACD,OAAO,IAAI,QACT,CAAC,IACD,OAAO,IAAY,QAAkB,EAAE,EAAE;IACvC,IAAI,CAAC,EAAE,QAAQ,EAAE,MAAM,IAAI,MAAM;IACjC,IAAI,IAAI,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,KAAK;IACtD,IAAI,EAAE,OAAO,CAAC,MAAM,EAAE;MACpB,MAAM,EAAE,KAAK,CAAC,QAAQ,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO;IAC5D;IACA,OAAO;EACT,EAAC"}
|
package/merge.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// Merging a branch into main (D-404): what the branch changed since its fork,
|
|
2
|
+
// as its store reads it (@yaks/graph `Branches.changes`), written to main as
|
|
3
|
+
// one batch. Each property merges three ways, against its value at the fork:
|
|
4
|
+
//
|
|
5
|
+
// only the branch changed it main takes the branch's value
|
|
6
|
+
// only main changed it main keeps its own
|
|
7
|
+
// both changed it, the same nothing to write
|
|
8
|
+
// both changed it, apart a conflict: the fork's, the branch's and
|
|
9
|
+
// main's values side by side, and a person
|
|
10
|
+
// picks one
|
|
11
|
+
//
|
|
12
|
+
// Nothing merges by itself: while any conflict has no pick, nothing is
|
|
13
|
+
// written, and the merge answers the conflicts instead. A component one side
|
|
14
|
+
// removed counts as every property of it changed on that side, so a removal
|
|
15
|
+
// against the other side's edit is a conflict too, and so is an entity one
|
|
16
|
+
// side deleted and the other changed, since a deletion removes every
|
|
17
|
+
// component.
|
|
18
|
+
//
|
|
19
|
+
// Only what a client may write is merged: a stamped property is main's to
|
|
20
|
+
// stamp on the merge's own write, and a computed one is nobody's. A component
|
|
21
|
+
// stamped whole merges only where it is a mark, whose presence is its say. Each
|
|
22
|
+
// property written carries main's value as its `$was`, so a main that moved
|
|
23
|
+
// after the merge was read refuses it whole rather than losing that change.
|
|
24
|
+
|
|
25
|
+
import { groupBy } from '@yaks/fp'
|
|
26
|
+
import type { Actor, Bundle, Comp, Eid, Graph, Was } from 'jsr:@yaks/graph@0.2.36'
|
|
27
|
+
import { signed } from '@yaks/graph/provenance'
|
|
28
|
+
import type { Change } from 'jsr:@yaks/graph@0.2.36/storage'
|
|
29
|
+
import { TOMBSTONE } from '@yaks/graph/bundle'
|
|
30
|
+
import { token } from '@yaks/graph/sha256'
|
|
31
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
32
|
+
import { same } from '@yaks/fp'
|
|
33
|
+
|
|
34
|
+
/** A property both the branch and main changed since the fork, to different
|
|
35
|
+
* values: each side's value, null where it holds none. */
|
|
36
|
+
export type Conflict = {
|
|
37
|
+
eid: Eid
|
|
38
|
+
comp: string
|
|
39
|
+
prop: string
|
|
40
|
+
fork: unknown
|
|
41
|
+
branch: unknown
|
|
42
|
+
main: unknown
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Which side's value a conflict takes. With `main`, the value main held
|
|
46
|
+
* when the pick was made: a main that has moved since is a value the picker
|
|
47
|
+
* never saw, so the pick settles nothing and the conflict stands again, and
|
|
48
|
+
* of several picks of one property, the one made seeing main as it stands
|
|
49
|
+
* is taken. */
|
|
50
|
+
export type Choice = {
|
|
51
|
+
eid: Eid
|
|
52
|
+
comp: string
|
|
53
|
+
prop: string
|
|
54
|
+
take: 'branch' | 'main'
|
|
55
|
+
main?: unknown
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A merge, worked out: the conflicts no pick settles, and the bundles that
|
|
59
|
+
* write it to main, which are empty while any conflict stands. */
|
|
60
|
+
export type Merged = { conflicts: Conflict[]; bundles: Bundle[] }
|
|
61
|
+
|
|
62
|
+
let key = (eid: Eid, comp: string, prop: string) =>
|
|
63
|
+
JSON.stringify([eid, comp, prop])
|
|
64
|
+
|
|
65
|
+
// Whether a component merges: not one the server stamps whole, as `created`
|
|
66
|
+
// and `updated` are, which main stamps on the merge's own write. A mark is
|
|
67
|
+
// stamped whole too, but its presence is what it says, so it merges.
|
|
68
|
+
let merges = (vocab: Vocab, comp: string): boolean => {
|
|
69
|
+
let info = vocab.comp(comp)
|
|
70
|
+
return !info || !!info.writable.length || info.mark ||
|
|
71
|
+
!vocab.props(comp).length
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// What a client may write of a component: its stamped and computed
|
|
75
|
+
// properties left out.
|
|
76
|
+
let writable = (vocab: Vocab, comp: string, c: Comp | null): Comp | null => {
|
|
77
|
+
if (!c) return null
|
|
78
|
+
let mine = new Set(vocab.comp(comp)?.writable ?? [])
|
|
79
|
+
return Object.fromEntries(Object.entries(c).filter(([p]) => mine.has(p)))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A value as a side holds it: absent and null are the same.
|
|
83
|
+
let held = (c: Comp | null, prop: string): unknown => c?.[prop] ?? null
|
|
84
|
+
|
|
85
|
+
// One entity's part of the merge, its components' changes given.
|
|
86
|
+
type Part = {
|
|
87
|
+
bundle: Bundle
|
|
88
|
+
conflicts: Conflict[]
|
|
89
|
+
// whether a pick kept main's value against a branch's removal, which keeps
|
|
90
|
+
// the entity the branch deleted
|
|
91
|
+
kept: boolean
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let part = (
|
|
95
|
+
vocab: Vocab,
|
|
96
|
+
eid: Eid,
|
|
97
|
+
changes: Change[],
|
|
98
|
+
picks: Map<string, Choice[]>,
|
|
99
|
+
): Part => {
|
|
100
|
+
let bundle: Bundle = { entity: { eid } }
|
|
101
|
+
let conflicts: Conflict[] = []
|
|
102
|
+
let was: Was = {}
|
|
103
|
+
let kept = false
|
|
104
|
+
for (let c of changes) {
|
|
105
|
+
if (c.comp == TOMBSTONE || !merges(vocab, c.comp)) continue
|
|
106
|
+
let fork = writable(vocab, c.comp, c.fork)
|
|
107
|
+
let branch = writable(vocab, c.comp, c.branch)
|
|
108
|
+
let main = writable(vocab, c.comp, c.main)
|
|
109
|
+
if (same(branch, fork)) continue
|
|
110
|
+
let cut = { branch: !branch && !!fork, main: !main && !!fork }
|
|
111
|
+
let write: Comp = {}
|
|
112
|
+
let guard: Record<string, string | null> = {}
|
|
113
|
+
let keptHere = false
|
|
114
|
+
let props = new Set(
|
|
115
|
+
[fork, branch, main].flatMap((x) => Object.keys(x ?? {})),
|
|
116
|
+
)
|
|
117
|
+
for (let prop of props) {
|
|
118
|
+
let f = held(fork, prop), b = held(branch, prop), m = held(main, prop)
|
|
119
|
+
let byBranch = cut.branch || !same(b, f)
|
|
120
|
+
let byMain = cut.main || !same(m, f)
|
|
121
|
+
if (!byBranch || same(b, m)) continue
|
|
122
|
+
let pick = picks.get(key(eid, c.comp, prop))?.find((p) =>
|
|
123
|
+
!('main' in p) || same(p.main ?? null, m)
|
|
124
|
+
)
|
|
125
|
+
let take = !byMain ? 'branch' : pick?.take
|
|
126
|
+
if (!take) {
|
|
127
|
+
conflicts.push({ eid, comp: c.comp, prop, fork: f, branch: b, main: m })
|
|
128
|
+
} else if (take == 'main') keptHere = true
|
|
129
|
+
else {
|
|
130
|
+
write[prop] = b
|
|
131
|
+
guard[prop] = token(m)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
kept ||= keptHere && cut.branch
|
|
135
|
+
if (!branch && !keptHere) {
|
|
136
|
+
if (main) bundle[c.comp] = null
|
|
137
|
+
} else if (Object.keys(write).length || !main && branch && !keptHere) {
|
|
138
|
+
bundle[c.comp] = write
|
|
139
|
+
if (main) was[c.comp] = guard
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
let doomed = changes.find((c) => c.comp == TOMBSTONE)
|
|
143
|
+
if (doomed?.branch && !doomed.main && !kept) {
|
|
144
|
+
return { bundle: { entity: { eid }, $delete: true }, conflicts, kept }
|
|
145
|
+
}
|
|
146
|
+
if (Object.keys(was).length) bundle.$was = was
|
|
147
|
+
return { bundle, conflicts, kept }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* A branch's changes merged onto main, three ways per property, with `picks`
|
|
152
|
+
* settling conflicts: the conflicts left, and the bundles that write the merge
|
|
153
|
+
* as one batch once none is.
|
|
154
|
+
*/
|
|
155
|
+
export let merged = (
|
|
156
|
+
vocab: Vocab,
|
|
157
|
+
changes: Change[],
|
|
158
|
+
picks: Choice[] = [],
|
|
159
|
+
): Merged => {
|
|
160
|
+
let taken = groupBy(picks, (p) => key(p.eid, p.comp, p.prop))
|
|
161
|
+
let byEntity = groupBy(changes, (c) => c.eid)
|
|
162
|
+
let parts = [...byEntity].map(([eid, cs]) => part(vocab, eid, cs, taken))
|
|
163
|
+
let conflicts = parts.flatMap((p) => p.conflicts)
|
|
164
|
+
let bundles = conflicts.length ? [] : parts.map((p) => p.bundle)
|
|
165
|
+
.filter((b) => b.$delete || Object.keys(b).length > 1)
|
|
166
|
+
return { conflicts, bundles }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Merge a branch into main: `merge(g)(id, picks, actor)` reads branch `id`'s
|
|
171
|
+
* changes, merges them with `picks`, and, where no conflict is left, writes
|
|
172
|
+
* them to main through `g` as one batch, as `actor`. Answers what was worked
|
|
173
|
+
* out; its bundles are what was written, none while a conflict stands. The
|
|
174
|
+
* branch itself is left as it is.
|
|
175
|
+
*/
|
|
176
|
+
export let merge =
|
|
177
|
+
(g: Graph) =>
|
|
178
|
+
async (id: number, picks: Choice[] = [], actor?: Actor): Promise<Merged> => {
|
|
179
|
+
if (!g.branches) throw new Error('this graph keeps no branches')
|
|
180
|
+
let m = merged(g.vocab, await g.branches.changes(id), picks)
|
|
181
|
+
if (m.bundles.length) {
|
|
182
|
+
await g.apply(actor ? signed(m.bundles, actor) : m.bundles)
|
|
183
|
+
}
|
|
184
|
+
return m
|
|
185
|
+
}
|
package/mod.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/journal — who wrote what, and when: attribution and history for a
|
|
3
|
+
* {@link https://jsr.io/@yaks/graph | @yaks/graph}, with undo falling out of
|
|
4
|
+
* the same record, and a feed that carries every commit to the other hosts
|
|
5
|
+
* that opened the store.
|
|
6
|
+
*
|
|
7
|
+
* Take a page several people edit. Somebody renames it, somebody else rewrites
|
|
8
|
+
* a paragraph, somebody deletes a note attached to it. Afterwards the page
|
|
9
|
+
* holds only where it ended up — which is the question a graph answers well,
|
|
10
|
+
* and not the question being asked. This package writes the other one down.
|
|
11
|
+
*
|
|
12
|
+
* Throughout, a transaction is one call to `graph.apply()`: a list of bundles
|
|
13
|
+
* that all commit or none do. The type for a recorded one is {@link Batch},
|
|
14
|
+
* and its `seq` is its position in the total order.
|
|
15
|
+
*
|
|
16
|
+
* ## What it records
|
|
17
|
+
* Three append-only tables beside the graph's own. They hold no entities of
|
|
18
|
+
* their own — no eid, no minted id, never in a bundle or a client cache: they
|
|
19
|
+
* are the record of what was applied, not part of it.
|
|
20
|
+
*
|
|
21
|
+
* - `journal_tx` — one row per committed transaction: its id is both the total
|
|
22
|
+
* order and the cursor, with the timestamp and the actor from the
|
|
23
|
+
* transaction's `$actor` (one row per writer, where its bundles name more
|
|
24
|
+
* than one);
|
|
25
|
+
* - `journal_change` — one ordered row per component that transaction patched
|
|
26
|
+
* or removed;
|
|
27
|
+
* - `journal_field` — one ordered after-image per property that row wrote.
|
|
28
|
+
*
|
|
29
|
+
* After-images only. The before-value a history read needs is rebuilt from the
|
|
30
|
+
* entity's own rows in the log, a read bounded to one entity and never a table
|
|
31
|
+
* scan, which is what keeps the log about a third of the size of one that
|
|
32
|
+
* stores both sides. The rows go in inside the caller's transaction: a
|
|
33
|
+
* transaction that was refused leaves no trace, and one that committed always
|
|
34
|
+
* has a row.
|
|
35
|
+
*
|
|
36
|
+
* ## What it returns
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { graph } from '@yaks/graph'
|
|
39
|
+
* import { storage } from '@yaks/sqlite'
|
|
40
|
+
* import { open } from '@yaks/sqlite/db'
|
|
41
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
42
|
+
* import { ddl, journal, log, undo } from '@yaks/journal'
|
|
43
|
+
*
|
|
44
|
+
* let title = { type: 'string' }
|
|
45
|
+
* let vocab = loadVocab([{
|
|
46
|
+
* $defs: { page: { component: true, properties: { title } } },
|
|
47
|
+
* }])
|
|
48
|
+
* let db = open(':memory:')
|
|
49
|
+
* let store = storage(db, vocab)
|
|
50
|
+
* store.install()
|
|
51
|
+
* for (let s of ddl(vocab)) db.query(s)
|
|
52
|
+
* let j = log({ rows: (s) => db.query(s), vocab })
|
|
53
|
+
* let g = graph({ storage: store, vocab, plugins: [journal(j, vocab)] })
|
|
54
|
+
*
|
|
55
|
+
* let ada = '00000000-0000-4000-8000-000000000001'
|
|
56
|
+
* g.apply([{ entity: { eid: 'p1' }, page: { title: 'Kickoff' },
|
|
57
|
+
* $actor: { by: ada } }])
|
|
58
|
+
*
|
|
59
|
+
* j.history('p1') // every transaction that touched the page, oldest first
|
|
60
|
+
* j.since(0) // the feed: the transactions after a cursor, oldest first
|
|
61
|
+
* undo(g, j)(1) // the inverse, applied — and journaled in its turn
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* - {@link Log.history} — the changes to one entity, in order, each with the
|
|
65
|
+
* identity that wrote it and when it committed.
|
|
66
|
+
* - {@link undo} — the inverse of a transaction, applied through the graph, so
|
|
67
|
+
* an undo is a write like any other and undoing it is a redo. An entity the
|
|
68
|
+
* transaction deleted comes back with the components the journal recorded,
|
|
69
|
+
* and with whatever the delete cascaded; one it recorded none of refuses
|
|
70
|
+
* the undo ({@link Final}).
|
|
71
|
+
* - {@link Log.since} — the transactions after a cursor, oldest first.
|
|
72
|
+
* {@link applied} turns one back into the bundles it committed; a consumer
|
|
73
|
+
* that stores the cursor before it does the work runs effects at most once.
|
|
74
|
+
* - {@link follow} — the feed as one host sees it: every commit another host
|
|
75
|
+
* made to the same store since the last look, as what it wrote
|
|
76
|
+
* ({@link wrote}), journaled or not, its values read from the graph
|
|
77
|
+
* ({@link filled}). Each {@link Feed} writes as one host,
|
|
78
|
+
* so a graph's subscribers can be told of the commits its own `effect` phase
|
|
79
|
+
* never saw: another process's, another thread's. The feed keeps a commit
|
|
80
|
+
* for minutes ({@link KEEP}); it is how hosts hear each other, not history.
|
|
81
|
+
* - {@link backed} — the journal read as entities: `_tx` and `_change`, the
|
|
82
|
+
* computed components its vocabulary declares, queried like any other
|
|
83
|
+
* (`._change.target=T-5`) from the journal's own rows.
|
|
84
|
+
*
|
|
85
|
+
* ## What it is not
|
|
86
|
+
* It is not a backup and not a state machine: it records what moved, not the
|
|
87
|
+
* whole entity, so a graph journaled from its first write can answer anything
|
|
88
|
+
* about its past and one that started journaling later answers only from there
|
|
89
|
+
* on. It imports no platform API — the caller hands in one function,
|
|
90
|
+
* `rows(sql, params)` — so the same journal runs on a server, in a worker, and
|
|
91
|
+
* over an embedded database.
|
|
92
|
+
*
|
|
93
|
+
* @module
|
|
94
|
+
*/ export * from "./value.js";
|
|
95
|
+
export * from "./batch.js";
|
|
96
|
+
export * from "./log.js";
|
|
97
|
+
export * from "./undo.js";
|
|
98
|
+
export * from "./merge.js";
|
|
99
|
+
export * from "./feed.js";
|
|
100
|
+
export * from "./backed.js";
|
|
101
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6FC,GAED,2BAA0B;AAC1B,2BAA0B;AAC1B,yBAAwB;AACxB,0BAAyB;AACzB,2BAA0B;AAC1B,0BAAyB;AACzB,4BAA2B"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/journal — who wrote what, and when: attribution and history for a
|
|
3
|
+
* {@link https://jsr.io/@yaks/graph | @yaks/graph}, with undo falling out of
|
|
4
|
+
* the same record, and a feed that carries every commit to the other hosts
|
|
5
|
+
* that opened the store.
|
|
6
|
+
*
|
|
7
|
+
* Take a page several people edit. Somebody renames it, somebody else rewrites
|
|
8
|
+
* a paragraph, somebody deletes a note attached to it. Afterwards the page
|
|
9
|
+
* holds only where it ended up — which is the question a graph answers well,
|
|
10
|
+
* and not the question being asked. This package writes the other one down.
|
|
11
|
+
*
|
|
12
|
+
* Throughout, a transaction is one call to `graph.apply()`: a list of bundles
|
|
13
|
+
* that all commit or none do. The type for a recorded one is {@link Batch},
|
|
14
|
+
* and its `seq` is its position in the total order.
|
|
15
|
+
*
|
|
16
|
+
* ## What it records
|
|
17
|
+
* Three append-only tables beside the graph's own. They hold no entities of
|
|
18
|
+
* their own — no eid, no minted id, never in a bundle or a client cache: they
|
|
19
|
+
* are the record of what was applied, not part of it.
|
|
20
|
+
*
|
|
21
|
+
* - `journal_tx` — one row per committed transaction: its id is both the total
|
|
22
|
+
* order and the cursor, with the timestamp and the actor from the
|
|
23
|
+
* transaction's `$actor` (one row per writer, where its bundles name more
|
|
24
|
+
* than one);
|
|
25
|
+
* - `journal_change` — one ordered row per component that transaction patched
|
|
26
|
+
* or removed;
|
|
27
|
+
* - `journal_field` — one ordered after-image per property that row wrote.
|
|
28
|
+
*
|
|
29
|
+
* After-images only. The before-value a history read needs is rebuilt from the
|
|
30
|
+
* entity's own rows in the log, a read bounded to one entity and never a table
|
|
31
|
+
* scan, which is what keeps the log about a third of the size of one that
|
|
32
|
+
* stores both sides. The rows go in inside the caller's transaction: a
|
|
33
|
+
* transaction that was refused leaves no trace, and one that committed always
|
|
34
|
+
* has a row.
|
|
35
|
+
*
|
|
36
|
+
* ## What it returns
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { graph } from '@yaks/graph'
|
|
39
|
+
* import { storage } from '@yaks/sqlite'
|
|
40
|
+
* import { open } from '@yaks/sqlite/db'
|
|
41
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
42
|
+
* import { ddl, journal, log, undo } from '@yaks/journal'
|
|
43
|
+
*
|
|
44
|
+
* let title = { type: 'string' }
|
|
45
|
+
* let vocab = loadVocab([{
|
|
46
|
+
* $defs: { page: { component: true, properties: { title } } },
|
|
47
|
+
* }])
|
|
48
|
+
* let db = open(':memory:')
|
|
49
|
+
* let store = storage(db, vocab)
|
|
50
|
+
* store.install()
|
|
51
|
+
* for (let s of ddl(vocab)) db.query(s)
|
|
52
|
+
* let j = log({ rows: (s) => db.query(s), vocab })
|
|
53
|
+
* let g = graph({ storage: store, vocab, plugins: [journal(j, vocab)] })
|
|
54
|
+
*
|
|
55
|
+
* let ada = '00000000-0000-4000-8000-000000000001'
|
|
56
|
+
* g.apply([{ entity: { eid: 'p1' }, page: { title: 'Kickoff' },
|
|
57
|
+
* $actor: { by: ada } }])
|
|
58
|
+
*
|
|
59
|
+
* j.history('p1') // every transaction that touched the page, oldest first
|
|
60
|
+
* j.since(0) // the feed: the transactions after a cursor, oldest first
|
|
61
|
+
* undo(g, j)(1) // the inverse, applied — and journaled in its turn
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* - {@link Log.history} — the changes to one entity, in order, each with the
|
|
65
|
+
* identity that wrote it and when it committed.
|
|
66
|
+
* - {@link undo} — the inverse of a transaction, applied through the graph, so
|
|
67
|
+
* an undo is a write like any other and undoing it is a redo. An entity the
|
|
68
|
+
* transaction deleted comes back with the components the journal recorded,
|
|
69
|
+
* and with whatever the delete cascaded; one it recorded none of refuses
|
|
70
|
+
* the undo ({@link Final}).
|
|
71
|
+
* - {@link Log.since} — the transactions after a cursor, oldest first.
|
|
72
|
+
* {@link applied} turns one back into the bundles it committed; a consumer
|
|
73
|
+
* that stores the cursor before it does the work runs effects at most once.
|
|
74
|
+
* - {@link follow} — the feed as one host sees it: every commit another host
|
|
75
|
+
* made to the same store since the last look, as what it wrote
|
|
76
|
+
* ({@link wrote}), journaled or not, its values read from the graph
|
|
77
|
+
* ({@link filled}). Each {@link Feed} writes as one host,
|
|
78
|
+
* so a graph's subscribers can be told of the commits its own `effect` phase
|
|
79
|
+
* never saw: another process's, another thread's. The feed keeps a commit
|
|
80
|
+
* for minutes ({@link KEEP}); it is how hosts hear each other, not history.
|
|
81
|
+
* - {@link backed} — the journal read as entities: `_tx` and `_change`, the
|
|
82
|
+
* computed components its vocabulary declares, queried like any other
|
|
83
|
+
* (`._change.target=T-5`) from the journal's own rows.
|
|
84
|
+
*
|
|
85
|
+
* ## What it is not
|
|
86
|
+
* It is not a backup and not a state machine: it records what moved, not the
|
|
87
|
+
* whole entity, so a graph journaled from its first write can answer anything
|
|
88
|
+
* about its past and one that started journaling later answers only from there
|
|
89
|
+
* on. It imports no platform API — the caller hands in one function,
|
|
90
|
+
* `rows(sql, params)` — so the same journal runs on a server, in a worker, and
|
|
91
|
+
* over an embedded database.
|
|
92
|
+
*
|
|
93
|
+
* @module
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
export * from './value.js'
|
|
97
|
+
export * from './batch.js'
|
|
98
|
+
export * from './log.js'
|
|
99
|
+
export * from './undo.js'
|
|
100
|
+
export * from './merge.js'
|
|
101
|
+
export * from './feed.js'
|
|
102
|
+
export * from './backed.js'
|