@zoho_apptics/apptics-js-sdk 2.0.0-beta.1 → 2.0.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/CHANGELOG.md +127 -16
- package/README.md +357 -215
- package/dist/apptics-pn-sw.js +3 -0
- package/dist/apptics-pn-sw.js.map +1 -0
- package/dist/apptics.esm.min.js +2 -2
- package/dist/apptics.esm.min.js.map +1 -1
- package/dist/apptics.min.js +2 -2
- package/dist/apptics.min.js.map +1 -1
- package/dist/chunks/batch-Axq9sTzR.min.js +3 -0
- package/dist/chunks/batch-Axq9sTzR.min.js.map +1 -0
- package/dist/chunks/lifecycle-Cvg78jN_.min.js +3 -0
- package/dist/chunks/lifecycle-Cvg78jN_.min.js.map +1 -0
- package/dist/chunks/send-DBpesydB.min.js +3 -0
- package/dist/chunks/send-DBpesydB.min.js.map +1 -0
- package/dist/chunks/transport-B21X39p4.min.js +3 -0
- package/dist/chunks/transport-B21X39p4.min.js.map +1 -0
- package/dist/cjs/core/attempts.js +1 -1
- package/dist/cjs/core/batch.js +4 -4
- package/dist/cjs/core/consent.d.ts +17 -4
- package/dist/cjs/core/consent.js +81 -14
- package/dist/cjs/core/console-call.d.ts +23 -0
- package/dist/cjs/core/console-call.js +63 -0
- package/dist/cjs/core/constants.d.ts +66 -17
- package/dist/cjs/core/constants.js +76 -19
- package/dist/cjs/core/db.js +31 -7
- package/dist/cjs/core/debug.d.ts +6 -4
- package/dist/cjs/core/debug.js +36 -12
- package/dist/cjs/core/env.d.ts +0 -3
- package/dist/cjs/core/env.js +1 -9
- package/dist/cjs/core/framework-id.d.ts +1 -1
- package/dist/cjs/core/framework-id.js +3 -3
- package/dist/cjs/core/guard.js +19 -4
- package/dist/cjs/core/hooks.d.ts +29 -0
- package/dist/cjs/core/hooks.js +12 -1
- package/dist/cjs/core/locks.js +1 -1
- package/dist/cjs/core/logger.js +1 -1
- package/dist/cjs/core/shield.d.ts +31 -0
- package/dist/cjs/core/shield.js +129 -0
- package/dist/cjs/core/state.d.ts +11 -5
- package/dist/cjs/core/state.js +4 -4
- package/dist/cjs/core/storage.d.ts +7 -0
- package/dist/cjs/core/storage.js +28 -9
- package/dist/cjs/core/stylesheet.js +1 -1
- package/dist/cjs/core/transport.d.ts +15 -4
- package/dist/cjs/core/transport.js +72 -25
- package/dist/cjs/core/utils.js +1 -1
- package/dist/cjs/core/validate.js +6 -5
- package/dist/cjs/device/appversion.d.ts +10 -1
- package/dist/cjs/device/appversion.js +26 -3
- package/dist/cjs/device/index.d.ts +17 -2
- package/dist/cjs/device/index.js +251 -35
- package/dist/cjs/engagement/api-rules.js +1 -1
- package/dist/cjs/engagement/auto-api.js +4 -3
- package/dist/cjs/engagement/auto-screen.js +10 -6
- package/dist/cjs/engagement/defined-events.d.ts +2 -3
- package/dist/cjs/engagement/defined-events.js +10 -16
- package/dist/cjs/engagement/send.js +7 -3
- package/dist/cjs/engagement/session.d.ts +17 -0
- package/dist/cjs/engagement/session.js +154 -23
- package/dist/cjs/engagement/track.d.ts +4 -4
- package/dist/cjs/engagement/track.js +23 -17
- package/dist/cjs/errors/send.js +2 -2
- package/dist/cjs/errors/track.d.ts +2 -1
- package/dist/cjs/errors/track.js +8 -5
- package/dist/cjs/feedback/controls.d.ts +36 -4
- package/dist/cjs/feedback/controls.js +302 -67
- package/dist/cjs/feedback/i18n/index.d.ts +31 -0
- package/dist/cjs/feedback/i18n/index.js +187 -0
- package/dist/cjs/feedback/i18n/strings.d.ts +9 -0
- package/dist/cjs/feedback/i18n/strings.js +8 -0
- package/dist/cjs/feedback/index.js +1 -1
- package/dist/cjs/feedback/logs.d.ts +3 -6
- package/dist/cjs/feedback/logs.js +12 -21
- package/dist/cjs/feedback/quartz.js +5 -2
- package/dist/cjs/feedback/send.d.ts +6 -3
- package/dist/cjs/feedback/send.js +59 -41
- package/dist/cjs/feedback/styles.js +2 -2
- package/dist/cjs/feedback/template.d.ts +4 -2
- package/dist/cjs/feedback/template.js +73 -44
- package/dist/cjs/feedback/ui.d.ts +47 -4
- package/dist/cjs/feedback/ui.js +351 -97
- package/dist/cjs/heatmap/send.d.ts +2 -0
- package/dist/cjs/heatmap/send.js +3 -5
- package/dist/cjs/heatmap/track.js +13 -9
- package/dist/cjs/index.d.ts +1 -2
- package/dist/cjs/index.js +55 -18
- package/dist/cjs/lifecycle.d.ts +13 -2
- package/dist/cjs/lifecycle.js +233 -118
- package/dist/cjs/pushnotification/display.d.ts +65 -0
- package/dist/cjs/pushnotification/display.js +112 -0
- package/dist/cjs/pushnotification/index.d.ts +20 -0
- package/dist/cjs/pushnotification/index.js +96 -0
- package/dist/cjs/pushnotification/kept-opens.d.ts +23 -0
- package/dist/cjs/pushnotification/kept-opens.js +64 -0
- package/dist/cjs/pushnotification/notify.d.ts +45 -0
- package/dist/cjs/pushnotification/notify.js +476 -0
- package/dist/cjs/pushnotification/payload.d.ts +65 -0
- package/dist/cjs/pushnotification/payload.js +279 -0
- package/dist/cjs/pushnotification/session.d.ts +46 -0
- package/dist/cjs/pushnotification/session.js +413 -0
- package/dist/cjs/pushnotification/stats.d.ts +45 -0
- package/dist/cjs/pushnotification/stats.js +244 -0
- package/dist/cjs/pushnotification/wms.d.ts +30 -0
- package/dist/cjs/pushnotification/wms.js +374 -0
- package/dist/cjs/remoteconfig/country.js +1 -1
- package/dist/cjs/remoteconfig/custom.js +4 -3
- package/dist/cjs/remoteconfig/evaluate.js +1 -1
- package/dist/cjs/remoteconfig/fetch.d.ts +17 -10
- package/dist/cjs/remoteconfig/fetch.js +146 -32
- package/dist/cjs/remoteconfig/index.d.ts +9 -5
- package/dist/cjs/remoteconfig/index.js +24 -32
- package/dist/cjs/remoteconfig/store.d.ts +15 -7
- package/dist/cjs/remoteconfig/store.js +68 -35
- package/dist/cjs/remoteconfig/tz-countries.js +1 -1
- package/dist/cjs/{core → remotelog}/callsite.d.ts +5 -4
- package/dist/cjs/{core → remotelog}/callsite.js +6 -5
- package/dist/cjs/remotelog/gate.js +1 -1
- package/dist/cjs/remotelog/logger.js +2 -2
- package/dist/cjs/remotelog/send.js +6 -5
- package/dist/cjs/remotelogger/index.js +1 -1
- package/dist/cjs/scheduler.d.ts +3 -1
- package/dist/cjs/scheduler.js +34 -25
- package/dist/cjs/sw/apptics-pn-sw.d.ts +83 -0
- package/dist/cjs/sw/timers.d.ts +44 -0
- package/dist/cjs/types.d.ts +90 -29
- package/dist/cjs/user/index.d.ts +7 -0
- package/dist/cjs/user/index.js +25 -3
- package/dist/esm/core/attempts.js +1 -1
- package/dist/esm/core/batch.js +4 -4
- package/dist/esm/core/consent.d.ts +17 -4
- package/dist/esm/core/consent.js +84 -20
- package/dist/esm/core/console-call.d.ts +23 -0
- package/dist/esm/core/console-call.js +60 -0
- package/dist/esm/core/constants.d.ts +66 -17
- package/dist/esm/core/constants.js +73 -20
- package/dist/esm/core/db.js +31 -7
- package/dist/esm/core/debug.d.ts +6 -4
- package/dist/esm/core/debug.js +37 -13
- package/dist/esm/core/env.d.ts +0 -3
- package/dist/esm/core/env.js +2 -9
- package/dist/esm/core/framework-id.d.ts +1 -1
- package/dist/esm/core/framework-id.js +3 -3
- package/dist/esm/core/guard.js +19 -4
- package/dist/esm/core/hooks.d.ts +29 -0
- package/dist/esm/core/hooks.js +12 -1
- package/dist/esm/core/locks.js +1 -1
- package/dist/esm/core/logger.js +1 -1
- package/dist/esm/core/shield.d.ts +31 -0
- package/dist/esm/core/shield.js +119 -0
- package/dist/esm/core/state.d.ts +11 -5
- package/dist/esm/core/state.js +4 -4
- package/dist/esm/core/storage.d.ts +7 -0
- package/dist/esm/core/storage.js +28 -11
- package/dist/esm/core/stylesheet.js +1 -1
- package/dist/esm/core/transport.d.ts +15 -4
- package/dist/esm/core/transport.js +73 -28
- package/dist/esm/core/utils.js +1 -1
- package/dist/esm/core/validate.js +6 -5
- package/dist/esm/device/appversion.d.ts +10 -1
- package/dist/esm/device/appversion.js +26 -4
- package/dist/esm/device/index.d.ts +17 -2
- package/dist/esm/device/index.js +255 -42
- package/dist/esm/engagement/api-rules.js +1 -1
- package/dist/esm/engagement/auto-api.js +4 -3
- package/dist/esm/engagement/auto-screen.js +10 -6
- package/dist/esm/engagement/defined-events.d.ts +2 -3
- package/dist/esm/engagement/defined-events.js +10 -16
- package/dist/esm/engagement/send.js +7 -3
- package/dist/esm/engagement/session.d.ts +17 -0
- package/dist/esm/engagement/session.js +152 -24
- package/dist/esm/engagement/track.d.ts +4 -4
- package/dist/esm/engagement/track.js +24 -18
- package/dist/esm/errors/send.js +2 -2
- package/dist/esm/errors/track.d.ts +2 -1
- package/dist/esm/errors/track.js +8 -5
- package/dist/esm/feedback/controls.d.ts +36 -4
- package/dist/esm/feedback/controls.js +298 -68
- package/dist/esm/feedback/i18n/index.d.ts +31 -0
- package/dist/esm/feedback/i18n/index.js +178 -0
- package/dist/esm/feedback/i18n/strings.d.ts +9 -0
- package/dist/esm/feedback/i18n/strings.js +4 -0
- package/dist/esm/feedback/index.js +1 -1
- package/dist/esm/feedback/logs.d.ts +3 -6
- package/dist/esm/feedback/logs.js +12 -21
- package/dist/esm/feedback/quartz.js +5 -2
- package/dist/esm/feedback/send.d.ts +6 -3
- package/dist/esm/feedback/send.js +61 -44
- package/dist/esm/feedback/styles.js +2 -2
- package/dist/esm/feedback/template.d.ts +4 -2
- package/dist/esm/feedback/template.js +73 -44
- package/dist/esm/feedback/ui.d.ts +47 -4
- package/dist/esm/feedback/ui.js +353 -99
- package/dist/esm/heatmap/send.d.ts +2 -0
- package/dist/esm/heatmap/send.js +3 -5
- package/dist/esm/heatmap/track.js +13 -9
- package/dist/esm/index.d.ts +1 -2
- package/dist/esm/index.js +58 -21
- package/dist/esm/lifecycle.d.ts +13 -2
- package/dist/esm/lifecycle.js +238 -125
- package/dist/esm/pushnotification/display.d.ts +65 -0
- package/dist/esm/pushnotification/display.js +95 -0
- package/dist/esm/pushnotification/index.d.ts +20 -0
- package/dist/esm/pushnotification/index.js +91 -0
- package/dist/esm/pushnotification/kept-opens.d.ts +23 -0
- package/dist/esm/pushnotification/kept-opens.js +62 -0
- package/dist/esm/pushnotification/notify.d.ts +45 -0
- package/dist/esm/pushnotification/notify.js +469 -0
- package/dist/esm/pushnotification/payload.d.ts +65 -0
- package/dist/esm/pushnotification/payload.js +272 -0
- package/dist/esm/pushnotification/session.d.ts +46 -0
- package/dist/esm/pushnotification/session.js +402 -0
- package/dist/esm/pushnotification/stats.d.ts +45 -0
- package/dist/esm/pushnotification/stats.js +238 -0
- package/dist/esm/pushnotification/wms.d.ts +30 -0
- package/dist/esm/pushnotification/wms.js +368 -0
- package/dist/esm/remoteconfig/country.js +1 -1
- package/dist/esm/remoteconfig/custom.js +4 -3
- package/dist/esm/remoteconfig/evaluate.js +1 -1
- package/dist/esm/remoteconfig/fetch.d.ts +17 -10
- package/dist/esm/remoteconfig/fetch.js +147 -33
- package/dist/esm/remoteconfig/index.d.ts +9 -5
- package/dist/esm/remoteconfig/index.js +24 -32
- package/dist/esm/remoteconfig/store.d.ts +15 -7
- package/dist/esm/remoteconfig/store.js +68 -36
- package/dist/esm/remoteconfig/tz-countries.js +1 -1
- package/dist/esm/{core → remotelog}/callsite.d.ts +5 -4
- package/dist/esm/{core → remotelog}/callsite.js +6 -5
- package/dist/esm/remotelog/gate.js +1 -1
- package/dist/esm/remotelog/logger.js +2 -2
- package/dist/esm/remotelog/send.js +6 -5
- package/dist/esm/remotelogger/index.js +1 -1
- package/dist/esm/scheduler.d.ts +3 -1
- package/dist/esm/scheduler.js +35 -27
- package/dist/esm/sw/apptics-pn-sw.d.ts +83 -0
- package/dist/esm/sw/timers.d.ts +44 -0
- package/dist/esm/types.d.ts +90 -29
- package/dist/esm/user/index.d.ts +7 -0
- package/dist/esm/user/index.js +26 -5
- package/dist/feedback.esm.min.js +2 -2
- package/dist/feedback.esm.min.js.map +1 -1
- package/dist/pushnotification.esm.min.js +3 -0
- package/dist/pushnotification.esm.min.js.map +1 -0
- package/dist/remoteconfig.esm.min.js +3 -0
- package/dist/remoteconfig.esm.min.js.map +1 -0
- package/dist/remotelogger.esm.min.js +3 -0
- package/dist/remotelogger.esm.min.js.map +1 -0
- package/package.json +17 -3
- package/dist/chunks/lifecycle-VYr43KcB.min.js +0 -3
- package/dist/chunks/lifecycle-VYr43KcB.min.js.map +0 -1
- package/dist/cjs/auto-init.d.ts +0 -8
- package/dist/cjs/auto-init.js +0 -32
- package/dist/esm/auto-init.d.ts +0 -8
- package/dist/esm/auto-init.js +0 -29
|
@@ -1,24 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Asks for the rule set
|
|
2
|
+
* Asks for the rule set: the whole of it with flagtime 0 on the first fetch of a page load, then
|
|
3
|
+
* only what changed since the last answered request, which is merged into what is stored.
|
|
3
4
|
*
|
|
4
|
-
* Resolves with the
|
|
5
|
-
*
|
|
6
|
-
* normal case and not a failure.
|
|
5
|
+
* Resolves with the answer, whether or not it carried a rule set — a later answer with no
|
|
6
|
+
* `remoteconfig` block means nothing has changed, which is the normal case and not a failure.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* With `whole`, a cold fetch: flagtime 0 whatever this page has fetched already, and the answer
|
|
9
|
+
* replaces the stored set as the first fetch of a page load does.
|
|
10
|
+
*
|
|
11
|
+
* A second caller joins the first rather than making another request. A cold fetch joins one
|
|
12
|
+
* only when it asked with flagtime 0 too, and otherwise asks once it has answered.
|
|
9
13
|
*/
|
|
10
|
-
export declare function fetchRemoteConfig(): Promise<Record<string, unknown>>;
|
|
14
|
+
export declare function fetchRemoteConfig(whole?: boolean): Promise<Record<string, unknown>>;
|
|
11
15
|
/**
|
|
12
16
|
* reset(): forgets the rule set, its values and what the server last reported, and disowns a
|
|
13
17
|
* request still in flight, whose answer would otherwise put the old rule set back.
|
|
14
18
|
*/
|
|
15
19
|
export declare function resetRemoteConfig(): void;
|
|
16
20
|
/**
|
|
17
|
-
* Makes sure the values are this page's own: from a fetch that
|
|
18
|
-
*
|
|
19
|
-
*
|
|
21
|
+
* Makes sure the values are this page's own: from a fetch on this page that succeeded, kept up to
|
|
22
|
+
* date by the polling after it. With `coldFetch`, a fetch with flagtime 0 now, whatever this page
|
|
23
|
+
* already has, whose answer replaces what is stored: at most COLD_LIMIT a minute, and one past
|
|
24
|
+
* that answers from what this page has. Resolves true when they are, false when the
|
|
25
|
+
* fetch failed or could not be made — the caller then decides whether stored values will do.
|
|
26
|
+
* Never rejects.
|
|
20
27
|
*
|
|
21
28
|
* Waits for start-up and for the device to be registered, which a first visit is still doing
|
|
22
29
|
* when ready() resolves: getupdates is refused without the device ids.
|
|
23
30
|
*/
|
|
24
|
-
export declare function obtain(): Promise<boolean>;
|
|
31
|
+
export declare function obtain(coldFetch?: boolean): Promise<boolean>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! @zoho_apptics/apptics-js-sdk v2.0.0
|
|
1
|
+
/*! @zoho_apptics/apptics-js-sdk v2.0.0 | (c) Zoho Corporation Private Limited | see LICENSE */
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
var constants = require('../core/constants.js');
|
|
@@ -11,6 +11,7 @@ var index = require('../device/index.js');
|
|
|
11
11
|
var lifecycle = require('../lifecycle.js');
|
|
12
12
|
var custom = require('./custom.js');
|
|
13
13
|
var store = require('./store.js');
|
|
14
|
+
var shield = require('../core/shield.js');
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* The getupdates call.
|
|
@@ -30,15 +31,35 @@ const LABEL = 'AppticsRemoteConfig'; // no i18n
|
|
|
30
31
|
/** getupdatesobj caps moduleids at nine entries; this SDK asks about one. */
|
|
31
32
|
const MODULES = [Number(constants.SDK_MODULE.REMOTE_CONFIG)];
|
|
32
33
|
var inFlight = null;
|
|
34
|
+
/** Whether the request in flight asked with flagtime 0, which a cold fetch can join. */
|
|
35
|
+
var inFlightWhole = false;
|
|
33
36
|
/** Bumped by reset(); an answer to a request made before it is not stored. */
|
|
34
37
|
var generation = 0;
|
|
35
38
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
39
|
+
* When getupdates is asked again, after the first of a page load: every POLL_MS. Coming back to
|
|
40
|
+
* the tab asks nothing; the poll's timer carries on. fetchValues() never ask
|
|
41
|
+
* themselves once a fetch has succeeded: a value read on every render must not be a request on
|
|
42
|
+
* every render.
|
|
38
43
|
*/
|
|
39
|
-
const
|
|
40
|
-
/**
|
|
44
|
+
const POLL_MS = 3 * 60 * 1000;
|
|
45
|
+
/**
|
|
46
|
+
* coldFetch goes to the network every time, so it is limited: three in any minute, as on Android.
|
|
47
|
+
* One past that is answered from the values this page already has.
|
|
48
|
+
*/
|
|
49
|
+
const COLD_LIMIT = 3;
|
|
50
|
+
const COLD_WINDOW_MS = 60 * 1000;
|
|
51
|
+
/** When a fetch on this page last succeeded; 0 until one has, and the next one sends flagtime 0. */
|
|
41
52
|
var fetchedAt = 0;
|
|
53
|
+
/**
|
|
54
|
+
* When a fetch last failed, or 0. Until this page has an answer of its own, fetchValues asks again
|
|
55
|
+
* only RETRY_AFTER_FAILURE_MS after a failure: asked at once on every call, a page that calls it on
|
|
56
|
+
* every render, or one whose getupdates keeps being refused, asked hundreds of times a minute.
|
|
57
|
+
*/
|
|
58
|
+
var failedAt = 0;
|
|
59
|
+
const RETRY_AFTER_FAILURE_MS = 30 * 1000;
|
|
60
|
+
/** When the coldFetch calls of the last minute went out. */
|
|
61
|
+
var coldCalls = [];
|
|
62
|
+
var pollTimer = null;
|
|
42
63
|
/**
|
|
43
64
|
* Whether remote config is still worth asking about.
|
|
44
65
|
*
|
|
@@ -58,11 +79,11 @@ function wanted() {
|
|
|
58
79
|
* id for "macOS 15", not the small int this template types. languagecode is sent when there is
|
|
59
80
|
* one, since safe_string cannot match an empty string.
|
|
60
81
|
*/
|
|
61
|
-
function body() {
|
|
82
|
+
function body(since) {
|
|
62
83
|
var payload = {
|
|
63
84
|
appversionid: String(state._ZAP._device_info.appversionid || ''), // no i18n
|
|
64
85
|
moduleids: MODULES,
|
|
65
|
-
flagtime:
|
|
86
|
+
flagtime: since,
|
|
66
87
|
};
|
|
67
88
|
var language = String(navigator.language || '').slice(0, 10); // no i18n
|
|
68
89
|
if (language !== '') {
|
|
@@ -75,6 +96,9 @@ function body() {
|
|
|
75
96
|
*
|
|
76
97
|
* Only to stop asking: getsdkconfig decides whether the feature is on. A module reported off
|
|
77
98
|
* here is dropped from the next request, which is what the API asks for.
|
|
99
|
+
*
|
|
100
|
+
* A report that differs from the last one (on until one says otherwise) means the server's
|
|
101
|
+
* settings changed, so getsdkconfig, which decides, is asked now rather than at its next check.
|
|
78
102
|
*/
|
|
79
103
|
function applyReportedModules(data) {
|
|
80
104
|
var status = data.tracking_status;
|
|
@@ -83,32 +107,51 @@ function applyReportedModules(data) {
|
|
|
83
107
|
}
|
|
84
108
|
var entry = status[constants.SDK_MODULE.REMOTE_CONFIG];
|
|
85
109
|
if (entry && typeof entry === 'object') {
|
|
86
|
-
|
|
110
|
+
var reported = entry.enabled !== false;
|
|
111
|
+
var changed = reported !== state._ZAP._remoteConfigReported;
|
|
112
|
+
state._ZAP._remoteConfigReported = reported;
|
|
113
|
+
if (changed) {
|
|
114
|
+
logger.Console.log(LABEL + ': getupdates reported the module ' + (reported ? 'on' : 'off') + ', asking getsdkconfig'); // no i18n
|
|
115
|
+
hooks.hooks.askServerSettings();
|
|
116
|
+
}
|
|
87
117
|
}
|
|
88
118
|
}
|
|
89
119
|
/**
|
|
90
|
-
* Asks for the rule set
|
|
120
|
+
* Asks for the rule set: the whole of it with flagtime 0 on the first fetch of a page load, then
|
|
121
|
+
* only what changed since the last answered request, which is merged into what is stored.
|
|
91
122
|
*
|
|
92
|
-
* Resolves with the
|
|
93
|
-
*
|
|
94
|
-
* normal case and not a failure.
|
|
123
|
+
* Resolves with the answer, whether or not it carried a rule set — a later answer with no
|
|
124
|
+
* `remoteconfig` block means nothing has changed, which is the normal case and not a failure.
|
|
95
125
|
*
|
|
96
|
-
*
|
|
126
|
+
* With `whole`, a cold fetch: flagtime 0 whatever this page has fetched already, and the answer
|
|
127
|
+
* replaces the stored set as the first fetch of a page load does.
|
|
128
|
+
*
|
|
129
|
+
* A second caller joins the first rather than making another request. A cold fetch joins one
|
|
130
|
+
* only when it asked with flagtime 0 too, and otherwise asks once it has answered.
|
|
97
131
|
*/
|
|
98
|
-
function fetchRemoteConfig() {
|
|
132
|
+
function fetchRemoteConfig(whole) {
|
|
99
133
|
if (inFlight !== null) {
|
|
134
|
+
if (whole === true && !inFlightWhole) {
|
|
135
|
+
return inFlight.catch(function () { return {}; }).then(function () {
|
|
136
|
+
return fetchRemoteConfig(true);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
100
139
|
return inFlight;
|
|
101
140
|
}
|
|
102
141
|
if (!wanted()) {
|
|
103
142
|
logger.Console.log(LABEL + ': switched off by the server, not asking'); // no i18n
|
|
104
143
|
return Promise.resolve({});
|
|
105
144
|
}
|
|
106
|
-
var blocked = transport.rejectIfOffline(LABEL) || transport.rejectIfNoDeviceIds(LABEL);
|
|
145
|
+
var blocked = transport.rejectIfOffline(LABEL) || transport.rejectIfNoDeviceIds(LABEL, 'getupdates');
|
|
107
146
|
if (blocked) {
|
|
108
147
|
return blocked;
|
|
109
148
|
}
|
|
110
149
|
var asked = generation;
|
|
111
|
-
|
|
150
|
+
// the first fetch of the page asks for everything; the stored set is a previous visit's
|
|
151
|
+
var askWhole = whole === true || fetchedAt === 0;
|
|
152
|
+
var since = askWhole ? 0 : store.flagtime();
|
|
153
|
+
var askedAt = utils.nowMs();
|
|
154
|
+
var request = transport.postToApptics('getupdates', body(since), transport.apiHeaders(), LABEL).then(function (result) {
|
|
112
155
|
var data = (result.data || {});
|
|
113
156
|
if (asked !== generation) {
|
|
114
157
|
// asked before a reset(): the answer is about the device and app that were just forgotten
|
|
@@ -116,31 +159,44 @@ function fetchRemoteConfig() {
|
|
|
116
159
|
}
|
|
117
160
|
applyReportedModules(data);
|
|
118
161
|
var rules = data.remoteconfig;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (
|
|
122
|
-
|
|
162
|
+
fetchedAt = utils.nowMs();
|
|
163
|
+
failedAt = 0;
|
|
164
|
+
if (askWhole) {
|
|
165
|
+
// with flagtime 0 the answer is the whole set, and no block means there is nothing in it
|
|
166
|
+
store.store(rules && typeof rules === 'object' ? rules : {}, askedAt);
|
|
167
|
+
}
|
|
168
|
+
else if (rules && typeof rules === 'object') {
|
|
169
|
+
store.merge(rules, askedAt);
|
|
123
170
|
}
|
|
124
171
|
else {
|
|
125
172
|
// nothing has changed since our flagtime, which is the answer most of the time
|
|
126
|
-
store.storeUnchanged(
|
|
127
|
-
logger.Console.log(LABEL + ': no changes since ' +
|
|
173
|
+
store.storeUnchanged(askedAt);
|
|
174
|
+
logger.Console.log(LABEL + ': no changes since ' + since); // no i18n
|
|
128
175
|
}
|
|
129
176
|
return data;
|
|
130
177
|
}).then(function (data) {
|
|
131
178
|
settle();
|
|
132
179
|
return data;
|
|
133
180
|
}, function (err) {
|
|
181
|
+
if (asked === generation) {
|
|
182
|
+
failedAt = utils.nowMs();
|
|
183
|
+
}
|
|
134
184
|
settle();
|
|
135
185
|
return Promise.reject(err);
|
|
136
186
|
});
|
|
137
|
-
// a request reset() has disowned must not clear the in-flight slot of one made after it
|
|
187
|
+
// a request reset() has disowned must not clear the in-flight slot of one made after it, nor
|
|
188
|
+
// start the polling again
|
|
138
189
|
function settle() {
|
|
139
190
|
if (inFlight === request) {
|
|
140
191
|
inFlight = null;
|
|
192
|
+
inFlightWhole = false;
|
|
193
|
+
}
|
|
194
|
+
if (asked === generation) {
|
|
195
|
+
schedulePoll();
|
|
141
196
|
}
|
|
142
197
|
}
|
|
143
198
|
inFlight = request;
|
|
199
|
+
inFlightWhole = askWhole;
|
|
144
200
|
return inFlight;
|
|
145
201
|
}
|
|
146
202
|
/**
|
|
@@ -150,30 +206,57 @@ function fetchRemoteConfig() {
|
|
|
150
206
|
function resetRemoteConfig() {
|
|
151
207
|
generation++;
|
|
152
208
|
inFlight = null;
|
|
209
|
+
inFlightWhole = false;
|
|
153
210
|
fetchedAt = 0;
|
|
211
|
+
failedAt = 0;
|
|
212
|
+
coldCalls = [];
|
|
213
|
+
if (pollTimer !== null) {
|
|
214
|
+
clearTimeout(pollTimer);
|
|
215
|
+
pollTimer = null;
|
|
216
|
+
}
|
|
154
217
|
state._ZAP._remoteConfigReported = true;
|
|
155
218
|
custom.clearCustomConditionValues();
|
|
156
219
|
store.clearRemoteConfig();
|
|
157
220
|
}
|
|
158
221
|
/**
|
|
159
|
-
* Makes sure the values are this page's own: from a fetch that
|
|
160
|
-
*
|
|
161
|
-
*
|
|
222
|
+
* Makes sure the values are this page's own: from a fetch on this page that succeeded, kept up to
|
|
223
|
+
* date by the polling after it. With `coldFetch`, a fetch with flagtime 0 now, whatever this page
|
|
224
|
+
* already has, whose answer replaces what is stored: at most COLD_LIMIT a minute, and one past
|
|
225
|
+
* that answers from what this page has. Resolves true when they are, false when the
|
|
226
|
+
* fetch failed or could not be made — the caller then decides whether stored values will do.
|
|
227
|
+
* Never rejects.
|
|
162
228
|
*
|
|
163
229
|
* Waits for start-up and for the device to be registered, which a first visit is still doing
|
|
164
230
|
* when ready() resolves: getupdates is refused without the device ids.
|
|
165
231
|
*/
|
|
166
|
-
function obtain() {
|
|
232
|
+
function obtain(coldFetch) {
|
|
167
233
|
return lifecycle.ready().then(function (running) {
|
|
168
234
|
if (!running) {
|
|
169
235
|
return false;
|
|
170
236
|
}
|
|
171
|
-
var
|
|
172
|
-
if (
|
|
237
|
+
var cold = coldFetch === true;
|
|
238
|
+
if (cold) {
|
|
239
|
+
var now = utils.nowMs();
|
|
240
|
+
coldCalls = coldCalls.filter(function (at) { return now - at < COLD_WINDOW_MS; });
|
|
241
|
+
if (coldCalls.length >= COLD_LIMIT) {
|
|
242
|
+
logger.Console.warn(LABEL + ': coldFetch is limited to ' + COLD_LIMIT + ' a minute; this one is answered from the values this page has'); // no i18n
|
|
243
|
+
cold = false;
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
coldCalls.push(now);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (fetchedAt > 0 && !cold) {
|
|
173
250
|
return true;
|
|
174
251
|
}
|
|
252
|
+
// no answer on this page yet, and the last try failed moments ago: the stored values answer
|
|
253
|
+
// until RETRY_AFTER_FAILURE_MS has passed, and the polling keeps trying meanwhile
|
|
254
|
+
if (!cold && failedAt > 0 && utils.nowMs() - failedAt < RETRY_AFTER_FAILURE_MS) {
|
|
255
|
+
logger.Console.log(LABEL + ': the last fetch failed ' + (utils.nowMs() - failedAt) + ' ms ago; answering from the stored values'); // no i18n
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
175
258
|
return index.deviceReady().then(function () {
|
|
176
|
-
return fetchRemoteConfig();
|
|
259
|
+
return fetchRemoteConfig(cold);
|
|
177
260
|
}).then(function () {
|
|
178
261
|
return true;
|
|
179
262
|
}, function (err) {
|
|
@@ -182,9 +265,40 @@ function obtain() {
|
|
|
182
265
|
});
|
|
183
266
|
});
|
|
184
267
|
}
|
|
185
|
-
/**
|
|
268
|
+
/** A fetch for the polling: failures are logged, and the next tick tries again. */
|
|
269
|
+
function refresh(reason) {
|
|
270
|
+
if (!state._ZAP._ready) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
logger.Console.log(LABEL + ': asking for changes (' + reason + ')'); // no i18n
|
|
274
|
+
shield.quietly(LABEL, index.deviceReady().then(function () {
|
|
275
|
+
return fetchRemoteConfig();
|
|
276
|
+
}).catch(function (err) {
|
|
277
|
+
logger.Console.log(LABEL + ': fetch failed: ' + String(err)); // no i18n
|
|
278
|
+
}).then(keepPolling));
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* A fetch that never went out (offline, the device not registered yet, the server switched it
|
|
282
|
+
* off) schedules nothing itself: the polling carries on regardless, so it picks up again.
|
|
283
|
+
*/
|
|
284
|
+
function keepPolling() {
|
|
285
|
+
if (state._ZAP._ready && pollTimer === null && inFlight === null) {
|
|
286
|
+
schedulePoll();
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/** The next poll, POLL_MS after the last fetch settled, answered or not. */
|
|
290
|
+
function schedulePoll() {
|
|
291
|
+
if (pollTimer !== null) {
|
|
292
|
+
clearTimeout(pollTimer);
|
|
293
|
+
}
|
|
294
|
+
pollTimer = setTimeout(shield.shield(LABEL, function () {
|
|
295
|
+
pollTimer = null;
|
|
296
|
+
refresh('every ' + POLL_MS + ' ms'); // no i18n
|
|
297
|
+
}), POLL_MS);
|
|
298
|
+
}
|
|
299
|
+
/** Start-up's own fetch, so values are this session's without the app asking, then the polling. */
|
|
186
300
|
function fetchOnStart() {
|
|
187
|
-
obtain();
|
|
301
|
+
shield.quietly(LABEL, obtain().then(keepPolling));
|
|
188
302
|
}
|
|
189
303
|
hooks.hooks.resetRemoteConfig = resetRemoteConfig;
|
|
190
304
|
hooks.hooks.remoteConfigStarted = fetchOnStart;
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
/** AppticsRemoteConfig:
|
|
1
|
+
/** AppticsRemoteConfig: fetchValues and setCustomConditionValue, each with the parameter names the console shows. */
|
|
2
2
|
export declare const AppticsRemoteConfig: {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* The parameters named, or every parameter when none are: name -> value, null when there is
|
|
5
|
+
* none. coldFetch (false by default) asks the server for the whole set now.
|
|
6
|
+
*/
|
|
5
7
|
fetchValues: {
|
|
6
|
-
(names?: string[], callback?: (values: Record<string, string | null>) => void): Promise<Record<string, string | null>>;
|
|
7
|
-
(
|
|
8
|
+
(names?: string[], callback?: (values: Record<string, string | null>) => void, coldFetch?: boolean): Promise<Record<string, string | null>>;
|
|
9
|
+
(names: string[], coldFetch: boolean): Promise<Record<string, string | null>>;
|
|
10
|
+
(callback: (values: Record<string, string | null>) => void, coldFetch?: boolean): Promise<Record<string, string | null>>;
|
|
11
|
+
(coldFetch: boolean): Promise<Record<string, string | null>>;
|
|
8
12
|
};
|
|
9
13
|
/**
|
|
10
14
|
* What this device is for a custom condition, by the condition's name, as comma separated
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! @zoho_apptics/apptics-js-sdk v2.0.0
|
|
1
|
+
/*! @zoho_apptics/apptics-js-sdk v2.0.0 | (c) Zoho Corporation Private Limited | see LICENSE */
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
@@ -13,13 +13,15 @@ var store = require('./store.js');
|
|
|
13
13
|
* Remote config, shipped as its own entry point:
|
|
14
14
|
* import { AppticsRemoteConfig } from '@zoho_apptics/apptics-js-sdk/remote-config';
|
|
15
15
|
*
|
|
16
|
-
*
|
|
16
|
+
* fetchValues(['a']) reads one parameter, fetchValues(['a', 'b']) several, and fetchValues() every
|
|
17
17
|
* parameter the console has. Each parameter's value is the console's: the value of the first
|
|
18
18
|
* condition, in the console's order, that this device matches, else the parameter's default value.
|
|
19
19
|
* setCustomConditionValue(conditionname, 'a,b') answers the custom conditions.
|
|
20
|
-
* The rule set is stored and resolved on the device, so a
|
|
21
|
-
*
|
|
22
|
-
* on start-up,
|
|
20
|
+
* The rule set is stored and resolved on the device, so a read answers without waiting on the
|
|
21
|
+
* network, and a failed fetch answers with the values fetched before. The SDK fetches the whole set
|
|
22
|
+
* on start-up, then what changed every 3 minutes; coldFetch (true as
|
|
23
|
+
* the last argument) asks for the whole set now. It re-applies the rules on its own when
|
|
24
|
+
* AppticsUser.setUser changes the user.
|
|
23
25
|
*
|
|
24
26
|
* A name the console does not have is null, never undefined: a config system whose failure mode
|
|
25
27
|
* is `undefined` moves the problem into the app's rendering code.
|
|
@@ -40,30 +42,21 @@ function answer(value, callback) {
|
|
|
40
42
|
}
|
|
41
43
|
return value;
|
|
42
44
|
}
|
|
43
|
-
/**
|
|
44
|
-
* One parameter, fetched for this page, as Android's fetchValue: resolves with its value, or null
|
|
45
|
-
* when the console has no such parameter. A failed fetch answers with the value fetched before.
|
|
46
|
-
*/
|
|
47
|
-
function fetchValue(name, callback) {
|
|
48
|
-
return fetch.obtain().then(function () {
|
|
49
|
-
var value = raw(name);
|
|
50
|
-
return answer(value === undefined ? null : value, callback);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
45
|
/**
|
|
54
46
|
* Several parameters at once: `names` lists the ones wanted, and the result has exactly those
|
|
55
47
|
* names, null for one the console does not have. With no names (or an empty list), every
|
|
56
|
-
* parameter the console has. A callback may come first when there are no names
|
|
48
|
+
* parameter the console has. A callback may come first when there are no names, and `coldFetch`
|
|
49
|
+
* (false by default) last, in place of either.
|
|
57
50
|
*/
|
|
58
|
-
function fetchValues(names, callback) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
var wanted = wantedNames(names);
|
|
65
|
-
var done =
|
|
66
|
-
return fetch.obtain().then(function () {
|
|
51
|
+
function fetchValues(names, callback, coldFetch) {
|
|
52
|
+
var args = [names, callback, coldFetch];
|
|
53
|
+
// the first boolean is coldFetch, wherever it comes: fetchValues(true), fetchValues(names, true),
|
|
54
|
+
// fetchValues(callback, true)
|
|
55
|
+
var flag = args.filter(function (one) { return typeof one === 'boolean'; })[0]; // no i18n
|
|
56
|
+
var fn = args.filter(function (one) { return typeof one === 'function'; })[0]; // no i18n
|
|
57
|
+
var wanted = wantedNames(typeof names === 'boolean' || typeof names === 'function' ? undefined : names); // no i18n
|
|
58
|
+
var done = fn;
|
|
59
|
+
return fetch.obtain(flag === true).then(function () {
|
|
67
60
|
var values = store.all();
|
|
68
61
|
var out = {};
|
|
69
62
|
(wanted === null ? Object.keys(values) : wanted).forEach(function (name) {
|
|
@@ -91,16 +84,15 @@ function wantedNames(names) {
|
|
|
91
84
|
});
|
|
92
85
|
return out.length === 0 ? null : out;
|
|
93
86
|
}
|
|
94
|
-
const fetchValueGuarded = guard.guard('AppticsRemoteConfig.fetchValue', fetchValue); // no i18n
|
|
95
87
|
const fetchValuesGuarded = guard.guard('AppticsRemoteConfig.fetchValues', fetchValues); // no i18n
|
|
96
88
|
const setCustomConditionValueGuarded = guard.guard('AppticsRemoteConfig.setCustomConditionValue', custom.setCustomConditionValue); // no i18n
|
|
97
|
-
/** AppticsRemoteConfig:
|
|
89
|
+
/** AppticsRemoteConfig: fetchValues and setCustomConditionValue, each with the parameter names the console shows. */
|
|
98
90
|
const AppticsRemoteConfig = {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
fetchValues: function fetchValues(names, callback) {
|
|
91
|
+
/**
|
|
92
|
+
* The parameters named, or every parameter when none are: name -> value, null when there is
|
|
93
|
+
* none. coldFetch (false by default) asks the server for the whole set now.
|
|
94
|
+
*/
|
|
95
|
+
fetchValues: function fetchValues(names, callback, coldFetch) {
|
|
104
96
|
return guard.forward(fetchValuesGuarded, arguments);
|
|
105
97
|
}, // no i18n
|
|
106
98
|
/**
|
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
import type { Dict } from '../types.js';
|
|
2
|
-
/**
|
|
2
|
+
/** When the last answered getupdates went out; 0 when nothing is stored. */
|
|
3
3
|
export declare function flagtime(): number;
|
|
4
4
|
/** Re-resolves the stored rules against this device, without fetching. */
|
|
5
5
|
export declare function reresolve(): void;
|
|
6
|
-
/** Stores a rule set the server just sent, and resolves it. */
|
|
7
|
-
export declare function store(rules: Dict, at: number): void;
|
|
8
6
|
/**
|
|
9
|
-
*
|
|
7
|
+
* Stores a whole rule set, the answer to flagtime 0, in place of what was stored, and resolves
|
|
8
|
+
* it. `askedAt` is when the request went out: the next one asks for what changed since then.
|
|
9
|
+
*/
|
|
10
|
+
export declare function store(rules: Dict, askedAt: number): void;
|
|
11
|
+
/**
|
|
12
|
+
* Merges what a later getupdates sent, everything added or changed since its flagtime, into the
|
|
13
|
+
* stored rule set, and resolves the result. With nothing stored, it is the whole set.
|
|
14
|
+
*/
|
|
15
|
+
export declare function merge(changes: Dict, askedAt: number): void;
|
|
16
|
+
/**
|
|
17
|
+
* Records a later fetch that brought no changes.
|
|
10
18
|
*
|
|
11
|
-
* That is the normal answer
|
|
12
|
-
*
|
|
19
|
+
* That is the normal answer, and it is not a failure: what is stored stays, and the next request
|
|
20
|
+
* asks for what changed since this one went out.
|
|
13
21
|
*/
|
|
14
|
-
export declare function storeUnchanged(
|
|
22
|
+
export declare function storeUnchanged(askedAt: number): void;
|
|
15
23
|
/** Forgets the rule set and its values, so the next fetch asks with flagtime 0. */
|
|
16
24
|
export declare function clearRemoteConfig(): void;
|
|
17
25
|
/** Every parameter resolved for this device. Loads from storage on the first read. */
|
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
/*! @zoho_apptics/apptics-js-sdk v2.0.0
|
|
1
|
+
/*! @zoho_apptics/apptics-js-sdk v2.0.0 | (c) Zoho Corporation Private Limited | see LICENSE */
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
var logger = require('../core/logger.js');
|
|
5
5
|
var storage = require('../core/storage.js');
|
|
6
|
+
var utils = require('../core/utils.js');
|
|
6
7
|
var evaluate = require('./evaluate.js');
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* What remote config keeps between visits.
|
|
10
11
|
*
|
|
11
|
-
* The rule set is stored, not just the resolved values
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* flagtime 0 on every visit. And the resolved values depend on the device: an app version or a
|
|
15
|
-
* customer group that changed since the last visit has to re-resolve against the same rules.
|
|
12
|
+
* The rule set is stored, not just the resolved values: the values depend on the device, so an
|
|
13
|
+
* app version or a customer group that changed has to re-resolve against the same rules, and a
|
|
14
|
+
* visit that cannot reach the server still has the last visit's values to read.
|
|
16
15
|
*
|
|
17
|
-
* `flagtime` is the whole protocol here
|
|
18
|
-
*
|
|
16
|
+
* `flagtime` is the whole protocol here. The first getupdates of a page load sends 0 and gets the
|
|
17
|
+
* whole rule set, which replaces what is stored. Each later one sends the time the last answered
|
|
18
|
+
* request went out, and gets only what was added or changed since: those parameters and
|
|
19
|
+
* conditions are merged into the stored set, and one that did not change is kept as it was.
|
|
19
20
|
*/
|
|
20
21
|
const STORE_KEY = 'remoteconfig'; // no i18n
|
|
21
22
|
var values = {};
|
|
@@ -24,55 +25,86 @@ function read() {
|
|
|
24
25
|
var saved = storage.user_defaults.item(STORE_KEY);
|
|
25
26
|
return saved && typeof saved === 'object' ? saved : null;
|
|
26
27
|
}
|
|
27
|
-
/**
|
|
28
|
+
/** When the last answered getupdates went out; 0 when nothing is stored. */
|
|
28
29
|
function flagtime() {
|
|
29
30
|
var saved = read();
|
|
30
31
|
return saved && typeof saved.flagtime === 'number' ? saved.flagtime : 0;
|
|
31
32
|
}
|
|
32
|
-
/**
|
|
33
|
-
* The newest `version[].date` in a rule set, which is what the next fetch sends as flagtime.
|
|
34
|
-
*
|
|
35
|
-
* The array is a publication history and is not promised to be in order, so it is the highest
|
|
36
|
-
* date that matters rather than the first or last entry.
|
|
37
|
-
*/
|
|
38
|
-
function publishedAt(rules) {
|
|
39
|
-
var versions = Array.isArray(rules.version) ? rules.version : [];
|
|
40
|
-
var newest = 0;
|
|
41
|
-
versions.forEach(function (one) {
|
|
42
|
-
var at = Number(one.date);
|
|
43
|
-
if (isFinite(at) && at > newest) {
|
|
44
|
-
newest = at;
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
return newest;
|
|
48
|
-
}
|
|
49
33
|
/** Re-resolves the stored rules against this device, without fetching. */
|
|
50
34
|
function reresolve() {
|
|
51
35
|
var saved = read();
|
|
52
36
|
values = saved && saved.rules ? evaluate.resolveAll(saved.rules) : {};
|
|
53
37
|
loaded = true;
|
|
54
38
|
}
|
|
55
|
-
/**
|
|
56
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Stores a whole rule set, the answer to flagtime 0, in place of what was stored, and resolves
|
|
41
|
+
* it. `askedAt` is when the request went out: the next one asks for what changed since then.
|
|
42
|
+
*/
|
|
43
|
+
function store(rules, askedAt) {
|
|
57
44
|
evaluate.resetEvaluationWarnings();
|
|
58
|
-
var record = { flagtime:
|
|
45
|
+
var record = { flagtime: askedAt, rules: rules, fetchedat: utils.nowMs() };
|
|
59
46
|
storage.user_defaults.add(STORE_KEY, record);
|
|
60
47
|
values = evaluate.resolveAll(rules);
|
|
61
48
|
loaded = true;
|
|
62
|
-
logger.Console.log('AppticsRemoteConfig: ' + Object.keys(values).length + ' parameters,
|
|
49
|
+
logger.Console.log('AppticsRemoteConfig: ' + Object.keys(values).length + ' parameters, as of ' + askedAt); // no i18n
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* `changed` laid over `stored`, matched by `key`: an entry the server resent replaces the one
|
|
53
|
+
* with the same key, in its place; a new one goes on the end; the rest stay as they were.
|
|
54
|
+
*/
|
|
55
|
+
function mergeList(stored, changed, key) {
|
|
56
|
+
var out = Array.isArray(stored) ? stored.slice() : [];
|
|
57
|
+
if (!Array.isArray(changed)) {
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
changed.forEach(function (entry) {
|
|
61
|
+
if (!entry || typeof entry !== 'object') {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
var id = key(entry);
|
|
65
|
+
var at = id === '' ? -1 : out.findIndex(function (one) { return key(one) === id; });
|
|
66
|
+
if (at >= 0) {
|
|
67
|
+
out[at] = entry;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
out.push(entry);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
/** An entry's id, or its name when it has none, as the key to match a resent entry by. */
|
|
76
|
+
function idOr(idKey, nameKey) {
|
|
77
|
+
return function (entry) {
|
|
78
|
+
var id = entry[idKey] !== undefined && entry[idKey] !== null ? entry[idKey] : entry[nameKey];
|
|
79
|
+
return id === undefined || id === null ? '' : String(id); // no i18n
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Merges what a later getupdates sent, everything added or changed since its flagtime, into the
|
|
84
|
+
* stored rule set, and resolves the result. With nothing stored, it is the whole set.
|
|
85
|
+
*/
|
|
86
|
+
function merge(changes, askedAt) {
|
|
87
|
+
var saved = read();
|
|
88
|
+
var base = saved && saved.rules && typeof saved.rules === 'object' ? saved.rules : {};
|
|
89
|
+
var rules = Object.assign({}, base, changes);
|
|
90
|
+
rules.all_parameter = mergeList(base.all_parameter, changes.all_parameter, idOr('paramid', 'paramname')); // no i18n
|
|
91
|
+
rules.all_condition = mergeList(base.all_condition, changes.all_condition, idOr('conditionid', 'conditionname')); // no i18n
|
|
92
|
+
rules.version = mergeList(base.version, changes.version, idOr('versionid', 'date')); // no i18n
|
|
93
|
+
store(rules, askedAt);
|
|
63
94
|
}
|
|
64
95
|
/**
|
|
65
|
-
* Records a fetch that brought no
|
|
96
|
+
* Records a later fetch that brought no changes.
|
|
66
97
|
*
|
|
67
|
-
* That is the normal answer
|
|
68
|
-
*
|
|
98
|
+
* That is the normal answer, and it is not a failure: what is stored stays, and the next request
|
|
99
|
+
* asks for what changed since this one went out.
|
|
69
100
|
*/
|
|
70
|
-
function storeUnchanged(
|
|
101
|
+
function storeUnchanged(askedAt) {
|
|
71
102
|
var saved = read();
|
|
72
103
|
if (saved === null) {
|
|
73
104
|
return;
|
|
74
105
|
}
|
|
75
|
-
saved.
|
|
106
|
+
saved.flagtime = askedAt;
|
|
107
|
+
saved.fetchedat = utils.nowMs();
|
|
76
108
|
storage.user_defaults.add(STORE_KEY, saved);
|
|
77
109
|
}
|
|
78
110
|
/** Forgets the rule set and its values, so the next fetch asks with flagtime 0. */
|
|
@@ -93,6 +125,7 @@ function all() {
|
|
|
93
125
|
exports.all = all;
|
|
94
126
|
exports.clearRemoteConfig = clearRemoteConfig;
|
|
95
127
|
exports.flagtime = flagtime;
|
|
128
|
+
exports.merge = merge;
|
|
96
129
|
exports.reresolve = reresolve;
|
|
97
130
|
exports.store = store;
|
|
98
131
|
exports.storeUnchanged = storeUnchanged;
|