|
| 1 | +import React, { |
| 2 | + useReducer, |
| 3 | + useEffect, |
| 4 | + useState, |
| 5 | + useMemo, |
| 6 | + Suspense |
| 7 | +} from "./web_modules/react.js"; |
| 8 | +import htm from './web_modules/htm.js' |
| 9 | +import serializeEvent from "./event-to-object.js"; |
| 10 | + |
| 11 | +const html = htm.bind(React.createElement); |
| 12 | + |
| 13 | +const allUpdateTriggers = {}; |
| 14 | +const allModels = {}; |
| 15 | + |
| 16 | +function updateDynamicElement(elementId) { |
| 17 | + if (allUpdateTriggers.hasOwnProperty(elementId)) { |
| 18 | + allUpdateTriggers[elementId](); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +function Layout({ endpoint }) { |
| 23 | + // handle relative endpoint URI |
| 24 | + if (endpoint.startsWith(".") || endpoint.startsWith("/")) { |
| 25 | + let loc = window.location; |
| 26 | + let protocol; |
| 27 | + if (loc.protocol === "https:") { |
| 28 | + protocol = "wss:"; |
| 29 | + } else { |
| 30 | + protocol = "ws:"; |
| 31 | + } |
| 32 | + let new_uri = protocol + "//" + loc.host; |
| 33 | + if (endpoint.startsWith(".")) { |
| 34 | + new_url += loc.pathname + "/"; |
| 35 | + } |
| 36 | + endpoint = new_uri + endpoint; |
| 37 | + } |
| 38 | + |
| 39 | + const socket = useMemo(() => { |
| 40 | + return new WebSocket(endpoint); |
| 41 | + }, [endpoint]); |
| 42 | + |
| 43 | + const [root, setRoot] = useState(null); |
| 44 | + |
| 45 | + socket.onmessage = event => { |
| 46 | + const msg = JSON.parse(event.data); |
| 47 | + Object.assign(allModels, msg.body.render.new); |
| 48 | + msg.body.render.old.forEach(elementId => { |
| 49 | + delete allModels[elementId]; |
| 50 | + }); |
| 51 | + updateDynamicElement(msg.body.render.src); |
| 52 | + if (!root) { |
| 53 | + setRoot(msg.body.render.root); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + const sendMsg = msg => { |
| 58 | + socket.send(JSON.stringify(msg)); |
| 59 | + }; |
| 60 | + const sendEvent = event => { |
| 61 | + sendMsg({ |
| 62 | + header: {}, |
| 63 | + body: { event: event } |
| 64 | + }); |
| 65 | + }; |
| 66 | + |
| 67 | + if (root) { |
| 68 | + return html`<DynamicElement elementId={root} sendEvent={sendEvent} />`; |
| 69 | + } else { |
| 70 | + return html`<div />`; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function DynamicElement({ elementId, sendEvent }) { |
| 75 | + allUpdateTriggers[elementId] = useForceUpdate(); |
| 76 | + const model = allModels[elementId]; |
| 77 | + if (model) { |
| 78 | + return html`<Element model={model} sendEvent={sendEvent} />`; |
| 79 | + } else { |
| 80 | + return html`<div />`; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function Element({ model, sendEvent }) { |
| 85 | + const children = elementChildren(model, sendEvent); |
| 86 | + const attributes = elementAttributes(model, sendEvent); |
| 87 | + if (model.importSource) { |
| 88 | + const lazy = lazyComponent(model); |
| 89 | + return html` |
| 90 | + <Suspense fallback={model.importSource.fallback}> |
| 91 | + {React.createElement(lazy, attributes, children)} |
| 92 | + </Suspense> |
| 93 | + `; |
| 94 | + } else if (model.children && model.children.length) { |
| 95 | + return React.createElement(model.tagName, attributes, children); |
| 96 | + } else { |
| 97 | + return React.createElement(model.tagName, attributes); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +function elementChildren(model, sendEvent) { |
| 102 | + if (!model.children) { |
| 103 | + return []; |
| 104 | + } else { |
| 105 | + return model.children.map(child => { |
| 106 | + switch (child.type) { |
| 107 | + case "ref": |
| 108 | + return html` |
| 109 | + <DynamicElement |
| 110 | + elementId={child.data} |
| 111 | + sendEvent={sendEvent} |
| 112 | + /> |
| 113 | + `; |
| 114 | + case "obj": |
| 115 | + return html`<Element model={child.data} sendEvent={sendEvent} />`; |
| 116 | + case "str": |
| 117 | + return child.data; |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function elementAttributes(model, sendEvent) { |
| 124 | + const attributes = Object.assign({}, model.attributes); |
| 125 | + |
| 126 | + if (model.eventHandlers) { |
| 127 | + Object.keys(model.eventHandlers).forEach(eventName => { |
| 128 | + const eventSpec = model.eventHandlers[eventName]; |
| 129 | + attributes[eventName] = function(event) { |
| 130 | + const data = Array.from(arguments).map(value => { |
| 131 | + if (typeof value === "object" && value.nativeEvent) { |
| 132 | + if (eventSpec["preventDefault"]) { |
| 133 | + value.preventDefault(); |
| 134 | + } |
| 135 | + if (eventSpec["stopPropagation"]) { |
| 136 | + value.stopPropagation(); |
| 137 | + } |
| 138 | + return serializeEvent(value); |
| 139 | + } else { |
| 140 | + return value; |
| 141 | + } |
| 142 | + }); |
| 143 | + const sentEvent = new Promise((resolve, reject) => { |
| 144 | + const msg = { |
| 145 | + data: data, |
| 146 | + target: eventSpec["target"] |
| 147 | + }; |
| 148 | + sendEvent(msg); |
| 149 | + resolve(msg); |
| 150 | + }); |
| 151 | + }; |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + return attributes; |
| 156 | +} |
| 157 | +function useForceUpdate() { |
| 158 | + const [, setState] = useState(true); |
| 159 | + const forceUpdate = () => { |
| 160 | + setState(state => !state); |
| 161 | + }; |
| 162 | + return forceUpdate; |
| 163 | +} |
| 164 | + |
| 165 | +export default Layout; |
0 commit comments