@take-out/native-hot-update 0.4.3 → 0.4.4

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.
@@ -2,171 +2,215 @@ var __create = Object.create;
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf,
6
- __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
7
  var __export = (target, all) => {
8
- for (var name in all) __defProp(target, name, {
9
- get: all[name],
10
- enumerable: !0
11
- });
12
- },
13
- __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true
11
+ });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
16
  get: () => from[key],
16
17
  enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
18
  });
18
- return to;
19
- };
19
+ }
20
+ return to;
21
+ };
20
22
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
- value: mod,
27
- enumerable: !0
28
- }) : target, mod)),
29
- __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
30
- value: !0
31
- }), mod);
23
+ // If the importer is in node compatibility mode or this is not an ESM
24
+ // file that has been converted to a CommonJS file using a Babel-
25
+ // compatible transform (i.e. "__esModule" has not been set), then set
26
+ // "default" to the CommonJS "module.exports" for node compatibility.
27
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
28
+ value: mod,
29
+ enumerable: true
30
+ }) : target, mod));
31
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
+ value: true
33
+ }), mod);
32
34
  var createHotUpdater_exports = {};
33
35
  __export(createHotUpdater_exports, {
34
36
  createHotUpdater: () => createHotUpdater
35
37
  });
36
38
  module.exports = __toCommonJS(createHotUpdater_exports);
37
- var import_react_native = require("@hot-updater/react-native"),
38
- Application = __toESM(require("expo-application"), 1),
39
- import_react = require("react"),
40
- import_react_native2 = require("react-native-web");
41
- const INITIAL_OTA_ID = "00000000-0000-0000-0000-000000000000",
42
- BUNDLE_ID_KEY_PREFIX = "hotUpdater.bundleId",
43
- PRE_RELEASE_BUNDLE_ID_KEY = "hotUpdater.preReleaseBundleId";
39
+ var import_react_native = require("@hot-updater/react-native");
40
+ var Application = __toESM(require("expo-application"), 1);
41
+ var import_react = require("react");
42
+ var import_react_native2 = require("react-native-web");
43
+ const INITIAL_OTA_ID = "00000000-0000-0000-0000-000000000000";
44
+ const BUNDLE_ID_KEY_PREFIX = "hotUpdater.bundleId";
45
+ const PRE_RELEASE_BUNDLE_ID_KEY = "hotUpdater.preReleaseBundleId";
44
46
  function createHotUpdater(config) {
45
47
  const {
46
48
  storage,
47
49
  updateStrategy = "appVersion"
48
50
  } = config;
49
- let isUpdatePending = !1;
51
+ let isUpdatePending = false;
50
52
  const getAppliedOta = () => {
51
- const id = import_react_native.HotUpdater.getBundleId();
52
- return id === INITIAL_OTA_ID || id === import_react_native.HotUpdater.getMinBundleId() ? null : id;
53
- },
54
- getShortOtaId = () => {
55
- const fullId = getAppliedOta();
56
- return fullId ? fullId.slice(-12) : null;
57
- },
58
- getIsUpdatePending = () => isUpdatePending,
59
- getPreReleaseBundleId = () => storage.get(PRE_RELEASE_BUNDLE_ID_KEY),
60
- handleUpdateDownloaded = (id, options = {}) => {
61
- const appVersion = Application.nativeApplicationVersion;
62
- appVersion && storage.set(`${BUNDLE_ID_KEY_PREFIX}.${appVersion}`, id), options.isPreRelease ? storage.set(PRE_RELEASE_BUNDLE_ID_KEY, id) : storage.delete(PRE_RELEASE_BUNDLE_ID_KEY), isUpdatePending = !0;
63
- };
64
- return {
65
- useOtaUpdater: (options = {}) => {
66
- const {
67
- enabled = !0,
68
- onUpdateDownloaded: onUpdateDownloadedCallback,
69
- onError
70
- } = options,
71
- progress = (0, import_react_native.useHotUpdaterStore)(state => state.progress),
72
- isUpdating = progress > 0.01,
73
- [isUserClearedForAccess, setIsUserClearedForAccess] = (0, import_react.useState)(!1),
74
- isUserClearedForAccessRef = (0, import_react.useRef)(isUserClearedForAccess);
75
- return isUserClearedForAccessRef.current = isUserClearedForAccess, (0, import_react.useEffect)(() => {
76
- if (!isUpdating) {
77
- const timer = setTimeout(() => {
78
- isUserClearedForAccessRef.current || setIsUserClearedForAccess(!0);
79
- }, 5e3);
80
- return () => clearTimeout(timer);
81
- }
82
- }, [isUpdating]), (0, import_react.useEffect)(() => {
53
+ const id = import_react_native.HotUpdater.getBundleId();
54
+ if (id === INITIAL_OTA_ID) return null;
55
+ if (id === import_react_native.HotUpdater.getMinBundleId()) return null;
56
+ return id;
57
+ };
58
+ const getShortOtaId = () => {
59
+ const fullId = getAppliedOta();
60
+ return fullId ? fullId.slice(-12) : null;
61
+ };
62
+ const getIsUpdatePending = () => {
63
+ return isUpdatePending;
64
+ };
65
+ const getPreReleaseBundleId = () => {
66
+ return storage.get(PRE_RELEASE_BUNDLE_ID_KEY);
67
+ };
68
+ const handleUpdateDownloaded = (id, options = {}) => {
69
+ const appVersion = Application.nativeApplicationVersion;
70
+ if (appVersion) {
71
+ storage.set(`${BUNDLE_ID_KEY_PREFIX}.${appVersion}`, id);
72
+ }
73
+ if (options.isPreRelease) {
74
+ storage.set(PRE_RELEASE_BUNDLE_ID_KEY, id);
75
+ } else {
76
+ storage.delete(PRE_RELEASE_BUNDLE_ID_KEY);
77
+ }
78
+ isUpdatePending = true;
79
+ };
80
+ const useOtaUpdater = (options = {}) => {
81
+ const {
82
+ enabled = true,
83
+ onUpdateDownloaded: onUpdateDownloadedCallback,
84
+ onError
85
+ } = options;
86
+ const progress = (0, import_react_native.useHotUpdaterStore)(state => state.progress);
87
+ const isUpdating = progress > 0.01;
88
+ const [isUserClearedForAccess, setIsUserClearedForAccess] = (0, import_react.useState)(false);
89
+ const isUserClearedForAccessRef = (0, import_react.useRef)(isUserClearedForAccess);
90
+ isUserClearedForAccessRef.current = isUserClearedForAccess;
91
+ (0, import_react.useEffect)(() => {
92
+ if (!isUpdating) {
83
93
  const timer = setTimeout(() => {
84
- isUserClearedForAccessRef.current || setIsUserClearedForAccess(!0);
85
- }, 2e4);
94
+ if (!isUserClearedForAccessRef.current) {
95
+ setIsUserClearedForAccess(true);
96
+ }
97
+ }, 5e3);
86
98
  return () => clearTimeout(timer);
87
- }, []), (0, import_react.useEffect)(() => {
88
- let shouldContinue = !0;
89
- return (async () => {
90
- try {
91
- if (!enabled) {
92
- setIsUserClearedForAccess(!0);
99
+ }
100
+ return void 0;
101
+ }, [isUpdating]);
102
+ (0, import_react.useEffect)(() => {
103
+ const timer = setTimeout(() => {
104
+ if (!isUserClearedForAccessRef.current) {
105
+ setIsUserClearedForAccess(true);
106
+ }
107
+ }, 2e4);
108
+ return () => clearTimeout(timer);
109
+ }, []);
110
+ (0, import_react.useEffect)(() => {
111
+ let shouldContinue = true;
112
+ (async () => {
113
+ try {
114
+ if (!enabled) {
115
+ setIsUserClearedForAccess(true);
116
+ return;
117
+ }
118
+ if (!shouldContinue) return;
119
+ const updateInfo = await import_react_native.HotUpdater.checkForUpdate({
120
+ updateStrategy
121
+ });
122
+ if (!updateInfo) {
123
+ setIsUserClearedForAccess(true);
124
+ return;
125
+ }
126
+ if (!shouldContinue) return;
127
+ if (updateInfo.status === "ROLLBACK") {
128
+ const preReleaseBundleId = getPreReleaseBundleId();
129
+ const currentBundleId = import_react_native.HotUpdater.getBundleId();
130
+ if (preReleaseBundleId && preReleaseBundleId === currentBundleId && currentBundleId > updateInfo.id) {
131
+ import_react_native2.Alert.alert("Update Skipped", "Skipped rollback because you are using a newer pre-release bundle.");
132
+ setIsUserClearedForAccess(true);
93
133
  return;
94
134
  }
95
- if (!shouldContinue) return;
96
- const updateInfo = await import_react_native.HotUpdater.checkForUpdate({
97
- updateStrategy
98
- });
99
- if (!updateInfo) {
100
- setIsUserClearedForAccess(!0);
135
+ }
136
+ if (!updateInfo.shouldForceUpdate) {
137
+ setIsUserClearedForAccess(true);
138
+ }
139
+ if (!shouldContinue) return;
140
+ await updateInfo.updateBundle();
141
+ handleUpdateDownloaded(updateInfo.id);
142
+ const info = {
143
+ id: updateInfo.id,
144
+ isCriticalUpdate: updateInfo.shouldForceUpdate,
145
+ fileUrl: updateInfo.fileUrl,
146
+ message: updateInfo.message
147
+ };
148
+ onUpdateDownloadedCallback?.(info);
149
+ if (!shouldContinue) return;
150
+ if (updateInfo.shouldForceUpdate) {
151
+ if (!isUserClearedForAccessRef.current) {
152
+ import_react_native.HotUpdater.reload();
101
153
  return;
102
154
  }
103
- if (!shouldContinue) return;
104
- if (updateInfo.status === "ROLLBACK") {
105
- const preReleaseBundleId = getPreReleaseBundleId(),
106
- currentBundleId = import_react_native.HotUpdater.getBundleId();
107
- if (preReleaseBundleId && preReleaseBundleId === currentBundleId && currentBundleId > updateInfo.id) {
108
- import_react_native2.Alert.alert("Update Skipped", "Skipped rollback because you are using a newer pre-release bundle."), setIsUserClearedForAccess(!0);
109
- return;
110
- }
111
- }
112
- if (updateInfo.shouldForceUpdate || setIsUserClearedForAccess(!0), !shouldContinue) return;
113
- await updateInfo.updateBundle(), handleUpdateDownloaded(updateInfo.id);
114
- const info = {
115
- id: updateInfo.id,
116
- isCriticalUpdate: updateInfo.shouldForceUpdate,
117
- fileUrl: updateInfo.fileUrl,
118
- message: updateInfo.message
119
- };
120
- if (onUpdateDownloadedCallback?.(info), !shouldContinue) return;
121
- if (updateInfo.shouldForceUpdate) {
122
- if (!isUserClearedForAccessRef.current) {
123
- import_react_native.HotUpdater.reload();
124
- return;
125
- }
126
- import_react_native2.Alert.alert("Update Downloaded", "An important update has been downloaded. Reload now to apply it?", [{
127
- text: "Later",
128
- style: "cancel"
129
- }, {
130
- text: "Reload Now",
131
- onPress: () => import_react_native.HotUpdater.reload()
132
- }]);
133
- }
134
- } catch (error) {
135
- onError?.(error), setIsUserClearedForAccess(!0);
136
- } finally {
137
- isUserClearedForAccessRef.current || setIsUserClearedForAccess(!0);
155
+ import_react_native2.Alert.alert("Update Downloaded", "An important update has been downloaded. Reload now to apply it?", [{
156
+ text: "Later",
157
+ style: "cancel"
158
+ }, {
159
+ text: "Reload Now",
160
+ onPress: () => import_react_native.HotUpdater.reload()
161
+ }]);
138
162
  }
139
- })(), () => {
140
- shouldContinue = !1;
141
- };
142
- }, [enabled, onUpdateDownloadedCallback, onError]), {
143
- userClearedForAccess: isUserClearedForAccess,
144
- progress,
145
- isUpdatePending: getIsUpdatePending()
163
+ } catch (error) {
164
+ onError?.(error);
165
+ setIsUserClearedForAccess(true);
166
+ } finally {
167
+ if (!isUserClearedForAccessRef.current) {
168
+ setIsUserClearedForAccess(true);
169
+ }
170
+ }
171
+ })();
172
+ return () => {
173
+ shouldContinue = false;
146
174
  };
147
- },
148
- checkForUpdate: async (options = {}) => {
149
- const {
150
- channel,
151
- isPreRelease = !1
152
- } = options,
153
- requestHeaders = {};
154
- channel && (requestHeaders["X-Channel"] = channel);
155
- const updateInfo = await import_react_native.HotUpdater.checkForUpdate({
156
- updateStrategy,
157
- ...(Object.keys(requestHeaders).length > 0 && {
158
- requestHeaders
159
- })
160
- });
161
- return updateInfo ? (await updateInfo.updateBundle(), handleUpdateDownloaded(updateInfo.id, {
175
+ }, [enabled, onUpdateDownloadedCallback, onError]);
176
+ return {
177
+ userClearedForAccess: isUserClearedForAccess,
178
+ progress,
179
+ isUpdatePending: getIsUpdatePending()
180
+ };
181
+ };
182
+ const checkForUpdate = async (options = {}) => {
183
+ const {
184
+ channel,
185
+ isPreRelease = false
186
+ } = options;
187
+ const requestHeaders = {};
188
+ if (channel) {
189
+ requestHeaders["X-Channel"] = channel;
190
+ }
191
+ const updateInfo = await import_react_native.HotUpdater.checkForUpdate({
192
+ updateStrategy,
193
+ ...(Object.keys(requestHeaders).length > 0 && {
194
+ requestHeaders
195
+ })
196
+ });
197
+ if (updateInfo) {
198
+ await updateInfo.updateBundle();
199
+ handleUpdateDownloaded(updateInfo.id, {
162
200
  isPreRelease
163
- }), {
201
+ });
202
+ return {
164
203
  id: updateInfo.id,
165
204
  isCriticalUpdate: updateInfo.shouldForceUpdate,
166
205
  fileUrl: updateInfo.fileUrl,
167
206
  message: updateInfo.message
168
- }) : null;
169
- },
207
+ };
208
+ }
209
+ return null;
210
+ };
211
+ return {
212
+ useOtaUpdater,
213
+ checkForUpdate,
170
214
  getAppliedOta,
171
215
  getShortOtaId,
172
216
  getIsUpdatePending,