UNPKG

ember-source

Version:

A JavaScript framework for creating ambitious web applications

214 lines (208 loc) 7.67 kB
import { c as constants } from './fragment-Cc5k9Oy4.js'; import './debug-to-string-CFb7h0lY.js'; import { e as expect } from './collections-D_nY_0UJ.js'; import { g as getInternalHelperManager, d as getInternalModifierManager, e as getInternalComponentManager } from './api-BqXkkT0p.js'; import '../@glimmer/destroyable/index.js'; import '../@glimmer/global-context/index.js'; import { e as enumerate } from './array-utils-CZQxrdD3.js'; import '../@glimmer/validator/index.js'; import './reference-C3TKDRnP.js'; import { c as capabilityFlagsFrom, m as managerHasCapability } from './capabilities-O_xc7Yqk.js'; import { g as getComponentTemplate } from './template-kM-7TTcc.js'; import { I as InternalComponentCapabilities } from './flags-B9qxc-pB.js'; import { t as templateFactory } from './index-Q7JnrdBn.js'; import { SexpOpcodes as opcodes } from '../@glimmer/wire-format/index.js'; function unwrapHandle(handle) { if (typeof handle === 'number') { return handle; } else { let error = handle.errors[0]; throw new Error(`Compile Error: ${error.problem} @ ${error.span.start}..${error.span.end}`); } } function unwrapTemplate(template) { if (template.result === 'error') { throw new Error(`Compile Error: ${template.problem} @ ${template.span.start}..${template.span.end}`); } return template; } /** * Default component template, which is a plain yield */ const DEFAULT_TEMPLATE_BLOCK = [[[opcodes.Yield, 1, null]], ['&default'], []]; const DEFAULT_TEMPLATE = { // random uuid id: '1b32f5c2-7623-43d6-a0ad-9672898920a1', moduleName: '__default__.hbs', block: JSON.stringify(DEFAULT_TEMPLATE_BLOCK), scope: null, isStrictMode: true }; const WELL_KNOWN_EMPTY_ARRAY = Object.freeze([]); const STARTER_CONSTANTS = constants(WELL_KNOWN_EMPTY_ARRAY); const WELL_KNOWN_EMPTY_ARRAY_POSITION = STARTER_CONSTANTS.indexOf(WELL_KNOWN_EMPTY_ARRAY); class ConstantsImpl { reifiedArrs = { [WELL_KNOWN_EMPTY_ARRAY_POSITION]: WELL_KNOWN_EMPTY_ARRAY }; defaultTemplate = templateFactory(DEFAULT_TEMPLATE)(); // Used for tests and debugging purposes, and to be able to analyze large apps // This is why it's enabled even in production helperDefinitionCount = 0; modifierDefinitionCount = 0; componentDefinitionCount = 0; values = STARTER_CONSTANTS.slice(); indexMap = new Map(this.values.map((value, index) => [value, index])); helperDefinitionCache = new WeakMap(); modifierDefinitionCache = new WeakMap(); componentDefinitionCache = new WeakMap(); value(value) { let indexMap = this.indexMap; let index = indexMap.get(value); if (index === undefined) { index = this.values.push(value) - 1; indexMap.set(value, index); } return index; } array(values) { if (values.length === 0) { return WELL_KNOWN_EMPTY_ARRAY_POSITION; } let handles = new Array(values.length); for (let i = 0; i < values.length; i++) { handles[i] = this.value(values[i]); } return this.value(handles); } toPool() { return this.values; } hasHandle(handle) { return this.values.length > handle; } helper(definitionState, // TODO: Add a way to expose resolved name for debugging _resolvedName = null, isOptional) { let handle = this.helperDefinitionCache.get(definitionState); if (handle === undefined) { let managerOrHelper = getInternalHelperManager(definitionState, isOptional); if (managerOrHelper === null) { this.helperDefinitionCache.set(definitionState, null); return null; } let helper = typeof managerOrHelper === 'function' ? managerOrHelper : managerOrHelper.getHelper(definitionState); handle = this.value(helper); this.helperDefinitionCache.set(definitionState, handle); this.helperDefinitionCount++; } return handle; } modifier(definitionState, resolvedName = null, isOptional) { let handle = this.modifierDefinitionCache.get(definitionState); if (handle === undefined) { let manager = getInternalModifierManager(definitionState, isOptional); if (manager === null) { this.modifierDefinitionCache.set(definitionState, null); return null; } let definition = { resolvedName, manager, state: definitionState }; handle = this.value(definition); this.modifierDefinitionCache.set(definitionState, handle); this.modifierDefinitionCount++; } return handle; } component(definitionState, owner, isOptional, debugName) { let definition = this.componentDefinitionCache.get(definitionState); if (definition === undefined) { let manager = getInternalComponentManager(definitionState, isOptional); if (manager === null) { this.componentDefinitionCache.set(definitionState, null); return null; } let capabilities = capabilityFlagsFrom(manager.getCapabilities(definitionState)); let templateFactory = getComponentTemplate(definitionState); let compilable = null; let template; if (!managerHasCapability(manager, capabilities, InternalComponentCapabilities.dynamicLayout)) { template = templateFactory?.(owner) ?? this.defaultTemplate; } else { template = templateFactory?.(owner); } if (template !== undefined) { template = unwrapTemplate(template); compilable = managerHasCapability(manager, capabilities, InternalComponentCapabilities.wrapped) ? template.asWrappedLayout() : template.asLayout(); } definition = { resolvedName: null, handle: -1, // replaced momentarily manager, capabilities, state: definitionState, compilable }; definition.handle = this.value(definition); if (debugName) { definition.debugName = debugName; } this.componentDefinitionCache.set(definitionState, definition); this.componentDefinitionCount++; } return definition; } resolvedComponent(resolvedDefinition, resolvedName) { let definition = this.componentDefinitionCache.get(resolvedDefinition); if (definition === undefined) { let { manager, state, template } = resolvedDefinition; let capabilities = capabilityFlagsFrom(manager.getCapabilities(resolvedDefinition)); let compilable = null; if (!managerHasCapability(manager, capabilities, InternalComponentCapabilities.dynamicLayout)) { template = template ?? this.defaultTemplate; } if (template !== null) { template = unwrapTemplate(template); compilable = managerHasCapability(manager, capabilities, InternalComponentCapabilities.wrapped) ? template.asWrappedLayout() : template.asLayout(); } definition = { resolvedName, handle: -1, // replaced momentarily manager, capabilities, state, compilable }; definition.handle = this.value(definition); this.componentDefinitionCache.set(resolvedDefinition, definition); this.componentDefinitionCount++; } return expect(definition); } getValue(index) { return this.values[index]; } getArray(index) { let reifiedArrs = this.reifiedArrs; let reified = reifiedArrs[index]; if (reified === undefined) { let names = this.getValue(index); reified = new Array(names.length); for (const [i, name] of enumerate(names)) { reified[i] = this.getValue(name); } reifiedArrs[index] = reified; } return reified; } } export { ConstantsImpl as C, unwrapTemplate as a, unwrapHandle as u };