@hellotext/hellotext 2.5.7 → 2.5.8

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.
Files changed (102) hide show
  1. package/README.md +0 -25
  2. package/dist/hellotext.js +1 -1
  3. package/index.d.ts +0 -6
  4. package/lib/api/acks.cjs +17 -31
  5. package/lib/api/acks.js +17 -42
  6. package/lib/api/businesses.cjs +14 -28
  7. package/lib/api/businesses.js +13 -36
  8. package/lib/api/events.cjs +26 -40
  9. package/lib/api/events.js +28 -52
  10. package/lib/api/forms.cjs +24 -40
  11. package/lib/api/forms.js +24 -57
  12. package/lib/api/identifications.cjs +15 -29
  13. package/lib/api/identifications.js +15 -40
  14. package/lib/api/index.cjs +25 -51
  15. package/lib/api/index.js +27 -54
  16. package/lib/api/push/identities.cjs +30 -46
  17. package/lib/api/push/identities.js +30 -63
  18. package/lib/api/response.cjs +30 -52
  19. package/lib/api/response.js +31 -60
  20. package/lib/api/submissions.cjs +18 -34
  21. package/lib/api/submissions.js +18 -48
  22. package/lib/api/webchat/messages.cjs +44 -64
  23. package/lib/api/webchat/messages.js +44 -80
  24. package/lib/api/webchats.cjs +37 -56
  25. package/lib/api/webchats.js +37 -65
  26. package/lib/api/whatsapp_widgets.cjs +49 -72
  27. package/lib/api/whatsapp_widgets.js +48 -91
  28. package/lib/builders/input_builder.cjs +36 -48
  29. package/lib/builders/input_builder.js +36 -48
  30. package/lib/builders/logo_builder.cjs +10 -29
  31. package/lib/builders/logo_builder.js +20 -28
  32. package/lib/channels/application_channel.cjs +136 -178
  33. package/lib/channels/application_channel.js +137 -180
  34. package/lib/channels/webchat_channel.cjs +125 -170
  35. package/lib/channels/webchat_channel.js +125 -170
  36. package/lib/controllers/form_controller.cjs +82 -121
  37. package/lib/controllers/form_controller.js +82 -130
  38. package/lib/controllers/message_controller.cjs +211 -282
  39. package/lib/controllers/message_controller.js +209 -284
  40. package/lib/controllers/mixins/usePopover.cjs +3 -6
  41. package/lib/controllers/mixins/usePopover.js +17 -22
  42. package/lib/controllers/webchat/emoji_picker_controller.cjs +106 -150
  43. package/lib/controllers/webchat/emoji_picker_controller.js +105 -170
  44. package/lib/controllers/webchat/useBehaviour.js +5 -5
  45. package/lib/controllers/webchat/useOpeningSequence.js +6 -6
  46. package/lib/controllers/webchat/useTeaser.js +9 -9
  47. package/lib/controllers/webchat_controller.cjs +916 -1088
  48. package/lib/controllers/webchat_controller.js +926 -1152
  49. package/lib/core/configuration/forms.cjs +21 -35
  50. package/lib/core/configuration/forms.js +20 -35
  51. package/lib/core/configuration/locale.cjs +36 -71
  52. package/lib/core/configuration/locale.js +36 -70
  53. package/lib/core/configuration/push.cjs +9 -21
  54. package/lib/core/configuration/push.js +8 -20
  55. package/lib/core/configuration/webchat.cjs +142 -182
  56. package/lib/core/configuration/webchat.js +145 -189
  57. package/lib/core/configuration/whatsapp.cjs +76 -102
  58. package/lib/core/configuration/whatsapp.js +76 -105
  59. package/lib/core/configuration.cjs +60 -78
  60. package/lib/core/configuration.js +59 -78
  61. package/lib/core/event.cjs +36 -59
  62. package/lib/core/event.js +35 -61
  63. package/lib/core/sanitize_html.js +4 -4
  64. package/lib/errors/invalid_event.cjs +5 -26
  65. package/lib/errors/invalid_event.js +5 -26
  66. package/lib/errors/not_initialized_error.cjs +5 -26
  67. package/lib/errors/not_initialized_error.js +5 -26
  68. package/lib/hellotext.cjs +185 -223
  69. package/lib/hellotext.js +188 -247
  70. package/lib/index.js +1 -1
  71. package/lib/models/business.cjs +114 -152
  72. package/lib/models/business.js +117 -162
  73. package/lib/models/cookies.cjs +34 -51
  74. package/lib/models/cookies.js +34 -51
  75. package/lib/models/fingerprint.cjs +8 -23
  76. package/lib/models/fingerprint.js +25 -58
  77. package/lib/models/form.cjs +106 -142
  78. package/lib/models/form.js +105 -153
  79. package/lib/models/form_collection.cjs +63 -101
  80. package/lib/models/form_collection.js +61 -107
  81. package/lib/models/page.cjs +106 -129
  82. package/lib/models/page.js +106 -131
  83. package/lib/models/push.cjs +278 -322
  84. package/lib/models/push.js +284 -357
  85. package/lib/models/query.cjs +21 -41
  86. package/lib/models/query.js +21 -41
  87. package/lib/models/session.cjs +33 -65
  88. package/lib/models/session.js +30 -63
  89. package/lib/models/user.cjs +29 -51
  90. package/lib/models/user.js +29 -51
  91. package/lib/models/utm.cjs +15 -27
  92. package/lib/models/utm.js +17 -32
  93. package/lib/models/webchat.cjs +52 -76
  94. package/lib/models/webchat.js +52 -90
  95. package/lib/models/whatsapp_widget.cjs +41 -59
  96. package/lib/models/whatsapp_widget.js +41 -73
  97. package/lib/vanilla.cjs +4 -7
  98. package/lib/vanilla.js +2 -1
  99. package/package.json +1 -9
  100. package/src/index.bundle.js +0 -1
  101. package/src/vanilla.js +2 -1
  102. package/styles/index.css +0 -30
@@ -1,13 +1,3 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
3
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
5
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
6
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
7
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
8
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
9
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
10
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
11
1
  import API from '../api';
12
2
  import { Configuration } from '../core';
13
3
 
@@ -16,13 +6,12 @@ import { Configuration } from '../core';
16
6
  *
17
7
  * @property {Promise<void>|null} ready - Initialization promise, available after initialize().
18
8
  */
19
- var Push = /*#__PURE__*/function () {
9
+ class Push {
20
10
  /**
21
11
  * @param {Object} data - Push configuration from the business response.
22
12
  * @param {String} data.public_key - Base64url-encoded VAPID public key.
23
13
  */
24
- function Push(data) {
25
- _classCallCheck(this, Push);
14
+ constructor(data) {
26
15
  this.publicKey = data.public_key;
27
16
  this.serviceWorkerUrl = Configuration.push.serviceWorkerUrl;
28
17
  this.channelId = Configuration.push.channelId;
@@ -42,371 +31,309 @@ var Push = /*#__PURE__*/function () {
42
31
  *
43
32
  * @returns {Promise<void>}
44
33
  */
45
- _createClass(Push, [{
46
- key: "initialize",
47
- value: function initialize() {
48
- this.ready = this.restoreSubscription();
49
- return this.ready;
34
+ initialize() {
35
+ this.ready = this.restoreSubscription();
36
+ return this.ready;
37
+ }
38
+
39
+ /**
40
+ * Registers an existing Hellotext subscription with the server without prompting.
41
+ *
42
+ * @private
43
+ * @returns {Promise<void>}
44
+ */
45
+ async restoreSubscription() {
46
+ const registration = await this.getRegistration();
47
+ const subscription = await registration.pushManager.getSubscription();
48
+ if (this.disposed || this.unsubscribePromise || !subscription || !this.owns(subscription)) {
49
+ return;
50
50
  }
51
+ this.subscription = subscription;
52
+ await this.sync();
53
+ }
51
54
 
52
- /**
53
- * Registers an existing Hellotext subscription with the server without prompting.
54
- *
55
- * @private
56
- * @returns {Promise<void>}
57
- */
58
- }, {
59
- key: "restoreSubscription",
60
- value: function () {
61
- var _restoreSubscription = _asyncToGenerator(function* () {
62
- var registration = yield this.getRegistration();
63
- var subscription = yield registration.pushManager.getSubscription();
64
- if (this.disposed || this.unsubscribePromise || !subscription || !this.owns(subscription)) {
65
- return;
66
- }
67
- this.subscription = subscription;
68
- yield this.sync();
69
- });
70
- function restoreSubscription() {
71
- return _restoreSubscription.apply(this, arguments);
72
- }
73
- return restoreSubscription;
74
- }()
75
- /**
76
- * Creates or reuses a Push subscription. Call from a user click handler.
77
- * Concurrent calls share the same pending request.
78
- *
79
- * @returns {Promise<import('../api/response').Response|void>} Server registration response,
80
- * or no value when disposed.
81
- * Rejects when permission is denied or a browser or network operation fails.
82
- */
83
- }, {
84
- key: "subscribe",
85
- value: function subscribe() {
86
- if (this.disposed) return Promise.resolve();
87
- if (this.unsubscribePromise) return Promise.reject(new Error('Push unsubscribe is in progress'));
88
- if (this.subscribePromise) return this.subscribePromise;
55
+ /**
56
+ * Creates or reuses a Push subscription. Call from a user click handler.
57
+ * Concurrent calls share the same pending request.
58
+ *
59
+ * @returns {Promise<import('../api/response').Response|void>} Server registration response,
60
+ * or no value when disposed.
61
+ * Rejects when permission is denied or a browser or network operation fails.
62
+ */
63
+ subscribe() {
64
+ if (this.disposed) return Promise.resolve();
65
+ if (this.unsubscribePromise) return Promise.reject(new Error('Push unsubscribe is in progress'));
66
+ if (this.subscribePromise) return this.subscribePromise;
89
67
 
90
- // Request permission before awaiting worker readiness to retain the click's user activation.
91
- var permission = Notification.permission === 'default' ? Notification.requestPermission() : Promise.resolve(Notification.permission);
92
- this.subscribePromise = this.createSubscription(permission).finally(() => {
93
- this.subscribePromise = null;
94
- });
95
- return this.subscribePromise;
96
- }
68
+ // Request permission before awaiting worker readiness to retain the click's user activation.
69
+ const permission = Notification.permission === 'default' ? Notification.requestPermission() : Promise.resolve(Notification.permission);
70
+ this.subscribePromise = this.createSubscription(permission).finally(() => {
71
+ this.subscribePromise = null;
72
+ });
73
+ return this.subscribePromise;
74
+ }
97
75
 
98
- /**
99
- * Waits for permission and worker readiness, then subscribes and registers with Hellotext.
100
- *
101
- * @private
102
- * @param {Promise<NotificationPermission>} permission - Pending notification permission result.
103
- * @returns {Promise<import('../api/response').Response|void>}
104
- */
105
- }, {
106
- key: "createSubscription",
107
- value: function () {
108
- var _createSubscription = _asyncToGenerator(function* (permission) {
109
- if ((yield permission) !== 'granted') throw new Error('Push permission was not granted');
110
- var registration = yield this.getRegistration();
111
- if (this.disposed) return;
112
- var subscription = yield registration.pushManager.getSubscription();
113
- if (this.disposed) return;
114
- if (subscription && !this.owns(subscription)) {
115
- throw new Error('The existing Push subscription belongs to a different application');
116
- }
117
- subscription || (subscription = yield registration.pushManager.subscribe({
118
- userVisibleOnly: true,
119
- applicationServerKey: this.applicationServerKey
120
- }));
121
- if (this.disposed) return;
122
- this.subscription = subscription;
123
- return this.sync();
124
- });
125
- function createSubscription(_x) {
126
- return _createSubscription.apply(this, arguments);
127
- }
128
- return createSubscription;
129
- }()
130
- /**
131
- * Disables the server identity and removes the browser subscription.
132
- * Keeps the subscription when the server request fails so the caller can retry.
133
- *
134
- * @returns {Promise<import('../api/response').Response|null|void>} Server response, null when
135
- * no subscription exists, or no value when disposed. Rejects when a browser or network
136
- * operation fails.
137
- */
138
- }, {
139
- key: "unsubscribe",
140
- value: function unsubscribe() {
141
- if (this.disposed) return Promise.resolve();
142
- if (this.unsubscribePromise) return this.unsubscribePromise;
143
- this.clearRetryTimeout();
144
- this.unsubscribePromise = this.removeSubscription().finally(() => {
145
- this.unsubscribePromise = null;
146
- });
147
- return this.unsubscribePromise;
76
+ /**
77
+ * Waits for permission and worker readiness, then subscribes and registers with Hellotext.
78
+ *
79
+ * @private
80
+ * @param {Promise<NotificationPermission>} permission - Pending notification permission result.
81
+ * @returns {Promise<import('../api/response').Response|void>}
82
+ */
83
+ async createSubscription(permission) {
84
+ if ((await permission) !== 'granted') throw new Error('Push permission was not granted');
85
+ const registration = await this.getRegistration();
86
+ if (this.disposed) return;
87
+ let subscription = await registration.pushManager.getSubscription();
88
+ if (this.disposed) return;
89
+ if (subscription && !this.owns(subscription)) {
90
+ throw new Error('The existing Push subscription belongs to a different application');
148
91
  }
92
+ subscription ||= await registration.pushManager.subscribe({
93
+ userVisibleOnly: true,
94
+ applicationServerKey: this.applicationServerKey
95
+ });
96
+ if (this.disposed) return;
97
+ this.subscription = subscription;
98
+ return this.sync();
99
+ }
149
100
 
150
- /**
151
- * Finishes pending registration before disabling and removing the subscription.
152
- *
153
- * @private
154
- * @returns {Promise<import('../api/response').Response|null|void>}
155
- */
156
- }, {
157
- key: "removeSubscription",
158
- value: function () {
159
- var _removeSubscription = _asyncToGenerator(function* () {
160
- var _this$ready, _this$subscribePromis, _this$syncPromise;
161
- yield (_this$ready = this.ready) === null || _this$ready === void 0 ? void 0 : _this$ready.catch(() => {});
162
- yield (_this$subscribePromis = this.subscribePromise) === null || _this$subscribePromis === void 0 ? void 0 : _this$subscribePromis.catch(() => {});
163
- yield (_this$syncPromise = this.syncPromise) === null || _this$syncPromise === void 0 ? void 0 : _this$syncPromise.catch(() => {});
164
- var registration = yield this.getRegistration();
165
- var subscription = (yield registration.pushManager.getSubscription()) || this.subscription;
166
- if (this.disposed) return;
167
- if (!subscription) return null;
168
- if (!this.owns(subscription)) {
169
- throw new Error('The existing Push subscription belongs to a different application');
170
- }
171
- this.subscription = subscription;
172
- var response = yield API.pushIdentities.destroy({
173
- subscription: subscription.toJSON()
174
- });
175
- if (response.failed) return response;
176
- if (this.disposed) return;
177
- yield subscription.unsubscribe();
178
- this.subscription = null;
179
- this.clearRetryTimeout();
180
- return response;
181
- });
182
- function removeSubscription() {
183
- return _removeSubscription.apply(this, arguments);
184
- }
185
- return removeSubscription;
186
- }()
187
- /**
188
- * Registers the current subscription, sharing any request already in progress.
189
- *
190
- * @private
191
- * @returns {Promise<import('../api/response').Response|void>}
192
- */
193
- }, {
194
- key: "sync",
195
- value: function sync() {
196
- if (this.disposed) return Promise.resolve();
197
- if (this.syncPromise) return this.syncPromise;
198
- this.clearRetryTimeout();
199
- this.syncPromise = this.registerIdentity().finally(() => {
200
- this.syncPromise = null;
201
- });
202
- return this.syncPromise;
101
+ /**
102
+ * Disables the server identity and removes the browser subscription.
103
+ * Keeps the subscription when the server request fails so the caller can retry.
104
+ *
105
+ * @returns {Promise<import('../api/response').Response|null|void>} Server response, null when
106
+ * no subscription exists, or no value when disposed. Rejects when a browser or network
107
+ * operation fails.
108
+ */
109
+ unsubscribe() {
110
+ if (this.disposed) return Promise.resolve();
111
+ if (this.unsubscribePromise) return this.unsubscribePromise;
112
+ this.clearRetryTimeout();
113
+ this.unsubscribePromise = this.removeSubscription().finally(() => {
114
+ this.unsubscribePromise = null;
115
+ });
116
+ return this.unsubscribePromise;
117
+ }
118
+
119
+ /**
120
+ * Finishes pending registration before disabling and removing the subscription.
121
+ *
122
+ * @private
123
+ * @returns {Promise<import('../api/response').Response|null|void>}
124
+ */
125
+ async removeSubscription() {
126
+ await this.ready?.catch(() => {});
127
+ await this.subscribePromise?.catch(() => {});
128
+ await this.syncPromise?.catch(() => {});
129
+ const registration = await this.getRegistration();
130
+ const subscription = (await registration.pushManager.getSubscription()) || this.subscription;
131
+ if (this.disposed) return;
132
+ if (!subscription) return null;
133
+ if (!this.owns(subscription)) {
134
+ throw new Error('The existing Push subscription belongs to a different application');
203
135
  }
136
+ this.subscription = subscription;
137
+ const response = await API.pushIdentities.destroy({
138
+ subscription: subscription.toJSON()
139
+ });
140
+ if (response.failed) return response;
141
+ if (this.disposed) return;
142
+ await subscription.unsubscribe();
143
+ this.subscription = null;
144
+ this.clearRetryTimeout();
145
+ return response;
146
+ }
204
147
 
205
- /**
206
- * Sends the subscription to Hellotext and schedules a retry on failure.
207
- *
208
- * @private
209
- * @returns {Promise<import('../api/response').Response>}
210
- */
211
- }, {
212
- key: "registerIdentity",
213
- value: function () {
214
- var _registerIdentity = _asyncToGenerator(function* () {
215
- try {
216
- var response = yield API.pushIdentities.create(_objectSpread({
217
- subscription: this.subscription.toJSON()
218
- }, this.channelId ? {
219
- channel_id: this.channelId
220
- } : {}));
221
- if (response.succeeded) {
222
- this.retryAttempts = 0;
223
- } else {
224
- this.scheduleRetry();
225
- }
226
- return response;
227
- } catch (error) {
228
- this.scheduleRetry();
229
- throw error;
230
- }
148
+ /**
149
+ * Registers the current subscription, sharing any request already in progress.
150
+ *
151
+ * @private
152
+ * @returns {Promise<import('../api/response').Response|void>}
153
+ */
154
+ sync() {
155
+ if (this.disposed) return Promise.resolve();
156
+ if (this.syncPromise) return this.syncPromise;
157
+ this.clearRetryTimeout();
158
+ this.syncPromise = this.registerIdentity().finally(() => {
159
+ this.syncPromise = null;
160
+ });
161
+ return this.syncPromise;
162
+ }
163
+
164
+ /**
165
+ * Sends the subscription to Hellotext and schedules a retry on failure.
166
+ *
167
+ * @private
168
+ * @returns {Promise<import('../api/response').Response>}
169
+ */
170
+ async registerIdentity() {
171
+ try {
172
+ const response = await API.pushIdentities.create({
173
+ subscription: this.subscription.toJSON(),
174
+ ...(this.channelId ? {
175
+ channel_id: this.channelId
176
+ } : {})
231
177
  });
232
- function registerIdentity() {
233
- return _registerIdentity.apply(this, arguments);
178
+ if (response.succeeded) {
179
+ this.retryAttempts = 0;
180
+ } else {
181
+ this.scheduleRetry();
234
182
  }
235
- return registerIdentity;
236
- }()
237
- /**
238
- * Schedules up to three registration retries with increasing delays.
239
- *
240
- * @private
241
- * @returns {void}
242
- */
243
- }, {
244
- key: "scheduleRetry",
245
- value: function scheduleRetry() {
246
- if (this.disposed || this.unsubscribePromise || this.retryTimeout || this.retryAttempts >= 3) return;
247
- this.retryTimeout = setTimeout(() => {
248
- this.retryTimeout = null;
249
- this.sync().catch(() => {});
250
- }, 1000 * 2 ** this.retryAttempts);
251
- this.retryAttempts += 1;
183
+ return response;
184
+ } catch (error) {
185
+ this.scheduleRetry();
186
+ throw error;
252
187
  }
188
+ }
253
189
 
254
- /**
255
- * Cancels a pending registration retry.
256
- *
257
- * @private
258
- * @returns {void}
259
- */
260
- }, {
261
- key: "clearRetryTimeout",
262
- value: function clearRetryTimeout() {
263
- clearTimeout(this.retryTimeout);
190
+ /**
191
+ * Schedules up to three registration retries with increasing delays.
192
+ *
193
+ * @private
194
+ * @returns {void}
195
+ */
196
+ scheduleRetry() {
197
+ if (this.disposed || this.unsubscribePromise || this.retryTimeout || this.retryAttempts >= 3) return;
198
+ this.retryTimeout = setTimeout(() => {
264
199
  this.retryTimeout = null;
265
- }
200
+ this.sync().catch(() => {});
201
+ }, 1000 * 2 ** this.retryAttempts);
202
+ this.retryAttempts += 1;
203
+ }
266
204
 
267
- /**
268
- * Stops registration retries and prevents further use of this instance.
269
- *
270
- * @returns {void}
271
- */
272
- }, {
273
- key: "dispose",
274
- value: function dispose() {
275
- this.disposed = true;
276
- this.clearRetryTimeout();
277
- }
205
+ /**
206
+ * Cancels a pending registration retry.
207
+ *
208
+ * @private
209
+ * @returns {void}
210
+ */
211
+ clearRetryTimeout() {
212
+ clearTimeout(this.retryTimeout);
213
+ this.retryTimeout = null;
214
+ }
278
215
 
279
- /**
280
- * Whether this instance has a browser subscription, regardless of server registration.
281
- *
282
- * @returns {Boolean}
283
- */
284
- }, {
285
- key: "subscribed",
286
- get: function get() {
287
- return !!this.subscription;
288
- }
216
+ /**
217
+ * Stops registration retries and prevents further use of this instance.
218
+ *
219
+ * @returns {void}
220
+ */
221
+ dispose() {
222
+ this.disposed = true;
223
+ this.clearRetryTimeout();
224
+ }
289
225
 
290
- /**
291
- * Decodes the public key into the bytes expected by PushManager.subscribe().
292
- *
293
- * @private
294
- * @returns {Uint8Array}
295
- */
296
- }, {
297
- key: "applicationServerKey",
298
- get: function get() {
299
- var base64 = this.publicKey.replace(/-/g, '+').replace(/_/g, '/');
300
- return Uint8Array.from(atob(base64.padEnd(Math.ceil(base64.length / 4) * 4, '=')), char => char.charCodeAt(0));
301
- }
226
+ /**
227
+ * Whether this instance has a browser subscription, regardless of server registration.
228
+ *
229
+ * @returns {Boolean}
230
+ */
231
+ get subscribed() {
232
+ return !!this.subscription;
233
+ }
234
+
235
+ /**
236
+ * Decodes the public key into the bytes expected by PushManager.subscribe().
237
+ *
238
+ * @private
239
+ * @returns {Uint8Array}
240
+ */
241
+ get applicationServerKey() {
242
+ const base64 = this.publicKey.replace(/-/g, '+').replace(/_/g, '/');
243
+ return Uint8Array.from(atob(base64.padEnd(Math.ceil(base64.length / 4) * 4, '=')), char => char.charCodeAt(0));
244
+ }
302
245
 
303
- /**
304
- * Checks whether a subscription uses this instance's public key.
305
- *
306
- * @private
307
- * @param {PushSubscription} subscription - Browser subscription to check.
308
- * @returns {Boolean}
309
- */
310
- }, {
311
- key: "owns",
312
- value: function owns(subscription) {
313
- var _subscription$options;
314
- var key = (_subscription$options = subscription.options) === null || _subscription$options === void 0 ? void 0 : _subscription$options.applicationServerKey;
315
- if (!key) return false;
316
- var actual = new Uint8Array(key);
317
- var expected = this.applicationServerKey;
318
- return actual.length === expected.length && actual.every((value, index) => value === expected[index]);
246
+ /**
247
+ * Checks whether a subscription uses this instance's public key.
248
+ *
249
+ * @private
250
+ * @param {PushSubscription} subscription - Browser subscription to check.
251
+ * @returns {Boolean}
252
+ */
253
+ owns(subscription) {
254
+ const key = subscription.options?.applicationServerKey;
255
+ if (!key) return false;
256
+ const actual = new Uint8Array(key);
257
+ const expected = this.applicationServerKey;
258
+ return actual.length === expected.length && actual.every((value, index) => value === expected[index]);
259
+ }
260
+
261
+ /**
262
+ * Gets the worker registration, sharing any pending lookup.
263
+ * Failed lookups can be retried by a later call.
264
+ *
265
+ * @private
266
+ * @returns {Promise<ServiceWorkerRegistration>}
267
+ */
268
+ getRegistration() {
269
+ if (!this.registrationPromise) {
270
+ this.registrationPromise = this.loadRegistration().catch(error => {
271
+ this.registrationPromise = null;
272
+ throw error;
273
+ });
319
274
  }
275
+ return this.registrationPromise;
276
+ }
320
277
 
321
- /**
322
- * Gets the worker registration, sharing any pending lookup.
323
- * Failed lookups can be retried by a later call.
324
- *
325
- * @private
326
- * @returns {Promise<ServiceWorkerRegistration>}
327
- */
328
- }, {
329
- key: "getRegistration",
330
- value: function getRegistration() {
331
- if (!this.registrationPromise) {
332
- this.registrationPromise = this.loadRegistration().catch(error => {
333
- this.registrationPromise = null;
334
- throw error;
335
- });
278
+ /**
279
+ * Registers the configured worker or waits for the page's existing registration.
280
+ *
281
+ * @private
282
+ * @returns {Promise<ServiceWorkerRegistration>} An active registration.
283
+ * Rejects if registration fails or the worker readiness wait times out.
284
+ */
285
+ async loadRegistration() {
286
+ if (this.serviceWorkerUrl) {
287
+ const registration = await navigator.serviceWorker.register(this.serviceWorkerUrl);
288
+ if (registration.active && !registration.installing && !registration.waiting) {
289
+ // A returning visitor may have an older worker that does not include our Push handlers.
290
+ // Calling register() with the same URL can return that existing registration without
291
+ // checking whether the script served at that URL has changed since their last visit.
292
+ //
293
+ // If no replacement is already installing or waiting, explicitly check for an update
294
+ // before treating the active worker as ready. Awaiting update() completes the update
295
+ // check, but does not wait for a replacement worker to activate. The code below selects
296
+ // that replacement, if one was found, and waits for its activation before subscribing.
297
+ await registration.update();
336
298
  }
337
- return this.registrationPromise;
299
+ const worker = registration.installing || registration.waiting || registration.active;
300
+ if (worker?.state === 'activated') return registration;
301
+ return new Promise((resolve, reject) => {
302
+ const timeout = setTimeout(() => finish(new Error('Push service worker did not become active')), 10000);
303
+ const finish = error => {
304
+ clearTimeout(timeout);
305
+ worker?.removeEventListener('statechange', changed);
306
+ error ? reject(error) : resolve(registration);
307
+ };
308
+ const changed = () => {
309
+ if (worker?.state === 'activated') finish();
310
+ if (worker?.state === 'redundant') finish(new Error('Push service worker installation failed'));
311
+ };
312
+ worker?.addEventListener('statechange', changed);
313
+ changed();
314
+ });
338
315
  }
339
316
 
340
- /**
341
- * Registers the configured worker or waits for the page's existing registration.
342
- *
343
- * @private
344
- * @returns {Promise<ServiceWorkerRegistration>} An active registration.
345
- * Rejects if registration fails or the worker readiness wait times out.
346
- */
347
- }, {
348
- key: "loadRegistration",
349
- value: function () {
350
- var _loadRegistration = _asyncToGenerator(function* () {
351
- if (this.serviceWorkerUrl) {
352
- var registration = yield navigator.serviceWorker.register(this.serviceWorkerUrl);
353
- if (registration.active && !registration.installing && !registration.waiting) {
354
- // A returning visitor may have an older worker that does not include our Push handlers.
355
- // Calling register() with the same URL can return that existing registration without
356
- // checking whether the script served at that URL has changed since their last visit.
357
- //
358
- // If no replacement is already installing or waiting, explicitly check for an update
359
- // before treating the active worker as ready. Awaiting update() completes the update
360
- // check, but does not wait for a replacement worker to activate. The code below selects
361
- // that replacement, if one was found, and waits for its activation before subscribing.
362
- yield registration.update();
363
- }
364
- var worker = registration.installing || registration.waiting || registration.active;
365
- if ((worker === null || worker === void 0 ? void 0 : worker.state) === 'activated') return registration;
366
- return new Promise((resolve, reject) => {
367
- var timeout = setTimeout(() => finish(new Error('Push service worker did not become active')), 10000);
368
- var finish = error => {
369
- clearTimeout(timeout);
370
- worker === null || worker === void 0 ? void 0 : worker.removeEventListener('statechange', changed);
371
- error ? reject(error) : resolve(registration);
372
- };
373
- var changed = () => {
374
- if ((worker === null || worker === void 0 ? void 0 : worker.state) === 'activated') finish();
375
- if ((worker === null || worker === void 0 ? void 0 : worker.state) === 'redundant') finish(new Error('Push service worker installation failed'));
376
- };
377
- worker === null || worker === void 0 ? void 0 : worker.addEventListener('statechange', changed);
378
- changed();
379
- });
380
- }
381
-
382
- // Reuse the service worker already registered for this page.
383
- return new Promise((resolve, reject) => {
384
- var timeout = setTimeout(() => reject(new Error('Push service worker is not available')), 10000);
385
- navigator.serviceWorker.ready.then(registration => {
386
- clearTimeout(timeout);
387
- resolve(registration);
388
- }, error => {
389
- clearTimeout(timeout);
390
- reject(error);
391
- });
392
- });
317
+ // Reuse the service worker already registered for this page.
318
+ return new Promise((resolve, reject) => {
319
+ const timeout = setTimeout(() => reject(new Error('Push service worker is not available')), 10000);
320
+ navigator.serviceWorker.ready.then(registration => {
321
+ clearTimeout(timeout);
322
+ resolve(registration);
323
+ }, error => {
324
+ clearTimeout(timeout);
325
+ reject(error);
393
326
  });
394
- function loadRegistration() {
395
- return _loadRegistration.apply(this, arguments);
396
- }
397
- return loadRegistration;
398
- }()
399
- /**
400
- * Whether the current page provides the browser APIs required for Push.
401
- *
402
- * @returns {Boolean}
403
- */
404
- }], [{
405
- key: "supported",
406
- get: function get() {
407
- return typeof window !== 'undefined' && window.isSecureContext === true && typeof navigator !== 'undefined' && 'serviceWorker' in navigator && typeof PushManager !== 'undefined' && typeof Notification !== 'undefined';
408
- }
409
- }]);
410
- return Push;
411
- }();
327
+ });
328
+ }
329
+
330
+ /**
331
+ * Whether the current page provides the browser APIs required for Push.
332
+ *
333
+ * @returns {Boolean}
334
+ */
335
+ static get supported() {
336
+ return typeof window !== 'undefined' && window.isSecureContext === true && typeof navigator !== 'undefined' && 'serviceWorker' in navigator && typeof PushManager !== 'undefined' && typeof Notification !== 'undefined';
337
+ }
338
+ }
412
339
  export { Push };