-
Notifications
You must be signed in to change notification settings - Fork 623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP]Feat Suspense #2307
Open
chenjun1011
wants to merge
11
commits into
master
Choose a base branch
from
feat-suspense
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[WIP]Feat Suspense #2307
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
20654ef
feat: suspense
chenjun1011 10c67df
chore: has update error
chenjun1011 54961a8
fix: update
chenjun1011 ba1c171
fix: rerender
chenjun1011 0ca713c
refactor: mountComponet
chenjun1011 74fc87a
refactor: mountComponet
chenjun1011 76ff6cb
refactor: error hanlder
chenjun1011 015bc75
fix: unmount fallback
chenjun1011 489d71d
feat: lazy
chenjun1011 a250836
chore: remove dead file
chenjun1011 7dfadaa
revert: watch config
chenjun1011 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { LAZY_TYPE } from './constant'; | ||
|
||
const Uninitialized = -1; | ||
const Pending = 0; | ||
const Resolved = 1; | ||
const Rejected = 2; | ||
|
||
function lazyInitializer(payload) { | ||
if (payload._status === Uninitialized) { | ||
const ctor = payload._result; | ||
const thenable = ctor(); // Transition to the next state. | ||
|
||
const pending = payload; | ||
pending._status = Pending; | ||
pending._result = thenable; | ||
thenable.then((moduleObject) => { | ||
if (payload._status === Pending) { | ||
const defaultExport = moduleObject.default; | ||
const resolved = payload; | ||
resolved._status = Resolved; | ||
resolved._result = defaultExport; | ||
} | ||
}, (error) => { | ||
if (payload._status === Pending) { | ||
// Transition to the next state. | ||
const rejected = payload; | ||
rejected._status = Rejected; | ||
rejected._result = error; | ||
} | ||
}); | ||
} | ||
|
||
if (payload._status === Resolved) { | ||
return payload._result; | ||
} else { | ||
throw payload._result; | ||
} | ||
} | ||
|
||
export default function lazy(ctor) { | ||
var payload = { | ||
_status: -1, | ||
_result: ctor | ||
}; | ||
|
||
var lazyType = { | ||
$$typeof: LAZY_TYPE, | ||
_payload: payload, | ||
_init: lazyInitializer | ||
}; | ||
|
||
return lazyType; | ||
} |
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,31 @@ | ||
import Host from './host'; | ||
import toArray from '../toArray'; | ||
|
||
export default function(prevNativeNode) { | ||
let lastNativeNode = null; | ||
|
||
return (newNativeNode, parent) => { | ||
const driver = Host.driver; | ||
|
||
prevNativeNode = toArray(prevNativeNode); | ||
newNativeNode = toArray(newNativeNode); | ||
|
||
// If the new length large then prev | ||
for (let i = 0; i < newNativeNode.length; i++) { | ||
let nativeNode = newNativeNode[i]; | ||
if (prevNativeNode[i]) { | ||
driver.replaceChild(nativeNode, prevNativeNode[i]); | ||
} else if (lastNativeNode) { | ||
driver.insertAfter(nativeNode, lastNativeNode); | ||
} else { | ||
driver.appendChild(nativeNode, parent); | ||
} | ||
lastNativeNode = nativeNode; | ||
} | ||
|
||
// If the new length less then prev | ||
for (let i = newNativeNode.length; i < prevNativeNode.length; i++) { | ||
driver.removeChild(prevNativeNode[i]); | ||
} | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import BaseComponent from './base'; | ||
import instantiateComponent from './instantiateComponent'; | ||
import { INSTANCE, INTERNAL, RENDERED_COMPONENT, LAZY_TYPE } from '../constant'; | ||
import updater from './updater'; | ||
import performInSandbox from './performInSandbox'; | ||
import getNewNativeNodeMounter from './getNewNativeNodeMounter'; | ||
import shouldUpdateComponent from './shouldUpdateComponent'; | ||
|
||
/** | ||
* Suspense Component | ||
*/ | ||
class SuspenseComponent extends BaseComponent { | ||
__mountComponent(parent, parentInstance, context, nativeNodeMounter) { | ||
this.__initComponent(parent, parentInstance, context); | ||
|
||
const currentElement = this.__currentElement; | ||
const publicProps = currentElement.props; | ||
|
||
let instance = this[INSTANCE] = {}; | ||
instance[INTERNAL] = this; | ||
|
||
instance.props = publicProps; | ||
instance.context = context; | ||
instance.updater = updater; | ||
instance.type = currentElement.type; | ||
instance.nativeNodeMounter = nativeNodeMounter; | ||
|
||
instance.__handleError = this.__handleError; | ||
|
||
performInSandbox(() => { | ||
const renderedElement = publicProps.children; | ||
const renderedComponent = instantiateComponent(renderedElement); | ||
renderedComponent.__mountComponent( | ||
parent, | ||
instance, | ||
context, | ||
nativeNodeMounter | ||
); | ||
|
||
if (this.__didCapture) { | ||
// Unmount the broken component. | ||
// Component render error will be caught by sandbox. | ||
renderedComponent.unmountComponent(true); | ||
} else { | ||
this[RENDERED_COMPONENT] = renderedComponent; | ||
} | ||
}, instance, (error) => { | ||
this.__handleError(instance, error); | ||
}); | ||
|
||
return instance; | ||
} | ||
|
||
__handleError(instance, error) { | ||
if (!error.then) { | ||
throw error; | ||
} | ||
|
||
const { fallback, children } = instance.props; | ||
const internal = instance[INTERNAL]; | ||
|
||
const wakeable = error; | ||
wakeable.then(() => { | ||
performInSandbox(() => { | ||
if (!instance.__didMount) { | ||
const prevRenderedComponent = internal[RENDERED_COMPONENT]; | ||
internal.__mountRenderedComponent(children); | ||
prevRenderedComponent.unmountComponent(true); | ||
|
||
instance.__didMount = true; | ||
} else { | ||
instance.updater.forceUpdate(instance); | ||
} | ||
}, instance, (e) => { | ||
instance.__handleError(instance, e); | ||
}); | ||
}); | ||
|
||
if (!internal.__didCapture && !instance.__didMount) { | ||
internal.__mountRenderedComponent(fallback); | ||
internal.__didCapture = true; | ||
} | ||
} | ||
|
||
__mountRenderedComponent(component) { | ||
const prevRenderedComponent = this[RENDERED_COMPONENT]; | ||
|
||
let nativeNodeMounter = this[INSTANCE].nativeNodeMounter; | ||
|
||
if (prevRenderedComponent && prevRenderedComponent.__currentElement) { | ||
const prevNativeNode = prevRenderedComponent.__getNativeNode(); | ||
nativeNodeMounter = getNewNativeNodeMounter(prevNativeNode); | ||
} | ||
|
||
this[RENDERED_COMPONENT] = instantiateComponent(component); | ||
this[RENDERED_COMPONENT].__mountComponent( | ||
this._parent, | ||
this[INSTANCE], | ||
this._context, | ||
nativeNodeMounter | ||
); | ||
} | ||
|
||
__getNativeNode() { | ||
let renderedComponent = this[RENDERED_COMPONENT]; | ||
if (renderedComponent) { | ||
return renderedComponent.__getNativeNode(); | ||
} | ||
} | ||
|
||
__updateComponent( | ||
prevElement, | ||
nextElement, | ||
prevUnmaskedContext, | ||
nextUnmaskedContext, | ||
) { | ||
let instance = this[INSTANCE]; | ||
|
||
// Maybe update component that has already been unmounted or failed mount. | ||
if (!instance) { | ||
return; | ||
} | ||
|
||
performInSandbox(() => { | ||
let prevRenderedComponent = this[RENDERED_COMPONENT]; | ||
let prevRenderedElement = prevRenderedComponent.__currentElement; | ||
|
||
// Replace with next | ||
this.__currentElement = nextElement; | ||
this._context = nextUnmaskedContext; | ||
|
||
instance.props = nextElement.props; | ||
instance.context = nextUnmaskedContext; | ||
|
||
const { children } = nextElement.props || {}; | ||
let nextRenderedElement = children; | ||
|
||
if (children && children.$$typeof === LAZY_TYPE) { | ||
const payload = children._payload; | ||
const init = children._init; | ||
nextRenderedElement = init(payload); | ||
} | ||
|
||
if (shouldUpdateComponent(prevRenderedElement, nextRenderedElement)) { | ||
prevRenderedComponent.__updateComponent( | ||
prevRenderedElement, | ||
nextRenderedElement, | ||
prevUnmaskedContext, | ||
nextUnmaskedContext | ||
); | ||
} else { | ||
prevRenderedComponent.unmountComponent(true); | ||
this.__mountRenderedComponent(nextRenderedElement); | ||
} | ||
}, instance, (error) => { | ||
this.__handleError(instance, error); | ||
}); | ||
} | ||
} | ||
|
||
export default SuspenseComponent; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const export = moduleObject.default || moduleObject;
to support commonjs module?