-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63679df
commit 80f535e
Showing
2 changed files
with
39 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,49 @@ | ||
import Component from './types/Component' | ||
import useState from './useState' | ||
import useEffect from './useEffect' | ||
import { VirtualNode } from '../../../../modules/core/main' | ||
import useForceUpdate from './useForceUpdate' | ||
|
||
type Data = Record<string, any> | ||
type Getters<T extends Data> = { [K in keyof T]: () => T[K]} | ||
type View<T extends Data> = (render: (data: T) => VirtualNode) => () => VirtualNode | ||
|
||
function useData<T extends { [K in keyof T]: () => ReturnType<T[K]> }>( | ||
c: Component, | ||
source: T, | ||
action: (current: { [K in keyof T]: ReturnType<T[K]> }, previous: { [K in keyof T]: ReturnType<T[K]>}) => void | ||
): [ { [K in keyof T]: ReturnType<T[K]> }, { [K in keyof T]: ReturnType<T[K]> } ] | ||
|
||
function useData<T>(c: Component, get: () => T, action: (current: T, previous: T) => void): [T, T | undefined] | ||
|
||
function useData(c: Component, source: any, action: (current: any, previous: any) => void) { | ||
let ret: any | ||
|
||
if (typeof source === 'function') { | ||
ret = useSingleData(c, source, action) | ||
} else { | ||
ret = useMultiData(c, source, action) | ||
} | ||
|
||
return ret | ||
} | ||
|
||
export default useData | ||
|
||
// --- local -------------------------------------------------------- | ||
function useData<T extends Data>(c: Component, getters: Getters<T>): [T, T, View<T>] { | ||
const | ||
curr: T = {} as T, | ||
prev: T = {} as T, | ||
view: View<T> = (render: (data: T) => VirtualNode) => () => render(curr), | ||
[, setDummy] = useState(c, false) | ||
|
||
function useSingleData<T>(c: Component, get: () => T, action: (current: T, previous: T) => void): [T, T | undefined] { | ||
let | ||
current: T = get(), | ||
previous: T = undefined | ||
updateDataObject(curr, getters) | ||
clearDataObject(prev, getters) | ||
|
||
useEffect(c, () => { | ||
previous = current | ||
current = get() | ||
|
||
action(current, previous) | ||
setDummy((it: boolean) => { | ||
Object.assign(prev, curr) | ||
updateDataObject(curr, getters) | ||
return !it | ||
}) | ||
}) | ||
|
||
action(current, undefined) | ||
|
||
return [current, undefined] | ||
return [curr, prev, view] | ||
} | ||
|
||
function useMultiData<T>(c: Component, source: { [key: string]: () => any}, action: (current: T, previous: T) => void): [T, T | undefined] { | ||
const | ||
keys = Object.keys(source) | ||
|
||
function get() { | ||
const ret: any = {} | ||
|
||
for (let i = 0; i < keys.length; ++i) { | ||
const key = keys[i] | ||
|
||
ret[key] = source[key]() | ||
function updateDataObject(obj: any, getters: any) { | ||
for (let propName in getters) { | ||
if (getters.hasOwnProperty(propName)) { | ||
obj[propName] = getters[propName]() | ||
} | ||
|
||
return ret | ||
} | ||
} | ||
|
||
let | ||
current: any = get(), | ||
previous: any = undefined | ||
|
||
useEffect(c, () => { | ||
previous = current | ||
current = get() | ||
|
||
action(current, previous) | ||
}) | ||
|
||
action(current, undefined) | ||
|
||
return [current, undefined] | ||
function clearDataObject(obj: any, getters: any) { | ||
for (let propName in getters) { | ||
if (getters.hasOwnProperty(propName)) { | ||
obj[propName] = undefined | ||
} | ||
} | ||
} | ||
|
||
export default useData |