@applicaster/zapp-react-native-bridge 13.0.0-rc.99 → 14.0.0-alpha.1054425138

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.
@@ -0,0 +1,92 @@
1
+ import { BehaviorSubject } from "rxjs";
2
+ import { log_debug, SingleValueProvider } from "./StorageSingleSelectProvider";
3
+
4
+ export const screenStates: Record<string, Record<string, string>> = {};
5
+
6
+ export class ScreenSingleValueProvider implements SingleValueProvider {
7
+ private static providers: Record<
8
+ string,
9
+ Record<string, SingleValueProvider>
10
+ > = {};
11
+
12
+ // TODO: Screen State should be stored outside of this class and should be shared with screen multi-select provider
13
+ public static getState(screenRoute: string): Record<string, string> {
14
+ return screenStates[screenRoute] || {};
15
+ }
16
+
17
+ public static getProvider(
18
+ key: string,
19
+ screenRoute: string
20
+ ): SingleValueProvider {
21
+ if (!key) {
22
+ throw new Error("ScreenSingleValueProvider: Key is required");
23
+ }
24
+
25
+ if (!screenRoute) {
26
+ throw new Error("ScreenSingleValueProvider: Screen route is required");
27
+ }
28
+
29
+ if (!this.providers[screenRoute]) {
30
+ this.providers[screenRoute] = {};
31
+ }
32
+
33
+ if (!this.providers[screenRoute][key]) {
34
+ this.providers[screenRoute][key] = new ScreenSingleValueProvider(
35
+ key,
36
+ screenRoute
37
+ );
38
+ }
39
+
40
+ if (!screenStates[screenRoute]) {
41
+ screenStates[screenRoute] = {};
42
+ }
43
+
44
+ return this.providers[screenRoute][key];
45
+ }
46
+
47
+ // @ts-ignore
48
+ private valueSubject: BehaviorSubject<string | null>;
49
+ p;
50
+ // @ts-ignore
51
+ private constructor(
52
+ private key: string,
53
+ private route: string
54
+ ) {
55
+ if (!key) {
56
+ throw new Error("ScreenSingleValueProvider: Key is required");
57
+ }
58
+
59
+ // localStorage.addListener?.({ key, namespace }, this.reloadValue);
60
+
61
+ this.valueSubject = new BehaviorSubject<string | null>(null);
62
+
63
+ void this.getValueAsync();
64
+ log_debug("ScreenSingleValueProvider: Initializing");
65
+ }
66
+
67
+ clearValue(): Promise<void> {
68
+ delete [this.route][this.key];
69
+ this.valueSubject.next(null);
70
+
71
+ return Promise.resolve();
72
+ }
73
+
74
+ getObservable(): BehaviorSubject<string | null> {
75
+ return this.valueSubject;
76
+ }
77
+
78
+ getValue(): string | null {
79
+ return this.valueSubject.value;
80
+ }
81
+
82
+ getValueAsync(): Promise<string | null> {
83
+ return Promise.resolve(this.valueSubject.value);
84
+ }
85
+
86
+ setValue(value: string): Promise<void> {
87
+ screenStates[this.route][this.key] = value;
88
+ this.valueSubject.next(value);
89
+
90
+ return Promise.resolve();
91
+ }
92
+ }
@@ -0,0 +1,119 @@
1
+ import { log_debug, MultiSelectProvider } from "./StorageMultiSelectProvider";
2
+ import { BehaviorSubject } from "rxjs";
3
+ import { screenStates } from "./ScreenSingleValueProvider";
4
+
5
+ export class ScreenMultiSelectProvider implements MultiSelectProvider {
6
+ // @ts-ignore
7
+ private itemSubject: BehaviorSubject<string[] | null>;
8
+
9
+ private static providers: Record<
10
+ string,
11
+ Record<string, MultiSelectProvider>
12
+ > = {};
13
+
14
+ public static getProvider(
15
+ key: string,
16
+ screenRoute: string
17
+ ): MultiSelectProvider {
18
+ if (!key) {
19
+ throw new Error("ScreenSingleValueProvider: Key is required");
20
+ }
21
+
22
+ if (!screenRoute) {
23
+ throw new Error("ScreenSingleValueProvider: Screen route is required");
24
+ }
25
+
26
+ if (!this.providers[screenRoute]) {
27
+ this.providers[screenRoute] = {};
28
+ }
29
+
30
+ if (!this.providers[screenRoute][key]) {
31
+ this.providers[screenRoute][key] = new ScreenMultiSelectProvider(
32
+ key,
33
+ screenRoute
34
+ );
35
+ }
36
+
37
+ if (!screenStates[screenRoute]) {
38
+ screenStates[screenRoute] = {};
39
+ }
40
+
41
+ return this.providers[screenRoute][key];
42
+ }
43
+
44
+ // @ts-ignore
45
+ private constructor(
46
+ private key: string,
47
+ private route: string
48
+ ) {
49
+ if (!key) {
50
+ throw new Error("StorageMultiSelectProvider: Key is required");
51
+ }
52
+
53
+ this.key = key;
54
+ // this.storage.addListener?.({ key, namespace }, this.reloadItems);
55
+
56
+ this.itemSubject = new BehaviorSubject([]);
57
+
58
+ void this.getSelectedAsync();
59
+ log_debug("StorageMultiSelectProvider: Initializing");
60
+ }
61
+
62
+ private getSetOfCurrentItems() {
63
+ const currentResult: string = screenStates[this.route][this.key];
64
+
65
+ return new Set(currentResult ? currentResult.split(",") : []);
66
+ }
67
+
68
+ addItem(item: string): Promise<void> {
69
+ const items = this.getSetOfCurrentItems();
70
+ items.add(item);
71
+ screenStates[this.route][this.key] = Array.from(items).join(",");
72
+ this.itemSubject.next(Array.from(items) || []);
73
+
74
+ log_debug(
75
+ `addItem: item added: ${item}, current items: ${Array.from(items)}`
76
+ );
77
+
78
+ return Promise.resolve();
79
+ }
80
+
81
+ addItems(_items: string[]): Promise<void> {
82
+ return Promise.resolve();
83
+ }
84
+
85
+ public getObservable = (): BehaviorSubject<string[]> => this.itemSubject;
86
+
87
+ getSelectedAsync(): Promise<string[]> {
88
+ const values = screenStates[this.route][this.key]?.split(",");
89
+ this.itemSubject.next(values || []);
90
+
91
+ return Promise.resolve(values || []);
92
+ }
93
+
94
+ getSelectedItems(): string[] {
95
+ return this.itemSubject.value;
96
+ }
97
+
98
+ removeAllItems(): Promise<void> {
99
+ return Promise.resolve();
100
+ }
101
+
102
+ removeItem(item: string): Promise<void> {
103
+ const items = this.getSetOfCurrentItems();
104
+ items.delete(item);
105
+
106
+ screenStates[this.route][this.key] = Array.from(items).join(",");
107
+ this.itemSubject.next(Array.from(items) || []);
108
+
109
+ return Promise.resolve();
110
+ }
111
+
112
+ removeItems(_items: string[]): Promise<void> {
113
+ return Promise.resolve();
114
+ }
115
+
116
+ setSelectedItems(_items: string[]): Promise<void> {
117
+ return Promise.resolve();
118
+ }
119
+ }
@@ -1,8 +1,11 @@
1
1
  import { createLogger } from "@applicaster/zapp-react-native-utils/logger";
2
2
  import { bridgeLogger } from "../logger";
3
3
  import { BehaviorSubject } from "rxjs";
4
+ import { Storage } from "@applicaster/zapp-react-native-bridge/ZappStorage/Storage";
4
5
  import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
6
+ import { sessionStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/SessionStorage";
5
7
  import { getNamespaceAndKey } from "@applicaster/zapp-react-native-utils/appUtils/contextKeysManager/utils";
8
+ import { StorageType } from "@applicaster/zapp-react-native-utils/appUtils/contextKeysManager/consts";
6
9
 
7
10
  export const { log_verbose, log_debug, log_warning, log_info, log_error } =
8
11
  createLogger({
@@ -34,18 +37,28 @@ export interface MultiSelectProvider {
34
37
  export class StorageMultiSelectProvider implements MultiSelectProvider {
35
38
  // TODO: Unsubscribe and remove when there is no listeners
36
39
  private static multiSelectProviders: Record<
37
- string,
38
- StorageMultiSelectProvider
39
- > = {};
40
-
41
- public static getProvider(keyNamespace: string): StorageMultiSelectProvider {
42
- if (!this.multiSelectProviders[keyNamespace]) {
43
- this.multiSelectProviders[keyNamespace] = new StorageMultiSelectProvider(
44
- keyNamespace
45
- );
40
+ StorageType,
41
+ Record<string, StorageMultiSelectProvider>
42
+ > = {
43
+ [StorageType.local]: {},
44
+ [StorageType.session]: {},
45
+ // TODO: not implemented yet
46
+ [StorageType.secure]: {},
47
+ };
48
+
49
+ public static getProvider(
50
+ keyNamespace: string,
51
+ storage: StorageType = StorageType.local
52
+ ): MultiSelectProvider {
53
+ if (!this.multiSelectProviders[storage][keyNamespace]) {
54
+ const storageImpl =
55
+ storage === StorageType.session ? sessionStorage : localStorage;
56
+
57
+ this.multiSelectProviders[storage][keyNamespace] =
58
+ new StorageMultiSelectProvider(keyNamespace, storageImpl);
46
59
  }
47
60
 
48
- return this.multiSelectProviders[keyNamespace];
61
+ return this.multiSelectProviders[storage][keyNamespace];
49
62
  }
50
63
 
51
64
  private itemSubject: BehaviorSubject<string[] | null>;
@@ -53,7 +66,10 @@ export class StorageMultiSelectProvider implements MultiSelectProvider {
53
66
  private readonly key: string;
54
67
  private readonly namespace: string;
55
68
 
56
- private constructor(keyNamespace: string) {
69
+ private constructor(
70
+ keyNamespace: string,
71
+ private storage: Storage
72
+ ) {
57
73
  const { namespace, key } = getNamespaceAndKey(keyNamespace);
58
74
 
59
75
  if (!key) {
@@ -62,7 +78,7 @@ export class StorageMultiSelectProvider implements MultiSelectProvider {
62
78
 
63
79
  this.key = key;
64
80
  this.namespace = namespace;
65
- localStorage.addListener?.({ key, namespace }, this.reloadItems);
81
+ this.storage.addListener?.({ key, namespace }, this.reloadItems);
66
82
 
67
83
  this.itemSubject = new BehaviorSubject([]);
68
84
 
@@ -84,7 +100,7 @@ export class StorageMultiSelectProvider implements MultiSelectProvider {
84
100
  public getObservable = (): BehaviorSubject<string[]> => this.itemSubject;
85
101
 
86
102
  private async getSetOfCurrentItems() {
87
- const currentResult: string = await localStorage.getItem(
103
+ const currentResult: string = await this.storage.getItem(
88
104
  this.key,
89
105
  this.namespace
90
106
  );
@@ -95,7 +111,7 @@ export class StorageMultiSelectProvider implements MultiSelectProvider {
95
111
  private async updateItemsAndNotifyObservers(currentItems: Set<string>) {
96
112
  const arrayOfItems = Array.from(currentItems);
97
113
  const newValue = arrayOfItems.join(",");
98
- await localStorage.setItem(this.key, newValue, this.namespace);
114
+ await this.storage.setItem(this.key, newValue, this.namespace);
99
115
 
100
116
  this.itemSubject.next(arrayOfItems);
101
117
  }
@@ -150,14 +166,14 @@ export class StorageMultiSelectProvider implements MultiSelectProvider {
150
166
  };
151
167
 
152
168
  public removeAllItems = async (): Promise<void> => {
153
- await localStorage.removeItem(this.key, this.namespace);
169
+ await this.storage.removeItem(this.key, this.namespace);
154
170
  log_debug(`removeAllItems: Removing all items, current items: ${[]}`);
155
171
 
156
172
  this.itemSubject.next([]);
157
173
  };
158
174
 
159
175
  public getSelectedAsync = async (): Promise<string[]> => {
160
- const value = await localStorage.getItem(this.key, this.namespace);
176
+ const value = await this.storage.getItem(this.key, this.namespace);
161
177
  const selectedItems = value ? value.split(",") : [];
162
178
  this.itemSubject.next(selectedItems);
163
179
 
@@ -31,7 +31,7 @@ export class StorageSingleValueProvider implements SingleValueProvider {
31
31
  StorageSingleValueProvider
32
32
  > = {};
33
33
 
34
- public static getProvider(keyNamespace: string): StorageSingleValueProvider {
34
+ public static getProvider(keyNamespace: string): SingleValueProvider {
35
35
  if (!this.singleValueProviders[keyNamespace]) {
36
36
  this.singleValueProviders[keyNamespace] = new StorageSingleValueProvider(
37
37
  keyNamespace
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-bridge",
3
- "version": "13.0.0-rc.99",
3
+ "version": "14.0.0-alpha.1054425138",
4
4
  "description": "Applicaster Zapp React Native modules",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -32,7 +32,7 @@
32
32
  "homepage": "https://github.com/applicaster/quickbrick#readme",
33
33
  "dependencies": {
34
34
  "@applicaster/zapp-pipes-dev-kit": "1.4.7",
35
- "@applicaster/zapp-react-native-utils": "13.0.0-rc.99",
35
+ "@applicaster/zapp-react-native-utils": "14.0.0-alpha.1054425138",
36
36
  "md5": "^2.3.0",
37
37
  "uuid": "^3.3.2"
38
38
  },