@amedia/user 0.13.26 → 0.13.28
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/amedia-user-js.iml +9 -0
- package/checkVersion.js +12 -0
- package/dist/cache/engines/indexeddb.d.ts +7 -0
- package/dist/clients/europa.d.ts +3 -7
- package/dist/index.d.ts +36 -7
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +3 -3
- package/dist/index.js +1 -1
- package/dist/index.js.LICENSE.txt +14 -0
- package/dist/index.js.map +3 -3
- package/dist/janitor/index.spec.d.ts +4 -0
- package/dist/lib/PaywallUnlocker.d.ts +16 -0
- package/dist/lib/PaywallUnlocker.spec.d.ts +4 -0
- package/dist/lib/User.d.ts +21 -0
- package/dist/utils/debouncer.d.ts +26 -0
- package/dist/utils/debouncer.spec.d.ts +1 -0
- package/dist/utils/logValidationError.d.ts +1 -0
- package/eik.json +1 -1
- package/esm/actions/authentication.d.ts +5 -0
- package/esm/actions/authentication.spec.d.ts +4 -0
- package/esm/actions/authorization.d.ts +9 -0
- package/esm/actions/preferences.d.ts +9 -0
- package/esm/actions/preferences.spec.d.ts +1 -0
- package/esm/actions/storage.d.ts +4 -0
- package/esm/actions/userData.d.ts +2 -0
- package/esm/autologin/index.d.ts +1 -0
- package/esm/autologin/thirdPartyCookie.d.ts +3 -0
- package/esm/autologin/thirdPartyCookie.spec.d.ts +1 -0
- package/esm/cache/engines/global.d.ts +21 -0
- package/esm/cache/engines/global.spec.d.ts +1 -0
- package/esm/cache/engines/indexeddb.d.ts +7 -0
- package/esm/cache/index.d.ts +11 -0
- package/esm/cache/index.spec.d.ts +4 -0
- package/esm/cache/types.d.ts +11 -0
- package/esm/clients/europa.d.ts +28 -0
- package/esm/clients/europa.spec.d.ts +1 -0
- package/esm/clients/jupiter.d.ts +30 -0
- package/esm/clients/jupiter.spec.d.ts +1 -0
- package/esm/clients/mercury.d.ts +18 -0
- package/esm/clients/mercury.spec.d.ts +4 -0
- package/esm/clients/nebula.d.ts +2 -0
- package/esm/clients/nebula.spec.d.ts +1 -0
- package/esm/clients/pluto.d.ts +3 -0
- package/esm/clients/pluto.spec.d.ts +1 -0
- package/esm/eventHandlers/activation.d.ts +15 -0
- package/esm/eventHandlers/authentication.d.ts +17 -0
- package/esm/eventHandlers/userAttributes.d.ts +24 -0
- package/esm/exceptions/PaywallError.d.ts +7 -0
- package/esm/index-authentcation.spec.d.ts +4 -0
- package/esm/index-storage.spec.d.ts +4 -0
- package/esm/index-subscription.spec.d.ts +4 -0
- package/esm/index.d.ts +26 -0
- package/esm/janitor/index.d.ts +10 -0
- package/esm/janitor/jobs/accessCookieRefresher.d.ts +1 -0
- package/esm/janitor/jobs/accessCookieRefresher.spec.d.ts +4 -0
- package/esm/lib/PaywallUnlocker.d.ts +16 -0
- package/esm/lib/PaywallUnlocker.spec.d.ts +4 -0
- package/esm/lib/User.d.ts +21 -0
- package/esm/lib/cookie.d.ts +5 -0
- package/esm/lib/rpc.d.ts +6 -0
- package/esm/store/index.d.ts +22 -0
- package/esm/store/index.spec.d.ts +1 -0
- package/esm/types.d.ts +22 -0
- package/esm/user.js +2 -0
- package/esm/user.js.map +1 -0
- package/esm/utils/customEventWrapper.d.ts +17 -0
- package/esm/utils/deepEquals.d.ts +4 -0
- package/esm/utils/getOrUpdate.d.ts +6 -0
- package/esm/utils/getOrUpdate.spec.d.ts +1 -0
- package/esm/utils/location.d.ts +3 -0
- package/esm/utils/log.d.ts +26 -0
- package/esm/utils/queue.d.ts +12 -0
- package/esm/utils/url.d.ts +6 -0
- package/esm/utils/url.spec.d.ts +1 -0
- package/package.json +3 -2
- package/src/clients/europa.spec.ts +8 -13
- package/src/clients/europa.ts +41 -40
- package/src/clients/mercuryLog.ts +3 -1
- package/src/utils/debouncer.spec.ts +257 -0
- package/src/utils/debouncer.ts +123 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import deepmerge from 'ts-deepmerge';
|
|
2
|
+
|
|
3
|
+
/** Naive implementation of deep copy needed for copying parameters when
|
|
4
|
+
* merging multiple calls to perform.
|
|
5
|
+
*
|
|
6
|
+
* @param obj object to copy deeply
|
|
7
|
+
*/
|
|
8
|
+
function deepCopy<T>(obj: T): T {
|
|
9
|
+
if (typeof obj === 'string' || obj instanceof String) {
|
|
10
|
+
return `${obj}` as unknown as T;
|
|
11
|
+
}
|
|
12
|
+
if (obj instanceof Array) {
|
|
13
|
+
return [...obj].map(deepCopy) as unknown as T;
|
|
14
|
+
}
|
|
15
|
+
return Object.entries(obj).reduce(
|
|
16
|
+
(copy, [k, v]) => ({
|
|
17
|
+
...copy,
|
|
18
|
+
[k]: deepCopy(v),
|
|
19
|
+
}),
|
|
20
|
+
{}
|
|
21
|
+
) as T;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The Debouncer can be used when we want a clean way to say 'I want to fetch this and that, and once it's done,
|
|
26
|
+
* resolve my promise', but instead of the fetch being performed right away, we want to wait for more users to
|
|
27
|
+
* request other stuff. This will keep us from performing lots of individual fetches to the backend.
|
|
28
|
+
*
|
|
29
|
+
* Our main use case is a standard publication front page where one component on the page requests the euucp namespace
|
|
30
|
+
* from Europa, and then another component requests the privacy_preferences namespace. Instead of the Europa client
|
|
31
|
+
* calling two fetches to the server, one for euucp and another for privacy_preferences, we want the Debouncer to
|
|
32
|
+
* aggregate these calls into one call to the backend, that requests both euucp AND privacy_preferences, so the response
|
|
33
|
+
* to both components will be both namespaces (which the Europa client can filter before it actually reaches the
|
|
34
|
+
* component).
|
|
35
|
+
*/
|
|
36
|
+
class Debouncer<ParamsType, ResultType> {
|
|
37
|
+
private timer: ReturnType<typeof setTimeout> | undefined;
|
|
38
|
+
|
|
39
|
+
private params: ParamsType;
|
|
40
|
+
|
|
41
|
+
private queue: {
|
|
42
|
+
resolve: (arg0: any) => void;
|
|
43
|
+
reject: (reason?: any) => void;
|
|
44
|
+
}[] = [];
|
|
45
|
+
|
|
46
|
+
constructor(
|
|
47
|
+
// The number of milliseconds to wait for new calls to perform before actually performing workload
|
|
48
|
+
private timeoutMs: number,
|
|
49
|
+
|
|
50
|
+
// Default parameters if none provided, normally an empty object or list
|
|
51
|
+
private defaultParams: ParamsType,
|
|
52
|
+
|
|
53
|
+
// Function that can accumulate parameters from successive calls to perform, see examples below
|
|
54
|
+
private paramAccumulator: (
|
|
55
|
+
previousParams: ParamsType,
|
|
56
|
+
newParams: ParamsType
|
|
57
|
+
) => ParamsType,
|
|
58
|
+
|
|
59
|
+
// The workload we want to perform on accumulated parameters once timeout has been reached
|
|
60
|
+
private workload: (params: ParamsType) => Promise<ResultType>
|
|
61
|
+
) {
|
|
62
|
+
this.params = deepCopy(defaultParams);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Caller indicates that it wants to perform workload on a set of parameters. We will wait this.timeoutMs milliseconds
|
|
66
|
+
// and see if we get more calls to perform workload, if we do, we accumulate parameters and wait this.timeoutMs
|
|
67
|
+
// more milliseconds until we finally perform workload and resolve promises.
|
|
68
|
+
perform(newParams: ParamsType): Promise<ResultType> {
|
|
69
|
+
return new Promise((resolve, reject) => {
|
|
70
|
+
if (this.timer) {
|
|
71
|
+
clearTimeout(this.timer);
|
|
72
|
+
}
|
|
73
|
+
this.params = this.paramAccumulator(this.params, newParams);
|
|
74
|
+
this.queue.push({ resolve, reject });
|
|
75
|
+
this.timer = setTimeout(async () => {
|
|
76
|
+
// Copy current state to use for performing workload and resolving promises after workload completes
|
|
77
|
+
const params = deepCopy(this.params);
|
|
78
|
+
const promisesToKeep = [...this.queue];
|
|
79
|
+
|
|
80
|
+
// Setup new clean state for future calls to perform
|
|
81
|
+
this.params = this.defaultParams;
|
|
82
|
+
this.queue = [];
|
|
83
|
+
|
|
84
|
+
// Perform workload and resolve promises for all callers with result
|
|
85
|
+
this.workload(params)
|
|
86
|
+
.then((result) => {
|
|
87
|
+
promisesToKeep.forEach((promise) => {
|
|
88
|
+
promise.resolve(result);
|
|
89
|
+
});
|
|
90
|
+
})
|
|
91
|
+
.catch((e) => {
|
|
92
|
+
// If workload fails, we need to reject all promises so callers won't be left hanging
|
|
93
|
+
promisesToKeep.forEach((promise) => {
|
|
94
|
+
promise.reject(e);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}, this.timeoutMs);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Accumulate parameters as lists of strings
|
|
102
|
+
static stringListParamsAccumulator(
|
|
103
|
+
accumulatedParams: string[],
|
|
104
|
+
newParams: string[]
|
|
105
|
+
) {
|
|
106
|
+
newParams.forEach((param) => {
|
|
107
|
+
if (!accumulatedParams.includes(param)) {
|
|
108
|
+
accumulatedParams.push(param);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
return accumulatedParams;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Accumulate parameters as map with string keys
|
|
115
|
+
static stringMapParamsAccumulator(
|
|
116
|
+
previousParams: Record<string, any>,
|
|
117
|
+
newParams: Record<string, any>
|
|
118
|
+
) {
|
|
119
|
+
return deepmerge(previousParams, newParams);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export default Debouncer;
|