@applicaster/zapp-react-native-utils 14.0.0-rc.51 → 14.0.0-rc.53

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,108 @@
1
+ import { bridgeLogger } from "../../zapp-react-native-bridge/logger";
2
+ import { BehaviorSubject } from "rxjs";
3
+ import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
4
+ import { getNamespaceAndKey } from "../appUtils/contextKeysManager/utils";
5
+ import { createLogger } from "../logger";
6
+
7
+ export const { log_verbose, log_debug, log_warning, log_info, log_error } =
8
+ createLogger({
9
+ category: "StorageSingleValueProvider",
10
+ subsystem: "zapp-react-native-bridge",
11
+ parent: bridgeLogger,
12
+ });
13
+
14
+ export interface SingleValueProvider {
15
+ getObservable(): BehaviorSubject<string | null>;
16
+
17
+ setValue(value: string): Promise<void>;
18
+ getValue(): string | null;
19
+ getValueAsync(): Promise<string | null>;
20
+ clearValue(): Promise<void>;
21
+ }
22
+
23
+ interface StorageListenerArgs {
24
+ value: string | null;
25
+ }
26
+
27
+ export class StorageSingleValueProvider implements SingleValueProvider {
28
+ // Static cache of providers by namespace
29
+ private static singleValueProviders: Record<
30
+ string,
31
+ StorageSingleValueProvider
32
+ > = {};
33
+
34
+ public static getProvider(keyNamespace: string): SingleValueProvider {
35
+ if (!this.singleValueProviders[keyNamespace]) {
36
+ this.singleValueProviders[keyNamespace] = new StorageSingleValueProvider(
37
+ keyNamespace
38
+ );
39
+ }
40
+
41
+ return this.singleValueProviders[keyNamespace];
42
+ }
43
+
44
+ private valueSubject: BehaviorSubject<string | null>;
45
+
46
+ private readonly key: string;
47
+ private readonly namespace: string;
48
+
49
+ private constructor(keyNamespace: string) {
50
+ const { namespace, key } = getNamespaceAndKey(keyNamespace);
51
+
52
+ if (!key) {
53
+ throw new Error("StorageSingleValueProvider: Key is required");
54
+ }
55
+
56
+ this.key = key;
57
+ this.namespace = namespace;
58
+ localStorage.addListener?.({ key, namespace }, this.reloadValue);
59
+
60
+ this.valueSubject = new BehaviorSubject<string | null>(null);
61
+
62
+ void this.getValueAsync();
63
+ log_debug("StorageSingleValueProvider: Initializing");
64
+ }
65
+
66
+ private reloadValue = async ({ value }: StorageListenerArgs) => {
67
+ log_debug(`reloadValue: request to reload value: ${value}`);
68
+
69
+ this.valueSubject.next(value);
70
+ };
71
+
72
+ public getObservable = (): BehaviorSubject<string | null> =>
73
+ this.valueSubject;
74
+
75
+ private async updateValueAndNotifyObservers(value: string | null) {
76
+ if (value === null) {
77
+ await localStorage.removeItem(this.key, this.namespace);
78
+ } else {
79
+ await localStorage.setItem(this.key, value, this.namespace);
80
+ }
81
+
82
+ this.valueSubject.next(value);
83
+ }
84
+
85
+ public setValue = async (value: string): Promise<void> => {
86
+ log_debug(`setValue: Setting new value: ${value}`);
87
+
88
+ await this.updateValueAndNotifyObservers(value);
89
+ };
90
+
91
+ public clearValue = async (): Promise<void> => {
92
+ await localStorage.removeItem(this.key, this.namespace);
93
+ log_debug("clearValue: Removing value");
94
+
95
+ this.valueSubject.next(null);
96
+ };
97
+
98
+ public getValueAsync = async (): Promise<string | null> => {
99
+ const value = await localStorage.getItem(this.key, this.namespace);
100
+ this.valueSubject.next(value);
101
+
102
+ return value;
103
+ };
104
+
105
+ public getValue = (): string | null => {
106
+ return this.valueSubject.getValue();
107
+ };
108
+ }