esoftplay 0.0.118-d → 0.0.118-e

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/_cache.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import AsyncStorage from '@react-native-async-storage/async-storage';
2
+ import Storage from 'esoftplay/storage';
2
3
  import * as R from 'react';
3
4
  import { fastFilter, fastLoop } from './fast';
4
5
  const isEqual = require('react-fast-compare');
@@ -11,21 +12,22 @@ export interface UseCache_return<T> {
11
12
 
12
13
  export interface UseCache_options {
13
14
  persistKey?: string,
15
+ inFile?: boolean,
14
16
  listener?: (data: any) => void
15
17
  }
16
18
  export default (() => {
17
19
  let useCacheIdx = 0
18
- let useCacheSubscriber = []
19
- let value = []
20
+ let useCacheSubscriber: any[] = []
20
21
  return <T>(initValue: T, o?: UseCache_options): UseCache_return<T> => {
22
+ const STORAGE = o?.inFile ? new Storage() : AsyncStorage
21
23
  const _idx = useCacheIdx
22
24
  if (!useCacheSubscriber[_idx]) {
23
25
  useCacheSubscriber[_idx] = [];
24
26
  }
25
- value[_idx] = initValue
27
+ let value = initValue
26
28
  // rehidryte instant
27
29
  if (o?.persistKey) {
28
- AsyncStorage.getItem(o.persistKey).then((p) => {
30
+ STORAGE.getItem(o.persistKey).then((p) => {
29
31
  if (p)
30
32
  set(JSON.parse(p))
31
33
  })
@@ -33,14 +35,14 @@ export default (() => {
33
35
 
34
36
  function set(ns: T | ((x: T) => T)) {
35
37
  let isChange = false
36
- if (!isEqual(value[_idx], ns)) {
38
+ if (!isEqual(value, ns)) {
37
39
  isChange = true
38
40
  }
39
41
  if (isChange) {
40
- value[_idx] = ns instanceof Function ? ns(value[_idx]) : ns
41
- fastLoop(useCacheSubscriber[_idx], (c: any) => c?.(value[_idx]))
42
+ value = ns instanceof Function ? ns(value) : ns
43
+ fastLoop(useCacheSubscriber[_idx], (c: any) => c?.(value))
42
44
  if (o?.persistKey) {
43
- AsyncStorage.setItem(o.persistKey, JSON.stringify(value[_idx]))
45
+ STORAGE.setItem(o.persistKey, JSON.stringify(value))
44
46
  }
45
47
  if (o?.listener) {
46
48
  o?.listener?.(ns instanceof Function ? ns(value[_idx]) : ns)
@@ -50,7 +52,7 @@ export default (() => {
50
52
 
51
53
  function del() {
52
54
  if (o?.persistKey) {
53
- AsyncStorage.removeItem(o.persistKey)
55
+ STORAGE.removeItem(o.persistKey)
54
56
  }
55
57
  set(initValue)
56
58
  }
package/bin/router.js CHANGED
@@ -446,6 +446,7 @@ declare module "esoftplay" {
446
446
  }
447
447
  interface createCacheOption {
448
448
  persistKey?: string,
449
+ inFile?: boolean,
449
450
  listener?: (s:any)=> void
450
451
  }
451
452
  interface createCacheReturn<T> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esoftplay",
3
- "version": "0.0.118-d",
3
+ "version": "0.0.118-e",
4
4
  "description": "embedding data from esoftplay framework (web based) into mobile app",
5
5
  "main": "cache/index.js",
6
6
  "types": "../../index.d.ts",