@applicaster/zapp-react-native-utils 16.0.0-rc.122 → 16.0.0-rc.124

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 (21) hide show
  1. package/appUtils/contextKeysManager/__tests__/getKey/failure.test.ts +1 -22
  2. package/appUtils/contextKeysManager/__tests__/getKey/success/fullSearch.test.ts +1 -25
  3. package/appUtils/contextKeysManager/__tests__/getKey/success/searchInSecure.test.ts +1 -7
  4. package/appUtils/contextKeysManager/__tests__/removeKey/failure.test.ts +0 -25
  5. package/appUtils/contextKeysManager/__tests__/removeKey/success.test.ts +0 -6
  6. package/appUtils/contextKeysManager/__tests__/setKey/failure/failedToSaveInStorage.test.ts +0 -40
  7. package/appUtils/contextKeysManager/__tests__/setKey/{success/saveItemAndReference.test.ts → success.test.ts} +2 -42
  8. package/appUtils/contextKeysManager/index.ts +28 -129
  9. package/manifestUtils/keys.js +1 -1
  10. package/package.json +3 -2
  11. package/quickBrickAssets/__tests__/index.test.ts +18 -0
  12. package/quickBrickAssets/index.ts +35 -0
  13. package/reactHooks/actions/index.ts +29 -23
  14. package/appUtils/contextKeysManager/__tests__/getKey/success/byReference.test.ts +0 -56
  15. package/appUtils/contextKeysManager/__tests__/getKeyByReference/failure.test.ts +0 -265
  16. package/appUtils/contextKeysManager/__tests__/getKeyByReference/success.test.ts +0 -303
  17. package/appUtils/contextKeysManager/__tests__/removeReferenceForKey/failure.test.ts +0 -82
  18. package/appUtils/contextKeysManager/__tests__/removeReferenceForKey/success.test.ts +0 -41
  19. package/appUtils/contextKeysManager/__tests__/setKey/success/saveItemButNotReference.test.ts +0 -208
  20. package/appUtils/contextKeysManager/__tests__/setReferenceForKey/failure.test.ts +0 -93
  21. package/appUtils/contextKeysManager/__tests__/setReferenceForKey/success.test.ts +0 -45
@@ -1,6 +1,6 @@
1
1
  import { isNil } from "@applicaster/zapp-react-native-utils/utils";
2
2
 
3
- import { StorageLevel, StorageType } from "./consts";
3
+ import { StorageLevel } from "./consts";
4
4
  import {
5
5
  getNamespaceAndKey,
6
6
  keyIsValid,
@@ -59,18 +59,14 @@ type Setup = {
59
59
  sessionStorage?: Storage;
60
60
  localStorage?: Storage;
61
61
  secureStorage?: Storage;
62
- referenceStorage?: Storage;
63
62
  };
64
63
 
65
- export const REFERENCE_NAMESPACE = "applicaster.v2.references";
66
-
67
64
  export class ContextKeysManager {
68
65
  private static _instance: ContextKeysManager;
69
66
  private _logger: Logger;
70
67
  private _sessionStorage: Storage;
71
68
  private _localStorage: Storage;
72
69
  private _secureStorage: Storage;
73
- private _referenceStorage: Storage;
74
70
 
75
71
  public static get instance() {
76
72
  if (!this._instance) {
@@ -79,7 +75,6 @@ export class ContextKeysManager {
79
75
  sessionStorage: SessionStorage,
80
76
  localStorage: LocalStorage,
81
77
  secureStorage: SecureStorage,
82
- referenceStorage: SessionStorage,
83
78
  });
84
79
 
85
80
  this._instance = instance;
@@ -94,13 +89,11 @@ export class ContextKeysManager {
94
89
  sessionStorage = SessionStorage,
95
90
  localStorage = LocalStorage,
96
91
  secureStorage = SecureStorage,
97
- referenceStorage = SessionStorage,
98
- }: Setup) {
92
+ }: Setup = {}) {
99
93
  this._logger = logger;
100
94
  this._sessionStorage = sessionStorage;
101
95
  this._localStorage = localStorage;
102
96
  this._secureStorage = secureStorage;
103
- this._referenceStorage = referenceStorage;
104
97
  }
105
98
 
106
99
  private parseKey(key: KeyName): ParsedKeyAndNamespace {
@@ -119,41 +112,6 @@ export class ContextKeysManager {
119
112
  return { isValid: true, ...parsedKey };
120
113
  }
121
114
 
122
- // GET
123
- public async getKeyByReference(
124
- key: string,
125
- namespace: string
126
- ): Promise<ValueOrNothing> {
127
- // 1) look for storage
128
- // 2) if storage found - look value in it
129
- // 3) if storage not found return NOTHING
130
-
131
- return this._referenceStorage
132
- .getItem(buildNamespaceKey(key, namespace), REFERENCE_NAMESPACE)
133
- .then((storage: StorageType | unknown) => {
134
- switch (storage) {
135
- case StorageType.session:
136
- return this._sessionStorage.getItem(key, namespace);
137
- case StorageType.local:
138
- return this._localStorage.getItem(key, namespace);
139
- case StorageType.secure:
140
- return this._secureStorage.getItem(key, namespace);
141
- default:
142
- // unknown storage
143
- return NOTHING;
144
- }
145
- })
146
- .catch((error) => {
147
- // something went wrong
148
- this._logger.warn({
149
- message: error.message,
150
- data: { key, namespace },
151
- });
152
-
153
- return NOTHING;
154
- });
155
- }
156
-
157
115
  public async getKeys(
158
116
  keys: Array<KeyName>,
159
117
  contextObj?: { key: string; required?: boolean }[]
@@ -228,19 +186,7 @@ export class ContextKeysManager {
228
186
  return NOTHING;
229
187
  }
230
188
 
231
- // at first - try to find by reference
232
- // if not found, look in session > local > secure
233
-
234
- const resultByReference = await this.getKeyByReference(
235
- parsedKey,
236
- parsedNamespace
237
- );
238
-
239
- if (!isNil(resultByReference)) {
240
- return resultByReference;
241
- }
242
-
243
- // try to full-search
189
+ // look in session > local > secure
244
190
  try {
245
191
  const resultFromSessionStorage = await this._sessionStorage.getItem(
246
192
  parsedKey,
@@ -284,41 +230,6 @@ export class ContextKeysManager {
284
230
  // GET
285
231
 
286
232
  // SET
287
- public async setReferenceForKey(
288
- key: string,
289
- namespace: string,
290
- storageLevel: StorageLevel
291
- ): Promise<boolean> {
292
- return this._referenceStorage
293
- .setItem(
294
- buildNamespaceKey(key, namespace),
295
- storageLevel,
296
- REFERENCE_NAMESPACE
297
- )
298
- .then((result) => {
299
- if (savingResultIsSuccess(result)) {
300
- return true;
301
- }
302
-
303
- this._logger.warn({
304
- message:
305
- "While saving reference - reference storage return non expected value: true",
306
- data: { key, namespace, storageLevel },
307
- });
308
-
309
- return false;
310
- })
311
- .catch((error) => {
312
- // something went wrong
313
- this._logger.warn({
314
- message: error.message,
315
- data: { key, namespace, storageLevel },
316
- });
317
-
318
- return false;
319
- });
320
- }
321
-
322
233
  // for all keys where saving were succeed return true
323
234
  // for others return false
324
235
  public async setKeys(
@@ -352,29 +263,36 @@ export class ContextKeysManager {
352
263
  }
353
264
 
354
265
  try {
266
+ let setItemResult: boolean;
267
+
355
268
  switch (storageLevel) {
356
269
  case StorageLevel.persisted:
357
- return await Promise.all([
358
- this._localStorage.setItem(parsedKey, value, parsedNamespace),
359
- this.setReferenceForKey(parsedKey, parsedNamespace, storageLevel),
360
- ]).then(([setItemResult]) => {
361
- return savingResultIsSuccess(setItemResult);
362
- });
270
+ setItemResult = await this._localStorage.setItem(
271
+ parsedKey,
272
+ value,
273
+ parsedNamespace
274
+ );
275
+
276
+ break;
363
277
  case StorageLevel.secure:
364
- return await Promise.all([
365
- this._secureStorage.setItem(parsedKey, value, parsedNamespace),
366
- this.setReferenceForKey(parsedKey, parsedNamespace, storageLevel),
367
- ]).then(([setItemResult]) => {
368
- return savingResultIsSuccess(setItemResult);
369
- });
278
+ setItemResult = await this._secureStorage.setItem(
279
+ parsedKey,
280
+ value,
281
+ parsedNamespace
282
+ );
283
+
284
+ break;
370
285
  default:
371
- return await Promise.all([
372
- this._sessionStorage.setItem(parsedKey, value, parsedNamespace),
373
- this.setReferenceForKey(parsedKey, parsedNamespace, storageLevel),
374
- ]).then(([setItemResult]) => {
375
- return savingResultIsSuccess(setItemResult);
376
- });
286
+ setItemResult = await this._sessionStorage.setItem(
287
+ parsedKey,
288
+ value,
289
+ parsedNamespace
290
+ );
291
+
292
+ break;
377
293
  }
294
+
295
+ return savingResultIsSuccess(setItemResult);
378
296
  } catch (error) {
379
297
  // something went wrong
380
298
  this._logger.warn({
@@ -388,24 +306,6 @@ export class ContextKeysManager {
388
306
  // SET
389
307
 
390
308
  // REMOVE
391
- public async removeReferenceForKey(
392
- key: string,
393
- namespace: string
394
- ): Promise<boolean> {
395
- return this._referenceStorage
396
- .removeItem(buildNamespaceKey(key, namespace), REFERENCE_NAMESPACE)
397
- .then(() => true)
398
- .catch((error) => {
399
- // something went wrong
400
- this._logger.warn({
401
- message: error.message,
402
- data: { key, namespace },
403
- });
404
-
405
- return false;
406
- });
407
- }
408
-
409
309
  public async removeKey(key: KeyName): Promise<boolean> {
410
310
  const {
411
311
  isValid: isValidKey,
@@ -422,7 +322,6 @@ export class ContextKeysManager {
422
322
  this._sessionStorage.removeItem(parsedKey, parsedNamespace),
423
323
  this._localStorage.removeItem(parsedKey, parsedNamespace),
424
324
  this._secureStorage.removeItem(parsedKey, parsedNamespace),
425
- this.removeReferenceForKey(parsedKey, parsedNamespace),
426
325
  ])
427
326
  .then(() => true)
428
327
  .catch((error) => {
@@ -484,7 +484,7 @@ const MOBILE_ACTION_BUTTON_FIELDS = [
484
484
  { text: "share", value: "native_share_action" },
485
485
  { text: "downloads", value: "offline-content-button" },
486
486
  { text: "trailer", value: "trailer_action" },
487
- { text: "mute/unmute", value: "mute_unmute" },
487
+ { text: "mute/unmute", value: "mute_unmute_action" },
488
488
  ],
489
489
  },
490
490
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "16.0.0-rc.122",
3
+ "version": "16.0.0-rc.124",
4
4
  "description": "Applicaster Zapp React Native utilities package",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "homepage": "https://github.com/applicaster/quickbrick#readme",
29
29
  "dependencies": {
30
- "@applicaster/applicaster-types": "16.0.0-rc.122",
30
+ "@applicaster/applicaster-types": "16.0.0-rc.124",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",
@@ -36,6 +36,7 @@
36
36
  "prop-types": "^15.0.0"
37
37
  },
38
38
  "peerDependencies": {
39
+ "@applicaster/quick-brick-assets": "*",
39
40
  "@applicaster/zapp-pipes-v2-client": "*",
40
41
  "@react-native-community/netinfo": "*",
41
42
  "react": "*",
@@ -0,0 +1,18 @@
1
+ // The Jest stand-in for @applicaster/quick-brick-assets maps each existing key to itself
2
+ import { getQuickBrickAsset } from "..";
3
+
4
+ describe("getQuickBrickAsset", () => {
5
+ it("returns the asset for a plugin identifier and path", () => {
6
+ expect(
7
+ getQuickBrickAsset("mute_unmute_action", "action/flavour_1/mute.png")
8
+ ).toBe("mute_unmute_action/action/flavour_1/mute.png");
9
+ });
10
+
11
+ it("rejects unknown paths and plugin identifiers at compile time", () => {
12
+ // @ts-expect-error - no such file in mute_unmute_action
13
+ getQuickBrickAsset("mute_unmute_action", "action/flavour_9/mute.png");
14
+
15
+ // @ts-expect-error - no assets folder for this plugin
16
+ getQuickBrickAsset("unknown_plugin", "action/flavour_1/mute.png");
17
+ });
18
+ });
@@ -0,0 +1,35 @@
1
+ import assets from "@applicaster/quick-brick-assets";
2
+ import type {
3
+ QuickBrickAssetKey,
4
+ QuickBrickAssetSource,
5
+ } from "@applicaster/quick-brick-assets";
6
+
7
+ export type { QuickBrickAssetKey, QuickBrickAssetSource };
8
+
9
+ type PluginOf<K> = K extends `${infer Plugin}/${string}` ? Plugin : never;
10
+
11
+ type PathOf<K, Plugin extends string> = K extends `${Plugin}/${infer Path}`
12
+ ? Path
13
+ : never;
14
+
15
+ /** Plugin identifiers that have a folder in `@applicaster/quick-brick-assets`. */
16
+ export type QuickBrickAssetPlugin = PluginOf<QuickBrickAssetKey>;
17
+
18
+ /** Asset paths inside the given plugin's folder. */
19
+ export type QuickBrickAssetPath<Plugin extends QuickBrickAssetPlugin> = PathOf<
20
+ QuickBrickAssetKey,
21
+ Plugin
22
+ >;
23
+
24
+ /**
25
+ * Looks up an asset of `@applicaster/quick-brick-assets` by plugin identifier
26
+ * and path relative to the plugin folder, e.g.
27
+ * `getQuickBrickAsset("mute_unmute_action", "action/flavour_1/mute.png")`.
28
+ * Both arguments are checked against the package's files at compile time.
29
+ * Density variants (`@2x` / `@3x`) are not supported.
30
+ */
31
+ export const getQuickBrickAsset = <Plugin extends QuickBrickAssetPlugin>(
32
+ pluginIdentifier: Plugin,
33
+ assetPath: QuickBrickAssetPath<Plugin>
34
+ ): QuickBrickAssetSource =>
35
+ assets[`${pluginIdentifier}/${assetPath}` as QuickBrickAssetKey];
@@ -21,17 +21,45 @@ type ActionContexts<T = unknown> = {
21
21
  };
22
22
 
23
23
  /**
24
- * React Hook to subscribe to the custom action context
24
+ * A value that changes whenever a provider is added to or removed from
25
+ * `uiActionsRegistry`.
26
+ *
27
+ * Providers can register after the UI that wants them has rendered - the audio
28
+ * player registers playback speed only once its controller has state, and
29
+ * plugin-backed actions are bridged in from an effect. Put this in the
30
+ * dependency list of anything that resolves actions, so the list is re-read
31
+ * instead of being frozen at first paint.
32
+ */
33
+ export function useRegistryRevision(): number {
34
+ const [revision, setRevision] = useState(0);
35
+
36
+ useEffect(
37
+ () =>
38
+ uiActionsRegistry.subscribe(() => setRevision((current) => current + 1)),
39
+ []
40
+ );
41
+
42
+ return revision;
43
+ }
44
+
45
+ /**
46
+ * Resolves a legacy action context or falls back to the UI action registry.
25
47
  * @param {*} plugId - subscribes to the context of that action plugin
26
48
  * @return {*} - global action provider context or context for specific action plugin
27
49
  */
28
50
  export function useActions<T = unknown>(plugId: string): any {
29
51
  const [logged, setLogged] = useState(false);
30
52
  const context = useContext<ActionContexts<T>>(ActionsContext);
53
+ useRegistryRevision();
54
+
31
55
  const actionContext = context?.actions?.[plugId]?.module?.context;
32
56
 
33
57
  if (plugId) {
34
58
  if (!actionContext) {
59
+ const registeredAction = uiActionsRegistry.getAction(plugId);
60
+
61
+ if (registeredAction) return registeredAction;
62
+
35
63
  if (!logged) {
36
64
  logger.warning({
37
65
  message: `useActions: Couldn't find an action for ${plugId} plugin`,
@@ -139,25 +167,3 @@ export function useEntryActionState(
139
167
 
140
168
  return { state, invokeAction };
141
169
  }
142
-
143
- /**
144
- * A value that changes whenever a provider is added to or removed from
145
- * `uiActionsRegistry`.
146
- *
147
- * Providers can register after the UI that wants them has rendered - the audio
148
- * player registers playback speed only once its controller has state, and
149
- * plugin-backed actions are bridged in from an effect. Put this in the
150
- * dependency list of anything that resolves actions, so the list is re-read
151
- * instead of being frozen at first paint.
152
- */
153
- export function useRegistryRevision(): number {
154
- const [revision, setRevision] = useState(0);
155
-
156
- useEffect(
157
- () =>
158
- uiActionsRegistry.subscribe(() => setRevision((current) => current + 1)),
159
- []
160
- );
161
-
162
- return revision;
163
- }
@@ -1,56 +0,0 @@
1
- import { ContextKeysManager } from "../../..";
2
-
3
- describe("Context Keys Manager - getKey", () => {
4
- it("returns value if found by reference", async () => {
5
- // setup
6
- const key = "namespace.key";
7
- const value = "value";
8
-
9
- const mockedLogger = {
10
- warn: jest.fn(),
11
- };
12
-
13
- const mockedSessionStorage = {
14
- getItem: jest.fn(),
15
- setItem: jest.fn(),
16
- removeItem: jest.fn(),
17
- };
18
-
19
- const mockedLocalStorage = {
20
- getItem: jest.fn(),
21
- setItem: jest.fn(),
22
- removeItem: jest.fn(),
23
- };
24
-
25
- const mockedSecureStorage = {
26
- getItem: jest.fn(),
27
- setItem: jest.fn(),
28
- removeItem: jest.fn(),
29
- };
30
-
31
- const contextManager = new ContextKeysManager({
32
- logger: mockedLogger,
33
- sessionStorage: mockedSessionStorage,
34
- localStorage: mockedLocalStorage,
35
- secureStorage: mockedSecureStorage,
36
- });
37
-
38
- const getKeyByReference = jest
39
- .spyOn(contextManager, "getKeyByReference")
40
- .mockResolvedValueOnce(value);
41
-
42
- // run
43
- const result = await contextManager.getKey(key);
44
-
45
- // verify
46
- expect(result).toEqual(value);
47
-
48
- expect(mockedLogger.warn).not.toHaveBeenCalled();
49
-
50
- expect(getKeyByReference).toHaveBeenCalledWith("key", "namespace");
51
-
52
- expect(mockedSessionStorage.getItem).not.toHaveBeenCalled();
53
- expect(mockedLocalStorage.getItem).not.toHaveBeenCalled();
54
- expect(mockedSecureStorage.getItem).not.toHaveBeenCalled();
55
- });
56
- });