@agentshouse/mdruntime 0.1.0-c44866799613e → 0.1.0-c44d5cc00aee5

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 (314) hide show
  1. package/README.md +18 -17
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +299 -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 +302 -0
  11. package/dist/adapter.d.ts +110 -112
  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 +28 -51
  16. package/dist/bootstrap.d.ts +15 -18
  17. package/dist/bootstrap.d.ts.map +1 -1
  18. package/dist/bootstrap.js +252 -158
  19. package/dist/cell.d.ts +6 -0
  20. package/dist/cell.d.ts.map +1 -0
  21. package/dist/cell.js +9 -0
  22. package/dist/chain.d.ts +2 -1
  23. package/dist/chain.d.ts.map +1 -1
  24. package/dist/change-set.d.ts +4 -0
  25. package/dist/change-set.d.ts.map +1 -1
  26. package/dist/change-set.js +6 -0
  27. package/dist/change.d.ts +1 -0
  28. package/dist/change.d.ts.map +1 -1
  29. package/dist/change.js +4 -2
  30. package/dist/chat-types.d.ts +3 -0
  31. package/dist/chat-types.d.ts.map +1 -0
  32. package/dist/chat-types.js +11 -0
  33. package/dist/client.d.ts +27 -21
  34. package/dist/client.d.ts.map +1 -1
  35. package/dist/client.js +36 -17
  36. package/dist/collection.d.ts +23 -7
  37. package/dist/collection.d.ts.map +1 -1
  38. package/dist/collection.js +13 -19
  39. package/dist/compile.d.ts +8 -6
  40. package/dist/compile.d.ts.map +1 -1
  41. package/dist/compile.js +288 -329
  42. package/dist/connection.d.ts +5 -143
  43. package/dist/connection.d.ts.map +1 -1
  44. package/dist/connection.js +109 -5104
  45. package/dist/context.d.ts +2 -0
  46. package/dist/context.d.ts.map +1 -0
  47. package/dist/context.js +7 -0
  48. package/dist/conversation-live.d.ts +143 -0
  49. package/dist/conversation-live.d.ts.map +1 -0
  50. package/dist/conversation-live.js +254 -0
  51. package/dist/conversation-row.d.ts +33 -0
  52. package/dist/conversation-row.d.ts.map +1 -0
  53. package/dist/conversation-row.js +53 -0
  54. package/dist/conversation.d.ts +65 -48
  55. package/dist/conversation.d.ts.map +1 -1
  56. package/dist/conversation.js +987 -838
  57. package/dist/declaration.d.ts +10 -0
  58. package/dist/declaration.d.ts.map +1 -0
  59. package/dist/declaration.js +24 -0
  60. package/dist/discovery.d.ts +10 -0
  61. package/dist/discovery.d.ts.map +1 -0
  62. package/dist/discovery.js +150 -0
  63. package/dist/durability.d.ts +19 -1
  64. package/dist/durability.d.ts.map +1 -1
  65. package/dist/durability.js +4 -0
  66. package/dist/entry.d.ts +3 -2
  67. package/dist/entry.d.ts.map +1 -1
  68. package/dist/entry.js +19 -9
  69. package/dist/expectation.d.ts +26 -13
  70. package/dist/expectation.d.ts.map +1 -1
  71. package/dist/expectation.js +72 -19
  72. package/dist/failure.d.ts +3 -0
  73. package/dist/failure.d.ts.map +1 -1
  74. package/dist/failure.js +6 -0
  75. package/dist/figures.d.ts +49 -0
  76. package/dist/figures.d.ts.map +1 -0
  77. package/dist/figures.js +197 -0
  78. package/dist/holdings.d.ts +29 -0
  79. package/dist/holdings.d.ts.map +1 -0
  80. package/dist/holdings.js +95 -0
  81. package/dist/host.d.ts +21 -8
  82. package/dist/host.d.ts.map +1 -1
  83. package/dist/host.js +73 -40
  84. package/dist/identity.d.ts +0 -1
  85. package/dist/identity.d.ts.map +1 -1
  86. package/dist/identity.js +0 -3
  87. package/dist/index.d.ts +34 -12
  88. package/dist/index.d.ts.map +1 -1
  89. package/dist/index.js +16 -5
  90. package/dist/install.d.ts +54 -0
  91. package/dist/install.d.ts.map +1 -0
  92. package/dist/install.js +530 -0
  93. package/dist/lifecycle.d.ts +7 -0
  94. package/dist/lifecycle.d.ts.map +1 -0
  95. package/dist/lifecycle.js +277 -0
  96. package/dist/listing.d.ts +48 -0
  97. package/dist/listing.d.ts.map +1 -0
  98. package/dist/listing.js +139 -0
  99. package/dist/live.d.ts +5 -4
  100. package/dist/live.d.ts.map +1 -1
  101. package/dist/live.js +64 -23
  102. package/dist/locks.d.ts +7 -0
  103. package/dist/locks.d.ts.map +1 -0
  104. package/dist/locks.js +32 -0
  105. package/dist/lost.d.ts +5 -0
  106. package/dist/lost.d.ts.map +1 -0
  107. package/dist/lost.js +49 -0
  108. package/dist/media.d.ts +2 -2
  109. package/dist/media.d.ts.map +1 -1
  110. package/dist/media.js +23 -13
  111. package/dist/memory.d.ts +17 -7
  112. package/dist/memory.d.ts.map +1 -1
  113. package/dist/memory.js +210 -97
  114. package/dist/messages.d.ts +33 -5
  115. package/dist/messages.d.ts.map +1 -1
  116. package/dist/messages.js +104 -36
  117. package/dist/model.d.ts +2 -11
  118. package/dist/model.d.ts.map +1 -1
  119. package/dist/model.js +0 -23
  120. package/dist/models.d.ts +10 -0
  121. package/dist/models.d.ts.map +1 -0
  122. package/dist/models.js +153 -0
  123. package/dist/observation.d.ts +4 -3
  124. package/dist/observation.d.ts.map +1 -1
  125. package/dist/observation.js +23 -17
  126. package/dist/opened.d.ts +1 -1
  127. package/dist/opened.d.ts.map +1 -1
  128. package/dist/opened.js +5 -5
  129. package/dist/opening.d.ts +4 -0
  130. package/dist/opening.d.ts.map +1 -0
  131. package/dist/opening.js +53 -0
  132. package/dist/outcome.d.ts +7 -4
  133. package/dist/outcome.d.ts.map +1 -1
  134. package/dist/outcome.js +18 -0
  135. package/dist/overlay.d.ts +6 -1
  136. package/dist/overlay.d.ts.map +1 -1
  137. package/dist/overlay.js +47 -21
  138. package/dist/page-metadata.d.ts +8 -0
  139. package/dist/page-metadata.d.ts.map +1 -0
  140. package/dist/page-metadata.js +19 -0
  141. package/dist/parts.d.ts +4 -0
  142. package/dist/parts.d.ts.map +1 -0
  143. package/dist/parts.js +13 -0
  144. package/dist/paths.d.ts +2 -0
  145. package/dist/paths.d.ts.map +1 -1
  146. package/dist/paths.js +26 -0
  147. package/dist/preparation.d.ts +20 -0
  148. package/dist/preparation.d.ts.map +1 -0
  149. package/dist/preparation.js +227 -0
  150. package/dist/presence.d.ts +52 -0
  151. package/dist/presence.d.ts.map +1 -0
  152. package/dist/presence.js +220 -0
  153. package/dist/queries.d.ts +16 -0
  154. package/dist/queries.d.ts.map +1 -0
  155. package/dist/queries.js +282 -0
  156. package/dist/query.d.ts +14 -210
  157. package/dist/query.d.ts.map +1 -1
  158. package/dist/query.js +3 -505
  159. package/dist/questions.d.ts +29 -0
  160. package/dist/questions.d.ts.map +1 -0
  161. package/dist/questions.js +115 -0
  162. package/dist/queue.d.ts +40 -0
  163. package/dist/queue.d.ts.map +1 -0
  164. package/dist/queue.js +450 -0
  165. package/dist/reach.d.ts +15 -12
  166. package/dist/reach.d.ts.map +1 -1
  167. package/dist/reach.js +240 -149
  168. package/dist/react.d.ts +15 -2
  169. package/dist/react.d.ts.map +1 -1
  170. package/dist/react.js +33 -1
  171. package/dist/read.d.ts +7 -0
  172. package/dist/read.d.ts.map +1 -0
  173. package/dist/read.js +17 -0
  174. package/dist/readiness.d.ts +4 -19
  175. package/dist/readiness.d.ts.map +1 -1
  176. package/dist/readiness.js +4 -10
  177. package/dist/reads.d.ts +38 -10
  178. package/dist/reads.d.ts.map +1 -1
  179. package/dist/reads.js +163 -85
  180. package/dist/recovery.d.ts +3 -0
  181. package/dist/recovery.d.ts.map +1 -1
  182. package/dist/recovery.js +57 -1
  183. package/dist/refresh.d.ts +7 -5
  184. package/dist/refresh.d.ts.map +1 -1
  185. package/dist/refresh.js +35 -7
  186. package/dist/refusal.d.ts +5 -0
  187. package/dist/refusal.d.ts.map +1 -0
  188. package/dist/refusal.js +55 -0
  189. package/dist/replica.d.ts +12 -18
  190. package/dist/replica.d.ts.map +1 -1
  191. package/dist/replica.js +64 -34
  192. package/dist/retried.d.ts +30 -0
  193. package/dist/retried.d.ts.map +1 -0
  194. package/dist/retried.js +72 -0
  195. package/dist/room-draft.d.ts +24 -10
  196. package/dist/room-draft.d.ts.map +1 -1
  197. package/dist/room-draft.js +142 -106
  198. package/dist/room-drafts.d.ts +24 -0
  199. package/dist/room-drafts.d.ts.map +1 -0
  200. package/dist/room-drafts.js +217 -0
  201. package/dist/rows.d.ts +14 -21
  202. package/dist/rows.d.ts.map +1 -1
  203. package/dist/rows.js +49 -186
  204. package/dist/runtime.d.ts +34 -0
  205. package/dist/runtime.d.ts.map +1 -0
  206. package/dist/runtime.js +1 -0
  207. package/dist/save.d.ts +42 -84
  208. package/dist/save.d.ts.map +1 -1
  209. package/dist/save.js +349 -170
  210. package/dist/saves.d.ts +32 -0
  211. package/dist/saves.d.ts.map +1 -0
  212. package/dist/saves.js +410 -0
  213. package/dist/search.d.ts +12 -63
  214. package/dist/search.d.ts.map +1 -1
  215. package/dist/search.js +9 -52
  216. package/dist/seed.d.ts +11 -2
  217. package/dist/seed.d.ts.map +1 -1
  218. package/dist/seed.js +71 -1
  219. package/dist/shared.d.ts +1 -7
  220. package/dist/shared.d.ts.map +1 -1
  221. package/dist/shared.js +13 -105
  222. package/dist/shown.d.ts +33 -0
  223. package/dist/shown.d.ts.map +1 -0
  224. package/dist/shown.js +302 -0
  225. package/dist/socket/admission.d.ts +7 -0
  226. package/dist/socket/admission.d.ts.map +1 -0
  227. package/dist/socket/admission.js +39 -0
  228. package/dist/socket/answers.d.ts +6 -0
  229. package/dist/socket/answers.d.ts.map +1 -0
  230. package/dist/socket/answers.js +20 -0
  231. package/dist/socket/lanes.d.ts +28 -0
  232. package/dist/socket/lanes.d.ts.map +1 -0
  233. package/dist/socket/lanes.js +103 -0
  234. package/dist/socket/reads.d.ts +10 -0
  235. package/dist/socket/reads.d.ts.map +1 -0
  236. package/dist/socket/reads.js +31 -0
  237. package/dist/socket/session.d.ts +38 -0
  238. package/dist/socket/session.d.ts.map +1 -0
  239. package/dist/socket/session.js +23 -0
  240. package/dist/socket.d.ts +67 -0
  241. package/dist/socket.d.ts.map +1 -0
  242. package/dist/socket.js +164 -0
  243. package/dist/source.d.ts +54 -0
  244. package/dist/source.d.ts.map +1 -0
  245. package/dist/source.js +82 -0
  246. package/dist/status.d.ts +4 -5
  247. package/dist/status.d.ts.map +1 -1
  248. package/dist/status.js +3 -7
  249. package/dist/store/installs.d.ts +30 -0
  250. package/dist/store/installs.d.ts.map +1 -0
  251. package/dist/store/installs.js +34 -0
  252. package/dist/store/readiness.d.ts +10 -0
  253. package/dist/store/readiness.d.ts.map +1 -0
  254. package/dist/store/readiness.js +52 -0
  255. package/dist/store/records.d.ts +214 -0
  256. package/dist/store/records.d.ts.map +1 -0
  257. package/dist/store/records.js +438 -0
  258. package/dist/store/room-held.d.ts +8 -0
  259. package/dist/store/room-held.d.ts.map +1 -0
  260. package/dist/store/room-held.js +17 -0
  261. package/dist/store/space.d.ts +11 -0
  262. package/dist/store/space.d.ts.map +1 -0
  263. package/dist/store/space.js +41 -0
  264. package/dist/store/watchers.d.ts +17 -0
  265. package/dist/store/watchers.d.ts.map +1 -0
  266. package/dist/store/watchers.js +56 -0
  267. package/dist/store/writes.d.ts +24 -0
  268. package/dist/store/writes.d.ts.map +1 -0
  269. package/dist/store/writes.js +125 -0
  270. package/dist/store.d.ts +120 -171
  271. package/dist/store.d.ts.map +1 -1
  272. package/dist/store.js +166 -414
  273. package/dist/stored.d.ts +62 -57
  274. package/dist/stored.d.ts.map +1 -1
  275. package/dist/stored.js +317 -239
  276. package/dist/surface.d.ts +88 -0
  277. package/dist/surface.d.ts.map +1 -0
  278. package/dist/surface.js +220 -0
  279. package/dist/sync.d.ts +19 -0
  280. package/dist/sync.d.ts.map +1 -0
  281. package/dist/sync.js +535 -0
  282. package/dist/tables.d.ts +126 -65
  283. package/dist/tables.d.ts.map +1 -1
  284. package/dist/tables.js +250 -113
  285. package/dist/tallies.d.ts +18 -0
  286. package/dist/tallies.d.ts.map +1 -0
  287. package/dist/tallies.js +128 -0
  288. package/dist/transfer.d.ts +0 -5
  289. package/dist/transfer.d.ts.map +1 -1
  290. package/dist/validation.d.ts +3 -4
  291. package/dist/validation.d.ts.map +1 -1
  292. package/dist/validation.js +42 -100
  293. package/dist/windows.d.ts +1 -12
  294. package/dist/windows.d.ts.map +1 -1
  295. package/dist/windows.js +2 -75
  296. package/dist/wire.d.ts +65 -9
  297. package/dist/wire.d.ts.map +1 -1
  298. package/dist/wire.js +22 -5
  299. package/package.json +3 -2
  300. package/dist/attempt.d.ts +0 -16
  301. package/dist/attempt.d.ts.map +0 -1
  302. package/dist/attempt.js +0 -70
  303. package/dist/fence.d.ts +0 -6
  304. package/dist/fence.d.ts.map +0 -1
  305. package/dist/fence.js +0 -10
  306. package/dist/fulltext.d.ts +0 -27
  307. package/dist/fulltext.d.ts.map +0 -1
  308. package/dist/fulltext.js +0 -368
  309. package/dist/log.d.ts +0 -5
  310. package/dist/log.d.ts.map +0 -1
  311. package/dist/log.js +0 -14
  312. package/dist/words.d.ts +0 -19
  313. package/dist/words.d.ts.map +0 -1
  314. package/dist/words.js +0 -150
@@ -0,0 +1,530 @@
1
+ import { attachedModel } from './attached-model.js';
2
+ import { bootstrapRoom } from './bootstrap.js';
3
+ import { refusedForSpace } from './durability.js';
4
+ import { uncaught } from './failure.js';
5
+ import { name } from './listing.js';
6
+ import { NO_MODEL } from './model.js';
7
+ import { settle } from './queue.js';
8
+ import { advancement } from './refresh.js';
9
+ import { recoverReads } from './shown.js';
10
+ import { unconfirmed } from './transfer.js';
11
+ import { ROW_WEIGHT, yielded } from './turn.js';
12
+ // Bytes still arriving are reported at most this often, so readiness is not rebuilt for every chunk the network hands over while a slow link still moves the figure.
13
+ const ARRIVAL_REPORT_MS = 250;
14
+ const NOTHING_HELD = { bytes: 0 };
15
+ export const ERASE_BATCH = 512;
16
+ function transferGone(held) {
17
+ return {
18
+ ...held,
19
+ transfer: null,
20
+ arrived: null,
21
+ streaming: false,
22
+ underived: 0,
23
+ unindexed: [],
24
+ };
25
+ }
26
+ function amountOf(held, kept) {
27
+ if (held.transfer !== null) {
28
+ const owed = held.unindexed.reduce((sum, part) => sum + part.bytes, 0);
29
+ return {
30
+ done: held.transfer.done.bytes - Math.ceil(owed),
31
+ total: Math.max(held.arrived ?? 0, held.transfer.done.bytes),
32
+ };
33
+ }
34
+ return { done: kept, total: held.arrived ?? kept };
35
+ }
36
+ function setBack(held) {
37
+ const now = amountOf(held, NOTHING_HELD.bytes);
38
+ const earlier = held.startedOver;
39
+ return {
40
+ ...held,
41
+ startedOver: {
42
+ done: Math.max(now.done, earlier?.done ?? 0),
43
+ total: Math.max(now.total, earlier?.total ?? 0),
44
+ },
45
+ };
46
+ }
47
+ function owedIndexes(held, bytes, text) {
48
+ if (bytes <= 0 || text <= 0)
49
+ return held;
50
+ return { ...held, unindexed: [...held.unindexed, { bytes, text }] };
51
+ }
52
+ function built(held, done, text) {
53
+ const owed = [...held.unindexed];
54
+ let left = text;
55
+ while (!done && left > 0 && owed.length > 0) {
56
+ const [first] = owed;
57
+ if (left >= first.text) {
58
+ left -= first.text;
59
+ owed.shift();
60
+ continue;
61
+ }
62
+ owed[0] = {
63
+ bytes: (first.bytes * (first.text - left)) / first.text,
64
+ text: first.text - left,
65
+ };
66
+ left = 0;
67
+ }
68
+ return { ...held, unindexed: done ? [] : owed };
69
+ }
70
+ function batchEnd(entries, from, bytes) {
71
+ let end = from;
72
+ let spent = 0;
73
+ while (end < entries.length) {
74
+ const size = (entries[end].content?.length ?? 0) + ROW_WEIGHT;
75
+ if (spent + size > bytes && end > from)
76
+ break;
77
+ spent += size;
78
+ end += 1;
79
+ }
80
+ return end;
81
+ }
82
+ export function installationOf(store, coverage, held) {
83
+ const bytes = { done: 0, total: 0 };
84
+ let installing = 0;
85
+ let downloading = false;
86
+ for (const { room, state } of coverage) {
87
+ if (state === 'unadmitted')
88
+ continue;
89
+ const install = store.installs.of(room);
90
+ const underway = state === 'acquiring' || (install.installing && state !== 'complete');
91
+ if (underway)
92
+ installing += 1;
93
+ if (underway && (install.streaming || install.arrived === null))
94
+ downloading = true;
95
+ const kept = underway ? NOTHING_HELD.bytes : (held.get(room)?.bytes ?? NOTHING_HELD.bytes);
96
+ const now = amountOf(install, kept);
97
+ const earlier = underway ? install.startedOver : null;
98
+ bytes.done += Math.max(now.done, earlier?.done ?? 0);
99
+ bytes.total += Math.max(now.total, earlier?.total ?? 0);
100
+ }
101
+ const step = installing === 0
102
+ ? null
103
+ : downloading
104
+ ? 'downloading'
105
+ : bytes.done < bytes.total
106
+ ? 'writing'
107
+ : 'replaying';
108
+ return { step, bytes };
109
+ }
110
+ export function roomInFlight(store, locator) {
111
+ const { catching, transfer } = store.installs.of(locator);
112
+ return catching || transfer !== null;
113
+ }
114
+ function installing(runtime, locator) {
115
+ runtime.store.installs.change(locator, (held) => ({ ...held, installing: true }));
116
+ runtime.store.readiness.queue();
117
+ }
118
+ function installed(runtime, locator) {
119
+ if (!runtime.store.installs.of(locator).installing)
120
+ return;
121
+ runtime.store.installs.change(locator, (held) => ({ ...held, installing: false }));
122
+ runtime.store.readiness.queue();
123
+ }
124
+ export function sweep({ store, socket }) {
125
+ if (store.device.now().sweeping || socket.closed())
126
+ return;
127
+ store.device.set({ sweeping: true });
128
+ void (async () => {
129
+ try {
130
+ while (!socket.closed() && (await store.durably((local) => local.erase(null, ERASE_BATCH)))) {
131
+ await yielded();
132
+ }
133
+ }
134
+ catch (error) {
135
+ uncaught(error);
136
+ }
137
+ finally {
138
+ store.device.set({ sweeping: false });
139
+ }
140
+ })();
141
+ }
142
+ export function caughtUpTo(runtime, locator) {
143
+ if (!runtime.store.installs.of(locator).catching)
144
+ return;
145
+ runtime.store.installs.change(locator, (held) => ({ ...held, catching: false }));
146
+ recoverReads(runtime);
147
+ }
148
+ export function catching(store, locator) {
149
+ store.installs.change(locator, (held) => ({ ...held, catching: true }));
150
+ }
151
+ export function standingAt(store, locator, held) {
152
+ return { key: held?.key ?? null, confirmed: store.installs.of(locator).confirmations };
153
+ }
154
+ function standsAt(store, locator, held, from) {
155
+ const now = standingAt(store, locator, held);
156
+ return now.key === from.key && now.confirmed === from.confirmed;
157
+ }
158
+ function shownAt({ store, socket }, locator, held) {
159
+ const raised = store.installs.of(locator).highest ?? undefined;
160
+ if (held === undefined || raised === undefined)
161
+ return held ?? raised;
162
+ return socket.reads.reached(locator, held, raised) ? held : raised;
163
+ }
164
+ export function showing(system, locator, roomVersion, held) {
165
+ const at = shownAt(system, locator, held);
166
+ if (at === undefined || !system.socket.reads.reached(locator, at, roomVersion)) {
167
+ system.store.installs.change(locator, (room) => ({ ...room, highest: roomVersion }));
168
+ }
169
+ }
170
+ export function olderThan(system, locator, roomVersion, held) {
171
+ const { store, socket } = system;
172
+ if (store.installs.of(locator).beyondHorizon)
173
+ return false;
174
+ const at = shownAt(system, locator, held);
175
+ return (at !== undefined &&
176
+ socket.reads.reached(locator, at, roomVersion) &&
177
+ !socket.reads.reached(locator, roomVersion, at));
178
+ }
179
+ async function older(runtime, locator, roomVersion) {
180
+ const { store } = runtime;
181
+ if (store.installs.of(locator).beyondHorizon)
182
+ return false;
183
+ return olderThan(runtime, locator, roomVersion, (await store.record(locator))?.roomVersion);
184
+ }
185
+ export function replaced(store, locators) {
186
+ for (const locator of locators) {
187
+ store.installs.change(locator, (held) => ({ ...held, highest: null, beyondHorizon: true }));
188
+ }
189
+ }
190
+ export async function reachedBy({ store, socket }, locator, roomVersion) {
191
+ const held = await store.record(locator);
192
+ return held !== undefined && socket.reads.reached(locator, held.roomVersion, roomVersion);
193
+ }
194
+ export async function behind({ store, socket }, room) {
195
+ const held = (await store.record(room.locator))?.roomVersion;
196
+ return held !== undefined && !socket.reads.reached(room.locator, held, room.roomVersion);
197
+ }
198
+ export async function writtenAfter({ store, socket }, locator, roomVersion, path) {
199
+ const held = await store.record(locator);
200
+ if (held === undefined)
201
+ return false;
202
+ const [entry] = await (await store.open()).entries(held.key, 'confirmed', [path]);
203
+ const written = entry?.written ?? undefined;
204
+ return (written !== undefined &&
205
+ socket.reads.reached(locator, written, roomVersion) &&
206
+ !socket.reads.reached(locator, roomVersion, written));
207
+ }
208
+ async function writtenLater(runtime, local, locator, key, paths, roomVersion, heldAt) {
209
+ if (paths.length === 0)
210
+ return new Set();
211
+ const { reached } = runtime.socket.reads;
212
+ const behindShown = heldAt === undefined
213
+ ? await older(runtime, locator, roomVersion)
214
+ : olderThan(runtime, locator, roomVersion, heldAt);
215
+ if (!behindShown)
216
+ return new Set();
217
+ const entries = await local.entries(key, 'confirmed', paths);
218
+ return new Set(entries.flatMap(({ path, written }) => written !== null &&
219
+ reached(locator, written, roomVersion) &&
220
+ !reached(locator, roomVersion, written)
221
+ ? [path]
222
+ : []));
223
+ }
224
+ function remodel(runtime, answers, held) {
225
+ const answer = answers?.collection;
226
+ if (answer === undefined)
227
+ return held;
228
+ return { collection: attachedModel(answer, runtime.roomTypes) };
229
+ }
230
+ function refreshingOf(runtime, local, locator, key, composition, liveness, replaying = false, heldAt) {
231
+ const { store } = runtime;
232
+ const write = replaying ? store.finishingWrite : store.exclusive;
233
+ return {
234
+ turn() {
235
+ if (replaying)
236
+ installing(runtime, locator);
237
+ },
238
+ prior: async (paths) => new Map((await local.entries(key, 'refreshed', paths)).map((entry) => [entry.path, entry])),
239
+ later: (paths, roomVersion) => writtenLater(runtime, local, locator, key, paths, roomVersion, heldAt),
240
+ async stage(page) {
241
+ for (let written = 0;;) {
242
+ const end = batchEnd(page.entries, written, store.stageBytes());
243
+ const last = end === page.entries.length;
244
+ const entries = page.entries.slice(written, end);
245
+ const staged = await write(async () => {
246
+ if (!liveness())
247
+ return false;
248
+ await store.refreshPage(local, key, composition, last
249
+ ? { ...page, entries }
250
+ : { entries, removals: [], folders: [], folderRemovals: [] });
251
+ return true;
252
+ });
253
+ if (!staged)
254
+ return false;
255
+ if (last)
256
+ return true;
257
+ written = end;
258
+ }
259
+ },
260
+ };
261
+ }
262
+ export async function pullRefresh(runtime, authorized, record, liveness, hold, refresh) {
263
+ if (!liveness())
264
+ return unconfirmed;
265
+ const { store } = runtime;
266
+ const from = standingAt(store, record.locator, record);
267
+ const local = await store.open();
268
+ try {
269
+ await store.exclusive(() => local.unrefresh(record.key));
270
+ const result = await advancement(async (authorization, request) => {
271
+ hold.release();
272
+ try {
273
+ return await refresh(authorization, request);
274
+ }
275
+ finally {
276
+ hold.take();
277
+ }
278
+ }, authorized, record, (models, kept) => (liveness() ? remodel(runtime, models, kept) : kept), liveness, refreshingOf(runtime, local, record.locator, record.key, store.composed(record), liveness, false, record.roomVersion));
279
+ if (result.kind !== 'advanced')
280
+ return result;
281
+ return {
282
+ kind: 'advanced',
283
+ record: result.record,
284
+ from,
285
+ remodelled: result.record.model !== record.model,
286
+ };
287
+ }
288
+ catch (error) {
289
+ if (refusedForSpace(error))
290
+ return unconfirmed;
291
+ throw error;
292
+ }
293
+ }
294
+ async function abandoned(runtime, local, locator) {
295
+ await local.abandon(locator);
296
+ runtime.store.installs.change(locator, (held) => transferGone(setBack(held)));
297
+ recoverReads(runtime);
298
+ sweep(runtime);
299
+ }
300
+ function staging(runtime, local, liveness, unheld) {
301
+ const { store, socket } = runtime;
302
+ const derived = async (locator, key, composition) => {
303
+ store.installs.change(locator, (held) => ({ ...held, underived: 0 }));
304
+ for (;;) {
305
+ if (!liveness())
306
+ return false;
307
+ const done = await store.finishingWrite(async () => liveness() ? store.rederive(local, key, composition) : null);
308
+ if (done === null)
309
+ return false;
310
+ store.installs.change(locator, (held) => built(held, done.done, done.text));
311
+ runtime.store.readiness.queue();
312
+ if (done.done)
313
+ return true;
314
+ }
315
+ };
316
+ return {
317
+ held: (locator) => store.installs.of(locator).transfer,
318
+ async turn(locator, step) {
319
+ installing(runtime, locator);
320
+ return step === 'downloading' ? socket.heldStream() : () => { };
321
+ },
322
+ unqueued: socket.unqueued,
323
+ queued: socket.queued,
324
+ arriving(locator) {
325
+ let ended = false;
326
+ return {
327
+ at(bytes) {
328
+ if (ended)
329
+ return;
330
+ store.installs.change(locator, (held) => ({
331
+ ...held,
332
+ arrived: bytes,
333
+ streaming: true,
334
+ }));
335
+ if (!store.installs.arrivalDue(performance.now(), ARRIVAL_REPORT_MS))
336
+ return;
337
+ runtime.store.readiness.queue();
338
+ },
339
+ end() {
340
+ if (ended)
341
+ return;
342
+ ended = true;
343
+ if (!store.installs.of(locator).streaming)
344
+ return;
345
+ store.installs.change(locator, (held) => ({ ...held, streaming: false }));
346
+ runtime.store.readiness.queue();
347
+ },
348
+ };
349
+ },
350
+ streamed: socket.streamed,
351
+ async stage(record, page, composition) {
352
+ const { locator } = record;
353
+ installing(runtime, locator);
354
+ if (!liveness())
355
+ return null;
356
+ const key = await store.startingWrite(async () => {
357
+ if (!liveness())
358
+ return null;
359
+ return local.stage(record, composition, page);
360
+ });
361
+ if (key === null || !liveness())
362
+ return null;
363
+ const before = store.installs.of(locator);
364
+ const prior = before.transfer?.done.bytes ?? 0;
365
+ const held = { ...record, key };
366
+ const text = page.entries.reduce((sum, entry) => sum + (entry.content?.length ?? 0), 0);
367
+ const owed = text + before.underived;
368
+ store.installs.change(locator, (room) => ({
369
+ ...owedIndexes(room, record.done.bytes - prior, text),
370
+ transfer: held,
371
+ underived: owed,
372
+ }));
373
+ installing(runtime, locator);
374
+ recoverReads(runtime);
375
+ if (owed < store.deriveBytes())
376
+ return held;
377
+ return (await derived(locator, key, composition)) ? held : null;
378
+ },
379
+ async resume(record, parts, composition) {
380
+ const { locator } = record;
381
+ installing(runtime, locator);
382
+ if (!liveness())
383
+ return null;
384
+ const prior = unheld === undefined ? await store.record(locator) : undefined;
385
+ const published = prior !== undefined && prior.declaration.replica.selection === record.selection
386
+ ? prior.key
387
+ : null;
388
+ const kept = unheld?.staged ?? (await local.held()).staged.has(locator);
389
+ if (published === null && !kept)
390
+ return { record: null, staged: new Set() };
391
+ const resumed = await store.startingWrite(async () => {
392
+ if (!liveness())
393
+ return null;
394
+ return local.resume(record, composition, parts.map((part) => part.id), published);
395
+ });
396
+ if (resumed === null || !liveness())
397
+ return null;
398
+ const parted = new Set(resumed.staged);
399
+ if (resumed.key === null)
400
+ return { record: null, staged: parted };
401
+ const bytes = parts.reduce((sum, part) => sum + (parted.has(part.id) ? part.bytes : 0), 0);
402
+ const held = {
403
+ ...record,
404
+ transfer: resumed.transfer,
405
+ key: resumed.key,
406
+ done: { bytes },
407
+ };
408
+ store.installs.change(locator, (room) => ({ ...room, transfer: held }));
409
+ recoverReads(runtime);
410
+ return { record: held, staged: parted };
411
+ },
412
+ derive: derived,
413
+ async refreshing(locator, key, composition) {
414
+ await store.exclusive(() => local.unrefresh(key));
415
+ return refreshingOf(runtime, local, locator, key, composition, liveness, true);
416
+ },
417
+ abandon: (locator) => store.exclusive(async () => {
418
+ if (liveness())
419
+ await abandoned(runtime, local, locator);
420
+ }),
421
+ setAside: async (locator) => {
422
+ if (!liveness())
423
+ return;
424
+ store.installs.change(locator, (held) => transferGone(setBack(held)));
425
+ recoverReads(runtime);
426
+ },
427
+ release: (key) => store.exclusive(async () => {
428
+ await local.release(key);
429
+ sweep(runtime);
430
+ }),
431
+ };
432
+ }
433
+ export async function pullBootstrap(runtime, authorized, room, local, liveness, prior, from = runtime.socket.reads, unheld) {
434
+ if (!liveness())
435
+ return unconfirmed;
436
+ const { store } = runtime;
437
+ if (prior !== undefined) {
438
+ catching(store, room.locator);
439
+ recoverReads(runtime);
440
+ }
441
+ try {
442
+ const result = await bootstrapRoom(from, authorized, room, {
443
+ collection: prior?.model ?? runtime.presence.unheldModel(room.locator) ?? NO_MODEL,
444
+ }, runtime.builtIn, runtime.typedPaths, (models, held) => (liveness() ? remodel(runtime, models, held) : held), liveness, staging(runtime, local, liveness, unheld));
445
+ if (result.kind === 'staged' &&
446
+ (await older(runtime, room.locator, result.record.roomVersion))) {
447
+ caughtUpTo(runtime, room.locator);
448
+ installed(runtime, room.locator);
449
+ await abandoned(runtime, local, room.locator);
450
+ return { kind: 'transport-uncertainty' };
451
+ }
452
+ if (result.kind !== 'staged') {
453
+ caughtUpTo(runtime, room.locator);
454
+ installed(runtime, room.locator);
455
+ }
456
+ return result;
457
+ }
458
+ catch (error) {
459
+ caughtUpTo(runtime, room.locator);
460
+ installed(runtime, room.locator);
461
+ if (refusedForSpace(error))
462
+ return unconfirmed;
463
+ throw error;
464
+ }
465
+ }
466
+ export function publish(runtime, local, room, reached, landing, live) {
467
+ const { store } = runtime;
468
+ let published = true;
469
+ const landed = settle(runtime, [room.locator], async (held) => {
470
+ if (!live()) {
471
+ caughtUpTo(runtime, room.locator);
472
+ return [];
473
+ }
474
+ const prior = held.get(room.locator)?.replica;
475
+ if (!standsAt(store, room.locator, prior?.record, landing.from)) {
476
+ published = false;
477
+ if (landing.kind === 'refreshed') {
478
+ await local.unrefresh(reached.key);
479
+ return [];
480
+ }
481
+ caughtUpTo(runtime, room.locator);
482
+ await abandoned(runtime, local, room.locator);
483
+ return [];
484
+ }
485
+ const record = landing.kind === 'refreshed' && prior !== undefined
486
+ ? {
487
+ ...prior.record,
488
+ roomVersion: reached.roomVersion,
489
+ ...(landing.remodelled ? { model: reached.model } : {}),
490
+ }
491
+ : reached;
492
+ return [
493
+ {
494
+ locator: room.locator,
495
+ record,
496
+ installed: landing.kind === 'refreshed'
497
+ ? { refreshed: true }
498
+ : {
499
+ published: { from: prior?.record.key ?? null },
500
+ ...(landing.refreshed ? { refreshed: true } : {}),
501
+ },
502
+ after() {
503
+ runtime.presence.installed(room.locator, landing.kind === 'bootstrapped');
504
+ runtime.retries.succeeded('download', room.locator);
505
+ store.prepareAgain(room.locator);
506
+ if (landing.kind === 'bootstrapped') {
507
+ store.installs.change(room.locator, (held) => ({
508
+ ...transferGone(held),
509
+ highest: null,
510
+ beyondHorizon: false,
511
+ startedOver: null,
512
+ catching: false,
513
+ }));
514
+ if (prior !== undefined && prior.record.key !== record.key)
515
+ sweep(runtime);
516
+ void name(runtime, room.locator);
517
+ }
518
+ },
519
+ },
520
+ ];
521
+ }).then(() => published, (error) => {
522
+ if (!refusedForSpace(error))
523
+ throw error;
524
+ caughtUpTo(runtime, room.locator);
525
+ return published;
526
+ });
527
+ return landing.kind === 'refreshed'
528
+ ? landed
529
+ : landed.finally(() => installed(runtime, room.locator));
530
+ }
@@ -0,0 +1,7 @@
1
+ import type { Runtime } from './runtime.js';
2
+ export declare function scheduleCurrency(runtime: Runtime): void;
3
+ export declare function spaceFreed(runtime: Runtime): void;
4
+ export declare function close({ store, socket, presence, conversations, recovery, liveQueries, figures, }: Runtime): void;
5
+ export declare function stop(runtime: Runtime): Promise<void>;
6
+ export declare function start(runtime: Runtime, signal: AbortSignal | undefined): void;
7
+ //# sourceMappingURL=lifecycle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lifecycle.d.ts","sourceRoot":"","sources":["../src/lifecycle.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAgO5C,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,QAOhD;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,QAQ1C;AAED,wBAAgB,KAAK,CAAC,EACpB,KAAK,EACL,MAAM,EACN,QAAQ,EACR,aAAa,EACb,QAAQ,EACR,WAAW,EACX,OAAO,GACR,EAAE,OAAO,QAST;AAED,wBAAsB,IAAI,CAAC,OAAO,EAAE,OAAO,iBAS1C;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,GAAG,SAAS,QAyCtE"}