@splitsoftware/splitio-commons 1.4.2-rc.1 → 1.4.2-rc.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.
- package/cjs/listeners/browser.js +26 -11
- package/esm/listeners/browser.js +26 -11
- package/package.json +1 -1
- package/src/listeners/browser.ts +26 -10
- package/types/listeners/browser.d.ts +5 -5
package/cjs/listeners/browser.js
CHANGED
|
@@ -8,11 +8,12 @@ var objectAssign_1 = require("../utils/lang/objectAssign");
|
|
|
8
8
|
var constants_2 = require("../logger/constants");
|
|
9
9
|
var consent_1 = require("../consent");
|
|
10
10
|
var telemetrySubmitter_1 = require("../sync/submitters/telemetrySubmitter");
|
|
11
|
-
|
|
11
|
+
var VISIBILITYCHANGE_EVENT = 'visibilitychange';
|
|
12
|
+
var PAGEHIDE_EVENT = 'pagehide';
|
|
12
13
|
var UNLOAD_DOM_EVENT = 'unload';
|
|
13
14
|
var EVENT_NAME = 'for unload page event.';
|
|
14
15
|
/**
|
|
15
|
-
* We'll listen for
|
|
16
|
+
* We'll listen for events over the window object.
|
|
16
17
|
*/
|
|
17
18
|
var BrowserSignalListener = /** @class */ (function () {
|
|
18
19
|
function BrowserSignalListener(syncManager, settings, storage, serviceApi) {
|
|
@@ -21,30 +22,43 @@ var BrowserSignalListener = /** @class */ (function () {
|
|
|
21
22
|
this.storage = storage;
|
|
22
23
|
this.serviceApi = serviceApi;
|
|
23
24
|
this.flushData = this.flushData.bind(this);
|
|
25
|
+
this.flushDataIfHidden = this.flushDataIfHidden.bind(this);
|
|
26
|
+
this.stopSync = this.stopSync.bind(this);
|
|
24
27
|
this.fromImpressionsCollector = impressionsSubmitter_1.fromImpressionsCollector.bind(undefined, settings.core.labelsEnabled);
|
|
25
28
|
}
|
|
26
29
|
/**
|
|
27
30
|
* start method.
|
|
28
|
-
* Called when SplitFactory is initialized.
|
|
29
|
-
* We add a handler on unload events. The handler flushes remaining impressions and events to the backend.
|
|
31
|
+
* Called when SplitFactory is initialized, it adds event listeners to close streaming and flush impressions and events.
|
|
30
32
|
*/
|
|
31
33
|
BrowserSignalListener.prototype.start = function () {
|
|
32
34
|
if (typeof window !== 'undefined' && window.addEventListener) {
|
|
33
35
|
this.settings.log.debug(constants_2.CLEANUP_REGISTERING, [EVENT_NAME]);
|
|
34
|
-
|
|
36
|
+
// Flush data whenever the page is hidden or unloaded.
|
|
37
|
+
window.addEventListener(VISIBILITYCHANGE_EVENT, this.flushDataIfHidden);
|
|
38
|
+
// Some browsers like Safari does not fire the `visibilitychange` event when the page is being unloaded. So we also flush data in the `pagehide` event.
|
|
39
|
+
// If both events are triggered, the last one will find the storage empty, so no duplicated data will be submitted.
|
|
40
|
+
window.addEventListener(PAGEHIDE_EVENT, this.flushData);
|
|
41
|
+
// Stop streaming on 'unload' event. Used instead of 'beforeunload', because 'unload' is not a cancelable event, so no other listeners can stop the event from occurring.
|
|
42
|
+
window.addEventListener(UNLOAD_DOM_EVENT, this.stopSync);
|
|
35
43
|
}
|
|
36
44
|
};
|
|
37
45
|
/**
|
|
38
46
|
* stop method.
|
|
39
|
-
* Called when client is destroyed.
|
|
40
|
-
* We need to remove the handler for unload events, since it can break if called when Split context was destroyed.
|
|
47
|
+
* Called when client is destroyed, it removes event listeners.
|
|
41
48
|
*/
|
|
42
49
|
BrowserSignalListener.prototype.stop = function () {
|
|
43
50
|
if (typeof window !== 'undefined' && window.removeEventListener) {
|
|
44
51
|
this.settings.log.debug(constants_2.CLEANUP_DEREGISTERING, [EVENT_NAME]);
|
|
45
|
-
window.removeEventListener(
|
|
52
|
+
window.removeEventListener(VISIBILITYCHANGE_EVENT, this.flushDataIfHidden);
|
|
53
|
+
window.removeEventListener(PAGEHIDE_EVENT, this.flushData);
|
|
54
|
+
window.removeEventListener(UNLOAD_DOM_EVENT, this.stopSync);
|
|
46
55
|
}
|
|
47
56
|
};
|
|
57
|
+
BrowserSignalListener.prototype.stopSync = function () {
|
|
58
|
+
// Close streaming connection
|
|
59
|
+
if (this.syncManager && this.syncManager.pushManager)
|
|
60
|
+
this.syncManager.pushManager.stop();
|
|
61
|
+
};
|
|
48
62
|
/**
|
|
49
63
|
* flushData method.
|
|
50
64
|
* Called when unload event is triggered. It flushed remaining impressions and events to the backend,
|
|
@@ -71,9 +85,10 @@ var BrowserSignalListener = /** @class */ (function () {
|
|
|
71
85
|
var telemetryCacheAdapter = (0, telemetrySubmitter_1.telemetryCacheStatsAdapter)(this.storage.telemetry, this.storage.splits, this.storage.segments);
|
|
72
86
|
this._flushData(telemetryUrl + '/v1/metrics/usage/beacon', telemetryCacheAdapter, this.serviceApi.postMetricsUsage);
|
|
73
87
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
88
|
+
};
|
|
89
|
+
BrowserSignalListener.prototype.flushDataIfHidden = function () {
|
|
90
|
+
if (typeof document !== 'undefined' && document.visibilityState === 'hidden')
|
|
91
|
+
this.flushData(); // On a 'visibilitychange' event, flush data if state is hidden
|
|
77
92
|
};
|
|
78
93
|
BrowserSignalListener.prototype._flushData = function (url, cache, postService, fromCacheToPayload, extraMetadata) {
|
|
79
94
|
// if there is data in cache, send it to backend
|
package/esm/listeners/browser.js
CHANGED
|
@@ -5,11 +5,12 @@ import { objectAssign } from '../utils/lang/objectAssign';
|
|
|
5
5
|
import { CLEANUP_REGISTERING, CLEANUP_DEREGISTERING } from '../logger/constants';
|
|
6
6
|
import { isConsentGranted } from '../consent';
|
|
7
7
|
import { telemetryCacheStatsAdapter } from '../sync/submitters/telemetrySubmitter';
|
|
8
|
-
|
|
8
|
+
var VISIBILITYCHANGE_EVENT = 'visibilitychange';
|
|
9
|
+
var PAGEHIDE_EVENT = 'pagehide';
|
|
9
10
|
var UNLOAD_DOM_EVENT = 'unload';
|
|
10
11
|
var EVENT_NAME = 'for unload page event.';
|
|
11
12
|
/**
|
|
12
|
-
* We'll listen for
|
|
13
|
+
* We'll listen for events over the window object.
|
|
13
14
|
*/
|
|
14
15
|
var BrowserSignalListener = /** @class */ (function () {
|
|
15
16
|
function BrowserSignalListener(syncManager, settings, storage, serviceApi) {
|
|
@@ -18,30 +19,43 @@ var BrowserSignalListener = /** @class */ (function () {
|
|
|
18
19
|
this.storage = storage;
|
|
19
20
|
this.serviceApi = serviceApi;
|
|
20
21
|
this.flushData = this.flushData.bind(this);
|
|
22
|
+
this.flushDataIfHidden = this.flushDataIfHidden.bind(this);
|
|
23
|
+
this.stopSync = this.stopSync.bind(this);
|
|
21
24
|
this.fromImpressionsCollector = fromImpressionsCollector.bind(undefined, settings.core.labelsEnabled);
|
|
22
25
|
}
|
|
23
26
|
/**
|
|
24
27
|
* start method.
|
|
25
|
-
* Called when SplitFactory is initialized.
|
|
26
|
-
* We add a handler on unload events. The handler flushes remaining impressions and events to the backend.
|
|
28
|
+
* Called when SplitFactory is initialized, it adds event listeners to close streaming and flush impressions and events.
|
|
27
29
|
*/
|
|
28
30
|
BrowserSignalListener.prototype.start = function () {
|
|
29
31
|
if (typeof window !== 'undefined' && window.addEventListener) {
|
|
30
32
|
this.settings.log.debug(CLEANUP_REGISTERING, [EVENT_NAME]);
|
|
31
|
-
|
|
33
|
+
// Flush data whenever the page is hidden or unloaded.
|
|
34
|
+
window.addEventListener(VISIBILITYCHANGE_EVENT, this.flushDataIfHidden);
|
|
35
|
+
// Some browsers like Safari does not fire the `visibilitychange` event when the page is being unloaded. So we also flush data in the `pagehide` event.
|
|
36
|
+
// If both events are triggered, the last one will find the storage empty, so no duplicated data will be submitted.
|
|
37
|
+
window.addEventListener(PAGEHIDE_EVENT, this.flushData);
|
|
38
|
+
// Stop streaming on 'unload' event. Used instead of 'beforeunload', because 'unload' is not a cancelable event, so no other listeners can stop the event from occurring.
|
|
39
|
+
window.addEventListener(UNLOAD_DOM_EVENT, this.stopSync);
|
|
32
40
|
}
|
|
33
41
|
};
|
|
34
42
|
/**
|
|
35
43
|
* stop method.
|
|
36
|
-
* Called when client is destroyed.
|
|
37
|
-
* We need to remove the handler for unload events, since it can break if called when Split context was destroyed.
|
|
44
|
+
* Called when client is destroyed, it removes event listeners.
|
|
38
45
|
*/
|
|
39
46
|
BrowserSignalListener.prototype.stop = function () {
|
|
40
47
|
if (typeof window !== 'undefined' && window.removeEventListener) {
|
|
41
48
|
this.settings.log.debug(CLEANUP_DEREGISTERING, [EVENT_NAME]);
|
|
42
|
-
window.removeEventListener(
|
|
49
|
+
window.removeEventListener(VISIBILITYCHANGE_EVENT, this.flushDataIfHidden);
|
|
50
|
+
window.removeEventListener(PAGEHIDE_EVENT, this.flushData);
|
|
51
|
+
window.removeEventListener(UNLOAD_DOM_EVENT, this.stopSync);
|
|
43
52
|
}
|
|
44
53
|
};
|
|
54
|
+
BrowserSignalListener.prototype.stopSync = function () {
|
|
55
|
+
// Close streaming connection
|
|
56
|
+
if (this.syncManager && this.syncManager.pushManager)
|
|
57
|
+
this.syncManager.pushManager.stop();
|
|
58
|
+
};
|
|
45
59
|
/**
|
|
46
60
|
* flushData method.
|
|
47
61
|
* Called when unload event is triggered. It flushed remaining impressions and events to the backend,
|
|
@@ -68,9 +82,10 @@ var BrowserSignalListener = /** @class */ (function () {
|
|
|
68
82
|
var telemetryCacheAdapter = telemetryCacheStatsAdapter(this.storage.telemetry, this.storage.splits, this.storage.segments);
|
|
69
83
|
this._flushData(telemetryUrl + '/v1/metrics/usage/beacon', telemetryCacheAdapter, this.serviceApi.postMetricsUsage);
|
|
70
84
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
85
|
+
};
|
|
86
|
+
BrowserSignalListener.prototype.flushDataIfHidden = function () {
|
|
87
|
+
if (typeof document !== 'undefined' && document.visibilityState === 'hidden')
|
|
88
|
+
this.flushData(); // On a 'visibilitychange' event, flush data if state is hidden
|
|
74
89
|
};
|
|
75
90
|
BrowserSignalListener.prototype._flushData = function (url, cache, postService, fromCacheToPayload, extraMetadata) {
|
|
76
91
|
// if there is data in cache, send it to backend
|
package/package.json
CHANGED
package/src/listeners/browser.ts
CHANGED
|
@@ -14,12 +14,13 @@ import { ISyncManager } from '../sync/types';
|
|
|
14
14
|
import { isConsentGranted } from '../consent';
|
|
15
15
|
import { telemetryCacheStatsAdapter } from '../sync/submitters/telemetrySubmitter';
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
const VISIBILITYCHANGE_EVENT = 'visibilitychange';
|
|
18
|
+
const PAGEHIDE_EVENT = 'pagehide';
|
|
18
19
|
const UNLOAD_DOM_EVENT = 'unload';
|
|
19
20
|
const EVENT_NAME = 'for unload page event.';
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
|
-
* We'll listen for
|
|
23
|
+
* We'll listen for events over the window object.
|
|
23
24
|
*/
|
|
24
25
|
export class BrowserSignalListener implements ISignalListener {
|
|
25
26
|
|
|
@@ -32,33 +33,47 @@ export class BrowserSignalListener implements ISignalListener {
|
|
|
32
33
|
private serviceApi: ISplitApi,
|
|
33
34
|
) {
|
|
34
35
|
this.flushData = this.flushData.bind(this);
|
|
36
|
+
this.flushDataIfHidden = this.flushDataIfHidden.bind(this);
|
|
37
|
+
this.stopSync = this.stopSync.bind(this);
|
|
35
38
|
this.fromImpressionsCollector = fromImpressionsCollector.bind(undefined, settings.core.labelsEnabled);
|
|
36
39
|
}
|
|
37
40
|
|
|
38
41
|
/**
|
|
39
42
|
* start method.
|
|
40
|
-
* Called when SplitFactory is initialized.
|
|
41
|
-
* We add a handler on unload events. The handler flushes remaining impressions and events to the backend.
|
|
43
|
+
* Called when SplitFactory is initialized, it adds event listeners to close streaming and flush impressions and events.
|
|
42
44
|
*/
|
|
43
45
|
start() {
|
|
44
46
|
if (typeof window !== 'undefined' && window.addEventListener) {
|
|
45
47
|
this.settings.log.debug(CLEANUP_REGISTERING, [EVENT_NAME]);
|
|
46
|
-
|
|
48
|
+
|
|
49
|
+
// Flush data whenever the page is hidden or unloaded.
|
|
50
|
+
window.addEventListener(VISIBILITYCHANGE_EVENT, this.flushDataIfHidden);
|
|
51
|
+
// Some browsers like Safari does not fire the `visibilitychange` event when the page is being unloaded. So we also flush data in the `pagehide` event.
|
|
52
|
+
// If both events are triggered, the last one will find the storage empty, so no duplicated data will be submitted.
|
|
53
|
+
window.addEventListener(PAGEHIDE_EVENT, this.flushData);
|
|
54
|
+
// Stop streaming on 'unload' event. Used instead of 'beforeunload', because 'unload' is not a cancelable event, so no other listeners can stop the event from occurring.
|
|
55
|
+
window.addEventListener(UNLOAD_DOM_EVENT, this.stopSync);
|
|
47
56
|
}
|
|
48
57
|
}
|
|
49
58
|
|
|
50
59
|
/**
|
|
51
60
|
* stop method.
|
|
52
|
-
* Called when client is destroyed.
|
|
53
|
-
* We need to remove the handler for unload events, since it can break if called when Split context was destroyed.
|
|
61
|
+
* Called when client is destroyed, it removes event listeners.
|
|
54
62
|
*/
|
|
55
63
|
stop() {
|
|
56
64
|
if (typeof window !== 'undefined' && window.removeEventListener) {
|
|
57
65
|
this.settings.log.debug(CLEANUP_DEREGISTERING, [EVENT_NAME]);
|
|
58
|
-
window.removeEventListener(
|
|
66
|
+
window.removeEventListener(VISIBILITYCHANGE_EVENT, this.flushDataIfHidden);
|
|
67
|
+
window.removeEventListener(PAGEHIDE_EVENT, this.flushData);
|
|
68
|
+
window.removeEventListener(UNLOAD_DOM_EVENT, this.stopSync);
|
|
59
69
|
}
|
|
60
70
|
}
|
|
61
71
|
|
|
72
|
+
stopSync() {
|
|
73
|
+
// Close streaming connection
|
|
74
|
+
if (this.syncManager && this.syncManager.pushManager) this.syncManager.pushManager.stop();
|
|
75
|
+
}
|
|
76
|
+
|
|
62
77
|
/**
|
|
63
78
|
* flushData method.
|
|
64
79
|
* Called when unload event is triggered. It flushed remaining impressions and events to the backend,
|
|
@@ -86,9 +101,10 @@ export class BrowserSignalListener implements ISignalListener {
|
|
|
86
101
|
const telemetryCacheAdapter = telemetryCacheStatsAdapter(this.storage.telemetry, this.storage.splits, this.storage.segments);
|
|
87
102
|
this._flushData(telemetryUrl + '/v1/metrics/usage/beacon', telemetryCacheAdapter, this.serviceApi.postMetricsUsage);
|
|
88
103
|
}
|
|
104
|
+
}
|
|
89
105
|
|
|
90
|
-
|
|
91
|
-
if (
|
|
106
|
+
flushDataIfHidden() {
|
|
107
|
+
if (typeof document !== 'undefined' && document.visibilityState === 'hidden') this.flushData(); // On a 'visibilitychange' event, flush data if state is hidden
|
|
92
108
|
}
|
|
93
109
|
|
|
94
110
|
private _flushData<T>(url: string, cache: IRecorderCacheProducerSync<T>, postService: (body: string) => Promise<IResponse>, fromCacheToPayload?: (cacheData: T) => any, extraMetadata?: {}) {
|
|
@@ -4,7 +4,7 @@ import { ISplitApi } from '../services/types';
|
|
|
4
4
|
import { ISettings } from '../types';
|
|
5
5
|
import { ISyncManager } from '../sync/types';
|
|
6
6
|
/**
|
|
7
|
-
* We'll listen for
|
|
7
|
+
* We'll listen for events over the window object.
|
|
8
8
|
*/
|
|
9
9
|
export declare class BrowserSignalListener implements ISignalListener {
|
|
10
10
|
private syncManager;
|
|
@@ -15,22 +15,22 @@ export declare class BrowserSignalListener implements ISignalListener {
|
|
|
15
15
|
constructor(syncManager: ISyncManager | undefined, settings: ISettings, storage: IStorageSync, serviceApi: ISplitApi);
|
|
16
16
|
/**
|
|
17
17
|
* start method.
|
|
18
|
-
* Called when SplitFactory is initialized.
|
|
19
|
-
* We add a handler on unload events. The handler flushes remaining impressions and events to the backend.
|
|
18
|
+
* Called when SplitFactory is initialized, it adds event listeners to close streaming and flush impressions and events.
|
|
20
19
|
*/
|
|
21
20
|
start(): void;
|
|
22
21
|
/**
|
|
23
22
|
* stop method.
|
|
24
|
-
* Called when client is destroyed.
|
|
25
|
-
* We need to remove the handler for unload events, since it can break if called when Split context was destroyed.
|
|
23
|
+
* Called when client is destroyed, it removes event listeners.
|
|
26
24
|
*/
|
|
27
25
|
stop(): void;
|
|
26
|
+
stopSync(): void;
|
|
28
27
|
/**
|
|
29
28
|
* flushData method.
|
|
30
29
|
* Called when unload event is triggered. It flushed remaining impressions and events to the backend,
|
|
31
30
|
* using beacon API if possible, or falling back to regular post transport.
|
|
32
31
|
*/
|
|
33
32
|
flushData(): void;
|
|
33
|
+
flushDataIfHidden(): void;
|
|
34
34
|
private _flushData;
|
|
35
35
|
/**
|
|
36
36
|
* _sendBeacon method.
|