placementt-core 1.20.232 → 1.300.4

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 (101) hide show
  1. package/.eslintrc.js +40 -40
  2. package/.gitattributes +2 -2
  3. package/deploy-all.js +75 -0
  4. package/lib/config.d.ts +0 -13
  5. package/lib/config.js +1 -3
  6. package/lib/config.js.map +1 -1
  7. package/lib/constants.d.ts +1 -0
  8. package/lib/constants.js +48 -1
  9. package/lib/constants.js.map +1 -1
  10. package/lib/features/config.d.ts +133 -133
  11. package/lib/features/config.js +35 -35
  12. package/lib/features/contacts/contacts.d.ts +75 -75
  13. package/lib/features/contacts/contacts.js +105 -105
  14. package/lib/features/downtime/useDowntime.d.ts +11 -11
  15. package/lib/features/downtime/useDowntime.js +22 -22
  16. package/lib/features/placements/studentPlacements/activePlacement.js +1 -1
  17. package/lib/features/placements/studentPlacements/activePlacement.js.map +1 -1
  18. package/lib/features/placements/studentPlacements/studentPlacementsSlice.d.ts +63 -63
  19. package/lib/features/placements/studentPlacements/studentPlacementsSlice.js +81 -81
  20. package/lib/features/providerPlacements/providerPlacementsSlice.d.ts +19 -19
  21. package/lib/features/providerPlacements/providerPlacementsSlice.js +24 -24
  22. package/lib/features/studentPlacements/studentPlacementsSlice.d.ts +62 -62
  23. package/lib/features/studentPlacements/studentPlacementsSlice.js +87 -87
  24. package/lib/features/studentPlacements/useStudentPlacements.d.ts +6 -6
  25. package/lib/features/studentPlacements/useStudentPlacements.js +18 -18
  26. package/lib/features/userSlice.d.ts +26 -26
  27. package/lib/features/userSlice.js +23 -23
  28. package/lib/features/users/useUserFunctions.d.ts +25 -25
  29. package/lib/features/users/useUserFunctions.js +124 -124
  30. package/lib/features/users/userSlice.d.ts +46 -46
  31. package/lib/features/users/userSlice.js +48 -48
  32. package/lib/firebase/firebase.d.ts +1 -1
  33. package/lib/firebase/firebase.js +8 -3
  34. package/lib/firebase/firebase.js.map +1 -1
  35. package/lib/firebase/firebaseConfig.js +1 -0
  36. package/lib/firebase/firebaseConfig.js.map +1 -1
  37. package/lib/firebase/readDatabase.js +7 -7
  38. package/lib/firebase/readDatabase.js.map +1 -1
  39. package/lib/hooks.d.ts +85 -192
  40. package/lib/hooks.js +731 -671
  41. package/lib/hooks.js.map +1 -1
  42. package/lib/images/GatsbyBenchmarks.d.ts +1 -2
  43. package/lib/index.d.ts +0 -1
  44. package/lib/index.js +0 -1
  45. package/lib/index.js.map +1 -1
  46. package/lib/reduxHooks.d.ts +9 -41
  47. package/lib/reduxHooks.js +1 -49
  48. package/lib/reduxHooks.js.map +1 -1
  49. package/lib/tasksAndTips.js +368 -418
  50. package/lib/tasksAndTips.js.map +1 -1
  51. package/lib/typeDefinitions.d.ts +162 -83
  52. package/lib/util.d.ts +25 -1
  53. package/lib/util.js +329 -2
  54. package/lib/util.js.map +1 -1
  55. package/package.json +56 -56
  56. package/src/DatabaseDefinitions.ts +18 -18
  57. package/src/apiCalls.ts +128 -128
  58. package/src/config.ts +50 -53
  59. package/src/constants.ts +846 -796
  60. package/src/databaseTypes.ts +42 -42
  61. package/src/features/analytics/useAnalytics.tsx +63 -63
  62. package/src/features/contacts/contactsSlice.ts +147 -147
  63. package/src/features/contacts/useContacts.tsx +73 -73
  64. package/src/features/dropdown/useDropdown.tsx +52 -52
  65. package/src/features/global/downtime/useDowntime.tsx +23 -23
  66. package/src/features/global/users/useUserFunctions.tsx +132 -132
  67. package/src/features/jobs/jobsSlice.ts +71 -71
  68. package/src/features/placements/studentPlacements/activePlacement.ts +68 -68
  69. package/src/features/placements/studentPlacements/completedStudentPlacementsSlice.ts +97 -97
  70. package/src/features/placements/studentPlacements/upcomingStudentPlacementsSlice.ts +108 -108
  71. package/src/features/placements/studentPlacements/useStudentPlacements.tsx +9 -9
  72. package/src/features/placements/types.ts +10 -10
  73. package/src/features/referrals/useReferrals.tsx +56 -56
  74. package/src/features/updates/useUpdates.tsx +38 -38
  75. package/src/firebase/firebase.tsx +158 -153
  76. package/src/firebase/firebaseConfig.tsx +47 -45
  77. package/src/firebase/firebaseQuery.tsx +151 -151
  78. package/src/firebase/persistence.ts +84 -84
  79. package/src/firebase/readDatabase.tsx +236 -236
  80. package/src/firebase/util.tsx +352 -352
  81. package/src/firebase/writeDatabase.tsx +77 -77
  82. package/src/hooks.tsx +4476 -4463
  83. package/src/images/GatsbyBenchmarks.tsx +711 -711
  84. package/src/images/LogFuturePlacement.jsx +64 -64
  85. package/src/images/LogPreviousPlacement.jsx +228 -228
  86. package/src/images/gatsby_benchmarks.svg +466 -466
  87. package/src/images/log_future_placement.svg +114 -114
  88. package/src/images/log_previous_placement.svg +199 -199
  89. package/src/index.ts +34 -35
  90. package/src/readDatabase.tsx +3 -3
  91. package/src/reduxHooks.ts +295 -352
  92. package/src/tasksAndTips.ts +1267 -1267
  93. package/src/tutorialTips.ts +58 -58
  94. package/src/typeDefinitions.ts +1260 -1129
  95. package/src/util.ts +523 -177
  96. package/tsconfig.dev.json +5 -5
  97. package/tsconfig.json +21 -21
  98. package/lib/features/logs/logsSlice.d.ts +0 -93
  99. package/lib/features/logs/logsSlice.js +0 -146
  100. package/lib/features/logs/logsSlice.js.map +0 -1
  101. package/src/features/logs/logsSlice.ts +0 -208
@@ -1,151 +1,151 @@
1
- import {addDoc, collection, CollectionReference, deleteDoc, doc,
2
- DocumentData, DocumentReference, FieldValue, getCountFromServer, getDoc,
3
- getDocs, onSnapshot, Query, query, QueryConstraint, QueryDocumentSnapshot, setDoc, updateDoc, where} from "firebase/firestore";
4
- import {SetStateAction} from "react";
5
- import {db} from "./firebaseConfig";
6
- import {pathToArr} from "./util";
7
- import {Products} from "../typeDefinitions";
8
- import { AppDispatch } from "../config";
9
-
10
- export default class FirebaseQuery {
11
- private createQuery(queryType:"collection"|"doc", path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
12
- switch (queryType) {
13
- case "collection":
14
- return query(collection(db, ...(pathToArr(path) as [any])), ...[...(constraints ? Array.isArray(constraints) ? constraints : [constraints] : [])]);
15
- case "doc":
16
- return doc(db, ...(pathToArr(path) as [any]));
17
- }
18
- }
19
-
20
- async getCount(path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint):Promise<number> {
21
- console.log("GET COUNT", path, constraints);
22
- const countObject = await getCountFromServer(this.createQuery("collection", path, constraints) as Query).catch((e) => console.log(e));
23
- if (!countObject) return 0;
24
- return (countObject).data().count;
25
- }
26
-
27
- async add(path:string[]|string, data:{[key:string]:unknown}) {
28
- if (path.length % 2 === 0) {
29
- throw new Error("Provide a collection reference");
30
- }
31
- return await addDoc(this.createQuery("collection", path) as CollectionReference, data);
32
- }
33
-
34
- async set(path:string[]|string, data:{[key:string]:unknown}, merge=false) {
35
- if (path.length % 2 === 1) {
36
- throw new Error("Provide a document reference");
37
- }
38
- return await setDoc(this.createQuery("doc", path) as DocumentReference, data, {merge: merge});
39
- }
40
-
41
- async update(path:string[]|string, data:{ [key: string]: unknown}) {
42
- if (path.length % 2 === 1) {
43
- throw new Error("Provide a document reference");
44
- }
45
- console.log("update", path, data);
46
- return await updateDoc(this.createQuery("doc", path) as DocumentReference, data as { [key: string]: FieldValue | Partial<unknown> | undefined});
47
- }
48
-
49
- async delete(path:string[]|string) {
50
- if (path.length % 2 === 1) {
51
- throw new Error("Provide a document reference");
52
- }
53
- await deleteDoc(this.createQuery("doc", path) as DocumentReference);
54
- }
55
-
56
- async copy(path: string[]|string, id: string, oId: string, product: Products, additionalData?: {[key: string]: unknown}) {
57
- const existing = await this.getDocData([...path, id]);
58
- console.log("existing", existing);
59
- existing.name = await this.getUniqueDocName(path, existing.name, oId, product);
60
- if (existing.default) {
61
- existing.default = false;
62
- }
63
- return await this.add(path, {...existing, ...additionalData});
64
- }
65
-
66
- async getUniqueDocName(path: string[]|string, name: string, oId: string, product: Products): Promise<string> {
67
- const q = this.createQuery("collection", path, [where("name", "==", name), where("oId", "==", oId), where("product", "==", product)]);
68
-
69
- const querySnapshot = await getDocs(q as Query);
70
- if (!querySnapshot.empty) {
71
- return this.getUniqueDocName(path, name += "-", oId, product);
72
- }
73
- return name;
74
- }
75
-
76
- async getDocData(path:string[]|string) {
77
- console.log("GET DOC", path);
78
- if (path.length % 2 === 1) {
79
- throw new Error("Provide a document reference");
80
- }
81
- const result = await getDoc(this.createQuery("doc", path) as DocumentReference).catch((error) => {
82
- console.log(`Error in snapshot ${path}`, error);
83
- throw error;
84
- });
85
- if (!result.exists()) {
86
- throw new Error(`Document doesn't exist. ${typeof path === "string" ? path : path.join(", ")}`);
87
- }
88
- const resultData = result.data() || {};
89
- resultData.id = result.id;
90
- return resultData;
91
- }
92
-
93
- async getDocsWhere(path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, rawDocs=false) {
94
- const q = this.createQuery("collection", path, constraints);
95
-
96
- const docs = await getDocs(q as Query).catch((e) => console.log(e, q));
97
- if (rawDocs) return docs;
98
- if (!docs) return {};
99
- console.log("GET DOCS WHERE", docs.size, path, constraints);
100
-
101
- return docs.docs.reduce((acc: {[k: string]: any}, document) => {
102
- const docData = {...document.data(), id: document.id};
103
- acc[document.id] = docData;
104
- return acc;
105
- }, {});
106
- }
107
-
108
- async collectionUpdate(data:{[key:string]:unknown}, path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
109
- if (path.length % 2 === 0) {
110
- throw new Error("Provide a collection reference");
111
- }
112
- const q = this.createQuery("collection", path, constraints) as Query;
113
- (await getDocs(q)).docs.forEach((doc) => {
114
- this.update(doc.ref.path.split("/"), data);
115
- });
116
- }
117
-
118
- documentSnapshot(path: string[]|string, setState: SetStateAction<any>) {
119
- console.log("Building snapshot query: ", path);
120
- const q = this.createQuery("doc", path) as DocumentReference;
121
- console.log("Query created");
122
- return onSnapshot(q, (doc) => {
123
- console.log("Data", doc.data());
124
- setState({...doc.data(), id: doc.id});
125
- }, (error) => {
126
- console.log(`Error in snapshot ${path}`, error);
127
- });
128
- }
129
-
130
- collectionSnapshot(setState:SetStateAction<any>|((e: DocumentData|{[key: string]: any}) => void), path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, dispatch?: AppDispatch, raw?: boolean) {
131
- const q = this.createQuery("collection", path, constraints) as Query<DocumentData>;
132
-
133
- return onSnapshot(q, (querySnapshot: DocumentData) => {
134
- if (raw) {
135
- setState(querySnapshot);
136
- return;
137
- }
138
- const itemList: {[key: string]: any} = {};
139
-
140
- querySnapshot.docs.forEach((doc: QueryDocumentSnapshot) => {
141
- const item = doc.data();
142
- item.id = doc.id;
143
- itemList[doc.id] = item;
144
- });
145
- dispatch ? dispatch(setState(itemList)) : setState(itemList);
146
- }, (error) => {
147
- console.log(`Error in snapshot ${path}`, error);
148
- console.log(constraints)
149
- });
150
- }
151
- }
1
+ import {addDoc, collection, CollectionReference, deleteDoc, doc,
2
+ DocumentData, DocumentReference, FieldValue, getCountFromServer, getDoc,
3
+ getDocs, onSnapshot, Query, query, QueryConstraint, QueryDocumentSnapshot, setDoc, updateDoc, where} from "firebase/firestore";
4
+ import {SetStateAction} from "react";
5
+ import {db} from "./firebaseConfig";
6
+ import {pathToArr} from "./util";
7
+ import {Products} from "../typeDefinitions";
8
+ import { AppDispatch } from "../config";
9
+
10
+ export default class FirebaseQuery {
11
+ private createQuery(queryType:"collection"|"doc", path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
12
+ switch (queryType) {
13
+ case "collection":
14
+ return query(collection(db, ...(pathToArr(path) as [any])), ...[...(constraints ? Array.isArray(constraints) ? constraints : [constraints] : [])]);
15
+ case "doc":
16
+ return doc(db, ...(pathToArr(path) as [any]));
17
+ }
18
+ }
19
+
20
+ async getCount(path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint):Promise<number> {
21
+ console.log("GET COUNT", path, constraints);
22
+ const countObject = await getCountFromServer(this.createQuery("collection", path, constraints) as Query).catch((e) => console.log(e));
23
+ if (!countObject) return 0;
24
+ return (countObject).data().count;
25
+ }
26
+
27
+ async add(path:string[]|string, data:{[key:string]:unknown}) {
28
+ if (path.length % 2 === 0) {
29
+ throw new Error("Provide a collection reference");
30
+ }
31
+ return await addDoc(this.createQuery("collection", path) as CollectionReference, data);
32
+ }
33
+
34
+ async set(path:string[]|string, data:{[key:string]:unknown}, merge=false) {
35
+ if (path.length % 2 === 1) {
36
+ throw new Error("Provide a document reference");
37
+ }
38
+ return await setDoc(this.createQuery("doc", path) as DocumentReference, data, {merge: merge});
39
+ }
40
+
41
+ async update(path:string[]|string, data:{ [key: string]: unknown}) {
42
+ if (path.length % 2 === 1) {
43
+ throw new Error("Provide a document reference");
44
+ }
45
+ console.log("update", path, data);
46
+ return await updateDoc(this.createQuery("doc", path) as DocumentReference, data as { [key: string]: FieldValue | Partial<unknown> | undefined});
47
+ }
48
+
49
+ async delete(path:string[]|string) {
50
+ if (path.length % 2 === 1) {
51
+ throw new Error("Provide a document reference");
52
+ }
53
+ await deleteDoc(this.createQuery("doc", path) as DocumentReference);
54
+ }
55
+
56
+ async copy(path: string[]|string, id: string, oId: string, product: Products, additionalData?: {[key: string]: unknown}) {
57
+ const existing = await this.getDocData([...path, id]);
58
+ console.log("existing", existing);
59
+ existing.name = await this.getUniqueDocName(path, existing.name, oId, product);
60
+ if (existing.default) {
61
+ existing.default = false;
62
+ }
63
+ return await this.add(path, {...existing, ...additionalData});
64
+ }
65
+
66
+ async getUniqueDocName(path: string[]|string, name: string, oId: string, product: Products): Promise<string> {
67
+ const q = this.createQuery("collection", path, [where("name", "==", name), where("oId", "==", oId), where("product", "==", product)]);
68
+
69
+ const querySnapshot = await getDocs(q as Query);
70
+ if (!querySnapshot.empty) {
71
+ return this.getUniqueDocName(path, name += "-", oId, product);
72
+ }
73
+ return name;
74
+ }
75
+
76
+ async getDocData(path:string[]|string) {
77
+ console.log("GET DOC", path);
78
+ if (path.length % 2 === 1) {
79
+ throw new Error("Provide a document reference");
80
+ }
81
+ const result = await getDoc(this.createQuery("doc", path) as DocumentReference).catch((error) => {
82
+ console.log(`Error in snapshot ${path}`, error);
83
+ throw error;
84
+ });
85
+ if (!result.exists()) {
86
+ throw new Error(`Document doesn't exist. ${typeof path === "string" ? path : path.join(", ")}`);
87
+ }
88
+ const resultData = result.data() || {};
89
+ resultData.id = result.id;
90
+ return resultData;
91
+ }
92
+
93
+ async getDocsWhere(path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, rawDocs=false) {
94
+ const q = this.createQuery("collection", path, constraints);
95
+
96
+ const docs = await getDocs(q as Query).catch((e) => console.log(e, q));
97
+ if (rawDocs) return docs;
98
+ if (!docs) return {};
99
+ console.log("GET DOCS WHERE", docs.size, path, constraints);
100
+
101
+ return docs.docs.reduce((acc: {[k: string]: any}, document) => {
102
+ const docData = {...document.data(), id: document.id};
103
+ acc[document.id] = docData;
104
+ return acc;
105
+ }, {});
106
+ }
107
+
108
+ async collectionUpdate(data:{[key:string]:unknown}, path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
109
+ if (path.length % 2 === 0) {
110
+ throw new Error("Provide a collection reference");
111
+ }
112
+ const q = this.createQuery("collection", path, constraints) as Query;
113
+ (await getDocs(q)).docs.forEach((doc) => {
114
+ this.update(doc.ref.path.split("/"), data);
115
+ });
116
+ }
117
+
118
+ documentSnapshot(path: string[]|string, setState: SetStateAction<any>) {
119
+ console.log("Building snapshot query: ", path);
120
+ const q = this.createQuery("doc", path) as DocumentReference;
121
+ console.log("Query created");
122
+ return onSnapshot(q, (doc) => {
123
+ console.log("Data", doc.data());
124
+ setState({...doc.data(), id: doc.id});
125
+ }, (error) => {
126
+ console.log(`Error in snapshot ${path}`, error);
127
+ });
128
+ }
129
+
130
+ collectionSnapshot(setState:SetStateAction<any>|((e: DocumentData|{[key: string]: any}) => void), path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, dispatch?: AppDispatch, raw?: boolean) {
131
+ const q = this.createQuery("collection", path, constraints) as Query<DocumentData>;
132
+
133
+ return onSnapshot(q, (querySnapshot: DocumentData) => {
134
+ if (raw) {
135
+ setState(querySnapshot);
136
+ return;
137
+ }
138
+ const itemList: {[key: string]: any} = {};
139
+
140
+ querySnapshot.docs.forEach((doc: QueryDocumentSnapshot) => {
141
+ const item = doc.data();
142
+ item.id = doc.id;
143
+ itemList[doc.id] = item;
144
+ });
145
+ dispatch ? dispatch(setState(itemList)) : setState(itemList);
146
+ }, (error) => {
147
+ console.log(`Error in snapshot ${path}`, error);
148
+ console.log(constraints)
149
+ });
150
+ }
151
+ }
@@ -1,84 +1,84 @@
1
- import {Persistence, ReactNativeAsyncStorage} from "firebase/auth";
2
-
3
- export const enum PersistenceType {
4
- SESSION = "SESSION",
5
- LOCAL = "LOCAL",
6
- NONE = "NONE"
7
- }
8
-
9
- export type PersistedBlob = Record<string, unknown>;
10
-
11
- export interface Instantiator<T> {
12
- (blob: PersistedBlob): T;
13
- }
14
-
15
- export type PersistenceValue = PersistedBlob | string;
16
-
17
- export const STORAGE_AVAILABLE_KEY = "__sak";
18
-
19
- export interface StorageEventListener {
20
- (value: PersistenceValue | null): void;
21
- }
22
-
23
- export interface PersistenceInternal extends Persistence {
24
- type: PersistenceType;
25
- _isAvailable(): Promise<boolean>;
26
- _set(key: string, value: PersistenceValue): Promise<void>;
27
- _get<T extends PersistenceValue>(key: string): Promise<T | null>;
28
- _remove(key: string): Promise<void>;
29
- _addListener(key: string, listener: StorageEventListener): void;
30
- _removeListener(key: string, listener: StorageEventListener): void;
31
- // Should this persistence allow migration up the chosen hierarchy?
32
- _shouldAllowMigration?: boolean;
33
- }
34
-
35
- export function getReactNativePersistence(
36
- storage: ReactNativeAsyncStorage
37
- ): Persistence {
38
- // In the _getInstance() implementation (see src/core/persistence/index.ts),
39
- // we expect each "externs.Persistence" object passed to us by the user to
40
- // be able to be instantiated (as a class) using "new". That function also
41
- // expects the constructor to be empty. Since ReactNativeStorage requires the
42
- // underlying storage layer, we need to be able to create subclasses
43
- // (closures, esentially) that have the storage layer but empty constructor.
44
- return class implements PersistenceInternal {
45
- static type = "LOCAL" as const;
46
- readonly type: PersistenceType = PersistenceType.LOCAL;
47
-
48
- async _isAvailable(): Promise<boolean> {
49
- try {
50
- if (!storage) {
51
- return false;
52
- }
53
- await storage.setItem(STORAGE_AVAILABLE_KEY, "1");
54
- await storage.removeItem(STORAGE_AVAILABLE_KEY);
55
- return true;
56
- } catch {
57
- return false;
58
- }
59
- }
60
-
61
- _set(key: string, value: PersistenceValue): Promise<void> {
62
- return storage.setItem(key, JSON.stringify(value));
63
- }
64
-
65
- async _get<T extends PersistenceValue>(key: string): Promise<T | null> {
66
- const json = await storage.getItem(key);
67
- return json ? JSON.parse(json) : null;
68
- }
69
-
70
- _remove(key: string): Promise<void> {
71
- return storage.removeItem(key);
72
- }
73
-
74
- _addListener(_key: string, _listener: StorageEventListener): void {
75
- // Listeners are not supported for React Native storage.
76
- return;
77
- }
78
-
79
- _removeListener(_key: string, _listener: StorageEventListener): void {
80
- // Listeners are not supported for React Native storage.
81
- return;
82
- }
83
- };
84
- }
1
+ import {Persistence, ReactNativeAsyncStorage} from "firebase/auth";
2
+
3
+ export const enum PersistenceType {
4
+ SESSION = "SESSION",
5
+ LOCAL = "LOCAL",
6
+ NONE = "NONE"
7
+ }
8
+
9
+ export type PersistedBlob = Record<string, unknown>;
10
+
11
+ export interface Instantiator<T> {
12
+ (blob: PersistedBlob): T;
13
+ }
14
+
15
+ export type PersistenceValue = PersistedBlob | string;
16
+
17
+ export const STORAGE_AVAILABLE_KEY = "__sak";
18
+
19
+ export interface StorageEventListener {
20
+ (value: PersistenceValue | null): void;
21
+ }
22
+
23
+ export interface PersistenceInternal extends Persistence {
24
+ type: PersistenceType;
25
+ _isAvailable(): Promise<boolean>;
26
+ _set(key: string, value: PersistenceValue): Promise<void>;
27
+ _get<T extends PersistenceValue>(key: string): Promise<T | null>;
28
+ _remove(key: string): Promise<void>;
29
+ _addListener(key: string, listener: StorageEventListener): void;
30
+ _removeListener(key: string, listener: StorageEventListener): void;
31
+ // Should this persistence allow migration up the chosen hierarchy?
32
+ _shouldAllowMigration?: boolean;
33
+ }
34
+
35
+ export function getReactNativePersistence(
36
+ storage: ReactNativeAsyncStorage
37
+ ): Persistence {
38
+ // In the _getInstance() implementation (see src/core/persistence/index.ts),
39
+ // we expect each "externs.Persistence" object passed to us by the user to
40
+ // be able to be instantiated (as a class) using "new". That function also
41
+ // expects the constructor to be empty. Since ReactNativeStorage requires the
42
+ // underlying storage layer, we need to be able to create subclasses
43
+ // (closures, esentially) that have the storage layer but empty constructor.
44
+ return class implements PersistenceInternal {
45
+ static type = "LOCAL" as const;
46
+ readonly type: PersistenceType = PersistenceType.LOCAL;
47
+
48
+ async _isAvailable(): Promise<boolean> {
49
+ try {
50
+ if (!storage) {
51
+ return false;
52
+ }
53
+ await storage.setItem(STORAGE_AVAILABLE_KEY, "1");
54
+ await storage.removeItem(STORAGE_AVAILABLE_KEY);
55
+ return true;
56
+ } catch {
57
+ return false;
58
+ }
59
+ }
60
+
61
+ _set(key: string, value: PersistenceValue): Promise<void> {
62
+ return storage.setItem(key, JSON.stringify(value));
63
+ }
64
+
65
+ async _get<T extends PersistenceValue>(key: string): Promise<T | null> {
66
+ const json = await storage.getItem(key);
67
+ return json ? JSON.parse(json) : null;
68
+ }
69
+
70
+ _remove(key: string): Promise<void> {
71
+ return storage.removeItem(key);
72
+ }
73
+
74
+ _addListener(_key: string, _listener: StorageEventListener): void {
75
+ // Listeners are not supported for React Native storage.
76
+ return;
77
+ }
78
+
79
+ _removeListener(_key: string, _listener: StorageEventListener): void {
80
+ // Listeners are not supported for React Native storage.
81
+ return;
82
+ }
83
+ };
84
+ }