senza-sdk 4.2.44 → 4.2.46-2f4b6fa.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "senza-sdk",
3
- "version": "4.2.44",
3
+ "version": "4.2.46-2f4b6fa.0",
4
4
  "main": "./src/api.js",
5
5
  "description": "API for Senza application",
6
6
  "license": "MIT",
@@ -27,18 +27,32 @@ class AlarmManager extends EventTarget {
27
27
  constructor() {
28
28
  super();
29
29
  this._eventListeners = {};
30
+ this._isFirstAlarm = true;
30
31
  typeof document !== "undefined" && document.addEventListener("hs/alarmFiredEvent", (e) => {
31
32
  const logger = sdkLogger.withFields({alarmName: e.detail?.alarmName});
32
33
  logger.log("Got hs/alarmFiredEvent", JSON.stringify(e.detail));
33
34
  if (e.detail?.alarmName) {
34
35
  if (this._eventListeners[e.detail.alarmName]) {
35
36
  const event = new CustomEvent(e.detail.alarmName, {detail: e.detail.payload});
37
+ const timeBeforeAlarmHandling = Date.now();
38
+ let uiStateBeforeAlarmHandling;
39
+ if (lifecycle.state === lifecycle.UiState.FOREGROUND || lifecycle.state === lifecycle.UiState.IN_TRANSITION_TO_FOREGROUND) {
40
+ uiStateBeforeAlarmHandling = "foreground";
41
+ } else {
42
+ uiStateBeforeAlarmHandling = "background";
43
+ }
36
44
  Promise.allSettled(Array.from(this._eventListeners[e.detail.alarmName], cb => cb(event))).then(() => {
37
- logger.log(`All callbacks for alarm '${e.detail.alarmName}' are finished`);
45
+ const alarmHandlingDuration = Date.now() - timeBeforeAlarmHandling;
46
+ logger.log(`All callbacks for alarm '${e.detail.alarmName}' are finished within ${alarmHandlingDuration}ms`);
38
47
  // If the application was triggered due to an alarm, and after all callbacks are handled, the application is still in the background and
39
48
  // moveToForeground has not been called as a result of the alarm, we want to intentionally terminate the ui in order to save resources.
40
- if (getTriggerEvent().type === "alarmFiredEvent" && lifecycle.state === lifecycle.UiState.BACKGROUND && !this._moveToForegroundHasBeenCalled) {
41
- if (window.cefQuery) {
49
+ let isTriggering = false;
50
+ if (getTriggerEvent().type === "alarmFiredEvent" && lifecycle.state === lifecycle.UiState.BACKGROUND) {
51
+ if (this._isFirstAlarm) {
52
+ isTriggering = true;
53
+ this._isFirstAlarm = false;
54
+ }
55
+ if (!this._moveToForegroundHasBeenCalled && window.cefQuery) {
42
56
  logger.log("Application is about to be terminated since didn't move to foreground");
43
57
  const message = { type: "terminating" };
44
58
  const request = { target:"TC", waitForResponse: false, message: JSON.stringify(message) };
@@ -54,6 +68,22 @@ class AlarmManager extends EventTarget {
54
68
  });
55
69
  }
56
70
  }
71
+ // For monitoring purposes, log a json with all the necessary labels and a unique prefix [hs-sdk][metrics]
72
+ // The ui-streamer will recognize this prefix and handle the json accordingly.
73
+ let uiStateAfterAlarmHandling;
74
+ if (lifecycle.state === lifecycle.UiState.FOREGROUND || lifecycle.state === lifecycle.UiState.IN_TRANSITION_TO_FOREGROUND) {
75
+ uiStateAfterAlarmHandling = "foreground";
76
+ } else {
77
+ uiStateAfterAlarmHandling = "background";
78
+ }
79
+ logger.metrics({
80
+ type: "monitorAlarm",
81
+ alarmName: e.detail.alarmName,
82
+ isTriggering,
83
+ uiStateBefore: uiStateBeforeAlarmHandling,
84
+ uiStateAfter: uiStateAfterAlarmHandling,
85
+ alarmHandlingDuration
86
+ });
57
87
  });
58
88
  }
59
89
  }
package/src/utils.js CHANGED
@@ -38,6 +38,9 @@ class SdkLogger {
38
38
  error(...data) {
39
39
  console.error(this.formatLogString(data));
40
40
  }
41
+ metrics(metricObj) {
42
+ console.info(`[hs-sdk] [metrics] ${JSON.stringify(metricObj)}`);
43
+ }
41
44
  withFields(logFields) {
42
45
  return new SdkLogger({...this.logFields, ...logFields});
43
46
  }