@hot-updater/plugin-insights 1.0.0-rc.40 → 1.0.0-rc.41

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.
@@ -21,16 +21,22 @@ const normalizeUserId = (user) => {
21
21
  * ```ts
22
22
  * import { HotUpdater, insights } from "@hot-updater/react-native";
23
23
  *
24
- * const analytics = insights();
25
- * HotUpdater.init({ baseURL, plugins: [analytics] });
26
- * analytics.setUser({ userId: "user-123" });
24
+ * export const hotUpdater = HotUpdater.init({ baseURL, plugins: [insights()] });
25
+ * hotUpdater.insights.setUser({ userId: "user-123" });
27
26
  * ```
28
27
  */
29
- const insights = (options = {}) => {
28
+ const insights = (options = {}) => (0, _hot_updater_protocol.defineClientPlugin)({
29
+ id: "insights",
30
+ setup: (pluginContext) => setUpInsights(options, pluginContext)
31
+ });
32
+ /**
33
+ * One instance's Insights: its context, device state, and delivery queue,
34
+ * created by each `setup`, so a plugin object set up by two instances keeps
35
+ * their reports apart.
36
+ */
37
+ const setUpInsights = (options, pluginContext) => {
30
38
  let context = null;
31
39
  let state = null;
32
- /** A user set before setup, applied once the plugin has storage. */
33
- let pendingUser = null;
34
40
  /** A download in this runtime: a later no-change report would hide it. */
35
41
  let didDownload = false;
36
42
  let lastDownloadedSelection = null;
@@ -217,37 +223,27 @@ const insights = (options = {}) => {
217
223
  if (event.report !== null) state.recordReport(event.report);
218
224
  else state.forgetReport();
219
225
  };
220
- return (0, _hot_updater_protocol.defineClientPlugin)({
221
- id: "insights",
222
- setup(pluginContext) {
223
- context = pluginContext;
224
- state = require_state.createInsightsState(pluginContext.storage);
225
- if (pendingUser !== null) {
226
- state.writeUserId(pendingUser.userId);
227
- pendingUser = null;
228
- }
229
- if (pluginContext.isDebugBuild && options.debug !== true) return;
230
- send = require_sender.createInsightsEventSender(pluginContext.fetch, {
231
- admit,
232
- settle
233
- });
234
- return {
235
- onAppReady,
236
- onUpdateCheck,
237
- onBundleDownloaded,
238
- onUpdateError,
239
- onHttpResponse
240
- };
241
- },
242
- setUser(user) {
243
- const userId = normalizeUserId(user);
244
- if (state === null) {
245
- pendingUser = { userId };
246
- return;
247
- }
248
- state.writeUserId(userId);
249
- }
226
+ context = pluginContext;
227
+ const pluginState = require_state.createInsightsState(pluginContext.storage);
228
+ state = pluginState;
229
+ const api = Object.freeze({ setUser: (user) => {
230
+ pluginState.writeUserId(normalizeUserId(user));
231
+ } });
232
+ if (pluginContext.isDebugBuild && options.debug !== true) return { api };
233
+ send = require_sender.createInsightsEventSender(pluginContext.fetch, {
234
+ admit,
235
+ settle
250
236
  });
237
+ return {
238
+ api,
239
+ hooks: {
240
+ onAppReady,
241
+ onUpdateCheck,
242
+ onBundleDownloaded,
243
+ onUpdateError,
244
+ onHttpResponse
245
+ }
246
+ };
251
247
  };
252
248
  //#endregion
253
249
  exports.insights = insights;
@@ -11,15 +11,16 @@ interface InsightsOptions {
11
11
  interface InsightsUser {
12
12
  readonly userId: string | number;
13
13
  }
14
- interface InsightsPlugin extends HotUpdaterClientPlugin {
15
- readonly id: "insights";
14
+ /** Insights on the instance `HotUpdater.init` returns, as `hotUpdater.insights`. */
15
+ interface InsightsClient {
16
16
  /**
17
17
  * Attaches the signed-in user's id to later reports, or clears it with
18
- * `null`. It persists on the device, sends nothing by itself, and can be
19
- * called before `HotUpdater.init`.
18
+ * `null`. It persists on the device and sends nothing by itself.
20
19
  */
21
20
  setUser(user: InsightsUser | null): void;
22
21
  }
22
+ /** The `insights()` plugin: it adds `hotUpdater.insights`. */
23
+ type InsightsPlugin = HotUpdaterClientPlugin<"insights", InsightsClient>;
23
24
  /**
24
25
  * Reports update adoption to a server that runs the `insights()` server
25
26
  * plugin: launches (at most once a UTC day while nothing changes), downloads,
@@ -31,11 +32,10 @@ interface InsightsPlugin extends HotUpdaterClientPlugin {
31
32
  * ```ts
32
33
  * import { HotUpdater, insights } from "@hot-updater/react-native";
33
34
  *
34
- * const analytics = insights();
35
- * HotUpdater.init({ baseURL, plugins: [analytics] });
36
- * analytics.setUser({ userId: "user-123" });
35
+ * export const hotUpdater = HotUpdater.init({ baseURL, plugins: [insights()] });
36
+ * hotUpdater.insights.setUser({ userId: "user-123" });
37
37
  * ```
38
38
  */
39
39
  declare const insights: (options?: InsightsOptions) => InsightsPlugin;
40
40
  //#endregion
41
- export { InsightsOptions, InsightsPlugin, InsightsUser, insights };
41
+ export { InsightsClient, InsightsOptions, InsightsPlugin, InsightsUser, insights };
@@ -11,15 +11,16 @@ interface InsightsOptions {
11
11
  interface InsightsUser {
12
12
  readonly userId: string | number;
13
13
  }
14
- interface InsightsPlugin extends HotUpdaterClientPlugin {
15
- readonly id: "insights";
14
+ /** Insights on the instance `HotUpdater.init` returns, as `hotUpdater.insights`. */
15
+ interface InsightsClient {
16
16
  /**
17
17
  * Attaches the signed-in user's id to later reports, or clears it with
18
- * `null`. It persists on the device, sends nothing by itself, and can be
19
- * called before `HotUpdater.init`.
18
+ * `null`. It persists on the device and sends nothing by itself.
20
19
  */
21
20
  setUser(user: InsightsUser | null): void;
22
21
  }
22
+ /** The `insights()` plugin: it adds `hotUpdater.insights`. */
23
+ type InsightsPlugin = HotUpdaterClientPlugin<"insights", InsightsClient>;
23
24
  /**
24
25
  * Reports update adoption to a server that runs the `insights()` server
25
26
  * plugin: launches (at most once a UTC day while nothing changes), downloads,
@@ -31,11 +32,10 @@ interface InsightsPlugin extends HotUpdaterClientPlugin {
31
32
  * ```ts
32
33
  * import { HotUpdater, insights } from "@hot-updater/react-native";
33
34
  *
34
- * const analytics = insights();
35
- * HotUpdater.init({ baseURL, plugins: [analytics] });
36
- * analytics.setUser({ userId: "user-123" });
35
+ * export const hotUpdater = HotUpdater.init({ baseURL, plugins: [insights()] });
36
+ * hotUpdater.insights.setUser({ userId: "user-123" });
37
37
  * ```
38
38
  */
39
39
  declare const insights: (options?: InsightsOptions) => InsightsPlugin;
40
40
  //#endregion
41
- export { InsightsOptions, InsightsPlugin, InsightsUser, insights };
41
+ export { InsightsClient, InsightsOptions, InsightsPlugin, InsightsUser, insights };
@@ -20,16 +20,22 @@ const normalizeUserId = (user) => {
20
20
  * ```ts
21
21
  * import { HotUpdater, insights } from "@hot-updater/react-native";
22
22
  *
23
- * const analytics = insights();
24
- * HotUpdater.init({ baseURL, plugins: [analytics] });
25
- * analytics.setUser({ userId: "user-123" });
23
+ * export const hotUpdater = HotUpdater.init({ baseURL, plugins: [insights()] });
24
+ * hotUpdater.insights.setUser({ userId: "user-123" });
26
25
  * ```
27
26
  */
28
- const insights = (options = {}) => {
27
+ const insights = (options = {}) => defineClientPlugin({
28
+ id: "insights",
29
+ setup: (pluginContext) => setUpInsights(options, pluginContext)
30
+ });
31
+ /**
32
+ * One instance's Insights: its context, device state, and delivery queue,
33
+ * created by each `setup`, so a plugin object set up by two instances keeps
34
+ * their reports apart.
35
+ */
36
+ const setUpInsights = (options, pluginContext) => {
29
37
  let context = null;
30
38
  let state = null;
31
- /** A user set before setup, applied once the plugin has storage. */
32
- let pendingUser = null;
33
39
  /** A download in this runtime: a later no-change report would hide it. */
34
40
  let didDownload = false;
35
41
  let lastDownloadedSelection = null;
@@ -216,37 +222,27 @@ const insights = (options = {}) => {
216
222
  if (event.report !== null) state.recordReport(event.report);
217
223
  else state.forgetReport();
218
224
  };
219
- return defineClientPlugin({
220
- id: "insights",
221
- setup(pluginContext) {
222
- context = pluginContext;
223
- state = createInsightsState(pluginContext.storage);
224
- if (pendingUser !== null) {
225
- state.writeUserId(pendingUser.userId);
226
- pendingUser = null;
227
- }
228
- if (pluginContext.isDebugBuild && options.debug !== true) return;
229
- send = createInsightsEventSender(pluginContext.fetch, {
230
- admit,
231
- settle
232
- });
233
- return {
234
- onAppReady,
235
- onUpdateCheck,
236
- onBundleDownloaded,
237
- onUpdateError,
238
- onHttpResponse
239
- };
240
- },
241
- setUser(user) {
242
- const userId = normalizeUserId(user);
243
- if (state === null) {
244
- pendingUser = { userId };
245
- return;
246
- }
247
- state.writeUserId(userId);
248
- }
225
+ context = pluginContext;
226
+ const pluginState = createInsightsState(pluginContext.storage);
227
+ state = pluginState;
228
+ const api = Object.freeze({ setUser: (user) => {
229
+ pluginState.writeUserId(normalizeUserId(user));
230
+ } });
231
+ if (pluginContext.isDebugBuild && options.debug !== true) return { api };
232
+ send = createInsightsEventSender(pluginContext.fetch, {
233
+ admit,
234
+ settle
249
235
  });
236
+ return {
237
+ api,
238
+ hooks: {
239
+ onAppReady,
240
+ onUpdateCheck,
241
+ onBundleDownloaded,
242
+ onUpdateError,
243
+ onHttpResponse
244
+ }
245
+ };
250
246
  };
251
247
  //#endregion
252
248
  export { insights };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hot-updater/plugin-insights",
3
3
  "type": "module",
4
- "version": "1.0.0-rc.40",
4
+ "version": "1.0.0-rc.41",
5
5
  "description": "Hot Updater's Insights plugin: launch, download, and update failure events, for the server and the React Native app",
6
6
  "exports": {
7
7
  "./client": {
@@ -33,10 +33,10 @@
33
33
  "package.json"
34
34
  ],
35
35
  "dependencies": {
36
- "@hot-updater/protocol": "1.0.0-rc.40"
36
+ "@hot-updater/protocol": "1.0.0-rc.41"
37
37
  },
38
38
  "peerDependencies": {
39
- "@hot-updater/plugin-core": "^1.0.0-rc.40"
39
+ "@hot-updater/plugin-core": "^1.0.0-rc.41"
40
40
  },
41
41
  "peerDependenciesMeta": {
42
42
  "@hot-updater/plugin-core": {
@@ -46,8 +46,8 @@
46
46
  "devDependencies": {
47
47
  "@types/node": "^20",
48
48
  "vitest": "4.1.4",
49
- "@hot-updater/plugin-core": "1.0.0-rc.40",
50
- "@hot-updater/test-utils": "1.0.0-rc.40"
49
+ "@hot-updater/test-utils": "1.0.0-rc.41",
50
+ "@hot-updater/plugin-core": "1.0.0-rc.41"
51
51
  },
52
52
  "nx": {
53
53
  "implicitDependencies": [