UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

53 lines (51 loc) • 2.22 kB
function toNativeSourceStructs(structs) { const out = {}; for (const struct of structs ?? []) { out[struct.name] = { fields: struct.fields.map((field) => field.wire === void 0 ? { name: field.name, optional: field.optional } : { name: field.name, optional: field.optional, wire: field.wire }) }; } return out; } function methodWired(method) { return method.parameters.some((param) => param.wire) || method.resultWire !== void 0; } function methodSubscribed(method) { return method.subscription !== void 0; } function renderMethodCall(target, method, hash) { if (method.subscription) { const payload = method.subscription; const payloadWires = JSON.stringify(payload.payload.map((arg) => arg.wire ?? null)); return `${method.name}: (listener) => subscribeNativeSourceEvent(${JSON.stringify(target)}, ${JSON.stringify(method.name)}, listener, ${JSON.stringify(hash)}, ${payloadWires}, nativeSourceStructs)`; } if (!methodWired(method)) { return `${method.name}: (...args) => callNativeSource(${JSON.stringify(target)}, ${JSON.stringify(method.name)}, args, ${JSON.stringify(hash)})`; } const paramWires = JSON.stringify(method.parameters.map((param) => param.wire ?? null)); const resultWire = JSON.stringify(method.resultWire ?? null); return `${method.name}: (...args) => callNativeSource(${JSON.stringify(target)}, ${JSON.stringify(method.name)}, encodeNativeSourceArgs(args, ${paramWires}, nativeSourceStructs), ${JSON.stringify(hash)}).then((value) => decodeNativeSourceResult(value, ${resultWire}, nativeSourceStructs))`; } function renderViewWires(view, structs) { const props = {}; const callbacks = {}; for (const prop of view.props) { if (prop.wire) props[prop.name] = prop.wire; if (prop.callback && prop.callback.some((arg) => arg.wire)) { callbacks[prop.name] = prop.callback.map((arg) => arg.wire ?? null); } } if (Object.keys(props).length === 0 && Object.keys(callbacks).length === 0) return ""; return `, wires: ${JSON.stringify({ props, callbacks, structs: toNativeSourceStructs(structs) })}`; } export { methodSubscribed, methodWired, renderMethodCall, renderViewWires, toNativeSourceStructs }; //# sourceMappingURL=nativeSourceWires.mjs.map