@agentshouse/mdruntime 0.1.0-ca69b8fe1fa9f → 0.1.0-ca73fc3c2800a

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.
Files changed (352) hide show
  1. package/README.md +21 -19
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +328 -0
  5. package/dist/act-lanes.d.ts +30 -0
  6. package/dist/act-lanes.d.ts.map +1 -0
  7. package/dist/act-lanes.js +225 -0
  8. package/dist/actions.d.ts +26 -0
  9. package/dist/actions.d.ts.map +1 -0
  10. package/dist/actions.js +341 -0
  11. package/dist/adapter.d.ts +240 -97
  12. package/dist/adapter.d.ts.map +1 -1
  13. package/dist/attached-model.d.ts +3 -11
  14. package/dist/attached-model.d.ts.map +1 -1
  15. package/dist/attached-model.js +20 -60
  16. package/dist/{binary.d.ts → attachment.d.ts} +8 -4
  17. package/dist/attachment.d.ts.map +1 -0
  18. package/dist/attachment.js +15 -0
  19. package/dist/authored.d.ts +88 -0
  20. package/dist/authored.d.ts.map +1 -0
  21. package/dist/authored.js +345 -0
  22. package/dist/bootstrap.d.ts +9 -5
  23. package/dist/bootstrap.d.ts.map +1 -1
  24. package/dist/bootstrap.js +204 -86
  25. package/dist/cell.d.ts +6 -0
  26. package/dist/cell.d.ts.map +1 -0
  27. package/dist/cell.js +9 -0
  28. package/dist/chain.d.ts +1 -1
  29. package/dist/chain.d.ts.map +1 -1
  30. package/dist/chain.js +5 -2
  31. package/dist/change-set.d.ts +4 -0
  32. package/dist/change-set.d.ts.map +1 -1
  33. package/dist/change-set.js +6 -0
  34. package/dist/change.d.ts +7 -2
  35. package/dist/change.d.ts.map +1 -1
  36. package/dist/change.js +7 -2
  37. package/dist/chat-types.d.ts +3 -0
  38. package/dist/chat-types.d.ts.map +1 -0
  39. package/dist/chat-types.js +11 -0
  40. package/dist/client.d.ts +45 -24
  41. package/dist/client.d.ts.map +1 -1
  42. package/dist/client.js +58 -68
  43. package/dist/collection.d.ts +27 -4
  44. package/dist/collection.d.ts.map +1 -1
  45. package/dist/collection.js +34 -10
  46. package/dist/column.d.ts +5 -0
  47. package/dist/column.d.ts.map +1 -0
  48. package/dist/column.js +9 -0
  49. package/dist/compile.d.ts +9 -1
  50. package/dist/compile.d.ts.map +1 -1
  51. package/dist/compile.js +286 -120
  52. package/dist/connection.d.ts +6 -142
  53. package/dist/connection.d.ts.map +1 -1
  54. package/dist/connection.js +117 -5282
  55. package/dist/context.d.ts.map +1 -1
  56. package/dist/context.js +1 -1
  57. package/dist/conversation-live.d.ts +150 -0
  58. package/dist/conversation-live.d.ts.map +1 -0
  59. package/dist/conversation-live.js +260 -0
  60. package/dist/conversation-row.d.ts +55 -0
  61. package/dist/conversation-row.d.ts.map +1 -0
  62. package/dist/conversation-row.js +82 -0
  63. package/dist/conversation.d.ts +72 -47
  64. package/dist/conversation.d.ts.map +1 -1
  65. package/dist/conversation.js +1004 -901
  66. package/dist/declaration.d.ts +10 -0
  67. package/dist/declaration.d.ts.map +1 -0
  68. package/dist/declaration.js +24 -0
  69. package/dist/discovery.d.ts +12 -0
  70. package/dist/discovery.d.ts.map +1 -0
  71. package/dist/discovery.js +175 -0
  72. package/dist/download.d.ts +6 -0
  73. package/dist/download.d.ts.map +1 -0
  74. package/dist/download.js +42 -0
  75. package/dist/durability.d.ts +20 -0
  76. package/dist/durability.d.ts.map +1 -1
  77. package/dist/durability.js +7 -0
  78. package/dist/entry.d.ts +26 -1
  79. package/dist/entry.d.ts.map +1 -1
  80. package/dist/entry.js +126 -35
  81. package/dist/expectation.d.ts +26 -13
  82. package/dist/expectation.d.ts.map +1 -1
  83. package/dist/expectation.js +72 -19
  84. package/dist/failure.d.ts +3 -0
  85. package/dist/failure.d.ts.map +1 -1
  86. package/dist/failure.js +8 -0
  87. package/dist/figures.d.ts +49 -0
  88. package/dist/figures.d.ts.map +1 -0
  89. package/dist/figures.js +199 -0
  90. package/dist/file.d.ts +1 -1
  91. package/dist/file.d.ts.map +1 -1
  92. package/dist/file.js +3 -2
  93. package/dist/holdings.d.ts +29 -0
  94. package/dist/holdings.d.ts.map +1 -0
  95. package/dist/holdings.js +95 -0
  96. package/dist/host.d.ts +25 -4
  97. package/dist/host.d.ts.map +1 -1
  98. package/dist/host.js +119 -36
  99. package/dist/house-query.d.ts +24 -0
  100. package/dist/house-query.d.ts.map +1 -0
  101. package/dist/house-query.js +102 -0
  102. package/dist/identity.d.ts +1 -2
  103. package/dist/identity.d.ts.map +1 -1
  104. package/dist/identity.js +1 -4
  105. package/dist/index.d.ts +43 -16
  106. package/dist/index.d.ts.map +1 -1
  107. package/dist/index.js +21 -7
  108. package/dist/install.d.ts +60 -0
  109. package/dist/install.d.ts.map +1 -0
  110. package/dist/install.js +537 -0
  111. package/dist/lifecycle.d.ts +7 -0
  112. package/dist/lifecycle.d.ts.map +1 -0
  113. package/dist/lifecycle.js +292 -0
  114. package/dist/listing.d.ts +60 -0
  115. package/dist/listing.d.ts.map +1 -0
  116. package/dist/listing.js +168 -0
  117. package/dist/live.d.ts +22 -6
  118. package/dist/live.d.ts.map +1 -1
  119. package/dist/live.js +130 -34
  120. package/dist/loaded.d.ts +7 -0
  121. package/dist/loaded.d.ts.map +1 -0
  122. package/dist/loaded.js +46 -0
  123. package/dist/locks.d.ts +7 -0
  124. package/dist/locks.d.ts.map +1 -0
  125. package/dist/locks.js +34 -0
  126. package/dist/lost.d.ts +5 -0
  127. package/dist/lost.d.ts.map +1 -0
  128. package/dist/lost.js +49 -0
  129. package/dist/media.d.ts +20 -4
  130. package/dist/media.d.ts.map +1 -1
  131. package/dist/media.js +184 -37
  132. package/dist/memory.d.ts +12 -4
  133. package/dist/memory.d.ts.map +1 -1
  134. package/dist/memory.js +141 -41
  135. package/dist/messages.d.ts +42 -3
  136. package/dist/messages.d.ts.map +1 -1
  137. package/dist/messages.js +116 -25
  138. package/dist/modal.d.ts +2 -0
  139. package/dist/modal.d.ts.map +1 -0
  140. package/dist/modal.js +11 -0
  141. package/dist/model.d.ts +11 -15
  142. package/dist/model.d.ts.map +1 -1
  143. package/dist/model.js +6 -11
  144. package/dist/models.d.ts +10 -0
  145. package/dist/models.d.ts.map +1 -0
  146. package/dist/models.js +153 -0
  147. package/dist/name.d.ts +0 -1
  148. package/dist/name.d.ts.map +1 -1
  149. package/dist/name.js +3 -7
  150. package/dist/observation.d.ts +29 -12
  151. package/dist/observation.d.ts.map +1 -1
  152. package/dist/observation.js +217 -85
  153. package/dist/online.d.ts +7 -0
  154. package/dist/online.d.ts.map +1 -0
  155. package/dist/online.js +29 -0
  156. package/dist/opened.d.ts +6 -6
  157. package/dist/opened.d.ts.map +1 -1
  158. package/dist/opened.js +7 -7
  159. package/dist/opening.d.ts +4 -0
  160. package/dist/opening.d.ts.map +1 -0
  161. package/dist/opening.js +71 -0
  162. package/dist/outcome.d.ts +37 -14
  163. package/dist/outcome.d.ts.map +1 -1
  164. package/dist/outcome.js +39 -6
  165. package/dist/overlay.d.ts +24 -8
  166. package/dist/overlay.d.ts.map +1 -1
  167. package/dist/overlay.js +143 -48
  168. package/dist/page-metadata.d.ts +8 -0
  169. package/dist/page-metadata.d.ts.map +1 -0
  170. package/dist/page-metadata.js +19 -0
  171. package/dist/parts.d.ts +6 -0
  172. package/dist/parts.d.ts.map +1 -0
  173. package/dist/parts.js +36 -0
  174. package/dist/paths.d.ts +1 -0
  175. package/dist/paths.d.ts.map +1 -1
  176. package/dist/paths.js +22 -0
  177. package/dist/preparation.d.ts +46 -0
  178. package/dist/preparation.d.ts.map +1 -0
  179. package/dist/preparation.js +344 -0
  180. package/dist/presence.d.ts +58 -0
  181. package/dist/presence.d.ts.map +1 -0
  182. package/dist/presence.js +231 -0
  183. package/dist/queries.d.ts +16 -0
  184. package/dist/queries.d.ts.map +1 -0
  185. package/dist/queries.js +330 -0
  186. package/dist/query.d.ts +12 -4
  187. package/dist/query.d.ts.map +1 -1
  188. package/dist/questions.d.ts +30 -0
  189. package/dist/questions.d.ts.map +1 -0
  190. package/dist/questions.js +117 -0
  191. package/dist/queue.d.ts +40 -0
  192. package/dist/queue.d.ts.map +1 -0
  193. package/dist/queue.js +482 -0
  194. package/dist/reach.d.ts +20 -9
  195. package/dist/reach.d.ts.map +1 -1
  196. package/dist/reach.js +346 -127
  197. package/dist/react.d.ts +24 -2
  198. package/dist/react.d.ts.map +1 -1
  199. package/dist/react.js +33 -1
  200. package/dist/read.d.ts +7 -0
  201. package/dist/read.d.ts.map +1 -0
  202. package/dist/read.js +17 -0
  203. package/dist/readiness.d.ts +23 -5
  204. package/dist/readiness.d.ts.map +1 -1
  205. package/dist/readiness.js +18 -10
  206. package/dist/reads.d.ts +39 -0
  207. package/dist/reads.d.ts.map +1 -0
  208. package/dist/reads.js +189 -0
  209. package/dist/recovery.d.ts +4 -1
  210. package/dist/recovery.d.ts.map +1 -1
  211. package/dist/recovery.js +40 -9
  212. package/dist/refresh.d.ts +1 -1
  213. package/dist/refresh.d.ts.map +1 -1
  214. package/dist/refresh.js +14 -26
  215. package/dist/refusal.d.ts +5 -0
  216. package/dist/refusal.d.ts.map +1 -0
  217. package/dist/refusal.js +55 -0
  218. package/dist/replica.d.ts +11 -22
  219. package/dist/replica.d.ts.map +1 -1
  220. package/dist/replica.js +68 -38
  221. package/dist/retried.d.ts +30 -0
  222. package/dist/retried.d.ts.map +1 -0
  223. package/dist/retried.js +72 -0
  224. package/dist/room-draft.d.ts +33 -10
  225. package/dist/room-draft.d.ts.map +1 -1
  226. package/dist/room-draft.js +282 -72
  227. package/dist/room-drafts.d.ts +24 -0
  228. package/dist/room-drafts.d.ts.map +1 -0
  229. package/dist/room-drafts.js +222 -0
  230. package/dist/room-list.d.ts +10 -0
  231. package/dist/room-list.d.ts.map +1 -0
  232. package/dist/room-list.js +47 -0
  233. package/dist/rows.d.ts +24 -7
  234. package/dist/rows.d.ts.map +1 -1
  235. package/dist/rows.js +66 -61
  236. package/dist/run-tables.d.ts +25 -0
  237. package/dist/run-tables.d.ts.map +1 -0
  238. package/dist/run-tables.js +98 -0
  239. package/dist/running.d.ts +5 -0
  240. package/dist/running.d.ts.map +1 -0
  241. package/dist/running.js +10 -0
  242. package/dist/runs.d.ts +27 -0
  243. package/dist/runs.d.ts.map +1 -0
  244. package/dist/runs.js +92 -0
  245. package/dist/runtime.d.ts +37 -0
  246. package/dist/runtime.d.ts.map +1 -0
  247. package/dist/runtime.js +1 -0
  248. package/dist/save.d.ts +43 -86
  249. package/dist/save.d.ts.map +1 -1
  250. package/dist/save.js +332 -228
  251. package/dist/saves.d.ts +34 -0
  252. package/dist/saves.d.ts.map +1 -0
  253. package/dist/saves.js +464 -0
  254. package/dist/seed.d.ts +11 -2
  255. package/dist/seed.d.ts.map +1 -1
  256. package/dist/seed.js +71 -1
  257. package/dist/selector.d.ts +3 -0
  258. package/dist/selector.d.ts.map +1 -0
  259. package/dist/selector.js +22 -0
  260. package/dist/shared.d.ts +1 -6
  261. package/dist/shared.d.ts.map +1 -1
  262. package/dist/shared.js +13 -95
  263. package/dist/shown.d.ts +35 -0
  264. package/dist/shown.d.ts.map +1 -0
  265. package/dist/shown.js +327 -0
  266. package/dist/socket/admission.d.ts +7 -0
  267. package/dist/socket/admission.d.ts.map +1 -0
  268. package/dist/socket/admission.js +39 -0
  269. package/dist/socket/answers.d.ts +6 -0
  270. package/dist/socket/answers.d.ts.map +1 -0
  271. package/dist/socket/answers.js +20 -0
  272. package/dist/socket/lanes.d.ts +28 -0
  273. package/dist/socket/lanes.d.ts.map +1 -0
  274. package/dist/socket/lanes.js +103 -0
  275. package/dist/socket/reads.d.ts +10 -0
  276. package/dist/socket/reads.d.ts.map +1 -0
  277. package/dist/socket/reads.js +31 -0
  278. package/dist/socket/session.d.ts +41 -0
  279. package/dist/socket/session.d.ts.map +1 -0
  280. package/dist/socket/session.js +26 -0
  281. package/dist/socket.d.ts +70 -0
  282. package/dist/socket.d.ts.map +1 -0
  283. package/dist/socket.js +172 -0
  284. package/dist/source.d.ts +96 -0
  285. package/dist/source.d.ts.map +1 -0
  286. package/dist/source.js +149 -0
  287. package/dist/status.d.ts +0 -2
  288. package/dist/status.d.ts.map +1 -1
  289. package/dist/status.js +0 -6
  290. package/dist/store/installs.d.ts +30 -0
  291. package/dist/store/installs.d.ts.map +1 -0
  292. package/dist/store/installs.js +40 -0
  293. package/dist/store/readiness.d.ts +11 -0
  294. package/dist/store/readiness.d.ts.map +1 -0
  295. package/dist/store/readiness.js +68 -0
  296. package/dist/store/records.d.ts +234 -0
  297. package/dist/store/records.d.ts.map +1 -0
  298. package/dist/store/records.js +474 -0
  299. package/dist/store/room-held.d.ts +8 -0
  300. package/dist/store/room-held.d.ts.map +1 -0
  301. package/dist/store/room-held.js +17 -0
  302. package/dist/store/rooms-shown.d.ts +8 -0
  303. package/dist/store/rooms-shown.d.ts.map +1 -0
  304. package/dist/store/rooms-shown.js +13 -0
  305. package/dist/store/space.d.ts +11 -0
  306. package/dist/store/space.d.ts.map +1 -0
  307. package/dist/store/space.js +41 -0
  308. package/dist/store/watchers.d.ts +17 -0
  309. package/dist/store/watchers.d.ts.map +1 -0
  310. package/dist/store/watchers.js +56 -0
  311. package/dist/store/writes.d.ts +24 -0
  312. package/dist/store/writes.d.ts.map +1 -0
  313. package/dist/store/writes.js +125 -0
  314. package/dist/store.d.ts +116 -152
  315. package/dist/store.d.ts.map +1 -1
  316. package/dist/store.js +174 -395
  317. package/dist/stored.d.ts +89 -70
  318. package/dist/stored.d.ts.map +1 -1
  319. package/dist/stored.js +438 -174
  320. package/dist/surface.d.ts +100 -0
  321. package/dist/surface.d.ts.map +1 -0
  322. package/dist/surface.js +258 -0
  323. package/dist/sync.d.ts +21 -0
  324. package/dist/sync.d.ts.map +1 -0
  325. package/dist/sync.js +557 -0
  326. package/dist/tables.d.ts +134 -87
  327. package/dist/tables.d.ts.map +1 -1
  328. package/dist/tables.js +215 -283
  329. package/dist/tallies.d.ts +18 -0
  330. package/dist/tallies.d.ts.map +1 -0
  331. package/dist/tallies.js +128 -0
  332. package/dist/validation.d.ts +2 -0
  333. package/dist/validation.d.ts.map +1 -1
  334. package/dist/validation.js +53 -13
  335. package/dist/wire.d.ts +87 -12
  336. package/dist/wire.d.ts.map +1 -1
  337. package/dist/wire.js +37 -14
  338. package/package.json +4 -3
  339. package/dist/attempt.d.ts +0 -16
  340. package/dist/attempt.d.ts.map +0 -1
  341. package/dist/attempt.js +0 -70
  342. package/dist/binary.d.ts.map +0 -1
  343. package/dist/binary.js +0 -8
  344. package/dist/fence.d.ts +0 -6
  345. package/dist/fence.d.ts.map +0 -1
  346. package/dist/fence.js +0 -10
  347. package/dist/job.d.ts +0 -14
  348. package/dist/job.d.ts.map +0 -1
  349. package/dist/job.js +0 -28
  350. package/dist/log.d.ts +0 -5
  351. package/dist/log.d.ts.map +0 -1
  352. package/dist/log.js +0 -14
@@ -6,49 +6,198 @@ export function rowsOf(result) {
6
6
  export function whole(message) {
7
7
  return 'result' in message;
8
8
  }
9
- export function applied(keyed, message) {
10
- if (whole(message))
11
- return message;
12
- const result = keyed.result;
13
- if ('pending' in result)
14
- return keyed;
15
- const rows = rowsOf(result);
16
- const byKey = new Map(keyed.keys.map((key, index) => [key, rows[index]]));
17
- for (const key of message.removed)
18
- byKey.delete(key);
19
- for (const [key, row] of message.changed)
20
- byKey.set(key, row);
21
- const keys = message.order ?? keyed.keys.filter((key) => byKey.has(key));
22
- const kept = keys.map((key) => byKey.get(key));
23
- const coverage = message.coverage ?? result.coverage;
24
- const next = message.next === undefined ? result.next : (message.next ?? undefined);
25
- const total = message.total ?? result.total;
26
- const window = {
27
- ...(next === undefined ? {} : { next }),
28
- ...(total === undefined ? {} : { total }),
9
+ export function pendingChange(message) {
10
+ return 'pending' in message;
11
+ }
12
+ export function unchanged(change) {
13
+ return change.changed.length === 0 && change.removed.length === 0 && change.order === null;
14
+ }
15
+ export function keyedChange(held, keys, rows, same) {
16
+ const present = new Set(keys);
17
+ const changed = keys.flatMap((key, at) => {
18
+ const was = held.get(key);
19
+ return was !== undefined && same(was, rows[at]) ? [] : [[key, rows[at]]];
20
+ });
21
+ const removed = [];
22
+ const kept = [];
23
+ for (const key of held.keys())
24
+ (present.has(key) ? kept : removed).push(key);
25
+ const moved = kept.length !== keys.length || kept.some((key, at) => keys[at] !== key);
26
+ return { changed, removed, order: moved ? keys : null };
27
+ }
28
+ export function keyedAt(held, at, same) {
29
+ const changed = [];
30
+ const removed = [];
31
+ for (const [key, now] of at) {
32
+ const was = held.get(key);
33
+ if (was === undefined) {
34
+ if (now !== null)
35
+ return null;
36
+ }
37
+ else if (now === null)
38
+ removed.push(key);
39
+ else if (!same(was, now))
40
+ changed.push([key, now]);
41
+ }
42
+ return { changed, removed, order: null };
43
+ }
44
+ export function keyedInto(held, change) {
45
+ for (const key of change.removed)
46
+ held.delete(key);
47
+ for (const [key, row] of change.changed)
48
+ held.set(key, row);
49
+ if (change.order === null)
50
+ return;
51
+ const placed = change.order.map((key) => [key, held.get(key)]);
52
+ held.clear();
53
+ for (const [key, row] of placed)
54
+ held.set(key, row);
55
+ }
56
+ export function samePending(was, now) {
57
+ return was.awaits === now.awaits;
58
+ }
59
+ function keyedRows(keys, rows) {
60
+ const held = new Map(keys.map((key, at) => [key, rows[at]]));
61
+ let shown = rows;
62
+ return {
63
+ rows: () => shown,
64
+ apply(change) {
65
+ if (unchanged(change))
66
+ return shown;
67
+ keyedInto(held, change);
68
+ shown = [...held.values()];
69
+ return shown;
70
+ },
71
+ };
72
+ }
73
+ function heldModels(held, sent) {
74
+ return new Map(Object.entries(sent).map(([key, types]) => [key, held.get(key) ?? types]));
75
+ }
76
+ function namedModels(models, rooms) {
77
+ const named = new Set(rooms.flatMap(({ model, typedSelection }) => [model.types, typedSelection?.model.types]));
78
+ return new Map([...models].filter(([, types]) => named.has(types)));
79
+ }
80
+ function typed(model, models) {
81
+ return model.types === null ? model : { ...model, types: models.get(model.types) };
82
+ }
83
+ function appRoom({ model, typedSelection, ...room }, models) {
84
+ return {
85
+ ...room,
86
+ model: typed(model, models),
87
+ typedSelection: typedSelection === null
88
+ ? null
89
+ : { roots: typedSelection.roots, model: typed(typedSelection.model, models) },
90
+ };
91
+ }
92
+ export function appRooms({ rooms, models }) {
93
+ const held = heldModels(new Map(), models);
94
+ return rooms.map((room) => appRoom(room, held));
95
+ }
96
+ function keyedRooms(first) {
97
+ let models = new Map();
98
+ let held = keyedRows([], []);
99
+ const whole = (listed) => {
100
+ models = heldModels(models, listed.models);
101
+ held = keyedRows(listed.rooms.map(({ locator }) => locator), listed.rooms.map((room) => appRoom(room, models)));
102
+ return held.rows();
29
103
  };
30
104
  return {
31
- result: 'groups' in result
32
- ? { groups: kept, coverage, ...window }
33
- : { documents: kept, coverage, ...window },
34
- keys,
105
+ value: whole(first),
106
+ next(message) {
107
+ if ('rooms' in message)
108
+ return whole(message);
109
+ const change = message;
110
+ const sent = heldModels(models, change.models);
111
+ const rows = held.apply({
112
+ changed: change.changed.map(([locator, room]) => [locator, appRoom(room, sent)]),
113
+ removed: change.removed,
114
+ order: change.order,
115
+ });
116
+ models = namedModels(new Map([...models, ...sent]), rows);
117
+ return rows;
118
+ },
35
119
  };
36
120
  }
121
+ function covered(readiness) {
122
+ return keyedRows(readiness.coverage.rooms.map(({ room }) => room), readiness.coverage.rooms);
123
+ }
124
+ function keyedReadiness(first) {
125
+ let held = covered(first);
126
+ return {
127
+ value: first,
128
+ next(message) {
129
+ if (!('shown' in message)) {
130
+ held = covered(message);
131
+ return message;
132
+ }
133
+ const { shown, rooms } = message;
134
+ return { ...shown, coverage: { ...shown.coverage, rooms: held.apply(rooms) } };
135
+ },
136
+ };
137
+ }
138
+ function awaiting(pending) {
139
+ return keyedRows(pending.map(({ room }) => room), pending);
140
+ }
37
141
  function keyedResult(first) {
38
- let keyed = first;
142
+ let result = first.result;
143
+ let rows = keyedRows([], []);
144
+ let pending = awaiting([]);
145
+ const take = ({ result: next, keys }) => {
146
+ result = next;
147
+ if ('pending' in next)
148
+ pending = awaiting(next.pending);
149
+ else
150
+ rows = keyedRows(keys, rowsOf(next));
151
+ return result;
152
+ };
153
+ take(first);
39
154
  return {
40
155
  value: first.result,
41
156
  next(message) {
42
- keyed = applied(keyed, message);
43
- return keyed.result;
157
+ const asked = message;
158
+ if (whole(asked))
159
+ return take(asked);
160
+ if (pendingChange(asked)) {
161
+ if ('pending' in result)
162
+ result = { pending: pending.apply(asked.pending) };
163
+ return result;
164
+ }
165
+ if ('pending' in result)
166
+ return result;
167
+ const kept = rows.apply(asked);
168
+ const coverage = asked.coverage ?? result.coverage;
169
+ const next = asked.next === undefined ? result.next : (asked.next ?? undefined);
170
+ const total = asked.total ?? result.total;
171
+ const window = {
172
+ ...(next === undefined ? {} : { next }),
173
+ ...(total === undefined ? {} : { total }),
174
+ };
175
+ result =
176
+ 'groups' in result
177
+ ? { groups: kept, coverage, ...window }
178
+ : { documents: kept, coverage, ...window };
179
+ return result;
44
180
  },
45
181
  };
46
182
  }
183
+ const FOLDS = {
184
+ query: (first) => keyedResult(first),
185
+ rooms: (first) => keyedRooms(first),
186
+ readiness: (first) => keyedReadiness(first),
187
+ };
188
+ const WHOLE = {
189
+ query: (message) => whole(message),
190
+ rooms: (message) => 'rooms' in message,
191
+ readiness: (message) => !('shown' in message),
192
+ };
193
+ export function replaces(topic, message) {
194
+ return WHOLE[topic]?.(message) ?? true;
195
+ }
47
196
  function replaced(first) {
48
197
  return { value: first, next: (message) => message };
49
198
  }
50
- export function observed(first, stop, keyed) {
51
- const reducer = keyed ? keyedResult(first) : replaced(first);
199
+ export function observed(first, stop, topic) {
200
+ const reducer = (FOLDS[topic] ?? replaced)(first);
52
201
  let value = reducer.value;
53
202
  let open = true;
54
203
  const listeners = new Set();
@@ -86,34 +235,29 @@ export function observed(first, stop, keyed) {
86
235
  end,
87
236
  };
88
237
  }
89
- export function opened(watch, keyed) {
238
+ export function opened(watch, topic) {
90
239
  return new Promise((resolve, reject) => {
91
240
  let handle;
92
241
  let stop = () => { };
93
- try {
94
- stop = watch((message) => {
95
- if (handle !== undefined) {
96
- handle.receive(message);
97
- return;
98
- }
99
- handle = observed(message, () => stop(), keyed);
100
- resolve(handle.observation);
101
- }, (error) => {
102
- if (handle !== undefined)
103
- return;
104
- stop();
105
- reject(error);
106
- });
107
- }
108
- catch (error) {
242
+ stop = watch((message) => {
243
+ if (handle !== undefined) {
244
+ handle.receive(message);
245
+ return;
246
+ }
247
+ handle = observed(message, () => stop(), topic);
248
+ resolve(handle.observation);
249
+ }, (error) => {
250
+ if (handle !== undefined)
251
+ return;
252
+ stop();
109
253
  reject(error);
110
- }
254
+ });
111
255
  });
112
256
  }
113
257
  export function observing(watch) {
114
258
  return (topic, ...input) => opened((deliver, fail) => watch(topic, input, (message) => {
115
259
  deliver(structuredClone(message));
116
- }, fail), topic === 'query');
260
+ }, fail), topic);
117
261
  }
118
262
  export function hashed(text) {
119
263
  let first = 0xdeadbeef;
@@ -134,10 +278,9 @@ function rowDigest(row) {
134
278
  }
135
279
  export function digestOf({ result, keys }) {
136
280
  if ('pending' in result)
137
- return { pending: JSON.stringify(result) };
281
+ return { pending: new Map(result.pending.map((room) => [room.room, room])) };
138
282
  const rows = rowsOf(result);
139
283
  return {
140
- keys,
141
284
  rows: new Map(keys.map((key, index) => [key, rowDigest(rows[index])])),
142
285
  coverage: JSON.stringify(result.coverage),
143
286
  next: result.next,
@@ -145,16 +288,22 @@ export function digestOf({ result, keys }) {
145
288
  };
146
289
  }
147
290
  export function digestApplied(digest, message) {
148
- if (whole(message) || 'pending' in digest)
149
- return whole(message) ? digestOf(message) : digest;
150
- const rows = new Map(digest.rows);
151
- for (const key of message.removed)
152
- rows.delete(key);
153
- for (const [key, row] of message.changed)
154
- rows.set(key, rowDigest(row));
291
+ if (whole(message))
292
+ return digestOf(message);
293
+ if (pendingChange(message)) {
294
+ if ('pending' in digest)
295
+ keyedInto(digest.pending, message.pending);
296
+ return digest;
297
+ }
298
+ if ('pending' in digest)
299
+ return digest;
300
+ keyedInto(digest.rows, {
301
+ changed: message.changed.map(([key, row]) => [key, rowDigest(row)]),
302
+ removed: message.removed,
303
+ order: message.order,
304
+ });
155
305
  return {
156
- keys: message.order ?? digest.keys.filter((key) => rows.has(key)),
157
- rows,
306
+ rows: digest.rows,
158
307
  coverage: message.coverage === undefined ? digest.coverage : JSON.stringify(message.coverage),
159
308
  next: message.next === undefined ? digest.next : (message.next ?? undefined),
160
309
  total: message.total ?? digest.total,
@@ -162,35 +311,18 @@ export function digestApplied(digest, message) {
162
311
  }
163
312
  export function changeOf(previous, next) {
164
313
  const after = next.result;
165
- if ('pending' in previous || 'pending' in after) {
166
- return 'pending' in previous && 'pending' in after && previous.pending === JSON.stringify(after)
167
- ? null
168
- : next;
314
+ if ('pending' in previous && 'pending' in after) {
315
+ const pending = keyedChange(previous.pending, after.pending.map(({ room }) => room), after.pending, samePending);
316
+ return unchanged(pending) ? null : { pending };
169
317
  }
170
- const later = rowsOf(after);
171
- const present = new Set(next.keys);
172
- const changed = [];
173
- next.keys.forEach((key, index) => {
174
- const row = later[index];
175
- if (previous.rows.get(key) !== rowDigest(row))
176
- changed.push([key, row]);
177
- });
178
- const removed = previous.keys.filter((key) => !present.has(key));
179
- const kept = previous.keys.filter((key) => present.has(key));
180
- const moved = kept.length !== next.keys.length || kept.some((key, index) => next.keys[index] !== key);
181
- const coverage = JSON.stringify(after.coverage);
182
- const covered = previous.coverage !== coverage;
318
+ if ('pending' in previous || 'pending' in after)
319
+ return next;
320
+ const rows = keyedChange(previous.rows, next.keys, rowsOf(after), (was, row) => was === rowDigest(row));
321
+ const covered = previous.coverage !== JSON.stringify(after.coverage);
183
322
  const framing = reframed(previous, after);
184
- if (changed.length === 0 && removed.length === 0 && !moved && !covered && framing === null) {
323
+ if (unchanged(rows) && !covered && framing === null)
185
324
  return null;
186
- }
187
- return {
188
- changed,
189
- removed,
190
- order: moved ? next.keys : null,
191
- ...(covered ? { coverage: after.coverage } : {}),
192
- ...framing,
193
- };
325
+ return { ...rows, ...(covered ? { coverage: after.coverage } : {}), ...framing };
194
326
  }
195
327
  function reframed(before, after) {
196
328
  const next = before.next === after.next ? {} : { next: after.next ?? null };
@@ -0,0 +1,7 @@
1
+ import type { Authorization } from './adapter.js';
2
+ import type { Runtime } from './runtime.js';
3
+ export declare function offline(runtime: Runtime): Promise<boolean>;
4
+ export declare function askedOnline<T extends {
5
+ readonly kind: string;
6
+ }>(runtime: Runtime, ask: (authorization: Authorization) => Promise<T>): Promise<T | null>;
7
+ //# sourceMappingURL=online.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"online.d.ts","sourceRoot":"","sources":["../src/online.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAGlD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C,wBAAsB,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAOhE;AAED,wBAAsB,WAAW,CAAC,CAAC,SAAS;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EACnE,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,CAAC,aAAa,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,GAChD,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAgBnB"}
package/dist/online.js ADDED
@@ -0,0 +1,29 @@
1
+ import { connectionNow } from './preparation.js';
2
+ import { refuse } from './refusal.js';
3
+ export async function offline(runtime) {
4
+ const { socket } = runtime;
5
+ return (socket.closed() ||
6
+ socket.session.now().refusal !== null ||
7
+ (await connectionNow(runtime)) !== 'connected');
8
+ }
9
+ export async function askedOnline(runtime, ask) {
10
+ const { socket } = runtime;
11
+ try {
12
+ await socket.authorize();
13
+ }
14
+ catch {
15
+ return null;
16
+ }
17
+ const { closed, authorization, refusal } = socket.session.now();
18
+ if (closed || authorization === undefined || refusal !== null)
19
+ return null;
20
+ try {
21
+ const answer = await ask(authorization);
22
+ if (answer.kind === 'revoked-authorization')
23
+ await refuse(runtime, 'revoked-authorization');
24
+ return answer;
25
+ }
26
+ catch {
27
+ return null;
28
+ }
29
+ }
package/dist/opened.d.ts CHANGED
@@ -1,22 +1,22 @@
1
- import type { BinaryResult } from './adapter.js';
1
+ import type { AttachmentResult } from './adapter.js';
2
2
  import { type RuntimeDatabase } from './tables.js';
3
3
  export type OpenedName = {
4
4
  readonly locator: string;
5
5
  readonly path: string;
6
6
  readonly revision: string;
7
7
  };
8
- export type KeptBinary = {
8
+ export type KeptAttachment = {
9
9
  readonly kind: 'binary';
10
10
  readonly file: string;
11
11
  readonly byteLength: number;
12
12
  readonly mediaType: string;
13
13
  };
14
- export type KeptResult = KeptBinary | Exclude<BinaryResult, {
14
+ export type KeptResult = KeptAttachment | Exclude<AttachmentResult, {
15
15
  kind: 'binary';
16
16
  }>;
17
17
  export type OpenedFiles = {
18
- find(name: OpenedName): Promise<KeptBinary | null>;
19
- fetch(name: OpenedName, read: () => Promise<BinaryResult>): Promise<KeptResult>;
18
+ find(name: OpenedName): Promise<KeptAttachment | null>;
19
+ fetch(name: OpenedName, read: () => Promise<AttachmentResult>): Promise<KeptResult>;
20
20
  };
21
- export declare function openedFiles(database: RuntimeDatabase, fenced: (locator: string) => boolean): OpenedFiles;
21
+ export declare function openedFiles(database: RuntimeDatabase, lost: (locator: string) => boolean): OpenedFiles;
22
22
  //# sourceMappingURL=opened.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"opened.d.ts","sourceRoot":"","sources":["../src/opened.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAGjD,OAAO,EAAoC,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAErF,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,UAAU,GAAG,OAAO,CAAC,YAAY,EAAE;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,CAAC,CAAC;AAEhF,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC;IACnD,KAAK,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,YAAY,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CACjF,CAAC;AAsBF,wBAAgB,WAAW,CACzB,QAAQ,EAAE,eAAe,EACzB,MAAM,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,GACnC,WAAW,CAsFb"}
1
+ {"version":3,"file":"opened.d.ts","sourceRoot":"","sources":["../src/opened.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGrD,OAAO,EAAoC,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAErF,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,cAAc,GAAG,OAAO,CAAC,gBAAgB,EAAE;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,CAAC,CAAC;AAExF,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;IACvD,KAAK,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,gBAAgB,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CACrF,CAAC;AAsBF,wBAAgB,WAAW,CACzB,QAAQ,EAAE,eAAe,EACzB,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,GACjC,WAAW,CAsFb"}
package/dist/opened.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { digest } from './digest.js';
2
- import { binaryLocalId } from './identity.js';
2
+ import { attachmentLocalId } from './identity.js';
3
3
  import { FILE_PART_BYTES } from './tables.js';
4
4
  const UNCERTAIN = { kind: 'transport-uncertainty' };
5
5
  function kept({ key, byteLength, mediaType }) {
@@ -20,14 +20,14 @@ function taken(chunks, size) {
20
20
  }
21
21
  return part;
22
22
  }
23
- export function openedFiles(database, fenced) {
23
+ export function openedFiles(database, lost) {
24
24
  const fetching = new Map();
25
25
  async function find(name) {
26
26
  const held = await database.ask({ kind: 'opened.find', ...name });
27
27
  return held === null ? null : kept(held);
28
28
  }
29
29
  async function written(name, { body, mediaType, sha256 }) {
30
- const key = binaryLocalId();
30
+ const key = attachmentLocalId();
31
31
  const reader = body.getReader();
32
32
  const hashed = digest();
33
33
  const chunks = [];
@@ -36,14 +36,14 @@ export function openedFiles(database, fenced) {
36
36
  let part = 0;
37
37
  const write = async (bytes) => {
38
38
  if (!(await database.ask({ kind: 'opened.part', key, part, bytes }))) {
39
- throw new Error('the device no longer keeps this download');
39
+ throw new Error();
40
40
  }
41
41
  part += 1;
42
42
  };
43
43
  try {
44
- if (fenced(name.locator) ||
44
+ if (lost(name.locator) ||
45
45
  !(await database.ask({ kind: 'opened.begin', key, ...name, mediaType }))) {
46
- throw new Error('the device no longer reads this Room');
46
+ throw new Error();
47
47
  }
48
48
  for (;;) {
49
49
  const read = await reader.read();
@@ -61,7 +61,7 @@ export function openedFiles(database, fenced) {
61
61
  await write(taken(chunks, held));
62
62
  const received = hashed.hex();
63
63
  if (sha256 !== undefined && sha256 !== received) {
64
- throw new Error('the bytes are not the file the device asked for');
64
+ throw new Error();
65
65
  }
66
66
  const whole = await database.ask({
67
67
  kind: 'opened.keep',
@@ -0,0 +1,4 @@
1
+ import type { Runtime } from './runtime.js';
2
+ import type { RuntimeStore } from './store.js';
3
+ export declare function open(runtime: Runtime, opened: RuntimeStore): Promise<RuntimeStore>;
4
+ //# sourceMappingURL=opening.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opening.d.ts","sourceRoot":"","sources":["../src/opening.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,wBAAsB,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAuCxF"}
@@ -0,0 +1,71 @@
1
+ import { conversationsOpened } from './conversation.js';
2
+ import { detectStorageLoss, nextGeneration, refusedForSpace } from './durability.js';
3
+ import { uncaught } from './failure.js';
4
+ import { sweep } from './install.js';
5
+ import { eraseFenced } from './lost.js';
6
+ import { prepare } from './preparation.js';
7
+ import { unpair } from './queue.js';
8
+ import { runsOpened } from './runs.js';
9
+ import { emitRooms, emitStorageLoss, statusShown } from './shown.js';
10
+ export async function open(runtime, opened) {
11
+ const { store, socket, shell, conversations, build } = runtime;
12
+ if (socket.closed())
13
+ throw new Error('runtime closed');
14
+ const interrupted = await opened.fences();
15
+ const kept = shell.generation;
16
+ const storedGeneration = await opened.generation();
17
+ const loss = detectStorageLoss(storedGeneration, kept);
18
+ const generation = nextGeneration(storedGeneration, kept);
19
+ if (generation !== storedGeneration)
20
+ await opened.rememberGeneration(generation);
21
+ if (generation !== kept)
22
+ await shell.keepGeneration(generation);
23
+ if (loss !== null) {
24
+ store.device.set({ storageLoss: loss });
25
+ emitStorageLoss(runtime);
26
+ }
27
+ const held = await opened.opened();
28
+ store.device.set({
29
+ completedWhenOpened: (await opened.facts(['lastCompleted'])).lastCompleted !== null,
30
+ });
31
+ try {
32
+ await opened.demote();
33
+ await store.ask({ kind: 'read.builds', build });
34
+ for (const key of held.orphans)
35
+ await opened.release(key);
36
+ }
37
+ catch (error) {
38
+ if (!refusedForSpace(error))
39
+ throw error;
40
+ }
41
+ if (held.erasing || held.orphans.length > 0)
42
+ queueMicrotask(() => sweep(runtime));
43
+ await conversationsOpened(conversations, opened);
44
+ await runsOpened(runtime);
45
+ if (interrupted.length > 0) {
46
+ queueMicrotask(() => {
47
+ void eraseFenced(runtime, interrupted).catch(uncaught);
48
+ });
49
+ }
50
+ await factsKept(runtime, opened);
51
+ statusShown(runtime);
52
+ prepare(runtime);
53
+ emitRooms(runtime);
54
+ void unpair(runtime).catch(uncaught);
55
+ return opened;
56
+ }
57
+ async function factsKept({ socket, adopt }, opened) {
58
+ const { session } = socket;
59
+ const { unstored } = session.now();
60
+ if (unstored !== null) {
61
+ queueMicrotask(() => {
62
+ void adopt(unstored).catch(uncaught);
63
+ });
64
+ return;
65
+ }
66
+ const { capabilities, queriesOnly } = await opened.facts(['capabilities', 'queriesOnly']);
67
+ if (queriesOnly.length > 0 && !session.now().roomsNamed)
68
+ session.set({ queriesOnly });
69
+ if (capabilities.length > 0)
70
+ socket.readyFromStore();
71
+ }