-
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.
Removed implicit use of iterators from internals
- Loading branch information
1 parent
348677c
commit f1e610c
Showing
8 changed files
with
38 additions
and
30 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 was deleted.
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
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,19 +1,31 @@ | ||
import { DependencyList, useRef } from 'react'; | ||
import is from './is'; | ||
|
||
const intialValue: [any, DependencyList | undefined] = [undefined, undefined]; | ||
|
||
/** @ignore */ | ||
function dependencyListIs(a: DependencyList | undefined, b: DependencyList | undefined): boolean { | ||
if (!a || !b || a.length !== b.length) return false; | ||
|
||
for (let i = 0; i < a.length; ++i) { | ||
if (!Object.is(a[i], b[i])) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @ignore | ||
* An alternative to React useMemo that provides a semantic guarantee of referential stability. | ||
* This allows synchronous effects to be invoked safely during the render phase. | ||
* @param factory The factory function to compute a referentially stable value | ||
* @param deps The dependencies of the computation | ||
*/ | ||
export default function useSync<T>(factory: () => T, deps: DependencyList | undefined): T { | ||
const { current } = useRef<[T?, DependencyList?]>([]); | ||
const ref = useRef<[T, DependencyList | undefined]>(intialValue); | ||
|
||
if (!is(current[1], deps)) { | ||
current[0] = factory(); | ||
current[1] = deps; | ||
if (!dependencyListIs(ref.current[1], deps)) { | ||
ref.current = [factory(), deps]; | ||
} | ||
|
||
return current[0]!; | ||
return ref.current[0]; | ||
} |
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