@agentshouse/mdruntime 0.1.0-c1e540acf5f9d → 0.1.0-c1ea23b9ca7c6

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 (215) hide show
  1. package/README.md +14 -13
  2. package/dist/acceptance.js +1 -1
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +24 -5
  5. package/dist/adapter.d.ts +74 -2
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/authored.d.ts +88 -0
  8. package/dist/authored.d.ts.map +1 -0
  9. package/dist/authored.js +345 -0
  10. package/dist/change.d.ts +4 -0
  11. package/dist/change.d.ts.map +1 -1
  12. package/dist/change.js +3 -0
  13. package/dist/client.d.ts +10 -4
  14. package/dist/client.d.ts.map +1 -1
  15. package/dist/client.js +14 -44
  16. package/dist/collection.d.ts +5 -1
  17. package/dist/collection.d.ts.map +1 -1
  18. package/dist/collection.js +16 -8
  19. package/dist/column.d.ts +5 -0
  20. package/dist/column.d.ts.map +1 -0
  21. package/dist/column.js +9 -0
  22. package/dist/compile.d.ts.map +1 -1
  23. package/dist/compile.js +2 -3
  24. package/dist/connection.d.ts +1 -1
  25. package/dist/connection.d.ts.map +1 -1
  26. package/dist/connection.js +8 -4
  27. package/dist/context.d.ts.map +1 -1
  28. package/dist/context.js +1 -1
  29. package/dist/conversation-live.d.ts +7 -2
  30. package/dist/conversation-live.d.ts.map +1 -1
  31. package/dist/conversation-live.js +3 -0
  32. package/dist/conversation-row.d.ts +8 -1
  33. package/dist/conversation-row.d.ts.map +1 -1
  34. package/dist/conversation-row.js +20 -7
  35. package/dist/conversation.d.ts +8 -4
  36. package/dist/conversation.d.ts.map +1 -1
  37. package/dist/conversation.js +41 -22
  38. package/dist/discovery.d.ts +3 -1
  39. package/dist/discovery.d.ts.map +1 -1
  40. package/dist/discovery.js +30 -5
  41. package/dist/download.d.ts +6 -0
  42. package/dist/download.d.ts.map +1 -0
  43. package/dist/download.js +42 -0
  44. package/dist/durability.d.ts +1 -0
  45. package/dist/durability.d.ts.map +1 -1
  46. package/dist/durability.js +3 -0
  47. package/dist/entry.d.ts +14 -0
  48. package/dist/entry.d.ts.map +1 -1
  49. package/dist/entry.js +45 -16
  50. package/dist/failure.d.ts.map +1 -1
  51. package/dist/failure.js +3 -1
  52. package/dist/figures.d.ts.map +1 -1
  53. package/dist/figures.js +4 -2
  54. package/dist/file.d.ts +1 -1
  55. package/dist/file.d.ts.map +1 -1
  56. package/dist/file.js +3 -2
  57. package/dist/host.d.ts +5 -2
  58. package/dist/host.d.ts.map +1 -1
  59. package/dist/host.js +42 -13
  60. package/dist/house-query.d.ts +15 -0
  61. package/dist/house-query.d.ts.map +1 -0
  62. package/dist/house-query.js +43 -0
  63. package/dist/index.d.ts +8 -3
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +4 -0
  66. package/dist/install.d.ts +8 -2
  67. package/dist/install.d.ts.map +1 -1
  68. package/dist/install.js +40 -33
  69. package/dist/lifecycle.d.ts +1 -1
  70. package/dist/lifecycle.d.ts.map +1 -1
  71. package/dist/lifecycle.js +37 -24
  72. package/dist/listing.d.ts +24 -12
  73. package/dist/listing.d.ts.map +1 -1
  74. package/dist/listing.js +50 -21
  75. package/dist/live.d.ts +5 -3
  76. package/dist/live.d.ts.map +1 -1
  77. package/dist/live.js +71 -30
  78. package/dist/loaded.d.ts +5 -0
  79. package/dist/loaded.d.ts.map +1 -0
  80. package/dist/loaded.js +40 -0
  81. package/dist/locks.d.ts.map +1 -1
  82. package/dist/locks.js +4 -2
  83. package/dist/lost.js +1 -1
  84. package/dist/media.d.ts +18 -1
  85. package/dist/media.d.ts.map +1 -1
  86. package/dist/media.js +114 -24
  87. package/dist/memory.d.ts +2 -0
  88. package/dist/memory.d.ts.map +1 -1
  89. package/dist/memory.js +41 -9
  90. package/dist/messages.d.ts +3 -1
  91. package/dist/messages.d.ts.map +1 -1
  92. package/dist/messages.js +9 -3
  93. package/dist/modal.d.ts +2 -0
  94. package/dist/modal.d.ts.map +1 -0
  95. package/dist/modal.js +11 -0
  96. package/dist/model.d.ts +4 -2
  97. package/dist/model.d.ts.map +1 -1
  98. package/dist/model.js +7 -1
  99. package/dist/models.d.ts.map +1 -1
  100. package/dist/models.js +3 -3
  101. package/dist/observation.d.ts +26 -10
  102. package/dist/observation.d.ts.map +1 -1
  103. package/dist/observation.js +200 -69
  104. package/dist/online.d.ts +7 -0
  105. package/dist/online.d.ts.map +1 -0
  106. package/dist/online.js +29 -0
  107. package/dist/opening.d.ts.map +1 -1
  108. package/dist/opening.js +18 -2
  109. package/dist/outcome.d.ts +10 -1
  110. package/dist/outcome.d.ts.map +1 -1
  111. package/dist/outcome.js +17 -2
  112. package/dist/overlay.d.ts +1 -1
  113. package/dist/overlay.d.ts.map +1 -1
  114. package/dist/overlay.js +3 -5
  115. package/dist/parts.d.ts +3 -1
  116. package/dist/parts.d.ts.map +1 -1
  117. package/dist/parts.js +30 -7
  118. package/dist/preparation.d.ts +32 -7
  119. package/dist/preparation.d.ts.map +1 -1
  120. package/dist/preparation.js +186 -78
  121. package/dist/presence.d.ts +8 -2
  122. package/dist/presence.d.ts.map +1 -1
  123. package/dist/presence.js +24 -13
  124. package/dist/queries.d.ts +1 -1
  125. package/dist/queries.d.ts.map +1 -1
  126. package/dist/queries.js +62 -57
  127. package/dist/questions.d.ts +1 -0
  128. package/dist/questions.d.ts.map +1 -1
  129. package/dist/questions.js +3 -1
  130. package/dist/queue.d.ts.map +1 -1
  131. package/dist/queue.js +51 -21
  132. package/dist/reach.d.ts +14 -6
  133. package/dist/reach.d.ts.map +1 -1
  134. package/dist/reach.js +102 -16
  135. package/dist/react.d.ts +5 -1
  136. package/dist/react.d.ts.map +1 -1
  137. package/dist/readiness.d.ts +15 -3
  138. package/dist/readiness.d.ts.map +1 -1
  139. package/dist/readiness.js +10 -10
  140. package/dist/reads.d.ts +1 -1
  141. package/dist/reads.d.ts.map +1 -1
  142. package/dist/reads.js +2 -2
  143. package/dist/recovery.d.ts +1 -0
  144. package/dist/recovery.d.ts.map +1 -1
  145. package/dist/recovery.js +2 -1
  146. package/dist/replica.d.ts.map +1 -1
  147. package/dist/replica.js +2 -2
  148. package/dist/room-draft.d.ts +28 -3
  149. package/dist/room-draft.d.ts.map +1 -1
  150. package/dist/room-draft.js +230 -12
  151. package/dist/room-drafts.d.ts.map +1 -1
  152. package/dist/room-drafts.js +5 -2
  153. package/dist/room-list.d.ts +10 -0
  154. package/dist/room-list.d.ts.map +1 -0
  155. package/dist/room-list.js +45 -0
  156. package/dist/rows.d.ts +2 -1
  157. package/dist/rows.d.ts.map +1 -1
  158. package/dist/rows.js +6 -6
  159. package/dist/running.d.ts +5 -0
  160. package/dist/running.d.ts.map +1 -0
  161. package/dist/running.js +10 -0
  162. package/dist/runtime.d.ts +3 -2
  163. package/dist/runtime.d.ts.map +1 -1
  164. package/dist/save.d.ts.map +1 -1
  165. package/dist/save.js +17 -86
  166. package/dist/saves.d.ts +2 -0
  167. package/dist/saves.d.ts.map +1 -1
  168. package/dist/saves.js +41 -14
  169. package/dist/seed.d.ts.map +1 -1
  170. package/dist/seed.js +2 -2
  171. package/dist/shown.d.ts +3 -1
  172. package/dist/shown.d.ts.map +1 -1
  173. package/dist/shown.js +62 -38
  174. package/dist/socket/session.d.ts +4 -1
  175. package/dist/socket/session.d.ts.map +1 -1
  176. package/dist/socket/session.js +3 -0
  177. package/dist/socket.d.ts +1 -0
  178. package/dist/socket.d.ts.map +1 -1
  179. package/dist/socket.js +1 -0
  180. package/dist/source.d.ts +5 -0
  181. package/dist/source.d.ts.map +1 -1
  182. package/dist/source.js +13 -2
  183. package/dist/store/installs.d.ts +2 -2
  184. package/dist/store/installs.d.ts.map +1 -1
  185. package/dist/store/installs.js +10 -4
  186. package/dist/store/readiness.d.ts +5 -4
  187. package/dist/store/readiness.d.ts.map +1 -1
  188. package/dist/store/readiness.js +28 -12
  189. package/dist/store/records.d.ts +13 -1
  190. package/dist/store/records.d.ts.map +1 -1
  191. package/dist/store/records.js +49 -20
  192. package/dist/store/rooms-shown.d.ts +8 -0
  193. package/dist/store/rooms-shown.d.ts.map +1 -0
  194. package/dist/store/rooms-shown.js +13 -0
  195. package/dist/store.d.ts +14 -9
  196. package/dist/store.d.ts.map +1 -1
  197. package/dist/store.js +12 -5
  198. package/dist/stored.d.ts +31 -14
  199. package/dist/stored.d.ts.map +1 -1
  200. package/dist/stored.js +180 -44
  201. package/dist/surface.d.ts +10 -5
  202. package/dist/surface.d.ts.map +1 -1
  203. package/dist/surface.js +48 -15
  204. package/dist/sync.d.ts +4 -2
  205. package/dist/sync.d.ts.map +1 -1
  206. package/dist/sync.js +46 -24
  207. package/dist/tables.d.ts +10 -49
  208. package/dist/tables.d.ts.map +1 -1
  209. package/dist/tables.js +60 -322
  210. package/dist/validation.d.ts.map +1 -1
  211. package/dist/validation.js +3 -9
  212. package/dist/wire.d.ts +22 -8
  213. package/dist/wire.d.ts.map +1 -1
  214. package/dist/wire.js +11 -7
  215. package/package.json +3 -3
package/dist/listing.js CHANGED
@@ -1,5 +1,6 @@
1
+ import { typesPrint } from './collection.js';
1
2
  import { declared } from './declaration.js';
2
- import { modelTypes, NO_MODEL } from './model.js';
3
+ import { modelResult, NO_MODEL, sortedTypes, } from './model.js';
3
4
  import { governs, holds } from './reach.js';
4
5
  export function modelIn({ socket, presence }, locator, stored) {
5
6
  if (socket.session.now().refusal?.code === 'revoked-authorization')
@@ -26,6 +27,9 @@ export function listedLocators({ presence }, held) {
26
27
  const listed = presence.rooms().map(({ locator }) => locator);
27
28
  return [...new Set([...listed, ...held.keys()])];
28
29
  }
30
+ export function listedOrHeld({ presence }, held, locator) {
31
+ return presence.room(locator) !== undefined || held.has(locator);
32
+ }
29
33
  function selectionModel({ socket, builtIn }) {
30
34
  return socket.session.now().refusal?.code === 'revoked-authorization'
31
35
  ? { status: 'unavailable', reason: 'revoked' }
@@ -43,10 +47,8 @@ function listedRoom(runtime, locator, stored) {
43
47
  handle: room.handle,
44
48
  operations: room.operations,
45
49
  replica: room.replica,
46
- model: modelTypes(modelIn(runtime, locator, stored)),
47
- typedSelection: selection === null
48
- ? null
49
- : { roots: selection.roots, model: modelTypes(selectionModel(runtime)) },
50
+ model: modelIn(runtime, locator, stored),
51
+ typedSelection: selection === null ? null : { roots: selection.roots, model: selectionModel(runtime) },
50
52
  providerOwnedPaths: room.providerOwnedPaths,
51
53
  authoredPaths: room.authoredPaths,
52
54
  roomVersion: stored?.record.roomVersion ?? null,
@@ -64,35 +66,62 @@ function listingOf(runtime, held) {
64
66
  }
65
67
  return listing;
66
68
  }
67
- export async function patchedListing(runtime, changed) {
69
+ export async function listedEntries(runtime, touched) {
68
70
  const { store } = runtime;
69
- const { roomsShown } = store.device.now();
70
- if (roomsShown === null)
71
+ if (store.roomsShown.now() === null)
71
72
  return null;
72
- const held = await store.rooms([...changed]);
73
- const listing = new Map(roomsShown);
74
- for (const locator of changed) {
73
+ if (touched.length === 0)
74
+ return [];
75
+ const held = await store.rooms(touched);
76
+ const entries = [];
77
+ for (const locator of touched) {
75
78
  const room = listedRoom(runtime, locator, held.get(locator));
76
- if (room === undefined || !listing.has(locator))
79
+ if (room === undefined || store.roomsShown.now()?.has(locator) !== true)
77
80
  return null;
78
- listing.set(locator, room);
81
+ entries.push(room);
79
82
  }
80
- return listing;
83
+ return entries;
81
84
  }
82
85
  export async function listingNow(runtime) {
83
86
  return listingOf(runtime, await runtime.store.rooms());
84
87
  }
85
- export function shownRooms({ presence }, listing) {
86
- const locked = new Set(presence.lockedAmong(new Map([...listing].map(([locator, room]) => [locator, room.replica.offline]))));
87
- return structuredClone([...listing.values()].map(({ documents, ...room }) => locked.has(room.locator)
88
- ? {
88
+ function listedModel(state, models) {
89
+ return modelResult(state, (types) => {
90
+ const key = typesPrint(types);
91
+ if (!models.has(key))
92
+ models.set(key, sortedTypes(types));
93
+ return key;
94
+ });
95
+ }
96
+ function shownRoom({ presence }, { model, typedSelection, documents, ...room }, models) {
97
+ if (presence.lockedAs(room.locator, room.replica.offline)) {
98
+ return {
89
99
  ...room,
90
- model: modelTypes(NO_MODEL),
100
+ model: listedModel(NO_MODEL, models),
91
101
  typedSelection: null,
92
102
  documents: 0,
93
103
  locked: true,
94
- }
95
- : { ...room, documents, locked: false }));
104
+ };
105
+ }
106
+ return {
107
+ ...room,
108
+ model: listedModel(model, models),
109
+ typedSelection: typedSelection === null
110
+ ? null
111
+ : { roots: typedSelection.roots, model: listedModel(typedSelection.model, models) },
112
+ documents,
113
+ locked: false,
114
+ };
115
+ }
116
+ export function shownRooms(runtime, listing) {
117
+ const models = new Map();
118
+ const rooms = [...listing.values()].map((entry) => shownRoom(runtime, entry, models));
119
+ return structuredClone({ rooms, models: Object.fromEntries(models) });
120
+ }
121
+ export function shownChange(runtime, entries) {
122
+ const models = new Map();
123
+ const changed = entries.map((entry) => [entry.locator, shownRoom(runtime, entry, models)]);
124
+ return structuredClone({ changed, removed: [], order: null, models: Object.fromEntries(models) });
96
125
  }
97
126
  export async function roomFacts(runtime, reach = null) {
98
127
  const { store, socket, builtIn } = runtime;
package/dist/live.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { ChangeSet } from './change-set.js';
2
2
  import { type KeyedResult, type QueryMessage } from './observation.js';
3
- import type { PendingResult, Plan, QueryCoverage } from './query.js';
3
+ import type { PendingResult, PendingRoom, Plan, QueryCoverage } from './query.js';
4
4
  import type { Fail } from './reach.js';
5
5
  import type { QueryRoom } from './tables.js';
6
6
  export type Cover = {
@@ -11,13 +11,15 @@ export type Ran = KeyedResult & {
11
11
  readonly current: () => boolean;
12
12
  };
13
13
  export type Run = (plan: Plan, cover: () => Promise<Cover>) => Promise<Ran>;
14
+ export type PendingAt<H> = (held: H, rooms: readonly string[]) => ReadonlyMap<string, PendingRoom | null> | null;
14
15
  type Listener = (message: QueryMessage) => void;
15
16
  export type LiveQueries<H> = {
16
17
  touched(set: ChangeSet): void;
17
- recover(): void;
18
- watch(plan: Plan, identity: string, cover: (held: H) => Cover, deliver: Listener, failed: Fail): () => void;
18
+ recover(locators?: readonly string[]): void;
19
+ watch(plan: Plan, identity: string, cover: (held: H) => Cover, pendingAt: PendingAt<H>, deliver: Listener, failed: Fail): () => void;
19
20
  close(): void;
20
21
  };
22
+ export declare function readerOf(plan: Plan): (locator: string) => boolean;
21
23
  export declare function createLiveQueries<H>(run: Run, holding: () => Promise<H>): LiveQueries<H>;
22
24
  export {};
23
25
  //# sourceMappingURL=live.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"live.d.ts","sourceRoot":"","sources":["../src/live.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAIL,KAAK,WAAW,EAChB,KAAK,YAAY,EAClB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,aAAa,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACrE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAGvC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,MAAM,MAAM,KAAK,GAAG;IAAE,QAAQ,EAAE,aAAa,CAAC;IAAC,KAAK,EAAE,SAAS,SAAS,EAAE,CAAA;CAAE,GAAG,aAAa,CAAC;AAE7F,MAAM,MAAM,GAAG,GAAG,WAAW,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,OAAO,CAAA;CAAE,CAAC;AAEpE,MAAM,MAAM,GAAG,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;AAE5E,KAAK,QAAQ,GAAG,CAAC,OAAO,EAAE,YAAY,KAAK,IAAI,CAAC;AAchD,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;IAC3B,OAAO,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC;IAC9B,OAAO,IAAI,IAAI,CAAC;IAChB,KAAK,CACH,IAAI,EAAE,IAAI,EACV,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,KAAK,EACzB,OAAO,EAAE,QAAQ,EACjB,MAAM,EAAE,IAAI,GACX,MAAM,IAAI,CAAC;IACd,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAmCF,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CA2HxF"}
1
+ {"version":3,"file":"live.d.ts","sourceRoot":"","sources":["../src/live.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAQL,KAAK,WAAW,EAChB,KAAK,YAAY,EAClB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAClF,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAGvC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,MAAM,MAAM,KAAK,GAAG;IAAE,QAAQ,EAAE,aAAa,CAAC;IAAC,KAAK,EAAE,SAAS,SAAS,EAAE,CAAA;CAAE,GAAG,aAAa,CAAC;AAE7F,MAAM,MAAM,GAAG,GAAG,WAAW,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,OAAO,CAAA;CAAE,CAAC;AAEpE,MAAM,MAAM,GAAG,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;AAE5E,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CACzB,IAAI,EAAE,CAAC,EACP,KAAK,EAAE,SAAS,MAAM,EAAE,KACrB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;AAEpD,KAAK,QAAQ,GAAG,CAAC,OAAO,EAAE,YAAY,KAAK,IAAI,CAAC;AAiBhD,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;IAC3B,OAAO,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC;IAC9B,OAAO,CAAC,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC;IAC5C,KAAK,CACH,IAAI,EAAE,IAAI,EACV,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,KAAK,EACzB,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,EACvB,OAAO,EAAE,QAAQ,EACjB,MAAM,EAAE,IAAI,GACX,MAAM,IAAI,CAAC;IACd,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAWF,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAQjE;AAeD,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CA4JxF"}
package/dist/live.js CHANGED
@@ -1,27 +1,25 @@
1
- import { changeOf, digestOf, } from './observation.js';
1
+ import { changeOf, digestOf, keyedAt, keyedInto, samePending, unchanged, } from './observation.js';
2
2
  import { uncaught } from './failure.js';
3
- import { roomOfKey } from './rows.js';
3
+ import { roomsOfReach } from './rows.js';
4
4
  function narrowed(plan) {
5
5
  return (plan.paths !== null &&
6
6
  plan.hop === null &&
7
7
  plan.backlinks === null &&
8
8
  !(plan.shape.kind === 'documents' && plan.shape.backlinks));
9
9
  }
10
- function reads(plan, locator) {
11
- if (plan.locked.includes(locator))
12
- return false;
13
- if (plan.rooms !== null && !plan.rooms.includes(locator))
14
- return false;
15
- if (plan.reach === null)
16
- return true;
17
- return (plan.reach.rooms.includes(locator) ||
18
- plan.reach.documents.some((document) => roomOfKey(document) === locator));
10
+ export function readerOf(plan) {
11
+ const locked = new Set(plan.locked);
12
+ const named = plan.rooms === null ? null : new Set(plan.rooms);
13
+ const reached = plan.reach === null ? null : roomsOfReach(plan.reach);
14
+ return (locator) => !locked.has(locator) &&
15
+ (named === null || named.has(locator)) &&
16
+ (reached === null || reached.has(locator));
19
17
  }
20
- function affected(plan, set) {
21
- if (set.rooms.some((locator) => reads(plan, locator)))
18
+ function affected({ plan, reads }, set) {
19
+ if (set.rooms.some(reads))
22
20
  return true;
23
21
  const paths = narrowed(plan) ? new Set(plan.paths) : null;
24
- return set.documents.some(({ locator, path }) => reads(plan, locator) && (paths === null || path === null || paths.has(path)));
22
+ return set.documents.some(({ locator, path }) => reads(locator) && (paths === null || path === null || paths.has(path)));
25
23
  }
26
24
  function shown(entry, held) {
27
25
  const cover = entry.cover(held);
@@ -30,11 +28,13 @@ function shown(entry, held) {
30
28
  export function createLiveQueries(run, holding) {
31
29
  const entries = new Map();
32
30
  let closed = false;
33
- let recovering = false;
34
- let recoverAgain = false;
31
+ let recoveringNow = false;
32
+ let recoveringAll = false;
33
+ const recovering = new Set();
35
34
  function deliver(entry, next) {
36
35
  const previous = entry.digest;
37
36
  entry.digest = digestOf(next);
37
+ entry.pending = 'pending' in entry.digest ? entry.digest.pending : null;
38
38
  entry.covered = JSON.stringify('pending' in next.result ? next.result : next.result.coverage);
39
39
  const change = previous === undefined ? null : changeOf(previous, next);
40
40
  if (change !== null) {
@@ -87,58 +87,99 @@ export function createLiveQueries(run, holding) {
87
87
  }
88
88
  })();
89
89
  }
90
- function recovered() {
91
- if (recovering) {
92
- recoverAgain = true;
93
- return;
90
+ function pendingMoved(entry, held, rooms) {
91
+ const { pending } = entry;
92
+ if (pending === null || pending.size === 0 || entry.running || entry.fresh.size > 0) {
93
+ return false;
94
94
  }
95
- recovering = true;
95
+ const at = entry.pendingAt(held, rooms);
96
+ const change = at === null ? null : keyedAt(pending, at, samePending);
97
+ if (change === null || change.removed.length === pending.size)
98
+ return false;
99
+ keyedInto(pending, change);
100
+ entry.covered = '';
101
+ if (unchanged(change))
102
+ return true;
103
+ for (const listener of entry.ready)
104
+ listener({ pending: change });
105
+ return true;
106
+ }
107
+ function recovered(locators) {
108
+ if (locators === undefined)
109
+ recoveringAll = true;
110
+ else
111
+ for (const locator of locators)
112
+ recovering.add(locator);
113
+ if (recoveringNow)
114
+ return;
115
+ recoveringNow = true;
96
116
  void (async () => {
97
117
  try {
98
- do {
99
- recoverAgain = false;
100
- for (const entry of entries.values()) {
118
+ while ((recoveringAll || recovering.size > 0) && !closed) {
119
+ const all = recoveringAll;
120
+ const touched = [...recovering];
121
+ recoveringAll = false;
122
+ recovering.clear();
123
+ const reached = [...entries.values()].filter((entry) => all || touched.some(entry.reads));
124
+ for (const entry of reached) {
101
125
  if (entry.running)
102
126
  entry.dirty = true;
103
127
  }
104
- const waiting = [...entries.values()].filter((entry) => !entry.running);
105
- if (waiting.length === 0 || closed)
128
+ const waiting = reached.filter((entry) => !entry.running);
129
+ if (waiting.length === 0)
106
130
  continue;
107
131
  const held = await holding();
108
132
  for (const entry of waiting) {
109
133
  if (entry.running)
110
134
  continue;
135
+ const read = all ? [] : touched.filter(entry.reads);
136
+ if (read.length > 0 && pendingMoved(entry, held, read))
137
+ continue;
111
138
  if (entry.digest === undefined || shown(entry, held) !== entry.covered)
112
139
  schedule(entry);
113
140
  }
114
- } while (recoverAgain && !closed);
141
+ }
115
142
  }
116
143
  catch (error) {
117
144
  uncaught(error);
118
145
  }
119
146
  finally {
120
- recovering = false;
147
+ recoveringNow = false;
121
148
  }
122
149
  })();
123
150
  }
124
151
  return {
125
152
  touched(set) {
153
+ const pending = new Set();
126
154
  for (const entry of entries.values()) {
127
- if (affected(entry.plan, set))
155
+ if (!affected(entry, set))
156
+ continue;
157
+ if (entry.pending === null || entry.running) {
128
158
  schedule(entry);
159
+ continue;
160
+ }
161
+ for (const locator of set.rooms)
162
+ pending.add(locator);
163
+ for (const { locator } of set.documents)
164
+ pending.add(locator);
129
165
  }
166
+ if (pending.size > 0)
167
+ recovered([...pending]);
130
168
  },
131
169
  recover: recovered,
132
- watch(plan, identity, cover, listener, failed) {
170
+ watch(plan, identity, cover, pendingAt, listener, failed) {
133
171
  let entry = entries.get(identity);
134
172
  if (entry === undefined) {
135
173
  entry = {
136
174
  plan,
175
+ reads: readerOf(plan),
137
176
  cover,
177
+ pendingAt,
138
178
  listeners: new Set(),
139
179
  fresh: new Map(),
140
180
  ready: new Set(),
141
181
  digest: undefined,
182
+ pending: null,
142
183
  covered: '',
143
184
  dirty: false,
144
185
  running: false,
@@ -0,0 +1,5 @@
1
+ import type { BuildFile } from './client.js';
2
+ export declare function parsed(file: Blob | null): Promise<unknown>;
3
+ export declare function perBuild<T>(buildFile: BuildFile, key: string, make: () => Promise<T>): Promise<T>;
4
+ export declare function loaded<T>(buildFile: BuildFile, name: string, read?: (file: Blob | null) => Promise<unknown>): Promise<T | null>;
5
+ //# sourceMappingURL=loaded.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loaded.d.ts","sourceRoot":"","sources":["../src/loaded.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAc7C,wBAAsB,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,CAEhE;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAYjG;AAED,wBAAgB,MAAM,CAAC,CAAC,EACtB,SAAS,EAAE,SAAS,EACpB,IAAI,EAAE,MAAM,EACZ,IAAI,GAAE,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,KAAK,OAAO,CAAC,OAAO,CAAY,GACvD,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAEnB"}
package/dist/loaded.js ADDED
@@ -0,0 +1,40 @@
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
+ });
6
+ }
7
+ return path;
8
+ };
9
+ const holdings = new WeakMap();
10
+ async function imported(file) {
11
+ if (file === null)
12
+ return null;
13
+ const address = URL.createObjectURL(file);
14
+ try {
15
+ return await import(__rewriteRelativeImportExtension(address));
16
+ }
17
+ finally {
18
+ URL.revokeObjectURL(address);
19
+ }
20
+ }
21
+ export async function parsed(file) {
22
+ return file === null ? null : JSON.parse(await file.text());
23
+ }
24
+ export function perBuild(buildFile, key, make) {
25
+ let named = holdings.get(buildFile);
26
+ if (named === undefined) {
27
+ named = new Map();
28
+ holdings.set(buildFile, named);
29
+ }
30
+ const holding = named.get(key);
31
+ if (holding !== undefined)
32
+ return holding;
33
+ const making = make();
34
+ named.set(key, making);
35
+ making.catch(() => named.delete(key));
36
+ return making;
37
+ }
38
+ export function loaded(buildFile, name, read = imported) {
39
+ return perBuild(buildFile, name, () => buildFile(name).then(read));
40
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"locks.d.ts","sourceRoot":"","sources":["../src/locks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAG5C,wBAAsB,MAAM,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAE5F;AAED,wBAAsB,WAAW,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,OAAO,GAAG,OAAO,CAAC,SAAS,MAAM,EAAE,CAAC,CAK1F;AAED,wBAAsB,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,MAAM,EAAE,iBAKzE;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,QAU9C;AAED,wBAAgB,UAAU,CAAC,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAE7E"}
1
+ {"version":3,"file":"locks.d.ts","sourceRoot":"","sources":["../src/locks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAG5C,wBAAsB,MAAM,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAE5F;AAED,wBAAsB,WAAW,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,OAAO,GAAG,OAAO,CAAC,SAAS,MAAM,EAAE,CAAC,CAI1F;AAED,wBAAsB,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,MAAM,EAAE,iBAKzE;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,QAU9C;AAED,wBAAgB,UAAU,CAAC,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAE7E"}
package/dist/locks.js CHANGED
@@ -4,8 +4,10 @@ export async function locked({ store, presence }, locator) {
4
4
  return presence.locked(locator, async () => (await store.open()).offline(locator));
5
5
  }
6
6
  export async function lockedRooms({ store, presence }) {
7
- const held = (await store.covering()).rooms;
8
- return presence.lockedAmong(new Map([...held].map(([locator, { offline }]) => [locator, offline])));
7
+ if (!store.needed())
8
+ return [];
9
+ const { rooms, locking } = await store.covering();
10
+ return presence.lockedAmong(rooms, locking);
9
11
  }
10
12
  export async function relocked(runtime, before) {
11
13
  const after = await lockedRooms(runtime);
package/dist/lost.js CHANGED
@@ -42,7 +42,7 @@ export async function loseRooms(runtime, locators) {
42
42
  queue: true,
43
43
  drafts: held.filter((draft) => draft.locator === locator).map(({ path }) => path),
44
44
  }))));
45
- recoverReads(runtime);
45
+ recoverReads(runtime, fresh);
46
46
  }
47
47
  await unpair(runtime);
48
48
  await eraseFenced(runtime, fresh);
package/dist/media.d.ts CHANGED
@@ -1,8 +1,25 @@
1
1
  import type { MdRuntimeClient } from './client.js';
2
- import { type ShownAnswer } from './outcome.js';
2
+ import { type DownloadAnswer, type ShownAnswer } from './outcome.js';
3
3
  export declare const ROOM_MEDIA = "room-media";
4
+ export declare const KEYED_MEDIA = "ahc-attachment";
5
+ export declare const MEDIA = "room-media, ahc-attachment";
6
+ export declare const DIAGRAM = "md-diagram";
7
+ export declare const VIEWING = "ahc-viewer";
4
8
  type Show = (room: string, path: string, revision?: string) => Promise<ShownAnswer>;
9
+ export type MediaAt = {
10
+ readonly link: string;
11
+ } | {
12
+ readonly room: string;
13
+ readonly path: string;
14
+ readonly revision: string | undefined;
15
+ };
5
16
  export declare function shownBy(show: Show, client: MdRuntimeClient): MdRuntimeClient;
17
+ export declare function mediaAt(media: Element): MediaAt | null;
18
+ export declare function mediaTag(at: MediaAt): string;
19
+ export declare function downloadAt(client: MdRuntimeClient, at: MediaAt): Promise<DownloadAnswer>;
20
+ export declare function rendered(element: Element): boolean;
21
+ export declare function viewable(element: Element): boolean;
22
+ export declare function viewFile(client: MdRuntimeClient, media: Element): void;
6
23
  export declare function defineRoomMedia(client: Promise<MdRuntimeClient> | MdRuntimeClient): void;
7
24
  export {};
8
25
  //# sourceMappingURL=media.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../src/media.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAa,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3D,eAAO,MAAM,UAAU,eAAe,CAAC;AAwBvC,KAAK,IAAI,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,WAAW,CAAC,CAAC;AAIpF,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,eAAe,GAAG,eAAe,CAG5E;AAsBD,wBAAgB,eAAe,CAAC,MAAM,EAAE,OAAO,CAAC,eAAe,CAAC,GAAG,eAAe,GAAG,IAAI,CA0LxF"}
1
+ {"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../src/media.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD,OAAO,EAAqB,KAAK,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAExF,eAAO,MAAM,UAAU,eAAe,CAAC;AAEvC,eAAO,MAAM,WAAW,mBAAmB,CAAC;AAE5C,eAAO,MAAM,KAAK,+BAAkC,CAAC;AAMrD,eAAO,MAAM,OAAO,eAAe,CAAC;AAEpC,eAAO,MAAM,OAAO,eAAe,CAAC;AAgCpC,KAAK,IAAI,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,WAAW,CAAC,CAAC;AAEpF,MAAM,MAAM,OAAO,GACf;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACzB;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAI5F,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,eAAe,GAAG,eAAe,CAG5E;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,GAAG,IAAI,CAUtD;AAED,wBAAgB,QAAQ,CAAC,EAAE,EAAE,OAAO,GAAG,MAAM,CAE5C;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,eAAe,EAAE,EAAE,EAAE,OAAO,GAAG,OAAO,CAAC,cAAc,CAAC,CAIxF;AA6BD,wBAAgB,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAElD;AAED,wBAAgB,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAMlD;AA0CD,wBAAgB,QAAQ,CAAC,MAAM,EAAE,eAAe,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAEtE;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,OAAO,CAAC,eAAe,CAAC,GAAG,eAAe,GAAG,IAAI,CAgMxF"}
package/dist/media.js CHANGED
@@ -1,7 +1,18 @@
1
- import { UNCERTAIN } from './outcome.js';
1
+ import { isAudioType, isImageType, isVideoType } from '@agentshouse/mdmodel';
2
+ import { loaded } from './loaded.js';
3
+ import { downloadAttachment } from './download.js';
4
+ import { blobOf, UNCERTAIN } from './outcome.js';
2
5
  export const ROOM_MEDIA = 'room-media';
6
+ export const KEYED_MEDIA = 'ahc-attachment';
7
+ export const MEDIA = `${ROOM_MEDIA}, ${KEYED_MEDIA}`;
8
+ const VIEWER = 'viewer';
9
+ const RENDERED = '.ahc, .ahc-markdown';
10
+ export const DIAGRAM = 'md-diagram';
11
+ export const VIEWING = 'ahc-viewer';
12
+ const PRESSED_ELSEWHERE = 'a[href], button, input, select, textarea, summary, label, [role="button"], [role="link"]';
3
13
  const ATTRIBUTES = ['room', 'path', 'revision', 'width', 'height', 'alt'];
4
- const LOADING = new Set(['room', 'path', 'revision']);
14
+ const KEYED_ATTRIBUTES = ['link', 'alt'];
15
+ const LOADING = new Set(['link', 'room', 'path', 'revision']);
5
16
  const NEARING = '600px 0px';
6
17
  const RELINKS = 2;
7
18
  // The Room's stream brings a file seconds after the text that links it, minutes after on a first open, and a page's reach of its Rooms settles seconds after it opens, so a file not shown yet is asked for again every three seconds.
@@ -10,12 +21,42 @@ const SHAPE = '16 / 9';
10
21
  const STYLE = `:host{display:block}
11
22
  [part=box]{position:relative;overflow:hidden}
12
23
  [part=box]>img,[part=box]>video,[part=box]>audio{display:block;width:100%;height:100%;object-fit:contain}
13
- [part=box]>slot{display:flex;align-items:center;justify-content:center;width:100%;height:100%;text-align:center}`;
24
+ [part=box]>video:not([controls]){cursor:pointer}
25
+ [part=box]>slot{display:flex;align-items:center;justify-content:center;width:100%;height:100%;text-align:center}
26
+ [part=play]{position:absolute;top:50%;left:50%;width:56px;height:56px;margin:-28px 0 0 -28px;border-radius:50%;background:rgb(0 0 0/.6);pointer-events:none}
27
+ [part=play]::before{content:"";position:absolute;top:18px;left:22px;border-style:solid;border-width:10px 0 10px 16px;border-color:transparent transparent transparent #fff}`;
14
28
  const shows = new WeakMap();
15
29
  export function shownBy(show, client) {
16
30
  shows.set(client, show);
17
31
  return client;
18
32
  }
33
+ export function mediaAt(media) {
34
+ if (media.localName === KEYED_MEDIA) {
35
+ const link = media.getAttribute('link');
36
+ return link === null ? null : { link };
37
+ }
38
+ const room = media.getAttribute('room');
39
+ const path = media.getAttribute('path');
40
+ return room === null || path === null
41
+ ? null
42
+ : { room, path, revision: media.getAttribute('revision') ?? undefined };
43
+ }
44
+ export function mediaTag(at) {
45
+ return 'link' in at ? KEYED_MEDIA : ROOM_MEDIA;
46
+ }
47
+ export function downloadAt(client, at) {
48
+ return 'link' in at
49
+ ? downloadAttachment(client, at.link)
50
+ : client.binaryFile.download(at.room, at.path, at.revision);
51
+ }
52
+ function shownAt(client, at) {
53
+ if (at === null)
54
+ return null;
55
+ if ('link' in at)
56
+ return client.conversation.attachment(at.link);
57
+ const show = shows.get(client);
58
+ return show === undefined ? null : show(at.room, at.path, at.revision);
59
+ }
19
60
  function ratioOf(width, height) {
20
61
  const across = Number(width);
21
62
  const down = Number(height);
@@ -29,23 +70,74 @@ function refusedForGood(shown) {
29
70
  return (shown.kind === 'rejected' || shown.kind === 'scoped-refusal') && !shown.retryable;
30
71
  }
31
72
  function drawnFor(mediaType) {
32
- if (mediaType.startsWith('image/'))
73
+ if (isImageType(mediaType))
33
74
  return 'img';
34
- if (mediaType.startsWith('video/'))
75
+ if (isVideoType(mediaType))
35
76
  return 'video';
36
- if (mediaType.startsWith('audio/'))
77
+ if (isAudioType(mediaType))
37
78
  return 'audio';
38
79
  return null;
39
80
  }
81
+ export function rendered(element) {
82
+ return element.closest(RENDERED) !== null;
83
+ }
84
+ export function viewable(element) {
85
+ if (element.localName === 'figure')
86
+ return element.classList.contains(DIAGRAM);
87
+ if (!element.matches(MEDIA) || element.closest(`.${VIEWING}`) !== null)
88
+ return false;
89
+ return (rendered(element) || (element.shadowRoot?.querySelector('[part=box] > video') ?? null) !== null);
90
+ }
91
+ function viewed(client, opener, alone, taken = () => true) {
92
+ void loaded(client.buildFile, VIEWER).then((file) => {
93
+ if (!opener.isConnected || !taken())
94
+ return;
95
+ file?.openViewer({
96
+ opener,
97
+ alone,
98
+ download: (at) => void downloadAt(client, at),
99
+ });
100
+ });
101
+ }
102
+ function pressed(client) {
103
+ let latest = null;
104
+ const taken = (opener) => () => {
105
+ if (latest !== opener)
106
+ return false;
107
+ latest = null;
108
+ return true;
109
+ };
110
+ return (event) => {
111
+ if (event.defaultPrevented || event.detail > 1)
112
+ return;
113
+ for (const node of event.composedPath()) {
114
+ if (!(node instanceof Element))
115
+ continue;
116
+ if (viewable(node)) {
117
+ event.preventDefault();
118
+ latest = node;
119
+ void client.then((one) => viewed(one, node, !rendered(node), taken(node)));
120
+ return;
121
+ }
122
+ if (node.matches(PRESSED_ELSEWHERE))
123
+ return;
124
+ }
125
+ };
126
+ }
127
+ export function viewFile(client, media) {
128
+ viewed(client, media, true);
129
+ }
40
130
  export function defineRoomMedia(client) {
41
131
  if (customElements.get(ROOM_MEDIA) !== undefined)
42
132
  return;
43
133
  const connected = Promise.resolve(client);
134
+ document.addEventListener('click', pressed(connected));
44
135
  class RoomMedia extends HTMLElement {
45
136
  static observedAttributes = ATTRIBUTES;
46
137
  #root = this.attachShadow({ mode: 'open' });
47
138
  #box = document.createElement('div');
48
139
  #fallback = document.createElement('slot');
140
+ #play = document.createElement('span');
49
141
  #watching = null;
50
142
  #near = false;
51
143
  #asked = 0;
@@ -56,6 +148,7 @@ export function defineRoomMedia(client) {
56
148
  constructor() {
57
149
  super();
58
150
  this.#box.setAttribute('part', 'box');
151
+ this.#play.setAttribute('part', 'play');
59
152
  this.#root.append(Object.assign(document.createElement('style'), { textContent: STYLE }), this.#box);
60
153
  this.#shape();
61
154
  }
@@ -107,15 +200,8 @@ export function defineRoomMedia(client) {
107
200
  async #load() {
108
201
  const asked = (this.#asked += 1);
109
202
  this.#loaded = false;
110
- const room = this.getAttribute('room');
111
- const path = this.getAttribute('path');
112
- const revision = this.getAttribute('revision') ?? undefined;
113
- if (room === null || path === null) {
114
- this.#unreadable();
115
- return;
116
- }
117
- const show = shows.get(await connected);
118
- const shown = show === undefined ? null : await show(room, path, revision).catch(() => UNCERTAIN);
203
+ const showing = shownAt(await connected, mediaAt(this));
204
+ const shown = showing === null ? null : await showing.catch(() => UNCERTAIN);
119
205
  if (asked !== this.#asked)
120
206
  return;
121
207
  if (shown?.kind === 'link') {
@@ -129,11 +215,7 @@ export function defineRoomMedia(client) {
129
215
  this.#unshown(asked);
130
216
  return;
131
217
  }
132
- const copy = await new Response(shown.stream, {
133
- headers: { 'content-type': shown.mediaType },
134
- })
135
- .blob()
136
- .catch(() => null);
218
+ const copy = await blobOf(shown).catch(() => null);
137
219
  if (asked !== this.#asked)
138
220
  return;
139
221
  if (copy === null)
@@ -157,7 +239,9 @@ export function defineRoomMedia(client) {
157
239
  if (content === null)
158
240
  this.#box.replaceChildren();
159
241
  else if (content.parentNode !== this.#box)
160
- this.#box.replaceChildren(content);
242
+ this.#box.replaceChildren(...(content instanceof HTMLVideoElement && !content.controls
243
+ ? [content, this.#play]
244
+ : [content]));
161
245
  this.#label();
162
246
  }
163
247
  #unreadable() {
@@ -174,13 +258,16 @@ export function defineRoomMedia(client) {
174
258
  const kept = this.#drawn?.localName === tag ? this.#drawn : null;
175
259
  const drawn = kept ?? document.createElement(tag);
176
260
  if (kept === null) {
261
+ const plays = !(drawn instanceof HTMLVideoElement) || this.closest(`.${VIEWING}`) !== null;
177
262
  if (drawn instanceof HTMLImageElement)
178
263
  drawn.decoding = 'async';
179
264
  if (drawn instanceof HTMLMediaElement) {
180
- drawn.controls = true;
181
265
  drawn.preload = 'metadata';
182
- if (drawn instanceof HTMLVideoElement && 'playsInline' in drawn)
183
- drawn.playsInline = true;
266
+ drawn.controls = plays;
267
+ }
268
+ if (drawn instanceof HTMLVideoElement && plays) {
269
+ drawn.autoplay = true;
270
+ drawn.setAttribute('playsinline', '');
184
271
  }
185
272
  drawn.addEventListener(drawn instanceof HTMLImageElement ? 'load' : 'loadedmetadata', () => {
186
273
  if (drawn !== this.#drawn)
@@ -215,4 +302,7 @@ export function defineRoomMedia(client) {
215
302
  }
216
303
  }
217
304
  customElements.define(ROOM_MEDIA, RoomMedia);
305
+ customElements.define(KEYED_MEDIA, class extends RoomMedia {
306
+ static observedAttributes = KEYED_ATTRIBUTES;
307
+ });
218
308
  }