@yaks/draft 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 +157 -2
- package/_dist/browser.d.ts +30 -0
- package/_dist/browser.d.ts.map +1 -0
- package/_dist/desk.d.ts +55 -0
- package/_dist/desk.d.ts.map +1 -0
- package/_dist/gateway.d.ts +42 -0
- package/_dist/gateway.d.ts.map +1 -0
- package/_dist/graph.d.ts +6 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/input.d.ts +22 -0
- package/_dist/input.d.ts.map +1 -0
- package/_dist/merge.d.ts +15 -0
- package/_dist/merge.d.ts.map +1 -0
- package/_dist/mod.d.ts +24 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/plugin.d.ts +29 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/browser.js +300 -0
- package/browser.js.map +1 -0
- package/browser.ts +350 -0
- package/deno.json +1 -0
- package/desk.js +383 -0
- package/desk.js.map +1 -0
- package/desk.ts +477 -0
- package/gateway.js +112 -0
- package/gateway.js.map +1 -0
- package/gateway.ts +129 -0
- package/graph.js +13 -0
- package/graph.js.map +1 -0
- package/graph.ts +17 -0
- package/input.js +65 -0
- package/input.js.map +1 -0
- package/input.ts +90 -0
- package/merge.js +60 -0
- package/merge.js.map +1 -0
- package/merge.ts +65 -0
- package/mod.js +24 -0
- package/mod.js.map +1 -0
- package/mod.ts +38 -0
- package/package.json +37 -4
- package/plugin.js +157 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +122 -0
- package/vocab.js +17 -0
- package/vocab.js.map +1 -0
- package/vocab.json +119 -0
- package/vocab.ts +18 -0
package/desk.ts
ADDED
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A person's drafts, as an interface they type in holds them. `desk(client,
|
|
3
|
+
* opts)` binds them to the graph that keeps them, and each place the person
|
|
4
|
+
* types in reads its text (`text`), keeps what is typed (`type`), and ends it
|
|
5
|
+
* when it is sent or discarded (`spend`).
|
|
6
|
+
*
|
|
7
|
+
* A keystroke costs no round trip: `text` answers it at once, and the stash
|
|
8
|
+
* (Web Storage, which answers at once) keeps it, so a crash, a closed tab or a
|
|
9
|
+
* store out of reach loses nothing; the next load finds it there and sends it.
|
|
10
|
+
* The store is written at most once a pace per draft, one write in flight at
|
|
11
|
+
* a time, each saying the text it was typed over (`typed{over}`), and the
|
|
12
|
+
* store merges (./plugin.ts), keeping what another interface wrote meanwhile.
|
|
13
|
+
* The store's answer, and every text another interface makes it hold, is
|
|
14
|
+
* heard by its `rev`, newest last, and what was typed here since is merged
|
|
15
|
+
* onto it (./merge.ts): neither side's typing is dropped. A draft ends when
|
|
16
|
+
* its text is emptied, in the same batch as the send it went into.
|
|
17
|
+
*
|
|
18
|
+
* @module
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
batch,
|
|
23
|
+
effect,
|
|
24
|
+
type Signal,
|
|
25
|
+
signal,
|
|
26
|
+
untracked,
|
|
27
|
+
} from '@preact/signals-core'
|
|
28
|
+
import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
|
|
29
|
+
import { derivedEid } from '@yaks/graph/identity'
|
|
30
|
+
import { merge } from './merge.js'
|
|
31
|
+
|
|
32
|
+
/** The eid of the draft `by` types in `place`: the same in every interface,
|
|
33
|
+
* so each finds the one the others write.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { assertEquals, assertNotEquals } from '@std/assert'
|
|
37
|
+
* import { draftEid } from '@yaks/draft'
|
|
38
|
+
*
|
|
39
|
+
* assertEquals(draftEid('jo', 'search'), draftEid('jo', 'search'))
|
|
40
|
+
* assertNotEquals(draftEid('jo', 'search'), draftEid('al', 'search'))
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export let draftEid = (by: string, place: string): string =>
|
|
44
|
+
derivedEid(['draft', by, place].join('|'))
|
|
45
|
+
|
|
46
|
+
/** As much of a graph as a desk needs: a write answered with the batch as
|
|
47
|
+
* the store took it, and a watch. A @yaks/client `client()` is one. */
|
|
48
|
+
export type Client = {
|
|
49
|
+
mutate: (bundles: Bundle[]) => Bundle[] | Promise<Bundle[]>
|
|
50
|
+
watch: (query: string) => {
|
|
51
|
+
value: Bundle[]
|
|
52
|
+
subscribe: (fn: (rows: Bundle[]) => void) => () => void
|
|
53
|
+
close?: () => void
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Where what the store has not taken yet is kept across a reload: the part
|
|
58
|
+
* of Web Storage a desk uses. `localStorage` is one. */
|
|
59
|
+
export type Stash = Pick<
|
|
60
|
+
Storage,
|
|
61
|
+
'length' | 'key' | 'getItem' | 'setItem' | 'removeItem'
|
|
62
|
+
>
|
|
63
|
+
|
|
64
|
+
/** What a desk offers each place a person types in. */
|
|
65
|
+
export type Drafts = {
|
|
66
|
+
/** Whether this place has been held, including an intentionally empty edit. */
|
|
67
|
+
has: (place: string) => boolean
|
|
68
|
+
/** Every place holding text, in the order first held, read reactively. */
|
|
69
|
+
places: () => string[]
|
|
70
|
+
/** what is typed in `place` and not yet sent ('' for nothing), read
|
|
71
|
+
* reactively */
|
|
72
|
+
text: (place: string) => string
|
|
73
|
+
/** the person typed: keep it */
|
|
74
|
+
type: (place: string, text: string) => void
|
|
75
|
+
/** it was sent, or discarded: empty it at once, in one batch with `also`
|
|
76
|
+
* (the send it went into) */
|
|
77
|
+
spend: (
|
|
78
|
+
place: string,
|
|
79
|
+
also?: Bundle[],
|
|
80
|
+
opts?: { optimistic?: boolean },
|
|
81
|
+
) => Promise<boolean>
|
|
82
|
+
/** Restore text from another source, preserving subsequent typing.
|
|
83
|
+
* `over` is the source text already imported (default: empty). */
|
|
84
|
+
restore: (place: string, text: string, over?: string) => void
|
|
85
|
+
/** Run `fn` now and again whenever a text or place it read changes, until
|
|
86
|
+
* the returned function is called. */
|
|
87
|
+
follow: (fn: () => void) => () => void
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A desk can tell a recovery source when it can release its own copy. */
|
|
91
|
+
export interface Desk extends Drafts {
|
|
92
|
+
/** Whether the restored draft is durable across reload. */
|
|
93
|
+
restore: (place: string, text: string, over?: string) => boolean
|
|
94
|
+
/** Settle the current draft with the store before opening another surface. */
|
|
95
|
+
flush: (place: string) => Promise<boolean>
|
|
96
|
+
close: () => void
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** What a desk is bound with. */
|
|
100
|
+
export type DeskOpts = {
|
|
101
|
+
/** whose drafts they are: the person, read reactively, and nobody yet
|
|
102
|
+
* while it is not known (typing is kept, and written once it is) */
|
|
103
|
+
by: () => string | undefined
|
|
104
|
+
/** where untaken text waits across a reload (default: nowhere) */
|
|
105
|
+
stash?: Stash
|
|
106
|
+
/** the least time between two writes of one draft, in ms (default 500) */
|
|
107
|
+
pace?: number
|
|
108
|
+
/** a write the store refused; the text stays, and goes with the next */
|
|
109
|
+
report?: (err: unknown) => void
|
|
110
|
+
/** the line the store answers `by`'s drafts on (default `.draft.by=<by>`),
|
|
111
|
+
* where it names them otherwise, as a page graph of several stores does */
|
|
112
|
+
line?: (by: string) => string
|
|
113
|
+
/** Identify this desk's writes when saves and spends use separate doors. */
|
|
114
|
+
writer?: string
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// The store's text, and which revision of it.
|
|
118
|
+
type Word = { text: string; rev: number }
|
|
119
|
+
|
|
120
|
+
// One draft as this interface holds it.
|
|
121
|
+
type Held = {
|
|
122
|
+
/** the store's text as last heard, and its rev */
|
|
123
|
+
base: string
|
|
124
|
+
rev: number
|
|
125
|
+
confirmed?: number
|
|
126
|
+
/** what is typed here */
|
|
127
|
+
local: string
|
|
128
|
+
/** the write the store has not answered: its number and its text */
|
|
129
|
+
flying?: number
|
|
130
|
+
sent?: string
|
|
131
|
+
/** the newest text heard while waiting on it */
|
|
132
|
+
later?: Word
|
|
133
|
+
/** when the last write left */
|
|
134
|
+
left: number
|
|
135
|
+
/** the store refused the last write: the next waits for more typing */
|
|
136
|
+
stuck?: boolean
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
let fresh = (): Held => ({ base: '', rev: 0, local: '', left: 0 })
|
|
140
|
+
|
|
141
|
+
// The store's text heard. A newer one is the new base, and what was typed
|
|
142
|
+
// here since is merged onto it. While a write is in flight the answer to it
|
|
143
|
+
// comes first, since it may already hold this text; the newest waits.
|
|
144
|
+
let heard = (h: Held, w: Word): Held =>
|
|
145
|
+
w.rev <= h.rev
|
|
146
|
+
? h
|
|
147
|
+
: h.flying != null
|
|
148
|
+
? { ...h, later: h.later && h.later.rev >= w.rev ? h.later : w }
|
|
149
|
+
: { ...h, base: w.text, rev: w.rev, local: merge(h.base, h.local, w.text) }
|
|
150
|
+
|
|
151
|
+
// The answer to write `n`: the store's text once it took it. What was typed
|
|
152
|
+
// while it flew is merged onto it, and a newer text heard meanwhile after.
|
|
153
|
+
// An answer to a write since overtaken is only a text heard.
|
|
154
|
+
let answered = (h: Held, n: number, w: Word): Held => {
|
|
155
|
+
if (h.flying != n) return heard(h, w)
|
|
156
|
+
let next = { ...landed(h), confirmed: n }
|
|
157
|
+
if (w.rev > h.rev) {
|
|
158
|
+
next = { ...next, base: w.text, rev: w.rev }
|
|
159
|
+
next.local = merge(h.sent ?? h.base, h.local, w.text)
|
|
160
|
+
}
|
|
161
|
+
return h.later ? heard(next, h.later) : next
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Write `n` refused: nothing flies. What a refused spend emptied is typed
|
|
165
|
+
// again, since what it went into was not sent either, and nothing is written
|
|
166
|
+
// until the person types on.
|
|
167
|
+
let failed = (h: Held, n: number): Held => {
|
|
168
|
+
if (h.flying != n) return h
|
|
169
|
+
// Watches include the optimistic write. Its queued revision cannot prove
|
|
170
|
+
// acceptance after the write failed.
|
|
171
|
+
return { ...landed(h), stuck: true }
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// A write settled: nothing in flight.
|
|
175
|
+
let landed = (h: Held): Held => ({
|
|
176
|
+
...h,
|
|
177
|
+
flying: undefined,
|
|
178
|
+
sent: undefined,
|
|
179
|
+
later: undefined,
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
// Whether the store has yet to take what is typed here.
|
|
183
|
+
let due = (h: Held) => !h.stuck && h.flying == null && h.local != h.base
|
|
184
|
+
let behind = (h: Held) => h.flying != null || h.local != h.base
|
|
185
|
+
|
|
186
|
+
let STASH = 'draft:'
|
|
187
|
+
|
|
188
|
+
type Kept = { text: string; over: string; rev: number }
|
|
189
|
+
|
|
190
|
+
/** Bind a person's drafts to the graph that keeps them. */
|
|
191
|
+
/// let { client } = await import('@yaks/client')
|
|
192
|
+
/// let { loadVocab } = await import('@yaks/vocab')
|
|
193
|
+
/// let { draftDoc, drafts } = await import('@yaks/draft')
|
|
194
|
+
/// let g = client(loadVocab([draftDoc]), [drafts()], { vault: false })
|
|
195
|
+
/// let d = desk(g, { by: () => 'jo' })
|
|
196
|
+
/// d.type('search', '.task')
|
|
197
|
+
/// let typed = d.text('search') // at once
|
|
198
|
+
/// typed -> '.task'
|
|
199
|
+
/// d.close()
|
|
200
|
+
export let desk = (
|
|
201
|
+
client: Client,
|
|
202
|
+
opts: DeskOpts,
|
|
203
|
+
): Desk => {
|
|
204
|
+
let pace = opts.pace ?? 500
|
|
205
|
+
let held = new Map<string, Held>()
|
|
206
|
+
let shown = new Map<string, Signal<string>>()
|
|
207
|
+
// Counts the places held, so a reader of `places` hears a new one.
|
|
208
|
+
let grown = signal(0)
|
|
209
|
+
let timers = new Map<string, ReturnType<typeof setTimeout>>()
|
|
210
|
+
// Visible text can be emptied, or replaced by more typing, while a send
|
|
211
|
+
// waits. Each send keeps its own recovery copy until its answer arrives.
|
|
212
|
+
let recovery = new Map<string, Map<number, string>>()
|
|
213
|
+
let stashed = new Map<string, string>()
|
|
214
|
+
let writes = 0
|
|
215
|
+
let closed = false
|
|
216
|
+
|
|
217
|
+
let get = (place: string) => held.get(place) ?? fresh()
|
|
218
|
+
let seen = (place: string) => {
|
|
219
|
+
let s = shown.get(place)
|
|
220
|
+
if (!s) shown.set(place, s = signal(get(place).local))
|
|
221
|
+
return s
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// What is not the store's yet waits in the stash; what is, leaves it.
|
|
225
|
+
let keep = (place: string, h: Held) => {
|
|
226
|
+
let key = STASH + place
|
|
227
|
+
try {
|
|
228
|
+
let spent = [...recovery.get(place)?.values() ?? []]
|
|
229
|
+
if (!behind(h) && !spent.length) {
|
|
230
|
+
opts.stash?.removeItem(key)
|
|
231
|
+
return true
|
|
232
|
+
}
|
|
233
|
+
let kept: Kept = {
|
|
234
|
+
text: spent.reduce((text, copy) => merge('', copy, text), h.local),
|
|
235
|
+
over: spent.length ? '' : h.base,
|
|
236
|
+
rev: h.rev,
|
|
237
|
+
}
|
|
238
|
+
let value = JSON.stringify(kept)
|
|
239
|
+
opts.stash?.setItem(key, value)
|
|
240
|
+
stashed.set(place, value)
|
|
241
|
+
return !!opts.stash
|
|
242
|
+
} catch (err) {
|
|
243
|
+
opts.report?.(err)
|
|
244
|
+
return false
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
let hold = (place: string, h: Held) => {
|
|
249
|
+
batch(() => {
|
|
250
|
+
if (!held.has(place)) grown.value++
|
|
251
|
+
held.set(place, h)
|
|
252
|
+
seen(place).value = h.local
|
|
253
|
+
})
|
|
254
|
+
return keep(place, h)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
let step = (place: string, f: (h: Held) => Held) => {
|
|
258
|
+
let kept = hold(place, f(get(place)))
|
|
259
|
+
schedule(place)
|
|
260
|
+
return kept
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let forward = (bundles: Bundle[]) => {
|
|
264
|
+
if (!bundles.length) return Promise.resolve(true)
|
|
265
|
+
try {
|
|
266
|
+
let sent = client.mutate(bundles)
|
|
267
|
+
return Promise.resolve(sent).then(() => true, (err) => {
|
|
268
|
+
opts.report?.(err)
|
|
269
|
+
return false
|
|
270
|
+
})
|
|
271
|
+
} catch (err) {
|
|
272
|
+
opts.report?.(err)
|
|
273
|
+
return Promise.resolve(false)
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// A write of what is typed, over what it was typed over, with `also`
|
|
278
|
+
// beside it in one batch.
|
|
279
|
+
let send = (
|
|
280
|
+
place: string,
|
|
281
|
+
also: Bundle[] = [],
|
|
282
|
+
spent?: string,
|
|
283
|
+
) => {
|
|
284
|
+
clearTimeout(timers.get(place))
|
|
285
|
+
timers.delete(place)
|
|
286
|
+
let by = opts.by()
|
|
287
|
+
let h = get(place)
|
|
288
|
+
let n = ++writes
|
|
289
|
+
if (spent != null) {
|
|
290
|
+
let copies = recovery.get(place) ?? new Map<number, string>()
|
|
291
|
+
copies.set(n, spent)
|
|
292
|
+
recovery.set(place, copies)
|
|
293
|
+
h = { ...h, local: '', stuck: false }
|
|
294
|
+
}
|
|
295
|
+
let eid = by ? draftEid(by, place) : undefined
|
|
296
|
+
let typed = opts.writer
|
|
297
|
+
? {
|
|
298
|
+
over: h.base,
|
|
299
|
+
writer: opts.writer,
|
|
300
|
+
order: n,
|
|
301
|
+
...spent == null ? {} : { confirmed: h.confirmed ?? 0 },
|
|
302
|
+
}
|
|
303
|
+
: { over: h.flying != null ? h.sent ?? h.base : h.base }
|
|
304
|
+
hold(place, { ...h, flying: n, sent: h.local, left: Date.now() })
|
|
305
|
+
let fail = (err: unknown) => {
|
|
306
|
+
opts.report?.(err)
|
|
307
|
+
if (closed) return false
|
|
308
|
+
let text = recovery.get(place)?.get(n)
|
|
309
|
+
recovery.get(place)?.delete(n)
|
|
310
|
+
step(place, (h) => ({
|
|
311
|
+
...failed(h, n),
|
|
312
|
+
// Recovered sends are insertions: a newer empty watched draft must
|
|
313
|
+
// never read them as text another interface deleted.
|
|
314
|
+
base: text == null ? h.base : '',
|
|
315
|
+
confirmed: text == null ? h.confirmed : undefined,
|
|
316
|
+
local: text == null ? h.local : merge('', text, h.local),
|
|
317
|
+
}))
|
|
318
|
+
return false
|
|
319
|
+
}
|
|
320
|
+
try {
|
|
321
|
+
let bundles = [
|
|
322
|
+
...also,
|
|
323
|
+
...eid
|
|
324
|
+
? [{
|
|
325
|
+
entity: { eid },
|
|
326
|
+
draft: { by, place, text: h.local },
|
|
327
|
+
typed,
|
|
328
|
+
}]
|
|
329
|
+
: [],
|
|
330
|
+
]
|
|
331
|
+
let written = client.mutate(bundles)
|
|
332
|
+
return Promise.resolve(written).then((applied) => {
|
|
333
|
+
let d = applied.find((b) => b.entity.eid == eid)?.draft as
|
|
334
|
+
| { text?: string; rev?: number }
|
|
335
|
+
| undefined
|
|
336
|
+
if (eid && !d) {
|
|
337
|
+
return fail(new Error('write accepted without its draft'))
|
|
338
|
+
}
|
|
339
|
+
// A closed desk may reconcile the copy it left, but never overwrite
|
|
340
|
+
// what a replacement desk typed. Only this receipt proves its base;
|
|
341
|
+
// watched optimistic text cannot stand in for acceptance.
|
|
342
|
+
if (closed) {
|
|
343
|
+
try {
|
|
344
|
+
let copy = stashed.get(place)
|
|
345
|
+
if (copy == null || opts.stash?.getItem(STASH + place) !== copy) {
|
|
346
|
+
return true
|
|
347
|
+
}
|
|
348
|
+
} catch (error) {
|
|
349
|
+
opts.report?.(error)
|
|
350
|
+
return true
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
recovery.get(place)?.delete(n)
|
|
354
|
+
let accept = (next: Held) =>
|
|
355
|
+
d
|
|
356
|
+
? answered(next, n, word(d))
|
|
357
|
+
: next.flying == n
|
|
358
|
+
? { ...landed(next), base: '' }
|
|
359
|
+
: next
|
|
360
|
+
if (closed) keep(place, accept(get(place)))
|
|
361
|
+
else step(place, accept)
|
|
362
|
+
return true
|
|
363
|
+
}, fail)
|
|
364
|
+
} catch (err) {
|
|
365
|
+
return Promise.resolve(fail(err))
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
let saving = new Map<string, Promise<boolean>>()
|
|
370
|
+
let save = (place: string) => {
|
|
371
|
+
let pending = send(place).finally(() => {
|
|
372
|
+
if (saving.get(place) == pending) saving.delete(place)
|
|
373
|
+
})
|
|
374
|
+
saving.set(place, pending)
|
|
375
|
+
return pending
|
|
376
|
+
}
|
|
377
|
+
let flush = async (place: string): Promise<boolean> => {
|
|
378
|
+
let pending: Promise<boolean> | undefined
|
|
379
|
+
while ((pending = saving.get(place))) {
|
|
380
|
+
if (!await pending) return false
|
|
381
|
+
}
|
|
382
|
+
let h = get(place)
|
|
383
|
+
if (closed || !opts.by() || h.flying != null) return false
|
|
384
|
+
return h.local == h.base || await save(place)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Write a draft when the store is behind it: at once after a quiet spell,
|
|
388
|
+
// else once the pace since the last write runs out. One flies at a time;
|
|
389
|
+
// its answer schedules the next.
|
|
390
|
+
let schedule = (place: string) => {
|
|
391
|
+
let h = get(place)
|
|
392
|
+
if (closed || !due(h) || timers.has(place) || !opts.by()) return
|
|
393
|
+
let wait = h.left + pace - Date.now()
|
|
394
|
+
if (wait <= 0) return save(place)
|
|
395
|
+
timers.set(place, setTimeout(() => save(place), wait))
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
let word = (d: { text?: string; rev?: number }): Word => ({
|
|
399
|
+
text: d.text ?? '',
|
|
400
|
+
rev: d.rev ?? 0,
|
|
401
|
+
})
|
|
402
|
+
|
|
403
|
+
// What a last load typed and the store never took.
|
|
404
|
+
for (let i = 0; i < (opts.stash?.length ?? 0); i++) {
|
|
405
|
+
let key = opts.stash?.key(i) ?? ''
|
|
406
|
+
if (!key.startsWith(STASH)) continue
|
|
407
|
+
try {
|
|
408
|
+
let k = JSON.parse(opts.stash?.getItem(key) ?? '') as Kept
|
|
409
|
+
held.set(key.slice(STASH.length), {
|
|
410
|
+
base: k.over,
|
|
411
|
+
rev: k.rev,
|
|
412
|
+
local: k.text,
|
|
413
|
+
left: 0,
|
|
414
|
+
})
|
|
415
|
+
} catch { /* an item this build cannot read: the store has the rest */ }
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// Once it is known whose they are: the store's drafts heard as they
|
|
419
|
+
// change, and whatever is behind them written.
|
|
420
|
+
let stop = effect(() => {
|
|
421
|
+
let by = opts.by()
|
|
422
|
+
if (!by) return
|
|
423
|
+
return untracked(() => {
|
|
424
|
+
let watch = client.watch(opts.line?.(by) ?? `.draft.by=${by}`)
|
|
425
|
+
let hear = (rows: Bundle[]) =>
|
|
426
|
+
batch(() => {
|
|
427
|
+
for (let b of rows) {
|
|
428
|
+
let d = b.draft as { place?: string; text?: string; rev?: number }
|
|
429
|
+
if (!d?.place) continue
|
|
430
|
+
// A watch answers every draft at each change; one no newer than
|
|
431
|
+
// what is held changes nothing here.
|
|
432
|
+
let h = get(d.place), next = heard(h, word(d))
|
|
433
|
+
if (next !== h) step(d.place, () => next)
|
|
434
|
+
}
|
|
435
|
+
})
|
|
436
|
+
hear(watch.value)
|
|
437
|
+
let off = watch.subscribe(hear)
|
|
438
|
+
for (let place of held.keys()) schedule(place)
|
|
439
|
+
return () => {
|
|
440
|
+
off()
|
|
441
|
+
watch.close?.()
|
|
442
|
+
}
|
|
443
|
+
})
|
|
444
|
+
})
|
|
445
|
+
|
|
446
|
+
return {
|
|
447
|
+
has: (place) => held.has(place),
|
|
448
|
+
places: () => {
|
|
449
|
+
grown.value
|
|
450
|
+
return [...held.keys()].filter((place) => seen(place).value)
|
|
451
|
+
},
|
|
452
|
+
text: (place) => seen(place).value,
|
|
453
|
+
type: (place, text) =>
|
|
454
|
+
step(place, (h) => ({ ...h, local: text, stuck: false })),
|
|
455
|
+
flush,
|
|
456
|
+
spend: (place, also = []) => {
|
|
457
|
+
let h = get(place)
|
|
458
|
+
if (!behind(h) && !h.local) {
|
|
459
|
+
return forward(also)
|
|
460
|
+
}
|
|
461
|
+
return send(place, also, h.local)
|
|
462
|
+
},
|
|
463
|
+
restore: (place, text, over = '') =>
|
|
464
|
+
step(place, (h) => ({
|
|
465
|
+
...h,
|
|
466
|
+
local: merge(over, text, h.local),
|
|
467
|
+
stuck: false,
|
|
468
|
+
})),
|
|
469
|
+
follow: (fn) => effect(fn),
|
|
470
|
+
close: () => {
|
|
471
|
+
closed = true
|
|
472
|
+
stop()
|
|
473
|
+
for (let t of timers.values()) clearTimeout(t)
|
|
474
|
+
timers.clear()
|
|
475
|
+
},
|
|
476
|
+
}
|
|
477
|
+
}
|
package/gateway.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { token } from "@yaks/graph";
|
|
2
|
+
import { draftEid } from "./desk.js";
|
|
3
|
+
import { merge } from "./merge.js";
|
|
4
|
+
export class DraftRefused extends Error {
|
|
5
|
+
}
|
|
6
|
+
let identity = (who)=>{
|
|
7
|
+
let by = who.person || who.via;
|
|
8
|
+
if (!by || !/^[a-f0-9-]{36}$/i.test(by)) {
|
|
9
|
+
throw new DraftRefused('Open this page again to recover your saved draft.');
|
|
10
|
+
}
|
|
11
|
+
return by;
|
|
12
|
+
};
|
|
13
|
+
/** A private application's vouched draft door, using the same desk and merge
|
|
14
|
+
* protocol as a page whose graph is directly accessible. */ export let draftGateway = (db, who)=>{
|
|
15
|
+
let by = identity(who);
|
|
16
|
+
let read = (by)=>db.query(`.draft.by=${by}`);
|
|
17
|
+
let guestRows = (via)=>db.query(`.draft.via=${identity({
|
|
18
|
+
via
|
|
19
|
+
})} !draft.by ?created`);
|
|
20
|
+
return {
|
|
21
|
+
async snapshot () {
|
|
22
|
+
let rows = await (who.person ? read(by) : guestRows(by)), recover = [];
|
|
23
|
+
if (who.person) {
|
|
24
|
+
// Directory sign-in receipts fill guest authorship independently of
|
|
25
|
+
// an app-store failure. Their original via still identifies the input.
|
|
26
|
+
let attributed = await db.query(`.draft !draft.by .created.by=${by}`);
|
|
27
|
+
let earlier = attributed.filter((row)=>row.draft.via && row.draft.via === row.created?.via);
|
|
28
|
+
let claimed = who.browsers ?? (who.via ? [
|
|
29
|
+
who.via
|
|
30
|
+
] : []);
|
|
31
|
+
let current = (await Promise.all(claimed.map(guestRows))).flat();
|
|
32
|
+
let guest = [
|
|
33
|
+
...new Map([
|
|
34
|
+
...earlier,
|
|
35
|
+
...current.filter((row)=>!row.created?.by || row.created.by === by)
|
|
36
|
+
].map((row)=>[
|
|
37
|
+
row.entity.eid,
|
|
38
|
+
row
|
|
39
|
+
])).values()
|
|
40
|
+
];
|
|
41
|
+
recover = [
|
|
42
|
+
...new Set([
|
|
43
|
+
...earlier.map((row)=>row.draft.via),
|
|
44
|
+
...claimed.filter((via)=>!current.some((row)=>row.draft.via === via && row.created?.by && row.created.by !== by))
|
|
45
|
+
])
|
|
46
|
+
];
|
|
47
|
+
if (guest.length) {
|
|
48
|
+
let changes = [];
|
|
49
|
+
for (let row of guest){
|
|
50
|
+
let existing = rows.find((r)=>r.draft.place === row.draft.place);
|
|
51
|
+
let text = row.draft.text === '' ? '' : merge('', row.draft.text, existing?.draft.text ?? '');
|
|
52
|
+
changes.push({
|
|
53
|
+
entity: {
|
|
54
|
+
eid: draftEid(by, row.draft.place)
|
|
55
|
+
},
|
|
56
|
+
draft: {
|
|
57
|
+
by,
|
|
58
|
+
place: row.draft.place,
|
|
59
|
+
text
|
|
60
|
+
},
|
|
61
|
+
typed: {
|
|
62
|
+
over: existing?.draft.text ?? ''
|
|
63
|
+
}
|
|
64
|
+
}, {
|
|
65
|
+
entity: row.entity,
|
|
66
|
+
$delete: true,
|
|
67
|
+
$was: {
|
|
68
|
+
draft: {
|
|
69
|
+
text: token(row.draft.text)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
await db.apply(changes);
|
|
75
|
+
rows = await read(by);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
principal: by,
|
|
80
|
+
via: who.via,
|
|
81
|
+
drafts: rows,
|
|
82
|
+
recover
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
async write (value) {
|
|
86
|
+
if (value.principal !== by) {
|
|
87
|
+
throw new DraftRefused('Your sign-in changed. This text is kept with the person who typed it.');
|
|
88
|
+
}
|
|
89
|
+
if (typeof value.place !== 'string' || typeof value.text !== 'string' || typeof value.over !== 'string') throw new DraftRefused('Send the saved draft again.');
|
|
90
|
+
return await db.apply([
|
|
91
|
+
{
|
|
92
|
+
entity: {
|
|
93
|
+
eid: draftEid(by, value.place)
|
|
94
|
+
},
|
|
95
|
+
draft: {
|
|
96
|
+
...who.person ? {
|
|
97
|
+
by
|
|
98
|
+
} : {
|
|
99
|
+
via: by
|
|
100
|
+
},
|
|
101
|
+
place: value.place,
|
|
102
|
+
text: value.text
|
|
103
|
+
},
|
|
104
|
+
typed: {
|
|
105
|
+
over: value.over
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=gateway.js.map
|
package/gateway.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.js","sources":["./gateway.ts"],"names":[],"mappings":"AAAA,SAAsB,KAAK,2BAAgC;AAC3D,SAAS,QAAQ,oBAAmB;AACpC,SAAS,KAAK,qBAAoB;AAWlC,OAAO,MAAM,qBAAqB;AAAO;AAqBzC,IAAI,WAAW,CAAC;EACd,IAAI,KAAK,IAAI,MAAM,IAAI,IAAI,GAAG;EAC9B,IAAI,CAAC,MAAM,CAAC,mBAAmB,IAAI,CAAC,KAAK;IACvC,MAAM,IAAI,aAAa;EACzB;EACA,OAAO;AACT;AACA;0DAC0D,GAC1D,OAAO,IAAI,eAAe,CAAC,IAAe;EACxC,IAAI,KAAK,SAAS;EAClB,IAAI,OAAO,CAAC,KAAe,GAAG,KAAK,CAAC,CAAC,UAAU,EAAE,IAAI;EACrD,IA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