|
| 1 | +import Reconciler from "react-reconciler"; |
| 2 | + |
| 3 | +const createHostConfig = callback => ({ |
| 4 | + now: Date.now, |
| 5 | + getRootHostContext: () => { |
| 6 | + let rootContext = {}; |
| 7 | + return rootContext; |
| 8 | + }, |
| 9 | + getChildHostContext: (parentContext, fiberType) => { |
| 10 | + let context = { type: fiberType }; |
| 11 | + return context; |
| 12 | + }, |
| 13 | + shouldSetTextContent: () => { |
| 14 | + return false; |
| 15 | + }, |
| 16 | + createTextInstance: newText => { |
| 17 | + return { type: "text", value: newText }; |
| 18 | + }, |
| 19 | + createInstance: (type, newProps) => { |
| 20 | + return Object.keys(newProps) |
| 21 | + .filter(key => key !== "children") |
| 22 | + .reduce( |
| 23 | + (acc, key) => { |
| 24 | + acc[key] = newProps[key]; |
| 25 | + return acc; |
| 26 | + }, |
| 27 | + { type } |
| 28 | + ); |
| 29 | + }, |
| 30 | + appendChildToContainer: (parent, child) => { |
| 31 | + if (!parent.children) { |
| 32 | + parent.children = []; |
| 33 | + } |
| 34 | + |
| 35 | + parent.children.push(child); |
| 36 | + }, |
| 37 | + finalizeInitialChildren: () => { |
| 38 | + return false; |
| 39 | + }, |
| 40 | + prepareForCommit: () => {}, |
| 41 | + resetAfterCommit: () => {}, |
| 42 | + commitMount: () => {}, |
| 43 | + appendInitialChild: (parent, child) => { |
| 44 | + if (!parent.children) { |
| 45 | + parent.children = []; |
| 46 | + } |
| 47 | + |
| 48 | + parent.children.push(child); |
| 49 | + }, |
| 50 | + supportsMutation: true, |
| 51 | + removeChildFromContainer: (container, child) => { |
| 52 | + if (container.children) { |
| 53 | + container.children.splice(container.children.indexOf(child), 1); |
| 54 | + } |
| 55 | + }, |
| 56 | + prepareUpdate: () => {}, |
| 57 | + commitTextUpdate: (textInstance, oldText, newText) => { |
| 58 | + textInstance.value = newText; |
| 59 | + callback(); |
| 60 | + }, |
| 61 | + commitUpdate: () => { |
| 62 | + callback(); |
| 63 | + }, |
| 64 | + appendChild: (parentInstance, child) => { |
| 65 | + parentInstance.children.push(child); |
| 66 | + }, |
| 67 | + removeChild: (parentInstance, child) => { |
| 68 | + parentInstance.children.splice(parentInstance.children.indexOf(child), 1); |
| 69 | + }, |
| 70 | + resetTextContent: () => {}, |
| 71 | + shouldDeprioritizeSubtree: () => { |
| 72 | + return false; |
| 73 | + }, |
| 74 | + insertInContainerBefore: (container, child, beforeChild) => { |
| 75 | + container.children.splice( |
| 76 | + container.children.indexOf(beforeChild) - 1, |
| 77 | + 0, |
| 78 | + child |
| 79 | + ); |
| 80 | + }, |
| 81 | + insertBefore: (parentInstance, child, beforeChild) => { |
| 82 | + parentInstance.children.splice( |
| 83 | + parentInstance.children.indexOf(beforeChild) - 1, |
| 84 | + 0, |
| 85 | + child |
| 86 | + ); |
| 87 | + } |
| 88 | +}); |
| 89 | + |
| 90 | +const ReactJSON = { |
| 91 | + mount(element, renderDom, callback) { |
| 92 | + const isAsync = false; |
| 93 | + const reconcilerInstance = Reconciler(createHostConfig(callback)); |
| 94 | + const container = reconcilerInstance.createContainer(renderDom, isAsync); |
| 95 | + |
| 96 | + const parentComponent = null; |
| 97 | + reconcilerInstance.updateContainer( |
| 98 | + element, |
| 99 | + container, |
| 100 | + parentComponent, |
| 101 | + callback |
| 102 | + ); |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +export default ReactJSON; |
0 commit comments