one
Version:
One is a React framework for web, iOS, and Android apps.
79 lines (78 loc) • 3.2 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var nativeSourceWires_exports = {};
__export(nativeSourceWires_exports, {
methodSubscribed: () => methodSubscribed,
methodWired: () => methodWired,
renderMethodCall: () => renderMethodCall,
renderViewWires: () => renderViewWires,
toNativeSourceStructs: () => toNativeSourceStructs
});
module.exports = __toCommonJS(nativeSourceWires_exports);
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)
})}`;
}