@fireproof/core 0.3.0 → 0.3.1
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.
@@ -1,16 +1,15 @@
|
|
1
1
|
/* global localStorage */
|
2
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
3
2
|
// @ts-ignore
|
4
3
|
import { useEffect, useState, createContext } from 'react'
|
5
4
|
import { Fireproof, Listener, Hydrator } from '../index'
|
6
5
|
|
7
|
-
export interface FireproofCtxValue {
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
}
|
13
|
-
export const FireproofCtx = createContext
|
6
|
+
// export interface FireproofCtxValue {
|
7
|
+
// addSubscriber: (label: String, fn: Function) => void
|
8
|
+
// database: Fireproof
|
9
|
+
// ready: boolean
|
10
|
+
// persist: () => void
|
11
|
+
// }
|
12
|
+
export const FireproofCtx = createContext({
|
14
13
|
addSubscriber: () => {},
|
15
14
|
database: null,
|
16
15
|
ready: false
|
@@ -35,21 +34,21 @@ const initializeDatabase = name => {
|
|
35
34
|
* @param [setupDatabaseFn] Asynchronous function that sets up the database, run this to load fixture data etc
|
36
35
|
* @returns {FireproofCtxValue} { addSubscriber, database, ready }
|
37
36
|
*/
|
38
|
-
export function useFireproof(
|
39
|
-
defineDatabaseFn = (
|
40
|
-
setupDatabaseFn = async (
|
41
|
-
name
|
42
|
-
)
|
37
|
+
export function useFireproof (
|
38
|
+
defineDatabaseFn = () => {},
|
39
|
+
setupDatabaseFn = async () => {},
|
40
|
+
name
|
41
|
+
) {
|
43
42
|
const [ready, setReady] = useState(false)
|
44
43
|
initializeDatabase(name || 'useFireproof')
|
45
44
|
const localStorageKey = 'fp.' + database.name
|
46
45
|
|
47
|
-
const addSubscriber = (label
|
46
|
+
const addSubscriber = (label, fn) => {
|
48
47
|
inboundSubscriberQueue.set(label, fn)
|
49
48
|
}
|
50
49
|
|
51
50
|
const listenerCallback = async event => {
|
52
|
-
|
51
|
+
localSet(localStorageKey, JSON.stringify(database))
|
53
52
|
if (event._external) return
|
54
53
|
for (const [, fn] of inboundSubscriberQueue) fn()
|
55
54
|
}
|
@@ -84,7 +83,7 @@ export function useFireproof(
|
|
84
83
|
localSet(localStorageKey, JSON.stringify(database))
|
85
84
|
}
|
86
85
|
setReady(true)
|
87
|
-
listener.on('*', listenerCallback)//hushed('*', listenerCallback, 250))
|
86
|
+
listener.on('*', listenerCallback)// hushed('*', listenerCallback, 250))
|
88
87
|
}
|
89
88
|
doSetup()
|
90
89
|
}, [ready])
|
@@ -99,32 +98,32 @@ export function useFireproof(
|
|
99
98
|
}
|
100
99
|
}
|
101
100
|
|
102
|
-
const husherMap = new Map()
|
103
|
-
const husher = (id
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
}
|
113
|
-
const hushed =
|
114
|
-
|
115
|
-
|
116
|
-
|
101
|
+
// const husherMap = new Map()
|
102
|
+
// const husher = (id, workFn, ms) => {
|
103
|
+
// if (!husherMap.has(id)) {
|
104
|
+
// const start = Date.now()
|
105
|
+
// husherMap.set(
|
106
|
+
// id,
|
107
|
+
// workFn().finally(() => setTimeout(() => husherMap.delete(id), ms - (Date.now() - start)))
|
108
|
+
// )
|
109
|
+
// }
|
110
|
+
// return husherMap.get(id)
|
111
|
+
// }
|
112
|
+
// const hushed =
|
113
|
+
// (id, workFn, ms) =>
|
114
|
+
// (...args) =>
|
115
|
+
// husher(id, () => workFn(...args), ms)
|
117
116
|
|
118
117
|
let storageSupported = false
|
119
118
|
try {
|
120
119
|
storageSupported = window.localStorage && true
|
121
120
|
} catch (e) {}
|
122
|
-
export function localGet(key
|
121
|
+
export function localGet (key) {
|
123
122
|
if (storageSupported) {
|
124
123
|
return localStorage && localStorage.getItem(key)
|
125
124
|
}
|
126
125
|
}
|
127
|
-
function localSet(key
|
126
|
+
function localSet (key, value) {
|
128
127
|
if (storageSupported) {
|
129
128
|
return localStorage && localStorage.setItem(key, value)
|
130
129
|
}
|