ShareTypedStorage
Start from API_LEVEL
3.0. Please refer to API_LEVEL.
Shared typed key-value storage for cross-application scenarios. Application A publishes system properties with this class, and application B reads them with TypedStorage from @zos/share-storage and application A's appId. When using a custom scope, both applications must use the same scope.
Constructor
Create a shared typed storage instance. The scope groups keys. When omitted, the framework default shared group is used; when customized, readers must use the same value.
constructor(scope?: string)
Methods
getBool
Get a boolean value
getBool(key: string, defaultValue: boolean): boolean
getInt
Get an integer value
getInt(key: string, defaultValue: number): number
getInt64
Get a 64-bit integer value
getInt64(key: string, defaultValue: number): number
getDouble
Get a double value
getDouble(key: string, defaultValue: number): number
getString
Get a string value
getString(key: string, defaultValue: string): string
putBool
Set a boolean value
putBool(key: string, value: boolean): number
putInt
Set an integer value
putInt(key: string, value: number): number
putInt64
Set a 64-bit integer value
putInt64(key: string, value: number): number
putDouble
Set a double value
putDouble(key: string, value: number): number
putString
Set a string value
putString(key: string, value: string): number
has
Check whether a key exists
has(key: string): boolean
clear
Clear all shared typed storage data
clear(): void
remove
Delete a value by key
remove(key: string): boolean
Example
// ==================== Application A (data provider) ====================
import { ShareTypedStorage } from '@zos/storage'
const sharedStorage = new ShareTypedStorage('watchface-data')
sharedStorage.putBool('enabled', true)
sharedStorage.putInt('steps', 6000)
sharedStorage.putInt64('lastUpdatedAt', Date.now())
sharedStorage.putDouble('progress', 0.75)
sharedStorage.putString('theme', 'dark')
if (sharedStorage.has('legacy-theme')) {
sharedStorage.remove('legacy-theme')
}
// Clear all shared typed data when it is no longer needed.
// sharedStorage.clear()
// ==================== Application B (data consumer) ====================
// This section runs in application B. Use application A's appId and the same scope.
import { TypedStorage } from '@zos/share-storage'
const storage = new TypedStorage(appIdOfApplicationA, 'watchface-data')
const enabled = storage.getBool('enabled', false)
const steps = storage.getInt('steps', 0)
const lastUpdatedAt = storage.getInt64('lastUpdatedAt', 0)
const progress = storage.getDouble('progress', 0)
const theme = storage.getString('theme', 'light')