-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix hooks by tracking position This is still a naive implementation and will be improved to become more robust in the future. For now this should allow seamless integration of multiple hooks together. * Change signature of createSideEffect * Update docs
- Loading branch information
Showing
8 changed files
with
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,51 @@ | ||
import { $$hooks } from './util/symbols'; | ||
import { EMPTY, ActiveRenderState } from './util/types'; | ||
import { ActiveRenderState } from './util/types'; | ||
|
||
/** | ||
* Allow a function component to hook into lifecycle methods in a manner | ||
* consistent with class components. | ||
* consistent with class components. Meaning you can leverage existing lifecycle | ||
* events in a function component. | ||
* | ||
* @param {Function} sideEffectFn - A function that is called whenever the | ||
* component is mounted or updated. To invoke cleanup pass a second function | ||
* which will run whenever the component is removed. | ||
* @param {Function=} didMountOrUpdate - A function that is called whenever the | ||
* component is mounted. To hook into component updates, return a function. This | ||
* returned function will be called whenever the component updates. | ||
* | ||
* @param {Function=} unMount - A function that is called whenever a component | ||
* is unmounted. | ||
* | ||
* @returns {void} | ||
*/ | ||
export function createSideEffect(sideEffectFn) { | ||
export function createSideEffect(didMountOrUpdate, unMount) { | ||
if (ActiveRenderState.length === 0) { | ||
throw new Error('Cannot create side effect unless in render function'); | ||
} | ||
|
||
if (typeof sideEffectFn !== 'function') { | ||
if (typeof didMountOrUpdate !== 'function' && typeof unMount !== 'function') { | ||
throw new Error('Missing function for side effect'); | ||
} | ||
|
||
const [ activeComponent ] = ActiveRenderState; | ||
const activeHook = activeComponent[$$hooks].shift(); | ||
|
||
// Only do this the first time. | ||
if (!activeHook) { | ||
// First schedule a componentDidMount | ||
activeComponent.componentDidMount = activeComponent.componentDidUpdate = () => { | ||
const unMount = sideEffectFn() || EMPTY.FUN; | ||
// Schedule a componentDidMount if a function was provided | ||
if (typeof didMountOrUpdate === 'function') { | ||
activeComponent.componentDidMount = () => { | ||
const didUpdate = didMountOrUpdate(); | ||
|
||
if (typeof unMount === 'function') { | ||
activeComponent.componentWillUnmount = () => unMount(); | ||
// Then if the user specifies a return function, use that as didUpdate | ||
if (typeof didUpdate === 'function') { | ||
activeComponent.componentDidUpdate = () => { | ||
didUpdate(); | ||
}; | ||
} | ||
|
||
}; | ||
} | ||
|
||
// Return currentValue and setState. | ||
activeComponent[$$hooks].push(sideEffectFn); | ||
// Schedule a componentWillUnmount if a function is provided | ||
if (typeof unMount === 'function') { | ||
activeComponent.componentWillUnmount = () => unMount(); | ||
} | ||
|
||
// Increment the hooks count. | ||
activeComponent[$$hooks].i += 1; | ||
} |
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