@startlibs/core 1.0.3 → 1.0.5
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/lib/index.js +5 -4
- package/package.json +2 -2
- package/src/SlotFill.jsx +0 -165
- package/src/StartlibsProvider.jsx +0 -61
- package/src/hocs/WithLazyResource.jsx +0 -32
- package/src/hocs/WithRef.js +0 -20
- package/src/hocs/Wrapper.jsx +0 -40
- package/src/hocs/consumes.jsx +0 -17
- package/src/hocs/withDialogError.jsx +0 -16
- package/src/hocs/withGroupEvent.jsx +0 -43
- package/src/hooks/WithConstant.jsx +0 -7
- package/src/hooks/WithEffect.jsx +0 -6
- package/src/hooks/useAsyncEffect.jsx +0 -11
- package/src/hooks/useCallState.js +0 -15
- package/src/hooks/useCallbackStable.jsx +0 -12
- package/src/hooks/useConstant.js +0 -20
- package/src/hooks/useConsumerCatalog.jsx +0 -21
- package/src/hooks/useEnsureRef.js +0 -7
- package/src/hooks/useForceUpdate.jsx +0 -16
- package/src/hooks/useIsUnmounted.jsx +0 -12
- package/src/hooks/usePopupToggle.js +0 -30
- package/src/hooks/usePrevious.jsx +0 -27
- package/src/hooks/useRecycleFocus.jsx +0 -28
- package/src/hooks/useRefMemo.jsx +0 -10
- package/src/hooks/useRefState.js +0 -26
- package/src/hooks/useSignal.jsx +0 -193
- package/src/hooks/useTitle.jsx +0 -12
- package/src/hooks/useToggle.js +0 -124
- package/src/hooks/useUpdateEffect.js +0 -14
- package/src/hooks/useUpdateOnResize.jsx +0 -14
- package/src/hooks/willUseLayers.jsx +0 -42
- package/src/hooks/willUseSharedCallback.jsx +0 -61
- package/src/hooks/willUseSharedState.jsx +0 -34
- package/src/hooks/willUseSpreadState.jsx +0 -101
- package/src/index.js +0 -31
package/src/hooks/useRefMemo.jsx
DELETED
package/src/hooks/useRefState.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import React, {useMemo, useRef} from 'react'
|
|
2
|
-
import {_s, callIfFunction, callIfFunctions} from '@startlibs/utils'
|
|
3
|
-
|
|
4
|
-
const isPath = (path) => typeof path === 'string' || typeof path === 'number' || Array.isArray(path)
|
|
5
|
-
|
|
6
|
-
const getter = (value) => path => path !== "" && isPath(path) ? _s.get(path,value) : value
|
|
7
|
-
|
|
8
|
-
const uninitialized = {}
|
|
9
|
-
|
|
10
|
-
export const useRefState = (defaultValue, setCallback) => {
|
|
11
|
-
const ref = useRef(uninitialized)
|
|
12
|
-
if (ref.current === uninitialized) {
|
|
13
|
-
ref.current = callIfFunction(defaultValue)
|
|
14
|
-
}
|
|
15
|
-
return useMemo(() => {
|
|
16
|
-
const get = (path) => getter(ref.current)(path)
|
|
17
|
-
const set = (v, config) => {
|
|
18
|
-
const prev = ref.current
|
|
19
|
-
ref.current = callIfFunctions(v, () => get())
|
|
20
|
-
if (prev !== ref.current && (!config || !config.ignoreCallback)) {
|
|
21
|
-
callIfFunction(setCallback, prev, ref.current)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return {get, set}
|
|
25
|
-
}, [])
|
|
26
|
-
}
|
package/src/hooks/useSignal.jsx
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import React, {startTransition, use, useMemo, useCallback, useContext, useEffect, useRef, useState} from 'react'
|
|
2
|
-
import {useConstant, useForceUpdate, useLazyConstant} from '@startlibs/core'
|
|
3
|
-
import {_s, callIfFunction} from '@startlibs/utils'
|
|
4
|
-
import _ from 'lodash/fp'
|
|
5
|
-
import {last} from "lodash/array";
|
|
6
|
-
|
|
7
|
-
const IS_ARRAY_ITEMS_EQUALS = (a,b) => a.length === b.length && !a.some((x, i) => !Object.is(x, b[i]));
|
|
8
|
-
const IS_DEPS_EQUALS = (a = [],b = []) => Object.is(a,b) || IS_ARRAY_ITEMS_EQUALS(a,b)
|
|
9
|
-
|
|
10
|
-
const SHALLOW_IS_EQUAL = (a,b) => Object.is(a,b) || (_.isArray(a) && _.isArray(b) && IS_ARRAY_ITEMS_EQUALS(a,b))
|
|
11
|
-
|
|
12
|
-
const EMPTY_DEPS = []
|
|
13
|
-
const EMPTY_VALUE = {"EMPTY":true}
|
|
14
|
-
export const useUpdateSignal = (initialValue) => {
|
|
15
|
-
const value = useRef(initialValue)
|
|
16
|
-
value.current = initialValue
|
|
17
|
-
return useLazyConstant(() => {
|
|
18
|
-
let registered = []
|
|
19
|
-
const register = (selector = _.identity, isEqual = SHALLOW_IS_EQUAL, withTransition = false) => {
|
|
20
|
-
const selectorRef = useRef()
|
|
21
|
-
selectorRef.current = selector
|
|
22
|
-
const update = useForceUpdate()
|
|
23
|
-
const lastValueRef = useRef(_s.select(selectorRef.current, value.current))
|
|
24
|
-
useEffect(() => {
|
|
25
|
-
const item = [update, selectorRef, isEqual, lastValueRef, withTransition]
|
|
26
|
-
registered = [...registered, item]
|
|
27
|
-
return () => {
|
|
28
|
-
registered = registered.filter(v => v !== item)
|
|
29
|
-
}
|
|
30
|
-
}, [])
|
|
31
|
-
if (value.current === EMPTY_VALUE) {
|
|
32
|
-
return EMPTY_VALUE
|
|
33
|
-
}
|
|
34
|
-
return lastValueRef.current
|
|
35
|
-
}
|
|
36
|
-
const call = (...args) =>
|
|
37
|
-
registered.forEach(([fn, selectorRef, isEqual, lastValueRef, withTransition]) => {
|
|
38
|
-
const nextValue = _s.select(selectorRef.current, ...args)
|
|
39
|
-
if (!callIfFunction(isEqual,nextValue,lastValueRef.current)) {
|
|
40
|
-
lastValueRef.current = nextValue
|
|
41
|
-
if (withTransition) {
|
|
42
|
-
(withTransition === true ? startTransition : withTransition)(() => fn(nextValue))
|
|
43
|
-
} else {
|
|
44
|
-
fn(nextValue)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
return [register, call]
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const useSelectorSignal = (value) => {
|
|
53
|
-
const prevValue = useRef(value)
|
|
54
|
-
const [_register, call] = useUpdateSignal(value)
|
|
55
|
-
const register = useCallback((selector, deps, isEqual, withTransition) => {
|
|
56
|
-
const prevDeps = useRef(EMPTY_DEPS)
|
|
57
|
-
if (prevDeps.current !== EMPTY_DEPS && !IS_DEPS_EQUALS(deps, prevDeps.current)) {
|
|
58
|
-
call(prevValue.current)
|
|
59
|
-
}
|
|
60
|
-
prevDeps.current = deps
|
|
61
|
-
return _register(selector, isEqual, withTransition)
|
|
62
|
-
}, [])
|
|
63
|
-
useEffect(() => {
|
|
64
|
-
if (prevValue.current !== value) {
|
|
65
|
-
prevValue.current = value
|
|
66
|
-
call(value)
|
|
67
|
-
}
|
|
68
|
-
},[value])
|
|
69
|
-
return register
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const updateStateFn = (path,update) => state =>
|
|
73
|
-
path
|
|
74
|
-
? _.update(path,(v) => callIfFunction(update,v),state)
|
|
75
|
-
: callIfFunction(update,state)
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
export const createStatefulContext = (v,defaultIsEqual) => {
|
|
79
|
-
const Ctx = React.createContext(v)
|
|
80
|
-
const Provider = ({value, setValue, utils,children}) => {
|
|
81
|
-
const register = useSelectorSignal(value)
|
|
82
|
-
const v = useLazyConstant(() => [register,setValue, utils])
|
|
83
|
-
return <Ctx.Provider value={v}>{children}</Ctx.Provider>
|
|
84
|
-
}
|
|
85
|
-
const useCtx = () => useContext(Ctx)
|
|
86
|
-
|
|
87
|
-
const useSelectValue = (selector,deps,defaultIsEqual, withTransition) => {
|
|
88
|
-
const [useRegister] = useCtx()
|
|
89
|
-
if (deps === true) {
|
|
90
|
-
return useRegister(selector,[],undefined,startTransition)
|
|
91
|
-
} else if (_.isPlainObject(deps)) {
|
|
92
|
-
return useRegister(selector,deps.deps,deps.isEqual, deps.withTransition)
|
|
93
|
-
} else {
|
|
94
|
-
return useRegister(selector,_.isArray(deps) ? deps : deps?.deps,isEqual, withTransition)
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
const useSetValue = (path) => {
|
|
98
|
-
const [reg,set ] = useCtx()
|
|
99
|
-
return useConstant((update) => set(updateStateFn(path,update)))
|
|
100
|
-
}
|
|
101
|
-
const useUtils = (path,deps) => {
|
|
102
|
-
const [reg,set, utils ] = useCtx()
|
|
103
|
-
return utils
|
|
104
|
-
}
|
|
105
|
-
return [Provider,[useSelectValue, useSetValue, useUtils, useCtx]]
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export const createSelfStatefulContext = (v,defaultIsEqual) => {
|
|
109
|
-
const [Provider,[useSelectValue, useSetValue, useUtils, useCtx]] = createStatefulContext(v,defaultIsEqual)
|
|
110
|
-
|
|
111
|
-
const StateFullProvider = ({init = {}, utils,children}) => {
|
|
112
|
-
const [state,setState] = useState(init)
|
|
113
|
-
return <Provider value={state} setValue={setState} utils={utils}>
|
|
114
|
-
{children}
|
|
115
|
-
</Provider>
|
|
116
|
-
}
|
|
117
|
-
return [StateFullProvider,[useSelectValue, useSetValue, useUtils, useCtx]]
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
export const createLazyStatefulContext = (toPromise,defaultIsEqual) => {
|
|
122
|
-
const [Provider,[useSelectValue, useSetValue, useUtils, useCtx]] = createStatefulContext(EMPTY_VALUE,defaultIsEqual)
|
|
123
|
-
const useSelectLazyValue = (selector,deps,isEqual = defaultIsEqual) => {
|
|
124
|
-
const value = useSelectValue(selector,deps,isEqual)
|
|
125
|
-
const utils = useUtils()
|
|
126
|
-
if (value === EMPTY_VALUE) {
|
|
127
|
-
const loadedValue = use(utils._lazyValue())
|
|
128
|
-
return _s.select(selector,loadedValue)
|
|
129
|
-
}
|
|
130
|
-
return value
|
|
131
|
-
}
|
|
132
|
-
const useSetLazyValue = (path) => {
|
|
133
|
-
const [,setValue,utils] = useCtx()
|
|
134
|
-
return useConstant((update) => {
|
|
135
|
-
return setValue(value =>
|
|
136
|
-
value === EMPTY_VALUE
|
|
137
|
-
? updateStateFn(path,update)(utils.loadedValue())
|
|
138
|
-
: updateStateFn(path,update)(value)
|
|
139
|
-
)
|
|
140
|
-
})
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const useValuePromise = (selector,deps,isEqual) => {
|
|
144
|
-
const value = useSelectValue(selector,deps,isEqual)
|
|
145
|
-
const utils = useUtils()
|
|
146
|
-
return useCallback(() => {
|
|
147
|
-
if (value === EMPTY_VALUE) {
|
|
148
|
-
return utils._lazyValue().then(v => console.log(v,_s.select(selector,v),selector) || _s.select(selector,v))
|
|
149
|
-
}
|
|
150
|
-
return new Promise(value)
|
|
151
|
-
},[value,deps])
|
|
152
|
-
}
|
|
153
|
-
const LazyStateFullProvider = ({value, utils,children, fallback = null}) => {
|
|
154
|
-
const [state,setState] = useState(EMPTY_VALUE)
|
|
155
|
-
const promise = useRef()
|
|
156
|
-
const loadedValue = useRef()
|
|
157
|
-
const lazyUtils = useMemo(() => ({
|
|
158
|
-
...utils,
|
|
159
|
-
_lazyValue: () => {
|
|
160
|
-
promise.current ??= callIfFunction(toPromise || _.identity,value)
|
|
161
|
-
promise.current.then((value) => {
|
|
162
|
-
loadedValue.current = value
|
|
163
|
-
})
|
|
164
|
-
return promise.current
|
|
165
|
-
},
|
|
166
|
-
loadedValue: () => loadedValue.current
|
|
167
|
-
}),[value,utils])
|
|
168
|
-
return <Provider value={state} setValue={setState} utils={lazyUtils}>
|
|
169
|
-
{
|
|
170
|
-
fallback===false
|
|
171
|
-
? children
|
|
172
|
-
: <React.Suspense fallback={fallback}>{children}</React.Suspense>
|
|
173
|
-
}
|
|
174
|
-
</Provider>
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return [LazyStateFullProvider,[useSelectLazyValue, useSetLazyValue, useValuePromise]]
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export const createSelectableContext = (v) => {
|
|
181
|
-
const [Provider,[useSelectValue]] = createStatefulContext(v)
|
|
182
|
-
return [Provider,useSelectValue]
|
|
183
|
-
}
|
|
184
|
-
export const createTriggerUpdateContext = (v) => {
|
|
185
|
-
const [Provider,[useSelectValue]] = createStatefulContext(v)
|
|
186
|
-
const TriggerProvider = React.forwardRef(({children},ref) => {
|
|
187
|
-
const [v,setV] = useState(0)
|
|
188
|
-
const trigger = React.useMemo(() => () => setV(v => v+1),[v])
|
|
189
|
-
React.useImperativeHandle(ref, () => ({trigger}) , [])
|
|
190
|
-
return <Provider value={trigger}>{children}</Provider>
|
|
191
|
-
})
|
|
192
|
-
return [TriggerProvider,useSelectValue]
|
|
193
|
-
}
|
package/src/hooks/useTitle.jsx
DELETED
package/src/hooks/useToggle.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import React, {useRef, useMemo} from 'react'
|
|
2
|
-
import {callIfFunction} from '@startlibs/utils'
|
|
3
|
-
import {useRefState, useCallState, useIsUnmounted} from '@startlibs/core'
|
|
4
|
-
|
|
5
|
-
const useToggleInternal = (startWith = false, useStore, callback, key) => {
|
|
6
|
-
|
|
7
|
-
const isOpen = useStore(startWith)
|
|
8
|
-
const closing = useStore(false)
|
|
9
|
-
const refIsOpen = useRefState(isOpen.get())
|
|
10
|
-
const refIsClosing = useRefState(closing.get())
|
|
11
|
-
const timerId = useRef()
|
|
12
|
-
|
|
13
|
-
const isUnmounted = useIsUnmounted()
|
|
14
|
-
|
|
15
|
-
const clearClosing = () => {
|
|
16
|
-
if (refIsClosing.get() !== false) {
|
|
17
|
-
closing.set(false)
|
|
18
|
-
refIsClosing.set(false)
|
|
19
|
-
window.clearTimeout(timerId.current)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const toggleWith = (value = true) => {
|
|
24
|
-
const calledValue = callIfFunction(value,refIsOpen.get())
|
|
25
|
-
const newValue = refIsOpen.get() === calledValue ? !calledValue : calledValue
|
|
26
|
-
if (!isUnmounted() && refIsOpen.get() !== newValue) {
|
|
27
|
-
const result = callIfFunction(callback,newValue,refIsOpen.get())
|
|
28
|
-
if (result === false) { return }
|
|
29
|
-
isOpen.set(newValue)
|
|
30
|
-
refIsOpen.set(newValue)
|
|
31
|
-
clearClosing()
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const openWith = (value) => {
|
|
36
|
-
if (process.env.NODE_ENV !== 'production' && key) {
|
|
37
|
-
console.log(key,refIsOpen.get()!==value ? "toggle":"clear", refIsOpen.get(),value)
|
|
38
|
-
}
|
|
39
|
-
const nextValue = callIfFunction(value,refIsOpen.get())
|
|
40
|
-
if (refIsOpen.get() !== nextValue) {
|
|
41
|
-
toggleWith(() => nextValue)
|
|
42
|
-
} else {
|
|
43
|
-
clearClosing()
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const nextClosingTick = (ticks, tickIndex) => {
|
|
48
|
-
if (ticks.length) {
|
|
49
|
-
if (isNaN(ticks[0])) {
|
|
50
|
-
clearClosing()
|
|
51
|
-
} else if (!isNaN(ticks[0])) {
|
|
52
|
-
closing.set(tickIndex + 1)
|
|
53
|
-
refIsClosing.set(tickIndex + 1)
|
|
54
|
-
timerId.current = setTimeout(() => nextClosingTick(ticks.slice(1), tickIndex + 1), Number(ticks[0]))
|
|
55
|
-
}
|
|
56
|
-
} else {
|
|
57
|
-
openWith(false)
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const closeAfter = (...millisecs) => {
|
|
61
|
-
if (process.env.NODE_ENV !== 'production' && key) {
|
|
62
|
-
console.log(key,!refIsOpen.get() || refIsClosing.get() ? "addCLosing" : "closing" )
|
|
63
|
-
}
|
|
64
|
-
if (!refIsOpen.get() || refIsClosing.get()) return
|
|
65
|
-
nextClosingTick(millisecs, 0)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const _wait = (promise, start = true, end = false) => async (...args) => {
|
|
69
|
-
openWith(start)
|
|
70
|
-
try {
|
|
71
|
-
return await callIfFunction(promise,...args)
|
|
72
|
-
} finally {
|
|
73
|
-
openWith(end)
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const setters = useMemo(()=>({
|
|
78
|
-
wait: (promise,start = true, end = false) => _wait(promise,start, end)(),
|
|
79
|
-
willWait: _wait,
|
|
80
|
-
open: (e) => {
|
|
81
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
82
|
-
if (['string', 'number', 'boolean'].indexOf(typeof e) >= 0) {
|
|
83
|
-
console.log(`You are trying to open toggle with ${e}. Please, use openWith to open with a value`)
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return openWith(true)
|
|
87
|
-
},
|
|
88
|
-
close: () => openWith(false),
|
|
89
|
-
toggle: (e) => {
|
|
90
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
91
|
-
if (['string', 'number', 'boolean'].indexOf(typeof e) >= 0) {
|
|
92
|
-
console.log(`You are trying to toogle a toggle with ${e}. Please, use openWith to open with a value`)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
toggleWith(true)
|
|
96
|
-
},
|
|
97
|
-
willOpenWith: (value) => () => openWith(value),
|
|
98
|
-
willCloseAfter: (...millsecs) => () => closeAfter(...millsecs),
|
|
99
|
-
willToggleWith: (value) => () => toggleWith(value),
|
|
100
|
-
openWith,
|
|
101
|
-
closeAfter,
|
|
102
|
-
toggleWith,
|
|
103
|
-
get: refIsOpen.get
|
|
104
|
-
}),[])
|
|
105
|
-
const settersWithNewRef = {...setters}
|
|
106
|
-
if (closing.val) {
|
|
107
|
-
return Object.assign(settersWithNewRef, {isOpen: isOpen.val, closing: closing.val})
|
|
108
|
-
} else {
|
|
109
|
-
Object.defineProperties(settersWithNewRef, {
|
|
110
|
-
isOpen: {
|
|
111
|
-
enumerable: true,
|
|
112
|
-
get: isOpen.get
|
|
113
|
-
},
|
|
114
|
-
closing: {
|
|
115
|
-
enumerable: true,
|
|
116
|
-
get: closing.get
|
|
117
|
-
}
|
|
118
|
-
})
|
|
119
|
-
return settersWithNewRef
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export const useToggle = (startWith, callback,key) => useToggleInternal(startWith, useCallState, callback,key)
|
|
124
|
-
export const useRefToggle = (startWith) => useToggleInternal(startWith, useRefState)
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import React, {useRef, useEffect} from 'react'
|
|
2
|
-
|
|
3
|
-
export const useUpdateEffect = (effect,inputs) => {
|
|
4
|
-
|
|
5
|
-
const pastFirstTime = useRef()
|
|
6
|
-
|
|
7
|
-
useEffect(() => {
|
|
8
|
-
if (pastFirstTime.current) {
|
|
9
|
-
return effect()
|
|
10
|
-
} else {
|
|
11
|
-
pastFirstTime.current = true
|
|
12
|
-
}
|
|
13
|
-
},inputs)
|
|
14
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import {useEffect} from 'react'
|
|
2
|
-
import {useForceUpdate} from '@startlibs/core'
|
|
3
|
-
import _ from 'lodash/fp'
|
|
4
|
-
|
|
5
|
-
export const useUpdateOnResize = (debounceInterval = 200) => {
|
|
6
|
-
const update = useForceUpdate()
|
|
7
|
-
useEffect(() => {
|
|
8
|
-
const trigger = _.debounce(debounceInterval, update)
|
|
9
|
-
window.addEventListener('resize', trigger)
|
|
10
|
-
return () => {
|
|
11
|
-
window.removeEventListener('resize', trigger)
|
|
12
|
-
}
|
|
13
|
-
},[])
|
|
14
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import React, {useState, useCallback} from 'react'
|
|
2
|
-
import {willUseSharedCallback} from '@startlibs/core'
|
|
3
|
-
import {callIfFunction} from '@startlibs/utils'
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export const willUseLayers = (useEffect = React.useEffect) => {
|
|
7
|
-
const [useRegisterSharedCallback,callGroup,getSize] = willUseSharedCallback(useEffect)
|
|
8
|
-
const useLayer = ({onFirst, onEmpty, onTop, onRemove} = {}, listener) => {
|
|
9
|
-
|
|
10
|
-
const resetIndex = useCallback(() => {
|
|
11
|
-
setIndex(getIndex())
|
|
12
|
-
if (getIndex()+1 === getSize()) {
|
|
13
|
-
callIfFunction(onTop,getIndex)
|
|
14
|
-
}
|
|
15
|
-
},[])
|
|
16
|
-
|
|
17
|
-
const getIndex = useRegisterSharedCallback(resetIndex, listener)
|
|
18
|
-
const [index,setIndex] = useState(getIndex())
|
|
19
|
-
|
|
20
|
-
useEffect(() => {
|
|
21
|
-
if (getIndex() === 0) {
|
|
22
|
-
callIfFunction(onFirst)
|
|
23
|
-
}
|
|
24
|
-
resetIndex()
|
|
25
|
-
return () => {
|
|
26
|
-
callIfFunction(onRemove,getSize())
|
|
27
|
-
if (getSize() === 0) {
|
|
28
|
-
callIfFunction(onEmpty)
|
|
29
|
-
}
|
|
30
|
-
callGroup()
|
|
31
|
-
}
|
|
32
|
-
},[])
|
|
33
|
-
|
|
34
|
-
return [index,{
|
|
35
|
-
isFirst: ()=> getIndex() === 1,
|
|
36
|
-
isTop: () => getIndex() === getSize()-1,
|
|
37
|
-
isEmpty: () => getSize() === 0,
|
|
38
|
-
getSize: () => getSize()
|
|
39
|
-
}]
|
|
40
|
-
}
|
|
41
|
-
return [useLayer,getSize]
|
|
42
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import React, {useRef} from 'react'
|
|
2
|
-
import {callMany, mapGetOrSet} from '@startlibs/utils'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const callList = (list, firstArg, ...args) => list.map(({current}) => typeof firstArg === 'function' ? firstArg(current) : current(firstArg,...args))
|
|
6
|
-
|
|
7
|
-
export const willUseSharedCallback = (useEffect = React.useEffect) => {
|
|
8
|
-
let mountedCallbacks = []
|
|
9
|
-
let listenerCallbacks = []
|
|
10
|
-
const remove = (ref) => mountedCallbacks = mountedCallbacks.filter(_ => _ !== ref)
|
|
11
|
-
const add = (ref) => mountedCallbacks = mountedCallbacks.concat([ref])
|
|
12
|
-
const get = () => mountedCallbacks
|
|
13
|
-
|
|
14
|
-
const removeListener = (ref) => listenerCallbacks = listenerCallbacks.filter(_ => _ !== ref)
|
|
15
|
-
const addListener = (ref) => listenerCallbacks = listenerCallbacks.concat([ref])
|
|
16
|
-
const getListeners = (ref) => listenerCallbacks
|
|
17
|
-
|
|
18
|
-
const lastCall = {current:null,index:0}
|
|
19
|
-
|
|
20
|
-
const call = (...args) => {
|
|
21
|
-
const resp = callList(get(), ...args)
|
|
22
|
-
callList(getListeners(), ...args)
|
|
23
|
-
lastCall.current = args
|
|
24
|
-
lastCall.index++
|
|
25
|
-
return resp
|
|
26
|
-
}
|
|
27
|
-
const getSize = () => get().length
|
|
28
|
-
|
|
29
|
-
const useRegister = (callback, listener = false) => {
|
|
30
|
-
const ref = useRef(callback)
|
|
31
|
-
if (ref.current !== callback) {
|
|
32
|
-
ref.current = callback
|
|
33
|
-
}
|
|
34
|
-
const lastCallIndex = lastCall.index
|
|
35
|
-
useEffect(() => {
|
|
36
|
-
if (!ref.current) { return }
|
|
37
|
-
if (listener) {
|
|
38
|
-
addListener(ref)
|
|
39
|
-
} else {
|
|
40
|
-
add(ref)
|
|
41
|
-
if (lastCallIndex !== lastCall.index && lastCall.current) {
|
|
42
|
-
callList([ref],...lastCall.current)
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return () => {
|
|
46
|
-
if (listener) {
|
|
47
|
-
removeListener(ref)
|
|
48
|
-
} else {
|
|
49
|
-
remove(ref)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}, [])
|
|
53
|
-
return () => get().indexOf(ref)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return [
|
|
57
|
-
useRegister,
|
|
58
|
-
call,
|
|
59
|
-
getSize
|
|
60
|
-
]
|
|
61
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import React, {useEffect, useState, useCallback} from 'react'
|
|
2
|
-
import {callMany,callIfFunction,ifUndefined} from '@startlibs/utils'
|
|
3
|
-
import {willUseSharedCallback} from '@startlibs/core'
|
|
4
|
-
|
|
5
|
-
const notDefined = {}
|
|
6
|
-
|
|
7
|
-
export const willUseSharedState = (initialValue) => {
|
|
8
|
-
const [useRegisterGroupCallback,callGroup,getSize] = willUseSharedCallback()
|
|
9
|
-
let sharedValue = ifUndefined(initialValue,notDefined)
|
|
10
|
-
|
|
11
|
-
const setGroupState = (newValue) => {
|
|
12
|
-
sharedValue = callIfFunction(newValue,sharedValue)
|
|
13
|
-
callGroup(sharedValue)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const refState = () => {
|
|
17
|
-
useRegisterGroupCallback()
|
|
18
|
-
return [()=>sharedValue,setGroupState]
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const valueState = (defaultValue) => {
|
|
22
|
-
const prevValue = sharedValue !== notDefined ? sharedValue : defaultValue
|
|
23
|
-
if (prevValue === defaultValue) {
|
|
24
|
-
sharedValue = defaultValue
|
|
25
|
-
}
|
|
26
|
-
const [value, setValue] = useState(prevValue)
|
|
27
|
-
useRegisterGroupCallback(setValue)
|
|
28
|
-
return [value,setGroupState]
|
|
29
|
-
}
|
|
30
|
-
const clear = () => {
|
|
31
|
-
sharedValue = notDefined
|
|
32
|
-
}
|
|
33
|
-
return [valueState,refState,setGroupState,()=>sharedValue,clear]
|
|
34
|
-
}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import React, {useCallback, useContext, useEffect, useMemo, useState} from 'react'
|
|
2
|
-
import {_s, callIfFunction, callMany, formatPath, identityFn, ifUndefined} from '@startlibs/utils'
|
|
3
|
-
import _ from 'lodash/fp'
|
|
4
|
-
import {useCatalogManager, useConsumerCatalog, useRefState, useIsUnmounted} from '@startlibs/core'
|
|
5
|
-
|
|
6
|
-
const updateAllInputsState = (inputs, values) => {
|
|
7
|
-
const deferedUndefined = []
|
|
8
|
-
inputs.forEach((input) => {
|
|
9
|
-
const [setValue, selector] = input
|
|
10
|
-
const newValue = selector(values)
|
|
11
|
-
if (newValue === undefined) {
|
|
12
|
-
deferedUndefined.push([setValue,newValue])
|
|
13
|
-
} else {
|
|
14
|
-
setValue(newValue)
|
|
15
|
-
}
|
|
16
|
-
})
|
|
17
|
-
deferedUndefined.forEach(([set,value])=> set(value))
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const pathSelector = (path) => _s.get(path || [])
|
|
21
|
-
const parsePathOrSelector = (pathOrSelector) => {
|
|
22
|
-
const isNotSelector = typeof pathOrSelector !== 'function'
|
|
23
|
-
if (isNotSelector) {
|
|
24
|
-
return [formatPath(pathOrSelector), pathSelector(pathOrSelector)]
|
|
25
|
-
} else {
|
|
26
|
-
return ['', pathOrSelector]
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
export const willUseSpreadState = (defaultContextValue,options = {}) => {
|
|
30
|
-
|
|
31
|
-
const Context = React.createContext(defaultContextValue)
|
|
32
|
-
const EMPTY_GLOBAL = {}
|
|
33
|
-
let prevInitState = EMPTY_GLOBAL
|
|
34
|
-
let globalState = EMPTY_GLOBAL
|
|
35
|
-
const getGlobalState = (key) => _.isString(key) || _.isArray(key) ? _.get(key,globalState) : globalState
|
|
36
|
-
|
|
37
|
-
const useProvideSpreadState = (initState, setCallback) => {
|
|
38
|
-
const [consumerList, manager] = useConsumerCatalog((input) => updateAllInputsState([input], state.get()))
|
|
39
|
-
const state = useRefState(initState, setCallback)
|
|
40
|
-
|
|
41
|
-
if (options.initRefCheck) {
|
|
42
|
-
if (prevInitState !== initState) {
|
|
43
|
-
globalState = initState
|
|
44
|
-
}
|
|
45
|
-
prevInitState = initState
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (globalState === EMPTY_GLOBAL) {
|
|
49
|
-
globalState = initState
|
|
50
|
-
}
|
|
51
|
-
useEffect(() => {
|
|
52
|
-
globalState = state.get()
|
|
53
|
-
},[])
|
|
54
|
-
|
|
55
|
-
const setState = (newState, fastPath, config) => {
|
|
56
|
-
// console.log("Fastpath noop:"+fastPath)
|
|
57
|
-
state.set(newState, config)
|
|
58
|
-
globalState = state.get()
|
|
59
|
-
updateAllInputsState(consumerList(), state.get())
|
|
60
|
-
}
|
|
61
|
-
const contextValue = useMemo(() => [state.get, setState, manager], [])
|
|
62
|
-
|
|
63
|
-
return [Context.Provider, contextValue, [state.get, setState]]
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const useSpreadState = (pathOrSelector, inputs) => {
|
|
67
|
-
const [getSpreadState, setSpreadState, manager] = useContext(Context)
|
|
68
|
-
const [path, selector] = parsePathOrSelector(pathOrSelector)
|
|
69
|
-
const pathOrInput = [path] || inputs || []
|
|
70
|
-
const [value, setValue] = useState(selector(getSpreadState()))
|
|
71
|
-
|
|
72
|
-
const isUnmounted = useIsUnmounted()
|
|
73
|
-
|
|
74
|
-
const currentPath = useRefState(path)
|
|
75
|
-
if (path !== currentPath.get()) {
|
|
76
|
-
/* when path changes, the consumerCatalogCallback will update the new value
|
|
77
|
-
* another solution would be to call setValue here directly
|
|
78
|
-
* the return value will still be the old, but will be changed before render, just like getDerivedStateFromProps
|
|
79
|
-
*
|
|
80
|
-
* if there's a need for the return value to be the same, the value or, if enters here new value,
|
|
81
|
-
* can be returned in an temp variable
|
|
82
|
-
* */
|
|
83
|
-
currentPath.set(path)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const input = useMemo(() => [(v) => {
|
|
87
|
-
/* noop for elements that may be unregistered while looping through consumerList */
|
|
88
|
-
if (path === currentPath.get() && !isUnmounted()) {
|
|
89
|
-
setValue(v)
|
|
90
|
-
}
|
|
91
|
-
}, selector], pathOrInput)
|
|
92
|
-
|
|
93
|
-
useCatalogManager(manager, input)
|
|
94
|
-
const setState = useCallback((v) => setSpreadState((_.isFunction(v) ? (pathOrSelector === undefined ? () => v : _.update) : _.set)(path, v), path), pathOrInput)
|
|
95
|
-
const getValue = () => selector(getGlobalState())
|
|
96
|
-
return [value, setState, getValue]
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return [useProvideSpreadState, useSpreadState, Context, getGlobalState]
|
|
101
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export * from './StartlibsProvider'
|
|
2
|
-
export * from './hooks/willUseLayers'
|
|
3
|
-
export * from './hooks/willUseSharedCallback'
|
|
4
|
-
export * from './hooks/willUseSharedState'
|
|
5
|
-
export * from './hooks/willUseSpreadState'
|
|
6
|
-
export * from './SlotFill'
|
|
7
|
-
export * from './hocs/consumes'
|
|
8
|
-
export * from './hocs/withDialogError'
|
|
9
|
-
export * from './hocs/withGroupEvent'
|
|
10
|
-
export * from './hocs/WithRef'
|
|
11
|
-
export * from './hocs/WithLazyResource'
|
|
12
|
-
export * from './hooks/useCallbackStable'
|
|
13
|
-
export * from './hooks/WithEffect'
|
|
14
|
-
export * from './hooks/useToggle'
|
|
15
|
-
export * from './hooks/usePopupToggle'
|
|
16
|
-
export * from './hooks/useCallState'
|
|
17
|
-
export * from './hooks/useRefState'
|
|
18
|
-
export * from './hooks/useConsumerCatalog'
|
|
19
|
-
export * from './hooks/useTitle'
|
|
20
|
-
export * from './hooks/useRecycleFocus'
|
|
21
|
-
export * from './hooks/usePrevious'
|
|
22
|
-
export * from './hooks/useIsUnmounted'
|
|
23
|
-
export * from './hooks/useEnsureRef'
|
|
24
|
-
export * from './hooks/useConstant'
|
|
25
|
-
export * from './hooks/useForceUpdate'
|
|
26
|
-
export * from './hooks/useUpdateOnResize'
|
|
27
|
-
export * from './hooks/useAsyncEffect'
|
|
28
|
-
export * from './hooks/useUpdateEffect'
|
|
29
|
-
export * from './hooks/useRefMemo'
|
|
30
|
-
export * from './hooks/useSignal'
|
|
31
|
-
export * from './hooks/WithConstant'
|