parent
f2177a9e8e
commit
37c3b5aa45
@ -0,0 +1,42 @@
|
|||||||
|
const initListener = []
|
||||||
|
const authListener = []
|
||||||
|
|
||||||
|
export const onInit = (fn) => {
|
||||||
|
initListener.push(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const onAuth = (fn) => {
|
||||||
|
authListener.push(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const fireInit = async () => {
|
||||||
|
initListener.forEach(async (fn) => {
|
||||||
|
await fn()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const fireAuth = (obj) => {
|
||||||
|
authListener.forEach(fn => fn(obj))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zustand 中间件,用于订阅前端应用的生命周期,实验阶段
|
||||||
|
export const obervseLifecycle = (fn) => (set, get, store) => {
|
||||||
|
|
||||||
|
onInit(() => {
|
||||||
|
if (store.getState().hasOwnProperty('onInit')) {
|
||||||
|
store.getState().onInit()
|
||||||
|
} else {
|
||||||
|
console.info('store has no function: onInit.')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
onAuth(() => {
|
||||||
|
if (store.getState().hasOwnProperty('onAuth')) {
|
||||||
|
store.getState().onAuth()
|
||||||
|
} else {
|
||||||
|
console.info('store has no function: onAuth.')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return fn(set, get, store)
|
||||||
|
}
|
Loading…
Reference in New Issue