Last active
May 14, 2022 16:03
-
-
Save koba04/19e896afc276a2eac7d9e0660026f16d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* @providesModule ReactVoice | |
*/ | |
'use strict'; | |
const {spawnSync} = require('child_process'); | |
const ReactDOMFrameScheduling = require('ReactDOMFrameScheduling'); | |
const ReactFiberReconciler = require('ReactFiberReconciler'); | |
const sideEffect = (method, text) => spawnSync('say', ['-v', method, text]); | |
const VoiceRenderer = ReactFiberReconciler({ | |
getRootHostContext() { | |
return {}; | |
}, | |
getChildHostContext() { | |
return {}; | |
}, | |
getPublicInstance(instance) { | |
return null; | |
}, | |
createInstance(type, props) { | |
return {}; | |
}, | |
appendInitialChild(parentInstance, child) {}, | |
finalizeInitialChildren(host, type, props) { | |
if (typeof props.children === 'string') { | |
sideEffect(type, props.children); | |
} | |
return false; | |
}, | |
prepareUpdate(instance, type, oldProps, newProps) { | |
return {}; | |
}, | |
commitMount(instance, type, newProps) {}, | |
commitUpdate(instance, updatePayload, type, oldProps, newProps) { | |
if (typeof newProps.children === 'string') { | |
if (newProps.children !== oldProps.children) { | |
sideEffect(type, newProps.children); | |
} | |
} | |
}, | |
shouldSetTextContent(type, props) {}, | |
resetTextContent(instance) {}, | |
shouldDeprioritizeSubtree(type, props) {}, | |
createTextInstance(text, rootContainerInstance, hostContext, internalInstanceHandle) {}, | |
commitTextUpdate(textInstance, oldText, newText) {}, | |
appendChild(parentInstance, child) {}, | |
appendChildToContainer(parentInstance, child) {}, | |
insertBefore(parentInstance, child, beforeChild) {}, | |
insertInContainerBefore(container, child, beforeChild) {}, | |
removeChild(parentInstance, child) {}, | |
removeChildFromContainer(container, child) {}, | |
prepareForCommit() {}, | |
resetAfterCommit() {}, | |
scheduleDeferredCallback: ReactDOMFrameScheduling.rIC, | |
useSyncScheduling: true, | |
}); | |
let root; | |
const ReactVoice = { | |
render(element, callback) { | |
if (!root) { | |
const container = {}; | |
root = VoiceRenderer.createContainer(container); | |
} | |
VoiceRenderer.updateContainer(element, root, null, callback); | |
}, | |
}; | |
module.exports = ReactVoice; |
This file contains hidden or 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
const React = require('react'); | |
const ReactVoice = require('ReactVoice'); | |
describe('ReactVoice', () => { | |
it('should say a text', () => { | |
ReactVoice.render([ | |
<alex key={1}>Hello</alex>, | |
<victoria key={2}>React Fiber</victoria>, | |
<kyoko key={3}>こんにちは HTML5 Conference</kyoko>, | |
]); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment