@sigx/lynx-storage 0.1.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Andreas Ekdahl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # @sigx/lynx-storage
2
+
3
+ Persistent key-value storage for sigx-lynx. `UserDefaults` on iOS, `SharedPreferences` on Android. Same shape as `localStorage` / `AsyncStorage` (string keys, string values).
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @sigx/lynx-storage
9
+ ```
10
+
11
+ ```ts
12
+ // sigx.lynx.config.ts
13
+ export default defineLynxConfig({
14
+ modules: ['@sigx/lynx-storage'],
15
+ });
16
+ ```
17
+
18
+ No special permissions on either platform.
19
+
20
+ ## Usage
21
+
22
+ ```ts
23
+ import { Storage } from '@sigx/lynx-storage';
24
+
25
+ Storage.setItem('user', JSON.stringify({ name: 'Alice', id: 42 }));
26
+ const raw = await Storage.getItem('user');
27
+ const user = raw ? JSON.parse(raw) : null;
28
+
29
+ Storage.removeItem('user');
30
+
31
+ const keys = await Storage.getAllKeys();
32
+ Storage.clear(); // wipes everything in this app's namespace
33
+ ```
34
+
35
+ ## API
36
+
37
+ | Method | Notes |
38
+ | -------------------------------------------- | -------------------------------------------------------------------------------------------------- |
39
+ | `setItem(key: string, value: string): void` | Sync — fire-and-forget write. |
40
+ | `getItem(key: string): Promise<string \| null>` | Async. Returns `null` if the key isn't set. |
41
+ | `removeItem(key: string): void` | Sync — no error if the key doesn't exist. |
42
+ | `clear(): void` | Wipes the entire app namespace. |
43
+ | `getAllKeys(): Promise<string[]>` | Returns all currently-set keys. |
44
+ | `isAvailable(): boolean` | Whether the native module is registered in the current build. |
45
+
46
+ ## Gotchas
47
+
48
+ - **String values only.** Serialize objects with `JSON.stringify` / `JSON.parse` yourself. Don't dump raw binary — use `@sigx/lynx-file-system` for that.
49
+ - **Sync writes can race with reads** if you `setItem` then immediately `getItem` the same key from BG. UserDefaults / SharedPreferences both use a write-behind buffer; values are returned correctly within the same process, just be aware of cross-process scenarios (extension apps on iOS, etc.) where eventual consistency applies.
50
+ - **Persistence semantics.** Both stores survive app updates, are excluded from app-data exports on Android, and follow iCloud-backup rules on iOS (UserDefaults is included by default).
51
+
52
+ ## Reference app
53
+
54
+ `examples/lynx-one/my-sigx-app/src/cards/StorageCard.tsx` covers a write/read round-trip + getAllKeys + clear.
@@ -0,0 +1,68 @@
1
+ package com.sigx.storage
2
+
3
+ import android.content.Context
4
+ import android.content.SharedPreferences
5
+ import com.lynx.jsbridge.LynxMethod
6
+ import com.lynx.jsbridge.LynxModule
7
+ import com.lynx.react.bridge.Callback
8
+ import com.lynx.react.bridge.JavaOnlyArray
9
+ import com.lynx.react.bridge.JavaOnlyMap
10
+
11
+ /**
12
+ * Persistent key-value storage module (SharedPreferences-backed).
13
+ * JS usage: NativeModules.Storage.setItem("key", "value")
14
+ * NativeModules.Storage.getItem("key", callback)
15
+ */
16
+ class StorageModule(context: Context) : LynxModule(context) {
17
+
18
+ private val prefs: SharedPreferences by lazy {
19
+ mContext.getSharedPreferences("sigx_lynxgo_storage", Context.MODE_PRIVATE)
20
+ }
21
+
22
+ @LynxMethod
23
+ fun setItem(key: String?, value: String?) {
24
+ if (key != null) {
25
+ prefs.edit().putString(key, value).apply()
26
+ }
27
+ }
28
+
29
+ @LynxMethod
30
+ fun getItem(key: String?, callback: Callback?) {
31
+ val value = if (key != null) prefs.getString(key, null) else null
32
+ callback?.invoke(value)
33
+ }
34
+
35
+ @LynxMethod
36
+ fun removeItem(key: String?) {
37
+ if (key != null) {
38
+ prefs.edit().remove(key).apply()
39
+ }
40
+ }
41
+
42
+ @LynxMethod
43
+ fun clear() {
44
+ prefs.edit().clear().apply()
45
+ }
46
+
47
+ @LynxMethod
48
+ fun getAllKeys(callback: Callback?) {
49
+ val keys = JavaOnlyArray()
50
+ prefs.all.keys.forEach { keys.pushString(it) }
51
+ callback?.invoke(keys)
52
+ }
53
+
54
+ @LynxMethod
55
+ fun multiGet(keys: String?, callback: Callback?) {
56
+ // keys is a JSON-encoded string array for simplicity
57
+ if (keys == null) { callback?.invoke(null); return }
58
+ val result = JavaOnlyMap()
59
+ try {
60
+ val keyList = keys.split(",").map { it.trim() }
61
+ keyList.forEach { key ->
62
+ result.putString(key, prefs.getString(key, null))
63
+ }
64
+ } catch (_: Exception) {}
65
+ callback?.invoke(result)
66
+ }
67
+ }
68
+
@@ -0,0 +1,2 @@
1
+ export { Storage } from './storage.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { Storage } from './storage.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Persistent key-value storage APIs.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { Storage } from '@sigx/lynx-storage';
7
+ *
8
+ * Storage.setItem('user', JSON.stringify({ name: 'Alice' }));
9
+ * const user = await Storage.getItem('user');
10
+ * ```
11
+ */
12
+ export declare const Storage: {
13
+ readonly setItem: (key: string, value: string) => void;
14
+ readonly getItem: (key: string) => Promise<string | null>;
15
+ readonly removeItem: (key: string) => void;
16
+ readonly clear: () => void;
17
+ readonly getAllKeys: () => Promise<string[]>;
18
+ readonly isAvailable: () => boolean;
19
+ };
20
+ //# sourceMappingURL=storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,OAAO;4BACH,MAAM,SAAS,MAAM,KAAG,IAAI;4BAI5B,MAAM,KAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;+BAI5B,MAAM,KAAG,IAAI;0BAIpB,IAAI;+BAIC,OAAO,CAAC,MAAM,EAAE,CAAC;gCAIhB,OAAO;CAGhB,CAAC"}
@@ -0,0 +1,34 @@
1
+ import { callSync, callAsync, isModuleAvailable } from '@sigx/lynx-core';
2
+ const MODULE = 'Storage';
3
+ /**
4
+ * Persistent key-value storage APIs.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { Storage } from '@sigx/lynx-storage';
9
+ *
10
+ * Storage.setItem('user', JSON.stringify({ name: 'Alice' }));
11
+ * const user = await Storage.getItem('user');
12
+ * ```
13
+ */
14
+ export const Storage = {
15
+ setItem(key, value) {
16
+ callSync(MODULE, 'setItem', key, value);
17
+ },
18
+ getItem(key) {
19
+ return callAsync(MODULE, 'getItem', key);
20
+ },
21
+ removeItem(key) {
22
+ callSync(MODULE, 'removeItem', key);
23
+ },
24
+ clear() {
25
+ callSync(MODULE, 'clear');
26
+ },
27
+ getAllKeys() {
28
+ return callAsync(MODULE, 'getAllKeys');
29
+ },
30
+ isAvailable() {
31
+ return isModuleAvailable(MODULE);
32
+ },
33
+ };
34
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEzE,MAAM,MAAM,GAAG,SAAS,CAAC;AAEzB;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG;IACnB,OAAO,CAAC,GAAW,EAAE,KAAa;QAC9B,QAAQ,CAAC,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;IAC5C,CAAC;IAED,OAAO,CAAC,GAAW;QACf,OAAO,SAAS,CAAgB,MAAM,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC;IAC5D,CAAC;IAED,UAAU,CAAC,GAAW;QAClB,QAAQ,CAAC,MAAM,EAAE,YAAY,EAAE,GAAG,CAAC,CAAC;IACxC,CAAC;IAED,KAAK;QACD,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,CAAC;IAED,UAAU;QACN,OAAO,SAAS,CAAW,MAAM,EAAE,YAAY,CAAC,CAAC;IACrD,CAAC;IAED,WAAW;QACP,OAAO,iBAAiB,CAAC,MAAM,CAAC,CAAC;IACrC,CAAC;CACK,CAAC"}
@@ -0,0 +1,53 @@
1
+ import Foundation
2
+ import Lynx
3
+
4
+ /// Persistent key-value storage module (UserDefaults-backed).
5
+ /// JS usage: NativeModules.Storage.setItem("key", "value")
6
+ class StorageModule: NSObject, LynxModule {
7
+
8
+ @objc static var name: String { "Storage" }
9
+
10
+ @objc static var methodLookup: [String: String] {
11
+ [
12
+ "setItem": NSStringFromSelector(#selector(setItem(_:value:))),
13
+ "getItem": NSStringFromSelector(#selector(getItem(_:callback:))),
14
+ "removeItem": NSStringFromSelector(#selector(removeItem(_:))),
15
+ "clear": NSStringFromSelector(#selector(clear)),
16
+ "getAllKeys": NSStringFromSelector(#selector(getAllKeys(_:))),
17
+ ]
18
+ }
19
+
20
+ required override init() { super.init() }
21
+ required init(param: Any) { super.init() }
22
+
23
+ private lazy var defaults: UserDefaults = {
24
+ UserDefaults(suiteName: "com.sigx.lynxgo.storage") ?? .standard
25
+ }()
26
+
27
+ @objc func setItem(_ key: String?, value: String?) {
28
+ guard let key = key else { return }
29
+ defaults.set(value, forKey: key)
30
+ }
31
+
32
+ @objc func getItem(_ key: String?, callback: LynxCallbackBlock?) {
33
+ let value: String? = key != nil ? defaults.string(forKey: key!) : nil
34
+ callback?(value as Any)
35
+ }
36
+
37
+ @objc func removeItem(_ key: String?) {
38
+ guard let key = key else { return }
39
+ defaults.removeObject(forKey: key)
40
+ }
41
+
42
+ @objc func clear() {
43
+ guard let domain = defaults.persistentDomain(forName: "com.sigx.lynxgo.storage") else { return }
44
+ for key in domain.keys {
45
+ defaults.removeObject(forKey: key)
46
+ }
47
+ }
48
+
49
+ @objc func getAllKeys(_ callback: LynxCallbackBlock?) {
50
+ let keys = Array(defaults.dictionaryRepresentation().keys)
51
+ callback?(keys)
52
+ }
53
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@sigx/lynx-storage",
3
+ "version": "0.1.0",
4
+ "description": "Persistent key-value storage for sigx-lynx",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ },
13
+ "./sigx-module.json": "./sigx-module.json"
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "ios",
18
+ "android",
19
+ "sigx-module.json"
20
+ ],
21
+ "dependencies": {
22
+ "@sigx/lynx-core": "^0.1.0"
23
+ },
24
+ "devDependencies": {
25
+ "typescript": "^5.9.3"
26
+ },
27
+ "author": "Andreas Ekdahl",
28
+ "license": "MIT",
29
+ "scripts": {
30
+ "build": "tsc",
31
+ "dev": "tsc --watch"
32
+ }
33
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "Storage",
3
+ "package": "@sigx/lynx-storage",
4
+ "description": "Persistent key-value storage",
5
+ "platforms": ["android", "ios"],
6
+ "ios": {
7
+ "moduleClass": "StorageModule",
8
+ "sourceDir": "ios",
9
+ "methods": ["getItem","setItem","removeItem","clear","getAllKeys"]
10
+ },
11
+ "android": {
12
+ "moduleClass": "com.sigx.storage.StorageModule",
13
+ "sourceDir": "android",
14
+ "permissions": []
15
+ }
16
+ }