@robono/messaging-react-native 0.20.1 → 0.20.3

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.
@@ -8,6 +8,8 @@ export declare class RecentMessagingMedia {
8
8
  private now;
9
9
  private entries;
10
10
  private running;
11
+ private foreground;
12
+ private resumeTimer;
11
13
  constructor(source: MessagingMediaStorage, files: {
12
14
  copy: (file: LocalMessagingMedia) => Promise<LocalMessagingMedia>;
13
15
  remove: (file: LocalMessagingMedia) => Promise<void>;
@@ -15,6 +17,9 @@ export declare class RecentMessagingMedia {
15
17
  readonly storage: MessagingMediaStorage;
16
18
  sync(messages: MessagingMessage[]): void;
17
19
  clear(): void;
20
+ /** Pause speculation until the viewer closes/cancels, including while a
21
+ * streamed video is fetching bytes after its URL has been returned. */
22
+ private prioritize;
18
23
  private drop;
19
24
  private pump;
20
25
  private start;
@@ -7,6 +7,8 @@ export class RecentMessagingMedia {
7
7
  now;
8
8
  entries = new Map();
9
9
  running = 0;
10
+ foreground = new Map();
11
+ resumeTimer;
10
12
  constructor(source, files, now = Date.now) {
11
13
  this.source = source;
12
14
  this.files = files;
@@ -14,9 +16,32 @@ export class RecentMessagingMedia {
14
16
  }
15
17
  storage = {
16
18
  thumbnail: (message, context) => this.source.thumbnail?.(message, context) ?? Promise.resolve(null),
17
- stream: (message, context) => this.source.stream?.(message, context) ?? Promise.resolve(null),
19
+ stream: async (message, context) => {
20
+ if (context.signal.aborted)
21
+ throw Error('Cancelled.');
22
+ this.prioritize(context.signal, this.source.stream ? undefined : key(message));
23
+ const id = key(message), entry = this.entries.get(id);
24
+ // An already authorized original starts locally. Never wait behind a
25
+ // speculative transfer: a cold tap keeps the normal streaming path.
26
+ if (entry?.file && entry.expires > this.now()) {
27
+ const copy = await this.files.copy(entry.file);
28
+ if (context.signal.aborted || this.entries.get(id) !== entry) {
29
+ await this.files.remove(copy);
30
+ throw Error('Cancelled.');
31
+ }
32
+ return copy;
33
+ }
34
+ if (entry?.file)
35
+ this.drop(id, entry);
36
+ return this.source.stream?.(message, context) ?? null;
37
+ },
18
38
  upload: (file, context) => this.source.upload(file, context), discard: id => this.source.discard(id),
19
39
  download: async (message, context) => {
40
+ if (context.signal.aborted)
41
+ throw Error('Cancelled.');
42
+ this.prioritize(context.signal, key(message));
43
+ if (context.reauthorize)
44
+ return this.source.download(message, context);
20
45
  const id = key(message), entry = this.entries.get(id);
21
46
  if (!entry)
22
47
  return this.source.download(message, context);
@@ -57,7 +82,7 @@ export class RecentMessagingMedia {
57
82
  if (wanted.size >= 6)
58
83
  break;
59
84
  const size = m.media?.byteSize;
60
- if (this.source.stream && ['voice', 'video'].includes(m.kind))
85
+ if (this.source.stream && m.kind === 'video')
61
86
  continue;
62
87
  if (m.deleted || !['image', 'voice', 'video'].includes(m.kind) || !size || size > 2 * 1048576 || bytes + size > 6 * 1048576)
63
88
  continue;
@@ -81,8 +106,25 @@ export class RecentMessagingMedia {
81
106
  }
82
107
  this.pump();
83
108
  }
84
- clear() { for (const [id, entry] of this.entries)
109
+ clear() { clearTimeout(this.resumeTimer); for (const [signal, release] of this.foreground)
110
+ signal.removeEventListener('abort', release); this.foreground.clear(); for (const [id, entry] of this.entries)
85
111
  this.drop(id, entry); }
112
+ /** Pause speculation until the viewer closes/cancels, including while a
113
+ * streamed video is fetching bytes after its URL has been returned. */
114
+ prioritize(signal, keep) {
115
+ clearTimeout(this.resumeTimer);
116
+ if (!this.foreground.has(signal)) {
117
+ const release = () => { signal.removeEventListener('abort', release); this.foreground.delete(signal); clearTimeout(this.resumeTimer); this.resumeTimer = setTimeout(() => this.pump(), 0); };
118
+ this.foreground.set(signal, release);
119
+ signal.addEventListener('abort', release, { once: true });
120
+ }
121
+ for (const [id, entry] of this.entries) {
122
+ if (id === keep || entry.file || !entry.work)
123
+ continue;
124
+ this.drop(id, entry);
125
+ this.entries.set(id, { message: entry.message, abort: new AbortController(), expires: 0, attempted: false });
126
+ }
127
+ }
86
128
  drop(id, entry) {
87
129
  if (this.entries.get(id) === entry)
88
130
  this.entries.delete(id);
@@ -93,6 +135,8 @@ export class RecentMessagingMedia {
93
135
  }
94
136
  }
95
137
  pump() {
138
+ if (this.foreground.size)
139
+ return;
96
140
  for (const [id, entry] of this.entries) {
97
141
  if (this.running >= 2)
98
142
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@robono/messaging-react-native",
3
- "version": "0.20.1",
3
+ "version": "0.20.3",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "Optional, composable direct and group messaging screens for Robono.",
@@ -57,7 +57,7 @@
57
57
  "prepack": "npm run build && node ../../scripts/generate-sdk-file-sbom.mjs"
58
58
  },
59
59
  "peerDependencies": {
60
- "@robono/messaging": "^0.15.0",
60
+ "@robono/messaging": "^0.15.1",
61
61
  "react": ">=18.2 <20",
62
62
  "react-native": ">=0.76 <1",
63
63
  "expo-audio": ">=55 <57",