Skip to content

Instantly share code, notes, and snippets.

@koba04
Last active May 14, 2022 16:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koba04/19e896afc276a2eac7d9e0660026f16d to your computer and use it in GitHub Desktop.
Save koba04/19e896afc276a2eac7d9e0660026f16d to your computer and use it in GitHub Desktop.
/**
* @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;
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