@glimmer/manager
Advanced tools
Comparing version 0.85.5 to 0.85.6
@@ -1,700 +0,2 @@ | ||
import { debugToString, castToBrowser } from '@glimmer/util'; | ||
import { associateDestroyableChild, registerDestructor } from '@glimmer/destroyable'; | ||
import { valueForRef, createComputeRef, createConstRef, UNDEFINED_REFERENCE } from '@glimmer/reference'; | ||
import { track, createUpdatableTag, untrack } from '@glimmer/validator'; | ||
import { check, CheckNumber } from '@glimmer/debug'; | ||
import { InternalComponentCapabilities } from '@glimmer/vm'; | ||
const CUSTOM_TAG_FOR = new WeakMap(); | ||
function getCustomTagFor(obj) { | ||
return CUSTOM_TAG_FOR.get(obj); | ||
} | ||
function setCustomTagFor(obj, customTagFn) { | ||
CUSTOM_TAG_FOR.set(obj, customTagFn); | ||
} | ||
function convertToInt(prop) { | ||
if (typeof prop === 'symbol') return null; | ||
const num = Number(prop); | ||
if (isNaN(num)) return null; | ||
return num % 1 === 0 ? num : null; | ||
} | ||
function tagForNamedArg(namedArgs, key) { | ||
return track(() => { | ||
if (key in namedArgs) { | ||
valueForRef(namedArgs[key]); | ||
} | ||
}); | ||
} | ||
function tagForPositionalArg(positionalArgs, key) { | ||
return track(() => { | ||
if (key === '[]') { | ||
// consume all of the tags in the positional array | ||
positionalArgs.forEach(valueForRef); | ||
} | ||
const parsed = convertToInt(key); | ||
if (parsed !== null && parsed < positionalArgs.length) { | ||
// consume the tag of the referenced index | ||
valueForRef(positionalArgs[parsed]); | ||
} | ||
}); | ||
} | ||
class NamedArgsProxy { | ||
constructor(named) { | ||
this.named = named; | ||
} | ||
get(_target, prop) { | ||
const ref = this.named[prop]; | ||
if (ref !== undefined) { | ||
return valueForRef(ref); | ||
} | ||
} | ||
has(_target, prop) { | ||
return prop in this.named; | ||
} | ||
ownKeys() { | ||
return Object.keys(this.named); | ||
} | ||
isExtensible() { | ||
return false; | ||
} | ||
getOwnPropertyDescriptor(_target, prop) { | ||
if (!(prop in this.named)) { | ||
throw new Error(`args proxies do not have real property descriptors, so you should never need to call getOwnPropertyDescriptor yourself. This code exists for enumerability, such as in for-in loops and Object.keys(). Attempted to get the descriptor for \`${String(prop)}\``); | ||
} | ||
return { | ||
enumerable: true, | ||
configurable: true | ||
}; | ||
} | ||
} | ||
class PositionalArgsProxy { | ||
constructor(positional) { | ||
this.positional = positional; | ||
} | ||
get(target, prop) { | ||
let { | ||
positional | ||
} = this; | ||
if (prop === 'length') { | ||
return positional.length; | ||
} | ||
const parsed = convertToInt(prop); | ||
if (parsed !== null && parsed < positional.length) { | ||
return valueForRef(positional[parsed]); | ||
} | ||
return target[prop]; | ||
} | ||
isExtensible() { | ||
return false; | ||
} | ||
has(_target, prop) { | ||
const parsed = convertToInt(prop); | ||
return parsed !== null && parsed < this.positional.length; | ||
} | ||
} | ||
const argsProxyFor = (capturedArgs, type) => { | ||
const { | ||
named, | ||
positional | ||
} = capturedArgs; | ||
let getNamedTag = (_obj, key) => tagForNamedArg(named, key); | ||
let getPositionalTag = (_obj, key) => tagForPositionalArg(positional, key); | ||
const namedHandler = new NamedArgsProxy(named); | ||
const positionalHandler = new PositionalArgsProxy(positional); | ||
const namedTarget = Object.create(null); | ||
const positionalTarget = []; | ||
{ | ||
const setHandler = function (_target, prop) { | ||
throw new Error(`You attempted to set ${String(prop)} on the arguments of a component, helper, or modifier. Arguments are immutable and cannot be updated directly; they always represent the values that are passed down. If you want to set default values, you should use a getter and local tracked state instead.`); | ||
}; | ||
const forInDebugHandler = () => { | ||
throw new Error(`Object.keys() was called on the positional arguments array for a ${type}, which is not supported. This function is a low-level function that should not need to be called for positional argument arrays. You may be attempting to iterate over the array using for...in instead of for...of.`); | ||
}; | ||
namedHandler.set = setHandler; | ||
positionalHandler.set = setHandler; | ||
positionalHandler.ownKeys = forInDebugHandler; | ||
} | ||
const namedProxy = new Proxy(namedTarget, namedHandler); | ||
const positionalProxy = new Proxy(positionalTarget, positionalHandler); | ||
setCustomTagFor(namedProxy, getNamedTag); | ||
setCustomTagFor(positionalProxy, getPositionalTag); | ||
return { | ||
named: namedProxy, | ||
positional: positionalProxy | ||
}; | ||
}; | ||
const FROM_CAPABILITIES = new WeakSet() ; | ||
function buildCapabilities(capabilities) { | ||
{ | ||
FROM_CAPABILITIES.add(capabilities); | ||
Object.freeze(capabilities); | ||
} | ||
return capabilities; | ||
} | ||
const EMPTY = InternalComponentCapabilities.Empty; | ||
/** | ||
* Converts a ComponentCapabilities object into a 32-bit integer representation. | ||
*/ | ||
function capabilityFlagsFrom(capabilities) { | ||
return EMPTY | capability(capabilities, 'dynamicLayout') | capability(capabilities, 'dynamicTag') | capability(capabilities, 'prepareArgs') | capability(capabilities, 'createArgs') | capability(capabilities, 'attributeHook') | capability(capabilities, 'elementHook') | capability(capabilities, 'dynamicScope') | capability(capabilities, 'createCaller') | capability(capabilities, 'updateHook') | capability(capabilities, 'createInstance') | capability(capabilities, 'wrapped') | capability(capabilities, 'willDestroy') | capability(capabilities, 'hasSubOwner'); | ||
} | ||
function capability(capabilities, capability) { | ||
return capabilities[capability] ? InternalComponentCapabilities[capability] : EMPTY; | ||
} | ||
function managerHasCapability(_manager, capabilities, capability) { | ||
check(capabilities, CheckNumber); | ||
return !!(capabilities & capability); | ||
} | ||
function hasCapability(capabilities, capability) { | ||
check(capabilities, CheckNumber); | ||
return !!(capabilities & capability); | ||
} | ||
function helperCapabilities(managerAPI) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (managerAPI !== '3.23') { | ||
throw new Error('Invalid helper manager compatibility specified'); | ||
} | ||
if ((!(options.hasValue || options.hasScheduledEffect) || options.hasValue && options.hasScheduledEffect)) { | ||
throw new Error('You must pass either the `hasValue` OR the `hasScheduledEffect` capability when defining a helper manager. Passing neither, or both, is not permitted.'); | ||
} | ||
if (options.hasScheduledEffect) { | ||
throw new Error('The `hasScheduledEffect` capability has not yet been implemented for helper managers. Please pass `hasValue` instead'); | ||
} | ||
return buildCapabilities({ | ||
hasValue: Boolean(options.hasValue), | ||
hasDestroyable: Boolean(options.hasDestroyable), | ||
hasScheduledEffect: Boolean(options.hasScheduledEffect) | ||
}); | ||
} | ||
//////////// | ||
function hasValue(manager) { | ||
return manager.capabilities.hasValue; | ||
} | ||
function hasDestroyable(manager) { | ||
return manager.capabilities.hasDestroyable; | ||
} | ||
//////////// | ||
class CustomHelperManager { | ||
constructor(factory) { | ||
this.factory = factory; | ||
} | ||
helperManagerDelegates = new WeakMap(); | ||
undefinedDelegate = null; | ||
getDelegateForOwner(owner) { | ||
let delegate = this.helperManagerDelegates.get(owner); | ||
if (delegate === undefined) { | ||
let { | ||
factory | ||
} = this; | ||
delegate = factory(owner); | ||
if (!FROM_CAPABILITIES.has(delegate.capabilities)) { | ||
// TODO: This error message should make sense in both Ember and Glimmer https://github.com/glimmerjs/glimmer-vm/issues/1200 | ||
throw new Error(`Custom helper managers must have a \`capabilities\` property that is the result of calling the \`capabilities('3.23')\` (imported via \`import { capabilities } from '@ember/helper';\`). Received: \`${JSON.stringify(delegate.capabilities)}\` for: \`${delegate}\``); | ||
} | ||
this.helperManagerDelegates.set(owner, delegate); | ||
} | ||
return delegate; | ||
} | ||
getDelegateFor(owner) { | ||
if (owner === undefined) { | ||
let { | ||
undefinedDelegate | ||
} = this; | ||
if (undefinedDelegate === null) { | ||
let { | ||
factory | ||
} = this; | ||
this.undefinedDelegate = undefinedDelegate = factory(undefined); | ||
} | ||
return undefinedDelegate; | ||
} else { | ||
return this.getDelegateForOwner(owner); | ||
} | ||
} | ||
getHelper(definition) { | ||
return (capturedArgs, owner) => { | ||
let manager = this.getDelegateFor(owner); | ||
const args = argsProxyFor(capturedArgs, 'helper'); | ||
const bucket = manager.createHelper(definition, args); | ||
if (hasValue(manager)) { | ||
let cache = createComputeRef(() => manager.getValue(bucket), null, manager.getDebugName && manager.getDebugName(definition)); | ||
if (hasDestroyable(manager)) { | ||
associateDestroyableChild(cache, manager.getDestroyable(bucket)); | ||
} | ||
return cache; | ||
} else if (hasDestroyable(manager)) { | ||
let ref = createConstRef(undefined, (manager.getDebugName?.(definition) ?? 'unknown helper')); | ||
associateDestroyableChild(ref, manager.getDestroyable(bucket)); | ||
return ref; | ||
} else { | ||
return UNDEFINED_REFERENCE; | ||
} | ||
}; | ||
} | ||
} | ||
class FunctionHelperManager { | ||
capabilities = buildCapabilities({ | ||
hasValue: true, | ||
hasDestroyable: false, | ||
hasScheduledEffect: false | ||
}); | ||
createHelper(fn, args) { | ||
return { | ||
fn, | ||
args | ||
}; | ||
} | ||
getValue(_ref) { | ||
let { | ||
fn, | ||
args | ||
} = _ref; | ||
if (Object.keys(args.named).length > 0) { | ||
let argsForFn = [...args.positional, args.named]; | ||
return fn(...argsForFn); | ||
} | ||
return fn(...args.positional); | ||
} | ||
getDebugName(fn) { | ||
if (fn.name) { | ||
return `(helper function ${fn.name})`; | ||
} | ||
return '(anonymous helper function)'; | ||
} | ||
} | ||
const COMPONENT_MANAGERS = new WeakMap(); | ||
const MODIFIER_MANAGERS = new WeakMap(); | ||
const HELPER_MANAGERS = new WeakMap(); | ||
/////////// | ||
const getPrototypeOf$1 = Object.getPrototypeOf; | ||
function setManager(map, manager, obj) { | ||
if ((typeof obj !== 'object' || obj === null) && typeof obj !== 'function') { | ||
throw new Error(`Attempted to set a manager on a non-object value. Managers can only be associated with objects or functions. Value was ${debugToString(obj)}`); | ||
} | ||
if (map.has(obj)) { | ||
throw new Error(`Attempted to set the same type of manager multiple times on a value. You can only associate one manager of each type with a given value. Value was ${debugToString(obj)}`); | ||
} | ||
map.set(obj, manager); | ||
return obj; | ||
} | ||
function getManager(map, obj) { | ||
let pointer = obj; | ||
while (pointer !== undefined && pointer !== null) { | ||
const manager = map.get(pointer); | ||
if (manager !== undefined) { | ||
return manager; | ||
} | ||
pointer = getPrototypeOf$1(pointer); | ||
} | ||
return undefined; | ||
} | ||
/////////// | ||
function setInternalModifierManager(manager, definition) { | ||
return setManager(MODIFIER_MANAGERS, manager, definition); | ||
} | ||
function getInternalModifierManager(definition, isOptional) { | ||
if (typeof definition !== 'function' && (typeof definition !== 'object' || definition === null)) { | ||
throw new Error(`Attempted to use a value as a modifier, but it was not an object or function. Modifier definitions must be objects or functions with an associated modifier manager. The value was: ${definition}`); | ||
} | ||
const manager = getManager(MODIFIER_MANAGERS, definition); | ||
if (manager === undefined) { | ||
if (isOptional === true) { | ||
return null; | ||
} else { | ||
throw new Error(`Attempted to load a modifier, but there wasn't a modifier manager associated with the definition. The definition was: ${debugToString(definition)}`); | ||
} | ||
} | ||
return manager; | ||
} | ||
function setInternalHelperManager(manager, definition) { | ||
return setManager(HELPER_MANAGERS, manager, definition); | ||
} | ||
const DEFAULT_MANAGER = new CustomHelperManager(() => new FunctionHelperManager()); | ||
function getInternalHelperManager(definition, isOptional) { | ||
if (typeof definition !== 'function' && (typeof definition !== 'object' || definition === null)) { | ||
throw new Error(`Attempted to use a value as a helper, but it was not an object or function. Helper definitions must be objects or functions with an associated helper manager. The value was: ${definition}`); | ||
} | ||
let manager = getManager(HELPER_MANAGERS, definition); | ||
// Functions are special-cased because functions are defined | ||
// as the "default" helper, per: https://github.com/emberjs/rfcs/pull/756 | ||
if (manager === undefined && typeof definition === 'function') { | ||
manager = DEFAULT_MANAGER; | ||
} | ||
if (manager) { | ||
return manager; | ||
} else if (isOptional === true) { | ||
return null; | ||
} else { | ||
throw new Error(`Attempted to load a helper, but there wasn't a helper manager associated with the definition. The definition was: ${debugToString(definition)}`); | ||
} | ||
} | ||
function setInternalComponentManager(factory, obj) { | ||
return setManager(COMPONENT_MANAGERS, factory, obj); | ||
} | ||
function getInternalComponentManager(definition, isOptional) { | ||
if (typeof definition !== 'function' && (typeof definition !== 'object' || definition === null)) { | ||
throw new Error(`Attempted to use a value as a component, but it was not an object or function. Component definitions must be objects or functions with an associated component manager. The value was: ${definition}`); | ||
} | ||
const manager = getManager(COMPONENT_MANAGERS, definition); | ||
if (manager === undefined) { | ||
if (isOptional === true) { | ||
return null; | ||
} else { | ||
throw new Error(`Attempted to load a component, but there wasn't a component manager associated with the definition. The definition was: ${debugToString(definition)}`); | ||
} | ||
} | ||
return manager; | ||
} | ||
/////////// | ||
function hasInternalComponentManager(definition) { | ||
return getManager(COMPONENT_MANAGERS, definition) !== undefined; | ||
} | ||
function hasInternalHelperManager(definition) { | ||
return hasDefaultHelperManager(definition) || getManager(HELPER_MANAGERS, definition) !== undefined; | ||
} | ||
function hasInternalModifierManager(definition) { | ||
return getManager(MODIFIER_MANAGERS, definition) !== undefined; | ||
} | ||
function hasDefaultHelperManager(definition) { | ||
return typeof definition === 'function'; | ||
} | ||
const CAPABILITIES = { | ||
dynamicLayout: false, | ||
dynamicTag: false, | ||
prepareArgs: false, | ||
createArgs: true, | ||
attributeHook: false, | ||
elementHook: false, | ||
createCaller: false, | ||
dynamicScope: true, | ||
updateHook: true, | ||
createInstance: true, | ||
wrapped: false, | ||
willDestroy: false, | ||
hasSubOwner: false | ||
}; | ||
function componentCapabilities(managerAPI) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (managerAPI !== '3.13') { | ||
throw new Error('Invalid component manager compatibility specified'); | ||
} | ||
let updateHook = Boolean(options.updateHook); | ||
return buildCapabilities({ | ||
asyncLifeCycleCallbacks: Boolean(options.asyncLifecycleCallbacks), | ||
destructor: Boolean(options.destructor), | ||
updateHook | ||
}); | ||
} | ||
function hasAsyncLifeCycleCallbacks(delegate) { | ||
return delegate.capabilities.asyncLifeCycleCallbacks; | ||
} | ||
function hasUpdateHook(delegate) { | ||
return delegate.capabilities.updateHook; | ||
} | ||
function hasAsyncUpdateHook(delegate) { | ||
return hasAsyncLifeCycleCallbacks(delegate) && hasUpdateHook(delegate); | ||
} | ||
function hasDestructors(delegate) { | ||
return delegate.capabilities.destructor; | ||
} | ||
/** | ||
The CustomComponentManager allows addons to provide custom component | ||
implementations that integrate seamlessly into Ember. This is accomplished | ||
through a delegate, registered with the custom component manager, which | ||
implements a set of hooks that determine component behavior. | ||
To create a custom component manager, instantiate a new CustomComponentManager | ||
class and pass the delegate as the first argument: | ||
```js | ||
let manager = new CustomComponentManager({ | ||
// ...delegate implementation... | ||
}); | ||
``` | ||
## Delegate Hooks | ||
Throughout the lifecycle of a component, the component manager will invoke | ||
delegate hooks that are responsible for surfacing those lifecycle changes to | ||
the end developer. | ||
* `create()` - invoked when a new instance of a component should be created | ||
* `update()` - invoked when the arguments passed to a component change | ||
* `getContext()` - returns the object that should be | ||
*/ | ||
class CustomComponentManager { | ||
componentManagerDelegates = new WeakMap(); | ||
constructor(factory) { | ||
this.factory = factory; | ||
} | ||
getDelegateFor(owner) { | ||
let { | ||
componentManagerDelegates | ||
} = this; | ||
let delegate = componentManagerDelegates.get(owner); | ||
if (delegate === undefined) { | ||
let { | ||
factory | ||
} = this; | ||
delegate = factory(owner); | ||
if (!FROM_CAPABILITIES.has(delegate.capabilities)) { | ||
// TODO: This error message should make sense in both Ember and Glimmer https://github.com/glimmerjs/glimmer-vm/issues/1200 | ||
throw new Error(`Custom component managers must have a \`capabilities\` property that is the result of calling the \`capabilities('3.13')\` (imported via \`import { capabilities } from '@ember/component';\`). Received: \`${JSON.stringify(delegate.capabilities)}\` for: \`${delegate}\``); | ||
} | ||
componentManagerDelegates.set(owner, delegate); | ||
} | ||
return delegate; | ||
} | ||
create(owner, definition, vmArgs) { | ||
let delegate = this.getDelegateFor(owner); | ||
let args = argsProxyFor(vmArgs.capture(), 'component'); | ||
let component = delegate.createComponent(definition, args); | ||
return new CustomComponentState(component, delegate, args); | ||
} | ||
getDebugName(definition) { | ||
return typeof definition === 'function' ? definition.name : definition.toString(); | ||
} | ||
update(bucket) { | ||
let { | ||
delegate | ||
} = bucket; | ||
if (hasUpdateHook(delegate)) { | ||
let { | ||
component, | ||
args | ||
} = bucket; | ||
delegate.updateComponent(component, args); | ||
} | ||
} | ||
didCreate(_ref) { | ||
let { | ||
component, | ||
delegate | ||
} = _ref; | ||
if (hasAsyncLifeCycleCallbacks(delegate)) { | ||
delegate.didCreateComponent(component); | ||
} | ||
} | ||
didUpdate(_ref2) { | ||
let { | ||
component, | ||
delegate | ||
} = _ref2; | ||
if (hasAsyncUpdateHook(delegate)) { | ||
delegate.didUpdateComponent(component); | ||
} | ||
} | ||
didRenderLayout() {} | ||
didUpdateLayout() {} | ||
getSelf(_ref3) { | ||
let { | ||
component, | ||
delegate | ||
} = _ref3; | ||
return createConstRef(delegate.getContext(component), 'this'); | ||
} | ||
getDestroyable(bucket) { | ||
const { | ||
delegate | ||
} = bucket; | ||
if (hasDestructors(delegate)) { | ||
const { | ||
component | ||
} = bucket; | ||
registerDestructor(bucket, () => delegate.destroyComponent(component)); | ||
return bucket; | ||
} | ||
return null; | ||
} | ||
getCapabilities() { | ||
return CAPABILITIES; | ||
} | ||
} | ||
/** | ||
* Stores internal state about a component instance after it's been created. | ||
*/ | ||
class CustomComponentState { | ||
constructor(component, delegate, args) { | ||
this.component = component; | ||
this.delegate = delegate; | ||
this.args = args; | ||
} | ||
} | ||
function modifierCapabilities(managerAPI) { | ||
let optionalFeatures = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (managerAPI !== '3.22') { | ||
throw new Error('Invalid modifier manager compatibility specified'); | ||
} | ||
return buildCapabilities({ | ||
disableAutoTracking: Boolean(optionalFeatures.disableAutoTracking) | ||
}); | ||
} | ||
/** | ||
The CustomModifierManager allows addons to provide custom modifier | ||
implementations that integrate seamlessly into Ember. This is accomplished | ||
through a delegate, registered with the custom modifier manager, which | ||
implements a set of hooks that determine modifier behavior. | ||
To create a custom modifier manager, instantiate a new CustomModifierManager | ||
class and pass the delegate as the first argument: | ||
```js | ||
let manager = new CustomModifierManager({ | ||
// ...delegate implementation... | ||
}); | ||
``` | ||
## Delegate Hooks | ||
Throughout the lifecycle of a modifier, the modifier manager will invoke | ||
delegate hooks that are responsible for surfacing those lifecycle changes to | ||
the end developer. | ||
* `createModifier()` - invoked when a new instance of a modifier should be created | ||
* `installModifier()` - invoked when the modifier is installed on the element | ||
* `updateModifier()` - invoked when the arguments passed to a modifier change | ||
* `destroyModifier()` - invoked when the modifier is about to be destroyed | ||
*/ | ||
class CustomModifierManager { | ||
componentManagerDelegates = new WeakMap(); | ||
constructor(factory) { | ||
this.factory = factory; | ||
} | ||
getDelegateFor(owner) { | ||
let { | ||
componentManagerDelegates | ||
} = this; | ||
let delegate = componentManagerDelegates.get(owner); | ||
if (delegate === undefined) { | ||
let { | ||
factory | ||
} = this; | ||
delegate = factory(owner); | ||
if (!FROM_CAPABILITIES.has(delegate.capabilities)) { | ||
// TODO: This error message should make sense in both Ember and Glimmer https://github.com/glimmerjs/glimmer-vm/issues/1200 | ||
throw new Error(`Custom modifier managers must have a \`capabilities\` property that is the result of calling the \`capabilities('3.22')\` (imported via \`import { capabilities } from '@ember/modifier';\`). Received: \`${JSON.stringify(delegate.capabilities)}\` for: \`${delegate}\``); | ||
} | ||
componentManagerDelegates.set(owner, delegate); | ||
} | ||
return delegate; | ||
} | ||
create(owner, element, definition, capturedArgs) { | ||
let delegate = this.getDelegateFor(owner); | ||
let args = argsProxyFor(capturedArgs, 'modifier'); | ||
let instance = delegate.createModifier(definition, args); | ||
let tag = createUpdatableTag(); | ||
let state; | ||
state = { | ||
tag, | ||
element, | ||
delegate, | ||
args, | ||
modifier: instance | ||
}; | ||
{ | ||
state.debugName = typeof definition === 'function' ? definition.name : definition.toString(); | ||
} | ||
registerDestructor(state, () => delegate.destroyModifier(instance, args)); | ||
return state; | ||
} | ||
getDebugName(_ref) { | ||
let { | ||
debugName | ||
} = _ref; | ||
return debugName; | ||
} | ||
getTag(_ref2) { | ||
let { | ||
tag | ||
} = _ref2; | ||
return tag; | ||
} | ||
install(_ref3) { | ||
let { | ||
element, | ||
args, | ||
modifier, | ||
delegate | ||
} = _ref3; | ||
let { | ||
capabilities | ||
} = delegate; | ||
if (capabilities.disableAutoTracking === true) { | ||
untrack(() => delegate.installModifier(modifier, castToBrowser(element, 'ELEMENT'), args)); | ||
} else { | ||
delegate.installModifier(modifier, castToBrowser(element, 'ELEMENT'), args); | ||
} | ||
} | ||
update(_ref4) { | ||
let { | ||
args, | ||
modifier, | ||
delegate | ||
} = _ref4; | ||
let { | ||
capabilities | ||
} = delegate; | ||
if (capabilities.disableAutoTracking === true) { | ||
untrack(() => delegate.updateModifier(modifier, args)); | ||
} else { | ||
delegate.updateModifier(modifier, args); | ||
} | ||
} | ||
getDestroyable(state) { | ||
return state; | ||
} | ||
} | ||
function setComponentManager(factory, obj) { | ||
return setInternalComponentManager(new CustomComponentManager(factory), obj); | ||
} | ||
function setModifierManager(factory, obj) { | ||
return setInternalModifierManager(new CustomModifierManager(factory), obj); | ||
} | ||
function setHelperManager(factory, obj) { | ||
return setInternalHelperManager(new CustomHelperManager(factory), obj); | ||
} | ||
const TEMPLATES = new WeakMap(); | ||
const getPrototypeOf = Object.getPrototypeOf; | ||
function setComponentTemplate(factory, obj) { | ||
if (!(obj !== null && (typeof obj === 'object' || typeof obj === 'function'))) { | ||
throw new Error(`Cannot call \`setComponentTemplate\` on \`${debugToString(obj)}\``); | ||
} | ||
if (TEMPLATES.has(obj)) { | ||
throw new Error(`Cannot call \`setComponentTemplate\` multiple times on the same class (\`${debugToString(obj)}\`)`); | ||
} | ||
TEMPLATES.set(obj, factory); | ||
return obj; | ||
} | ||
function getComponentTemplate(obj) { | ||
let pointer = obj; | ||
while (pointer !== null) { | ||
let template = TEMPLATES.get(pointer); | ||
if (template !== undefined) { | ||
return template; | ||
} | ||
pointer = getPrototypeOf(pointer); | ||
} | ||
return undefined; | ||
} | ||
export { CustomComponentManager, CustomHelperManager, CustomModifierManager, capabilityFlagsFrom, componentCapabilities, getComponentTemplate, getCustomTagFor, getInternalComponentManager, getInternalHelperManager, getInternalModifierManager, hasCapability, hasDestroyable, hasInternalComponentManager, hasInternalHelperManager, hasInternalModifierManager, hasValue, helperCapabilities, managerHasCapability, modifierCapabilities, setComponentManager, setComponentTemplate, setCustomTagFor, setHelperManager, setInternalComponentManager, setInternalHelperManager, setInternalModifierManager, setModifierManager }; | ||
import{debugToString as e,castToBrowser as t}from"@glimmer/util";import{associateDestroyableChild as n,registerDestructor as r}from"@glimmer/destroyable";import{valueForRef as o,createComputeRef as a,createConstRef as i,UNDEFINED_REFERENCE as s}from"@glimmer/reference";import{track as l,createUpdatableTag as u,untrack as c}from"@glimmer/validator";import{check as f,CheckNumber as d}from"@glimmer/debug";import{InternalComponentCapabilities as p}from"@glimmer/vm";const h=new WeakMap;function m(e){return h.get(e)}function g(e,t){h.set(e,t)}function b(e){if("symbol"==typeof e)return null;const t=Number(e);return isNaN(t)?null:t%1==0?t:null}class w{constructor(e){this.named=e}get(e,t){const n=this.named[t];if(void 0!==n)return o(n)}has(e,t){return t in this.named}ownKeys(){return Object.keys(this.named)}isExtensible(){return!1}getOwnPropertyDescriptor(e,t){if(!(t in this.named))throw new Error(`args proxies do not have real property descriptors, so you should never need to call getOwnPropertyDescriptor yourself. This code exists for enumerability, such as in for-in loops and Object.keys(). Attempted to get the descriptor for \`${String(t)}\``);return{enumerable:!0,configurable:!0}}}class y{constructor(e){this.positional=e}get(e,t){let{positional:n}=this;if("length"===t)return n.length;const r=b(t);return null!==r&&r<n.length?o(n[r]):e[t]}isExtensible(){return!1}has(e,t){const n=b(t);return null!==n&&n<this.positional.length}}const v=(e,t)=>{const{named:n,positional:r}=e;const a=new w(n),i=new y(r),s=Object.create(null);{const e=function(e,t){throw new Error(`You attempted to set ${String(t)} on the arguments of a component, helper, or modifier. Arguments are immutable and cannot be updated directly; they always represent the values that are passed down. If you want to set default values, you should use a getter and local tracked state instead.`)},n=()=>{throw new Error(`Object.keys() was called on the positional arguments array for a ${t}, which is not supported. This function is a low-level function that should not need to be called for positional argument arrays. You may be attempting to iterate over the array using for...in instead of for...of.`)};a.set=e,i.set=e,i.ownKeys=n}const u=new Proxy(s,a),c=new Proxy([],i);return g(u,((e,t)=>function(e,t){return l((()=>{t in e&&o(e[t])}))}(n,t))),g(c,((e,t)=>function(e,t){return l((()=>{"[]"===t&&e.forEach(o);const n=b(t);null!==n&&n<e.length&&o(e[n])}))}(r,t))),{named:u,positional:c}},E=new WeakSet;function D(e){return E.add(e),Object.freeze(e),e}const k=p.Empty;function M(e){return k|C(e,"dynamicLayout")|C(e,"dynamicTag")|C(e,"prepareArgs")|C(e,"createArgs")|C(e,"attributeHook")|C(e,"elementHook")|C(e,"dynamicScope")|C(e,"createCaller")|C(e,"updateHook")|C(e,"createInstance")|C(e,"wrapped")|C(e,"willDestroy")|C(e,"hasSubOwner")}function C(e,t){return e[t]?p[t]:k}function j(e,t,n){return f(t,d),!!(t&n)}function S(e,t){return f(e,d),!!(e&t)}function O(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("3.23"!==e)throw new Error("Invalid helper manager compatibility specified");if(!t.hasValue&&!t.hasScheduledEffect||t.hasValue&&t.hasScheduledEffect)throw new Error("You must pass either the `hasValue` OR the `hasScheduledEffect` capability when defining a helper manager. Passing neither, or both, is not permitted.");if(t.hasScheduledEffect)throw new Error("The `hasScheduledEffect` capability has not yet been implemented for helper managers. Please pass `hasValue` instead");return D({hasValue:Boolean(t.hasValue),hasDestroyable:Boolean(t.hasDestroyable),hasScheduledEffect:Boolean(t.hasScheduledEffect)})}function T(e){return e.capabilities.hasValue}function $(e){return e.capabilities.hasDestroyable}class A{constructor(e){this.factory=e}helperManagerDelegates=new WeakMap;undefinedDelegate=null;getDelegateForOwner(e){let t=this.helperManagerDelegates.get(e);if(void 0===t){let{factory:n}=this;if(t=n(e),!E.has(t.capabilities))throw new Error(`Custom helper managers must have a \`capabilities\` property that is the result of calling the \`capabilities('3.23')\` (imported via \`import { capabilities } from '@ember/helper';\`). Received: \`${JSON.stringify(t.capabilities)}\` for: \`${t}\``);this.helperManagerDelegates.set(e,t)}return t}getDelegateFor(e){if(void 0===e){let{undefinedDelegate:e}=this;if(null===e){let{factory:t}=this;this.undefinedDelegate=e=t(void 0)}return e}return this.getDelegateForOwner(e)}getHelper(e){return(t,r)=>{let o=this.getDelegateFor(r);const l=v(t,"helper"),u=o.createHelper(e,l);if(T(o)){let t=a((()=>o.getValue(u)),null,o.getDebugName&&o.getDebugName(e));return $(o)&&n(t,o.getDestroyable(u)),t}if($(o)){let t=i(void 0,o.getDebugName?.(e)??"unknown helper");return n(t,o.getDestroyable(u)),t}return s}}}class N{capabilities=D({hasValue:!0,hasDestroyable:!1,hasScheduledEffect:!1});createHelper(e,t){return{fn:e,args:t}}getValue(e){let{fn:t,args:n}=e;if(Object.keys(n.named).length>0){return t(...[...n.positional,n.named])}return t(...n.positional)}getDebugName(e){return e.name?`(helper function ${e.name})`:"(anonymous helper function)"}}const H=new WeakMap,V=new WeakMap,L=new WeakMap,W=Object.getPrototypeOf;function x(t,n,r){if(("object"!=typeof r||null===r)&&"function"!=typeof r)throw new Error(`Attempted to set a manager on a non-object value. Managers can only be associated with objects or functions. Value was ${e(r)}`);if(t.has(r))throw new Error(`Attempted to set the same type of manager multiple times on a value. You can only associate one manager of each type with a given value. Value was ${e(r)}`);return t.set(r,n),r}function F(e,t){let n=t;for(;null!=n;){const t=e.get(n);if(void 0!==t)return t;n=W(n)}}function P(e,t){return x(V,e,t)}function B(t,n){if("function"!=typeof t&&("object"!=typeof t||null===t))throw new Error(`Attempted to use a value as a modifier, but it was not an object or function. Modifier definitions must be objects or functions with an associated modifier manager. The value was: ${t}`);const r=F(V,t);if(void 0===r){if(!0===n)return null;throw new Error(`Attempted to load a modifier, but there wasn't a modifier manager associated with the definition. The definition was: ${e(t)}`)}return r}function I(e,t){return x(L,e,t)}const R=new A((()=>new N));function Y(t,n){if("function"!=typeof t&&("object"!=typeof t||null===t))throw new Error(`Attempted to use a value as a helper, but it was not an object or function. Helper definitions must be objects or functions with an associated helper manager. The value was: ${t}`);let r=F(L,t);if(void 0===r&&"function"==typeof t&&(r=R),r)return r;if(!0===n)return null;throw new Error(`Attempted to load a helper, but there wasn't a helper manager associated with the definition. The definition was: ${e(t)}`)}function J(e,t){return x(H,e,t)}function U(t,n){if("function"!=typeof t&&("object"!=typeof t||null===t))throw new Error(`Attempted to use a value as a component, but it was not an object or function. Component definitions must be objects or functions with an associated component manager. The value was: ${t}`);const r=F(H,t);if(void 0===r){if(!0===n)return null;throw new Error(`Attempted to load a component, but there wasn't a component manager associated with the definition. The definition was: ${e(t)}`)}return r}function K(e){return void 0!==F(H,e)}function z(e){return function(e){return"function"==typeof e}(e)||void 0!==F(L,e)}function q(e){return void 0!==F(V,e)}const G={dynamicLayout:!1,dynamicTag:!1,prepareArgs:!1,createArgs:!0,attributeHook:!1,elementHook:!1,createCaller:!1,dynamicScope:!0,updateHook:!0,createInstance:!0,wrapped:!1,willDestroy:!1,hasSubOwner:!1};function Q(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("3.13"!==e)throw new Error("Invalid component manager compatibility specified");let n=Boolean(t.updateHook);return D({asyncLifeCycleCallbacks:Boolean(t.asyncLifecycleCallbacks),destructor:Boolean(t.destructor),updateHook:n})}function X(e){return e.capabilities.asyncLifeCycleCallbacks}function Z(e){return e.capabilities.updateHook}class _{componentManagerDelegates=new WeakMap;constructor(e){this.factory=e}getDelegateFor(e){let{componentManagerDelegates:t}=this,n=t.get(e);if(void 0===n){let{factory:r}=this;if(n=r(e),!E.has(n.capabilities))throw new Error(`Custom component managers must have a \`capabilities\` property that is the result of calling the \`capabilities('3.13')\` (imported via \`import { capabilities } from '@ember/component';\`). Received: \`${JSON.stringify(n.capabilities)}\` for: \`${n}\``);t.set(e,n)}return n}create(e,t,n){let r=this.getDelegateFor(e),o=v(n.capture(),"component"),a=r.createComponent(t,o);return new ee(a,r,o)}getDebugName(e){return"function"==typeof e?e.name:e.toString()}update(e){let{delegate:t}=e;if(Z(t)){let{component:n,args:r}=e;t.updateComponent(n,r)}}didCreate(e){let{component:t,delegate:n}=e;X(n)&&n.didCreateComponent(t)}didUpdate(e){let{component:t,delegate:n}=e;(function(e){return X(e)&&Z(e)})(n)&&n.didUpdateComponent(t)}didRenderLayout(){}didUpdateLayout(){}getSelf(e){let{component:t,delegate:n}=e;return i(n.getContext(t),"this")}getDestroyable(e){const{delegate:t}=e;if(function(e){return e.capabilities.destructor}(t)){const{component:n}=e;return r(e,(()=>t.destroyComponent(n))),e}return null}getCapabilities(){return G}}class ee{constructor(e,t,n){this.component=e,this.delegate=t,this.args=n}}function te(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("3.22"!==e)throw new Error("Invalid modifier manager compatibility specified");return D({disableAutoTracking:Boolean(t.disableAutoTracking)})}class ne{componentManagerDelegates=new WeakMap;constructor(e){this.factory=e}getDelegateFor(e){let{componentManagerDelegates:t}=this,n=t.get(e);if(void 0===n){let{factory:r}=this;if(n=r(e),!E.has(n.capabilities))throw new Error(`Custom modifier managers must have a \`capabilities\` property that is the result of calling the \`capabilities('3.22')\` (imported via \`import { capabilities } from '@ember/modifier';\`). Received: \`${JSON.stringify(n.capabilities)}\` for: \`${n}\``);t.set(e,n)}return n}create(e,t,n,o){let a,i=this.getDelegateFor(e),s=v(o,"modifier"),l=i.createModifier(n,s);return a={tag:u(),element:t,delegate:i,args:s,modifier:l},a.debugName="function"==typeof n?n.name:n.toString(),r(a,(()=>i.destroyModifier(l,s))),a}getDebugName(e){let{debugName:t}=e;return t}getTag(e){let{tag:t}=e;return t}install(e){let{element:n,args:r,modifier:o,delegate:a}=e,{capabilities:i}=a;!0===i.disableAutoTracking?c((()=>a.installModifier(o,t(n,"ELEMENT"),r))):a.installModifier(o,t(n,"ELEMENT"),r)}update(e){let{args:t,modifier:n,delegate:r}=e,{capabilities:o}=r;!0===o.disableAutoTracking?c((()=>r.updateModifier(n,t))):r.updateModifier(n,t)}getDestroyable(e){return e}}function re(e,t){return J(new _(e),t)}function oe(e,t){return P(new ne(e),t)}function ae(e,t){return I(new A(e),t)}const ie=new WeakMap,se=Object.getPrototypeOf;function le(t,n){if(null===n||"object"!=typeof n&&"function"!=typeof n)throw new Error(`Cannot call \`setComponentTemplate\` on \`${e(n)}\``);if(ie.has(n))throw new Error(`Cannot call \`setComponentTemplate\` multiple times on the same class (\`${e(n)}\`)`);return ie.set(n,t),n}function ue(e){let t=e;for(;null!==t;){let e=ie.get(t);if(void 0!==e)return e;t=se(t)}}export{_ as CustomComponentManager,A as CustomHelperManager,ne as CustomModifierManager,M as capabilityFlagsFrom,Q as componentCapabilities,ue as getComponentTemplate,m as getCustomTagFor,U as getInternalComponentManager,Y as getInternalHelperManager,B as getInternalModifierManager,S as hasCapability,$ as hasDestroyable,K as hasInternalComponentManager,z as hasInternalHelperManager,q as hasInternalModifierManager,T as hasValue,O as helperCapabilities,j as managerHasCapability,te as modifierCapabilities,re as setComponentManager,le as setComponentTemplate,g as setCustomTagFor,ae as setHelperManager,J as setInternalComponentManager,I as setInternalHelperManager,P as setInternalModifierManager,oe as setModifierManager}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@glimmer/manager", | ||
"type": "module", | ||
"version": "0.85.5", | ||
"version": "0.85.6", | ||
"repository": "https://github.com/glimmerjs/glimmer-vm/tree/master/packages/@glimmer/program", | ||
"dependencies": { | ||
"@glimmer/env": "0.1.7", | ||
"@glimmer/debug": "^0.85.5", | ||
"@glimmer/destroyable": "^0.85.5", | ||
"@glimmer/vm": "^0.85.5", | ||
"@glimmer/global-context": "^0.85.5", | ||
"@glimmer/interfaces": "^0.85.5", | ||
"@glimmer/reference": "^0.85.5", | ||
"@glimmer/util": "^0.85.5", | ||
"@glimmer/validator": "^0.85.5" | ||
"@glimmer/debug": "^0.85.6", | ||
"@glimmer/destroyable": "^0.85.6", | ||
"@glimmer/vm": "^0.85.6", | ||
"@glimmer/global-context": "^0.85.6", | ||
"@glimmer/interfaces": "^0.85.6", | ||
"@glimmer/reference": "^0.85.6", | ||
"@glimmer/util": "^0.85.6", | ||
"@glimmer/validator": "^0.85.6" | ||
}, | ||
@@ -24,3 +24,3 @@ "files": [ | ||
"publint": "^0.2.5", | ||
"@glimmer/local-debug-flags": "^0.85.5", | ||
"@glimmer/local-debug-flags": "^0.85.6", | ||
"@glimmer-workspace/build-support": "^1.0.0" | ||
@@ -27,0 +27,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115609
1040
Updated@glimmer/debug@^0.85.6
Updated@glimmer/destroyable@^0.85.6
Updated@glimmer/interfaces@^0.85.6
Updated@glimmer/reference@^0.85.6
Updated@glimmer/util@^0.85.6
Updated@glimmer/validator@^0.85.6
Updated@glimmer/vm@^0.85.6