-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
12 changed files
with
159 additions
and
93 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
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
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,34 +1,9 @@ | ||
import { detectIsUndefined, detectIsDepsDifferent } from '../helpers'; | ||
import { componentFiberHelper } from '../scope'; | ||
import { useMemo } from '../use-memo'; | ||
|
||
function useCallback<T = Function>(callback: T, deps: Array<any>): T { | ||
const fiber = componentFiberHelper.get(); | ||
const { hook } = fiber; | ||
const { idx, values } = hook; | ||
const value = useMemo(() => callback, deps); | ||
|
||
if (detectIsUndefined(values[idx])) { | ||
values[idx] = { | ||
deps, | ||
value: callback, | ||
}; | ||
|
||
hook.idx++; | ||
|
||
return callback; | ||
} | ||
|
||
const hookValue = values[idx]; | ||
const prevDeps = hookValue.deps as Array<any>; | ||
const isDepsDifferent = detectIsDepsDifferent(deps, prevDeps); | ||
|
||
if (isDepsDifferent) { | ||
hookValue.deps = deps; | ||
hookValue.value = callback; | ||
} | ||
|
||
hook.idx++; | ||
|
||
return hookValue.value; | ||
return value; | ||
} | ||
|
||
export { useCallback }; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './use-event'; |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useMemo } from '../use-memo'; | ||
import { useCallback } from '../use-callback'; | ||
|
||
function useEvent<T extends (...args) => any>(fn: T) { | ||
const scope = useMemo(() => ({ fn }), []); | ||
|
||
scope.fn = fn; | ||
|
||
const callback = useCallback((...args) => { | ||
return scope.fn(...args); | ||
}, []); | ||
|
||
return callback as unknown as T; | ||
} | ||
|
||
export { useEvent }; |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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