@rpcbase/client 0.98.0 → 0.100.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/auth/index.js CHANGED
@@ -3,11 +3,13 @@ import debug from "debug"
3
3
  import {Platform} from "react-native"
4
4
  import isHexadecimal from "validator/lib/isHexadecimal"
5
5
 
6
+ import storage from "../storage"
6
7
  import {disconnect as rts_disconnect} from "../rts"
7
8
  import post from "../helpers/post"
8
9
 
9
10
  import redirect_sign_in from "./helpers/redirect_sign_in"
10
11
 
12
+
11
13
  const LAST_TENANT_KEY = "rb.last_tenant_id"
12
14
  const uid_storage_key = (tenant_id) => `rb.${tenant_id}.user_id`
13
15
 
@@ -118,7 +120,7 @@ export const set_uid = (val) => {
118
120
  export const get_tenant_id = () => __tenant_id
119
121
  export const set_tenant_id = (val) => {
120
122
  __tenant_id = val
121
- localStorage.setItem(LAST_TENANT_KEY, val)
123
+ storage.setItem(LAST_TENANT_KEY, val)
122
124
  }
123
125
 
124
126
  export const set_is_signed_in = (val) => __is_authenticated = val
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.98.0",
3
+ "version": "0.100.0",
4
4
  "scripts": {
5
5
  "build-firebase": "webpack -c firebase/webpack.config.js",
6
6
  "build": "yarn build-firebase",
@@ -1,22 +1,29 @@
1
1
  /* @flow */
2
- import AsyncStorage from "@react-native-async-storage/async-storage"
2
+ import {MMKV} from "react-native-mmkv"
3
+
4
+ // TODO: add encryption key
5
+ // https://github.com/mrousavy/react-native-mmkv#customize
6
+ const mmkv = new MMKV
3
7
 
4
8
  const getStorage = () => {
5
9
  const storage = {
6
- get: async(key) => {
10
+ getItem: (key) => {
7
11
  try {
8
- const res = await AsyncStorage.getItem(key)
12
+ const res = mmkv.getString(key)
13
+ if (!res) return
9
14
  const val = JSON.parse(res)
10
15
  if (val) return val
11
16
  } catch (error) {
12
17
  // There was an error on the native side
18
+ console.log("storage.getItem error:", error)
13
19
  }
14
20
  },
15
- set: async(key, value) => {
16
- try {
17
- await AsyncStorage.setItem(key, JSON.stringify(value))
18
- } catch (error) {
19
- // There was an error on the native side
21
+ setItem: (key, value) => {
22
+ if (value === null || typeof value === "undefined") {
23
+ mmkv.delete(key)
24
+ return
25
+ } else {
26
+ mmkv.set(key, JSON.stringify(value))
20
27
  }
21
28
  }
22
29
  }
@@ -1,81 +1,21 @@
1
1
  /* @flow */
2
- const storeName = "store"
3
- const version = 1
4
-
5
- const getStorage = (
6
- dbName = "rb-store"
7
- ) => {
8
- let __db
9
-
10
- const openRequest = indexedDB.open(dbName, version)
11
-
12
- // Handle the creation or upgrade of the database
13
- openRequest.onupgradeneeded = (event) => {
14
- const db = event.target.result
15
- if (!db.objectStoreNames.contains(storeName)) {
16
- db.createObjectStore(storeName, { keyPath: "key" })
17
- }
18
- }
19
-
20
- // Handle errors when opening the database
21
- openRequest.onerror = (event) => {
22
- console.error("Error opening database:", event.target.errorCode)
23
- }
24
-
25
- openRequest.onsuccess = (event) => {
26
- const db = event.target.result
27
- __db = db
28
- }
29
-
30
2
 
3
+ const getStorage = () => {
31
4
  const storage = {
32
- get: (key) => new Promise((resolve, reject) => {
33
- const transaction = __db.transaction([storeName], "readonly")
34
- const objectStore = transaction.objectStore(storeName)
35
- const request = objectStore.get(key)
36
-
37
- request.onsuccess = (event) => {
38
- const obj = event.target.result?.obj
39
- resolve(obj)
40
- }
41
-
42
- request.onerror = (event) => {
43
- console.error("Error reading data:", event.target)
44
- reject()
45
- }
46
-
47
- }),
48
- set: (key, obj) => new Promise((resolve, reject) => {
49
-
50
- const transaction = __db.transaction([storeName], "readwrite")
51
- const objectStore = transaction.objectStore(storeName)
52
- const request = objectStore.put({obj, key})
53
-
54
- request.onsuccess = (event) => {
55
- // console.log("Data written successfully:", event.target.result)
56
- resolve()
57
- }
58
-
59
- request.onerror = (event) => {
60
- // console.error("Error writing data:", event.target.errorCode)
61
- console.log("ERRR", event.target)
62
- reject()
63
- }
64
- }),
65
- delete: (key) => new Promise((resolve, reject) => {
66
- const transaction = __db.transaction([storeName], "readwrite")
67
- const objectStore = transaction.objectStore(storeName)
68
- const request = objectStore.delete(key)
69
-
70
- request.onsuccess = (event) => {
71
- resolve()
5
+ getItem: (key) => {
6
+ try {
7
+ const res = localStorage.getItem(key)
8
+ if (!res) return
9
+ const val = JSON.parse(res)
10
+ return val
11
+ } catch (error) {
12
+ // There was an error on the native side
13
+ console.log(error)
72
14
  }
73
-
74
- request.onerror = (event) => {
75
- console.error("Error deleting data:", event.target.errorCode)
76
- reject()
77
- }
78
- }),
15
+ },
16
+ setItem: (key, value) => {
17
+ localStorage.setItem(key, JSON.stringify(value))
18
+ }
79
19
  }
80
20
 
81
21
  return storage