-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental AsyncStorage support for react-native (#983)
* Experimental AsyncStorage support for react-native Use @react-native-async-storage/async-storage to implement an AsyncStorage based storage. * Finalize Use in react native env Also downgrades chai-as-promise because the esm only has effects on running tests locally * Merge * fix package-lock and ignore strange types * fix package-lock
- Loading branch information
Showing
8 changed files
with
19,813 additions
and
14,171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
packages/matter.js-react-native/src/storage/StorageBackendAsyncStorage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/** | ||
* @license | ||
* Copyright 2022-2024 Matter.js Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { | ||
MaybeAsyncStorage, | ||
StorageError, | ||
SupportedStorageTypes, | ||
fromJson, | ||
toJson, | ||
} from "@project-chip/matter.js/storage"; | ||
import AsyncStorage from "@react-native-async-storage/async-storage"; | ||
|
||
export class StorageBackendAsyncStorage extends MaybeAsyncStorage { | ||
#namespace: string; | ||
protected isInitialized = false; | ||
|
||
/** | ||
* Creates a new instance of the AsyncStorage storage backend. In a "namespace" is provided then the keys will be | ||
* prefixed with the namespace (separated with a # which is normally not used in matter.js keys). | ||
*/ | ||
constructor(namespace?: string) { | ||
super(); | ||
this.#namespace = namespace ?? ""; | ||
} | ||
|
||
get initialized() { | ||
return this.isInitialized; | ||
} | ||
|
||
initialize() { | ||
this.isInitialized = true; | ||
} | ||
|
||
close() { | ||
this.isInitialized = false; | ||
} | ||
|
||
clear() { | ||
// @ts-expect-error AsyncStorage types are not correct | ||
return AsyncStorage.clear(); | ||
} | ||
|
||
getContextBaseKey(contexts: string[], allowEmptyContext = false) { | ||
const contextKey = contexts.join("."); | ||
if ( | ||
(!contextKey.length && !allowEmptyContext) || | ||
contextKey.includes("..") || | ||
contextKey.startsWith(".") || | ||
contextKey.endsWith(".") | ||
) | ||
throw new StorageError("Context must not be an empty and not contain dots."); | ||
return `${this.#namespace.length ? `${this.#namespace}#` : ""}${contextKey}`; | ||
} | ||
|
||
buildStorageKey(contexts: string[], key: string) { | ||
if (!key.length) { | ||
throw new StorageError("Key must not be an empty string."); | ||
} | ||
const contextKey = this.getContextBaseKey(contexts); | ||
return `${contextKey}.${key}`; | ||
} | ||
|
||
async get<T extends SupportedStorageTypes>(contexts: string[], key: string): Promise<T | undefined> { | ||
// @ts-expect-error AsyncStorage types are not correct | ||
const value = await AsyncStorage.getItem(this.buildStorageKey(contexts, key)); | ||
if (value === null) return undefined; | ||
return fromJson(value) as T; | ||
} | ||
|
||
set(contexts: string[], key: string, value: SupportedStorageTypes): Promise<void>; | ||
set(contexts: string[], values: Record<string, SupportedStorageTypes>): Promise<void>; | ||
async set( | ||
contexts: string[], | ||
keyOrValues: string | Record<string, SupportedStorageTypes>, | ||
value?: SupportedStorageTypes, | ||
) { | ||
if (typeof keyOrValues === "string") { | ||
// @ts-expect-error AsyncStorage types are not correct | ||
await AsyncStorage.setItem(this.buildStorageKey(contexts, keyOrValues), toJson(value)); | ||
} else { | ||
for (const [key, value] of Object.entries(keyOrValues)) { | ||
// @ts-expect-error AsyncStorage types are not correct | ||
await AsyncStorage.setItem(this.buildStorageKey(contexts, key), toJson(value)); | ||
} | ||
} | ||
} | ||
|
||
delete(contexts: string[], key: string) { | ||
// @ts-expect-error AsyncStorage types are not correct | ||
return AsyncStorage.removeItem(this.buildStorageKey(contexts, key)); | ||
} | ||
|
||
/** Returns all keys of a storage context without keys of sub-contexts */ | ||
async keys(contexts: string[]) { | ||
const contextKey = this.getContextBaseKey(contexts); | ||
const keys = []; | ||
const contextKeyStart = `${contextKey}.`; | ||
// @ts-expect-error AsyncStorage types are not correct | ||
const allKeys = await AsyncStorage.getAllKeys(); | ||
for (const key of allKeys) { | ||
if (key.startsWith(contextKeyStart) && key.indexOf(".", contextKeyStart.length) === -1) { | ||
keys.push(key.substring(contextKeyStart.length)); | ||
} | ||
} | ||
return keys; | ||
} | ||
|
||
async values(contexts: string[]) { | ||
// Initialize and context checks are done by keys method | ||
const values = {} as Record<string, SupportedStorageTypes>; | ||
for (const key of await this.keys(contexts)) { | ||
values[key] = await this.get(contexts, key); | ||
} | ||
return values; | ||
} | ||
|
||
async contexts(contexts: string[]) { | ||
const contextKey = this.getContextBaseKey(contexts, true); | ||
const startContextKey = contextKey.length ? `${contextKey}.` : ""; | ||
const foundContexts = new Array<string>(); | ||
// @ts-expect-error AsyncStorage types are not correct | ||
const allKeys = await AsyncStorage.getAllKeys(); | ||
for (const key of allKeys) { | ||
if (key.startsWith(startContextKey)) { | ||
const subKeys = key.substring(startContextKey.length).split("."); | ||
if (subKeys.length === 1) continue; // found leaf key | ||
const context = subKeys[0]; | ||
if (!foundContexts.includes(context)) { | ||
foundContexts.push(context); | ||
} | ||
} | ||
} | ||
return foundContexts; | ||
} | ||
|
||
async clearAll(contexts: string[]) { | ||
const contextKey = this.getContextBaseKey(contexts, true); | ||
const startContextKey = contextKey.length ? `${contextKey}.` : ""; | ||
// @ts-expect-error AsyncStorage types are not correct | ||
const allKeys = await AsyncStorage.getAllKeys(); | ||
for (const key of allKeys) { | ||
if (key.startsWith(startContextKey)) { | ||
// @ts-expect-error AsyncStorage types are not correct | ||
await AsyncStorage.removeItem(key); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters