-
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
005d965
commit 8446dbf
Showing
9 changed files
with
174 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
createElement, | ||
defineComponent, | ||
useCallback, | ||
useState, | ||
Boundary | ||
} from 'js-surface'; | ||
|
||
const ErrorTrigger = defineComponent({ | ||
displayName: 'ErrorTrigger', | ||
|
||
render() { | ||
const | ||
[errorMsg, setErrorMsg] = useState(null), | ||
onButtonClick = useCallback(() => setErrorMsg('Simulated error!')) | ||
|
||
if (errorMsg) { | ||
throw new Error(errorMsg); | ||
} | ||
|
||
return ( | ||
<button onClick={onButtonClick}> | ||
Click to trigger errror | ||
</button> | ||
) | ||
} | ||
}) | ||
|
||
const ErrorBoundary = defineComponent({ | ||
displayName: 'ErrorBoundary', | ||
|
||
render(props) { | ||
const | ||
[error, setError] = useState(null), | ||
|
||
handleReset = useCallback(() => { | ||
setError(null) | ||
}), | ||
|
||
handleError = useCallback((error: any, info: any) => { | ||
console.log(error) | ||
console.log('Error info: ', info) | ||
setError(error) | ||
}) | ||
|
||
return ( | ||
<Boundary handle={handleError}> | ||
{ | ||
error | ||
? <div> | ||
Catched error: <i>{error.message} </i> | ||
<button onClick={handleReset}>Reset</button> | ||
</div> | ||
: <ErrorTrigger/> | ||
} | ||
</Boundary> | ||
) | ||
} | ||
}) | ||
|
||
export default <ErrorBoundary/> |
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,40 @@ | ||
import defineComponent from './defineComponent' | ||
import isNode from './isNode' | ||
|
||
type BoundaryProps = { | ||
handle: (exception: any) => void, | ||
children?: any // TODO | ||
} | ||
|
||
const Boundary = defineComponent<BoundaryProps>({ | ||
displayName: 'Boundary', | ||
|
||
properties: { | ||
handle: { | ||
type: Function, | ||
required: true | ||
}, | ||
|
||
children: { | ||
validate: isNode | ||
} | ||
}, | ||
|
||
render(props) { | ||
delegate = delegate || (Boundary as any).__apply | ||
|
||
if (!delegate) { | ||
throw new Error('[Boundary] Adapter has not been initialized') | ||
} | ||
|
||
return delegate(props) | ||
} | ||
}) | ||
|
||
// --- locals ------------------------------------------------------- | ||
|
||
let delegate: any = null // TODO | ||
|
||
//--- exports ------------------------------------------------------- | ||
|
||
export default Boundary |
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