UNPKG

lingo3d

Version:

Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor

101 lines 4.27 kB
import { isPoint } from "../../utils/isPoint"; import getDefaultValue from "../../interface/utils/getDefaultValue"; import unsafeGetValue from "../../utils/unsafeGetValue"; import unsafeSetValue from "../../utils/unsafeSetValue"; import { extendFunction, omitFunction } from "@lincode/utils"; import NullableCallback from "../../interface/utils/NullableCallback"; import getStaticProperties from "../../display/utils/getStaticProperties"; import { isDefaultMethodArg, defaultMethodArgs, isNullableCallbackParam, nullableCallbackParams } from "../../collections/typeGuards"; import { disableSchema } from "../../collections/disableSchema"; import { runtimeSchemaMap } from "../../collections/runtimeCollections"; export class PassthroughCallback { callback; handle; constructor(callback, handle) { this.callback = callback; this.handle = handle; } } const proxyInstanceMap = new WeakMap(); const setProxyInstance = (proxy, instance) => { proxyInstanceMap.set(proxy, instance); return proxy; }; export const getOriginalInstance = (manager) => proxyInstanceMap.get(manager) ?? manager; const filterSchema = (schema, runtimeSchema, includeKeys) => { if (!includeKeys) return { ...schema, ...runtimeSchema }; const result = {}; for (const key of includeKeys) result[key] = schema[key] ?? runtimeSchema?.[key]; return result; }; const isObject = (val) => val && typeof val === "object" && !Array.isArray(val); const structuredCloneDefaultValue = (val) => { if (isNullableCallbackParam(val)) { const cloned = structuredClone(val); nullableCallbackParams.add(cloned); return [cloned, true]; } if (isDefaultMethodArg(val)) { const cloned = structuredClone(val); defaultMethodArgs.add(cloned); return [cloned, true]; } return [structuredClone(val), false]; }; export default (manager, includeKeys, skipFunctions) => { const { schema } = getStaticProperties(manager); if (!schema) return [{}, manager]; const schemaKeyNullableCallbackParamMap = new Map(); const schemaKeyDefaultMethodArgMap = new Map(); const rawParams = {}; const methodParams = {}; const callbackParams = {}; for (const schemaKey of Object.keys(filterSchema(schema, runtimeSchemaMap.get(manager), includeKeys))) { if (disableSchema.has(schemaKey)) continue; const functionPtr = [undefined]; const [defaultValue, isDefaultValue] = structuredCloneDefaultValue(getDefaultValue(manager, schemaKey, true, true, functionPtr)); if (isObject(defaultValue) && !isPoint(defaultValue) && !isDefaultValue) continue; const [fn] = functionPtr; if (skipFunctions && fn) continue; if (fn instanceof NullableCallback) { schemaKeyNullableCallbackParamMap.set(schemaKey, fn.param); callbackParams[schemaKey] = defaultValue; continue; } if (fn) { schemaKeyDefaultMethodArgMap.set(schemaKey, fn.arg); methodParams[schemaKey] = defaultValue; continue; } rawParams[schemaKey] = defaultValue; } return [ { ...rawParams, ...callbackParams, ...methodParams }, setProxyInstance(new Proxy(manager, { get(_, prop) { if (schemaKeyNullableCallbackParamMap.has(prop)) return schemaKeyNullableCallbackParamMap.get(prop); if (schemaKeyDefaultMethodArgMap.has(prop)) return schemaKeyDefaultMethodArgMap.get(prop); return unsafeGetValue(manager, prop); }, set(_, prop, val) { if (schemaKeyNullableCallbackParamMap.has(prop) && val instanceof PassthroughCallback) { const extended = unsafeSetValue(manager, prop, extendFunction(unsafeGetValue(manager, prop), val.callback)); val.handle.then(() => omitFunction(extended, val.callback)); return true; } unsafeSetValue(manager, prop, val); return true; } }), manager) ]; }; //# sourceMappingURL=createParams.js.map