@amedia/user 0.5.3 → 0.6.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/README.md +2 -2
- package/cloudbuild.json +29 -0
- package/cop.json +8 -0
- package/dist/actions/authentication.d.ts +1 -1
- package/dist/actions/preferences.d.ts +9 -3
- package/dist/cache/index.d.ts +3 -2
- package/dist/clients/europa.d.ts +12 -0
- package/dist/clients/europa.spec.d.ts +1 -0
- package/dist/clients/jupiter.d.ts +29 -0
- package/dist/clients/jupiter.spec.d.ts +1 -0
- package/dist/clients/mercury.d.ts +18 -0
- package/dist/clients/mercury.spec.d.ts +4 -0
- package/dist/clients/nebula.d.ts +22 -0
- package/dist/clients/nebula.spec.d.ts +1 -0
- package/dist/clients/pluto.d.ts +8 -0
- package/dist/clients/pluto.spec.d.ts +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/lib/rpc.d.ts +6 -1
- package/dist/store/store.d.ts +13 -0
- package/dist/store/store.spec.d.ts +1 -0
- package/dist/types.d.ts +7 -0
- package/dist/utils/getOrUpdate.d.ts +3 -0
- package/package.json +4 -3
- package/src/actions/authentication.ts +2 -2
- package/src/actions/authorization.ts +4 -7
- package/src/actions/preferences.spec.ts +4 -5
- package/src/actions/preferences.ts +49 -47
- package/src/actions/userData.ts +2 -0
- package/src/cache/index.spec.ts +7 -2
- package/src/cache/index.ts +4 -3
- package/src/{services → clients}/europa.spec.ts +19 -20
- package/src/{services → clients}/europa.ts +3 -3
- package/src/{services → clients}/mercury.ts +0 -1
- package/src/index-authentcation.spec.ts +2 -2
- package/src/index-userdata.spec.ts +11 -11
- package/src/index.ts +4 -3
- package/src/lib/rpc.ts +9 -3
- package/src/store/store.spec.ts +34 -0
- package/src/store/store.ts +46 -0
- package/src/types.ts +8 -0
- package/src/utils/getOrUpdate.ts +41 -0
- package/src/utils/log.ts +2 -1
- /package/src/{services → clients}/jupiter.spec.ts +0 -0
- /package/src/{services → clients}/jupiter.ts +0 -0
- /package/src/{services → clients}/mercury.spec.ts +0 -0
- /package/src/{services → clients}/nebula.spec.ts +0 -0
- /package/src/{services → clients}/nebula.ts +0 -0
- /package/src/{services → clients}/pluto.spec.ts +0 -0
- /package/src/{services → clients}/pluto.ts +0 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { State, UserAttributes } from '../types';
|
|
2
|
+
|
|
3
|
+
type StoreDataMap = {
|
|
4
|
+
userAttributes: UserAttributes;
|
|
5
|
+
state: State;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type Listener<T> = (data: T) => void;
|
|
9
|
+
type Listeners<T = StoreDataMap> = {
|
|
10
|
+
[key in keyof T]?: Array<Listener<T[key]>>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const store: { [index in keyof StoreDataMap]?: StoreDataMap[index] } = {};
|
|
14
|
+
const listeners: Listeners = {};
|
|
15
|
+
|
|
16
|
+
const emit = (key: keyof StoreDataMap): void => {
|
|
17
|
+
listeners[key]?.forEach((listener) => listener(store[key]));
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const on = (
|
|
21
|
+
key: keyof StoreDataMap,
|
|
22
|
+
listener: Listener<StoreDataMap[typeof key]>
|
|
23
|
+
) => {
|
|
24
|
+
if (!listeners[key]) {
|
|
25
|
+
listeners[key] = [];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// @ts-ignore todo How can this possibly be undefined?
|
|
29
|
+
listeners[key].push(listener);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const set = (key: keyof StoreDataMap, value: StoreDataMap[typeof key]) => {
|
|
33
|
+
// @ts-ignore todo Not sure why this type fails.
|
|
34
|
+
store[key] = { ...store[key], ...value };
|
|
35
|
+
emit(key);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const get = (key: keyof StoreDataMap): StoreDataMap[typeof key] | undefined =>
|
|
39
|
+
store[key];
|
|
40
|
+
|
|
41
|
+
const clear = () => {
|
|
42
|
+
store.state = undefined;
|
|
43
|
+
store.userAttributes = undefined;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export default { set, get, on, clear };
|
package/src/types.ts
CHANGED
|
@@ -5,6 +5,7 @@ export type UserAttributes = {
|
|
|
5
5
|
trackingKey?: string;
|
|
6
6
|
extraData?: [];
|
|
7
7
|
access?: string[];
|
|
8
|
+
privacyPreferences?: PrivacyPreferences;
|
|
8
9
|
};
|
|
9
10
|
|
|
10
11
|
export type State = {
|
|
@@ -15,3 +16,10 @@ export type State = {
|
|
|
15
16
|
export type AnonymousAttributes = {
|
|
16
17
|
age: number;
|
|
17
18
|
};
|
|
19
|
+
|
|
20
|
+
export type PrivacyPreferences = {
|
|
21
|
+
allowResearchUsage: Boolean;
|
|
22
|
+
connectWithSurveys: Boolean;
|
|
23
|
+
personalizedAds: Boolean;
|
|
24
|
+
personalizedContent: Boolean;
|
|
25
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Runtype, Static } from 'runtypes';
|
|
2
|
+
import queue from './queue';
|
|
3
|
+
import cache from '../cache';
|
|
4
|
+
|
|
5
|
+
const DEFAULT_TTL_10_SECONDS = 10000;
|
|
6
|
+
const cacheTTLMap: { [index: string]: number } = {};
|
|
7
|
+
|
|
8
|
+
const getOrUpdate = <T extends Runtype>(
|
|
9
|
+
key: string,
|
|
10
|
+
schema: T,
|
|
11
|
+
action: () => Promise<Static<T>>
|
|
12
|
+
): Promise<Static<T>> =>
|
|
13
|
+
new Promise((resolve, reject) => {
|
|
14
|
+
queue.add(key, { resolve, reject });
|
|
15
|
+
if (queue.peekQueueSize(key) > 1) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
cache.get(key, schema).then((cacheResponse) => {
|
|
20
|
+
if (cacheResponse) {
|
|
21
|
+
queue.getAll(key).forEach((promise) => {
|
|
22
|
+
promise.resolve(cacheResponse);
|
|
23
|
+
});
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
action()
|
|
27
|
+
.then((res) => {
|
|
28
|
+
cache.set(key, res, cacheTTLMap[key] || DEFAULT_TTL_10_SECONDS);
|
|
29
|
+
queue.getAll(key).forEach((promise) => {
|
|
30
|
+
promise.resolve(res);
|
|
31
|
+
});
|
|
32
|
+
})
|
|
33
|
+
.catch(() => {
|
|
34
|
+
queue.getAll(key).forEach((promise) => {
|
|
35
|
+
promise.reject();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export default getOrUpdate;
|
package/src/utils/log.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import mercury from '../
|
|
1
|
+
import mercury from '../clients/mercury';
|
|
2
2
|
|
|
3
3
|
export type Logger = {
|
|
4
4
|
debug: (message: string | any, ...extra: any[]) => void;
|
|
@@ -13,6 +13,7 @@ export const setLogger = (engine: Logger) => {
|
|
|
13
13
|
logger = engine;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
16
17
|
const getLogger = () => logger || mercury;
|
|
17
18
|
|
|
18
19
|
/* export default {
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|