@astratra/native 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +251 -2
- package/package.json +9 -3
- package/src/connectivity.js +359 -0
- package/src/index.d.ts +307 -0
- package/src/index.js +5 -1
- package/src/mediaCache.js +262 -0
- package/src/pictureSource.js +696 -0
- package/src/updates.js +196 -0
package/src/updates.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When to look for an over-the-air update, and when to apply it.
|
|
3
|
+
*
|
|
4
|
+
* Fixing a typo should not cost a store review: that is what remote updates
|
|
5
|
+
* are for. But they are worth nothing if the app never asks for them — the
|
|
6
|
+
* first app this came from never did — and they do harm when applied at the
|
|
7
|
+
* wrong moment: reloading the screen while someone is filling in a payment
|
|
8
|
+
* form loses the form.
|
|
9
|
+
*
|
|
10
|
+
* expo-updates is INJECTED, like the keystore: it is a native module, and the
|
|
11
|
+
* rules below must run in plain Node.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** One hour between checks: more often pays network for nothing. */
|
|
15
|
+
const UPDATE_CHECK_INTERVAL_MS = 60 * 60 * 1000;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The grace period before applying in the background. Answering a text and
|
|
19
|
+
* coming back takes a few seconds: reloading during that round trip sent the
|
|
20
|
+
* person back to the splash screen, their page gone. Past the grace period the
|
|
21
|
+
* absence is a real one — nobody sees the reload.
|
|
22
|
+
*/
|
|
23
|
+
const UPDATE_APPLY_GRACE_MS = 45 * 1000;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Should the update server be asked?
|
|
27
|
+
* @param {{enabled: boolean, online: boolean, lastCheckAt: number|null, now: number}} context
|
|
28
|
+
* `enabled` is false for a build without updates (a development client, a
|
|
29
|
+
* build with updates switched off).
|
|
30
|
+
* @param {number} [intervalMs]
|
|
31
|
+
*/
|
|
32
|
+
function shouldCheckForUpdate(context, intervalMs = UPDATE_CHECK_INTERVAL_MS) {
|
|
33
|
+
if (!context || !context.enabled || !context.online) return false;
|
|
34
|
+
if (context.lastCheckAt === null || context.lastCheckAt === undefined) return true;
|
|
35
|
+
return context.now - context.lastCheckAt >= intervalMs;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Reload now? Asked when the grace period lapses, never at the instant the app
|
|
40
|
+
* goes to the background.
|
|
41
|
+
*
|
|
42
|
+
* Never in front of the person — if they are already back, nothing happens —
|
|
43
|
+
* and never with writes still queued: a reload in the middle of flushing the
|
|
44
|
+
* queue could replay an entry before it is marked as sent.
|
|
45
|
+
*
|
|
46
|
+
* @param {{downloaded: boolean, stillInBackground: boolean, pendingWrites: number}} context
|
|
47
|
+
*/
|
|
48
|
+
function shouldApplyUpdate(context) {
|
|
49
|
+
return Boolean(context && context.downloaded && context.stillInBackground && context.pendingWrites === 0);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The release name for crash reporting: the binary AND the update it runs. A
|
|
54
|
+
* crash report that names only the store version cannot tell two updates of
|
|
55
|
+
* the same binary apart.
|
|
56
|
+
*/
|
|
57
|
+
function describeRelease({ version, updateId, isEmbeddedLaunch } = {}) {
|
|
58
|
+
const base = version || '0.0.0';
|
|
59
|
+
if (isEmbeddedLaunch || !updateId) return `${base}+embedded`;
|
|
60
|
+
return `${base}+${String(updateId).slice(0, 8)}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Wires the rules to expo-updates and the app state.
|
|
65
|
+
*
|
|
66
|
+
* @param {object} options
|
|
67
|
+
* @param {object} options.updates expo-updates' module namespace: isEnabled,
|
|
68
|
+
* isEmbeddedLaunch, updateId, channel, runtimeVersion, checkForUpdateAsync,
|
|
69
|
+
* fetchUpdateAsync, reloadAsync.
|
|
70
|
+
* @param {{currentState: string, addEventListener: Function}} options.appState
|
|
71
|
+
* React Native's AppState.
|
|
72
|
+
* @param {() => boolean} [options.isOnline] Default: always. Pass the
|
|
73
|
+
* connectivity monitor's shouldAttemptRequest.
|
|
74
|
+
* @param {() => number} [options.pendingWrites] Writes still waiting for the
|
|
75
|
+
* network. Default: none.
|
|
76
|
+
* @param {(error: unknown, context: {where: string}) => void} [options.onError]
|
|
77
|
+
* @param {number} [options.checkIntervalMs]
|
|
78
|
+
* @param {number} [options.graceMs]
|
|
79
|
+
* @param {() => number} [options.now]
|
|
80
|
+
*/
|
|
81
|
+
function createUpdateWatcher(options = {}) {
|
|
82
|
+
const updates = options.updates;
|
|
83
|
+
const appState = options.appState;
|
|
84
|
+
if (!updates) throw new Error('createUpdateWatcher: an updates module is required.');
|
|
85
|
+
if (!appState) throw new Error('createUpdateWatcher: an appState is required.');
|
|
86
|
+
|
|
87
|
+
const isOnline = typeof options.isOnline === 'function' ? options.isOnline : () => true;
|
|
88
|
+
const pendingWrites = typeof options.pendingWrites === 'function' ? options.pendingWrites : () => 0;
|
|
89
|
+
const onError = typeof options.onError === 'function' ? options.onError : () => undefined;
|
|
90
|
+
const checkIntervalMs = Number.isFinite(options.checkIntervalMs) ? options.checkIntervalMs : UPDATE_CHECK_INTERVAL_MS;
|
|
91
|
+
const graceMs = Number.isFinite(options.graceMs) ? options.graceMs : UPDATE_APPLY_GRACE_MS;
|
|
92
|
+
const now = typeof options.now === 'function' ? options.now : () => Date.now();
|
|
93
|
+
|
|
94
|
+
let lastCheckAt = null;
|
|
95
|
+
let downloaded = false;
|
|
96
|
+
let applyTimer = null;
|
|
97
|
+
let subscription = null;
|
|
98
|
+
|
|
99
|
+
async function check(at = now()) {
|
|
100
|
+
const due = shouldCheckForUpdate(
|
|
101
|
+
{ enabled: Boolean(updates.isEnabled), online: Boolean(isOnline()), lastCheckAt, now: at },
|
|
102
|
+
checkIntervalMs
|
|
103
|
+
);
|
|
104
|
+
if (!due) return false;
|
|
105
|
+
lastCheckAt = at;
|
|
106
|
+
try {
|
|
107
|
+
const result = await updates.checkForUpdateAsync();
|
|
108
|
+
if (!result || !result.isAvailable) return false;
|
|
109
|
+
const fetched = await updates.fetchUpdateAsync();
|
|
110
|
+
const isNew = Boolean(fetched && fetched.isNew);
|
|
111
|
+
downloaded = downloaded || isNew;
|
|
112
|
+
return isNew;
|
|
113
|
+
} catch (error) {
|
|
114
|
+
/* An unreachable update server is not an app crash; it is reported to be
|
|
115
|
+
seen if it happens often, nothing more. */
|
|
116
|
+
onError(error, { where: 'checkForUpdate' });
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/* When the grace period lapses. On iOS a timer frozen in the background
|
|
122
|
+
fires on return to the foreground: the stillInBackground guard turns that
|
|
123
|
+
late shot into a non-event, never into a reload under the person's eyes. */
|
|
124
|
+
async function applyAfterGrace() {
|
|
125
|
+
applyTimer = null;
|
|
126
|
+
const stillInBackground = appState.currentState !== 'active';
|
|
127
|
+
if (!shouldApplyUpdate({ downloaded, stillInBackground, pendingWrites: Number(pendingWrites()) || 0 })) return;
|
|
128
|
+
downloaded = false;
|
|
129
|
+
try {
|
|
130
|
+
await updates.reloadAsync();
|
|
131
|
+
} catch (error) {
|
|
132
|
+
onError(error, { where: 'reloadAsync' });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function cancelApply() {
|
|
137
|
+
if (applyTimer) clearTimeout(applyTimer);
|
|
138
|
+
applyTimer = null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function onAppStateChange(state) {
|
|
142
|
+
if (state === 'active') {
|
|
143
|
+
/* The person is back: the deferred reload no longer has a reason to be. */
|
|
144
|
+
cancelApply();
|
|
145
|
+
void check();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (state === 'background' && downloaded && !applyTimer) {
|
|
149
|
+
applyTimer = setTimeout(() => void applyAfterGrace(), graceMs);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
/** Call once, when the root mounts. Idempotent. */
|
|
155
|
+
start() {
|
|
156
|
+
if (subscription) return;
|
|
157
|
+
subscription = appState.addEventListener('change', onAppStateChange) || { remove() {} };
|
|
158
|
+
void check();
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
stop() {
|
|
162
|
+
cancelApply();
|
|
163
|
+
if (subscription && typeof subscription.remove === 'function') subscription.remove();
|
|
164
|
+
subscription = null;
|
|
165
|
+
},
|
|
166
|
+
|
|
167
|
+
check,
|
|
168
|
+
|
|
169
|
+
/** Whether an update is downloaded and waiting for the next quiet moment. */
|
|
170
|
+
hasPendingUpdate: () => downloaded,
|
|
171
|
+
|
|
172
|
+
/** What this phone actually runs — for crash reports and a help screen. */
|
|
173
|
+
describeBuild(version) {
|
|
174
|
+
return {
|
|
175
|
+
updateId: updates.updateId ?? null,
|
|
176
|
+
channel: updates.channel ?? null,
|
|
177
|
+
runtimeVersion: updates.runtimeVersion ?? null,
|
|
178
|
+
isEmbeddedLaunch: Boolean(updates.isEmbeddedLaunch),
|
|
179
|
+
release: describeRelease({
|
|
180
|
+
version: version ?? null,
|
|
181
|
+
updateId: updates.updateId,
|
|
182
|
+
isEmbeddedLaunch: updates.isEmbeddedLaunch
|
|
183
|
+
})
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
module.exports = {
|
|
190
|
+
UPDATE_CHECK_INTERVAL_MS,
|
|
191
|
+
UPDATE_APPLY_GRACE_MS,
|
|
192
|
+
shouldCheckForUpdate,
|
|
193
|
+
shouldApplyUpdate,
|
|
194
|
+
describeRelease,
|
|
195
|
+
createUpdateWatcher
|
|
196
|
+
};
|