-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17143 from emberjs/modifier-managers
Modifier Manager
- Loading branch information
Showing
13 changed files
with
429 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
packages/@ember/-internals/glimmer/lib/modifiers/custom.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Factory } from '@ember/-internals/owner'; | ||
import { Opaque } from '@glimmer/interfaces'; | ||
import { Tag } from '@glimmer/reference'; | ||
import { Arguments, CapturedArguments, ModifierManager } from '@glimmer/runtime'; | ||
import { ManagerArgs, valueForCapturedArgs } from '../utils/managers'; | ||
|
||
export interface CustomModifierDefinitionState<ModifierInstance> { | ||
ModifierClass: Factory<ModifierInstance>; | ||
name: string; | ||
delegate: ModifierManagerDelegate<ModifierInstance>; | ||
} | ||
|
||
export interface Capabilities {} | ||
|
||
// Currently there are no capabilities for modifiers | ||
export function capabilities(_managerAPI: string, _optionalFeatures?: {}): Capabilities { | ||
return {}; | ||
} | ||
|
||
export class CustomModifierDefinition<ModifierInstance> { | ||
public state: CustomModifierDefinitionState<ModifierInstance>; | ||
public manager = CUSTOM_MODIFIER_MANAGER; | ||
constructor( | ||
public name: string, | ||
public ModifierClass: Factory<ModifierInstance>, | ||
public delegate: ModifierManagerDelegate<ModifierInstance> | ||
) { | ||
this.state = { | ||
ModifierClass, | ||
name, | ||
delegate, | ||
}; | ||
} | ||
} | ||
|
||
export class CustomModifierState<ModifierInstance> { | ||
constructor( | ||
public element: Element, | ||
public delegate: ModifierManagerDelegate<ModifierInstance>, | ||
public modifier: ModifierInstance, | ||
public args: CapturedArguments | ||
) {} | ||
|
||
destroy() { | ||
const { delegate, modifier, args } = this; | ||
let modifierArgs = valueForCapturedArgs(args); | ||
delegate.destroyModifier(modifier, modifierArgs); | ||
} | ||
} | ||
|
||
export interface ModifierManagerDelegate<ModifierInstance> { | ||
capabilities: Capabilities; | ||
createModifier(factory: Opaque, args: ManagerArgs): ModifierInstance; | ||
installModifier(instance: ModifierInstance, element: Element, args: ManagerArgs): void; | ||
updateModifier(instance: ModifierInstance, args: ManagerArgs): void; | ||
destroyModifier(instance: ModifierInstance, args: ManagerArgs): void; | ||
} | ||
|
||
class CustomModifierManager<ModifierInstance> | ||
implements | ||
ModifierManager< | ||
CustomModifierState<ModifierInstance>, | ||
CustomModifierDefinitionState<ModifierInstance> | ||
> { | ||
create( | ||
element: Element, | ||
definition: CustomModifierDefinitionState<ModifierInstance>, | ||
args: Arguments | ||
) { | ||
const capturedArgs = args.capture(); | ||
let modifierArgs = valueForCapturedArgs(capturedArgs); | ||
let instance = definition.delegate.createModifier(definition.ModifierClass, modifierArgs); | ||
return new CustomModifierState(element, definition.delegate, instance, capturedArgs); | ||
} | ||
|
||
getTag({ args }: CustomModifierState<ModifierInstance>): Tag { | ||
return args.tag; | ||
} | ||
|
||
install(state: CustomModifierState<ModifierInstance>) { | ||
let { element, args, delegate, modifier } = state; | ||
let modifierArgs = valueForCapturedArgs(args); | ||
delegate.installModifier(modifier, element, modifierArgs); | ||
} | ||
|
||
update(state: CustomModifierState<ModifierInstance>) { | ||
let { args, delegate, modifier } = state; | ||
let modifierArgs = valueForCapturedArgs(args); | ||
delegate.updateModifier(modifier, modifierArgs); | ||
} | ||
|
||
getDestructor(state: CustomModifierState<ModifierInstance>) { | ||
return state; | ||
} | ||
} | ||
|
||
const CUSTOM_MODIFIER_MANAGER = new CustomModifierManager(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 15 additions & 18 deletions
33
packages/@ember/-internals/glimmer/lib/utils/custom-component-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,24 @@ | ||
import { Owner } from '@ember/-internals/owner'; | ||
import { GLIMMER_CUSTOM_COMPONENT_MANAGER } from '@ember/canary-features'; | ||
import { Opaque } from '@glimmer/interfaces'; | ||
import { getManager, ManagerFactory, setManager } from './managers'; | ||
|
||
const getPrototypeOf = Object.getPrototypeOf; | ||
const MANAGERS: WeakMap<any, string> = new WeakMap(); | ||
|
||
export function setComponentManager(managerId: string, obj: any) { | ||
MANAGERS.set(obj, managerId); | ||
|
||
return obj; | ||
export function setComponentManager(stringOrFunction: string | ManagerFactory<Opaque>, obj: any) { | ||
let factory; | ||
if (typeof stringOrFunction === 'string') { | ||
factory = function(owner: Owner) { | ||
return owner.lookup(`component-manager:${stringOrFunction}`); | ||
}; | ||
} else { | ||
factory = stringOrFunction; | ||
} | ||
return setManager(factory, obj); | ||
} | ||
|
||
export function getComponentManager(obj: any): string | undefined { | ||
export function getComponentManager<T>(obj: any): undefined | ManagerFactory<T> { | ||
if (!GLIMMER_CUSTOM_COMPONENT_MANAGER) { | ||
return; | ||
} | ||
|
||
let pointer = obj; | ||
while (pointer !== undefined && pointer !== null) { | ||
if (MANAGERS.has(pointer)) { | ||
return MANAGERS.get(pointer); | ||
} | ||
|
||
pointer = getPrototypeOf(pointer); | ||
} | ||
|
||
return; | ||
return getManager(obj); | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/@ember/-internals/glimmer/lib/utils/custom-modifier-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { GLIMMER_MODIFIER_MANAGER } from '@ember/canary-features'; | ||
import { Opaque } from '@glimmer/util'; | ||
import { getManager, ManagerFactory, setManager } from './managers'; | ||
|
||
export function setModifierManager(factory: ManagerFactory<Opaque>, obj: any) { | ||
return setManager(factory, obj); | ||
} | ||
|
||
export function getModifierManager<T>(obj: any): undefined | ManagerFactory<T> { | ||
if (!GLIMMER_MODIFIER_MANAGER) { | ||
return; | ||
} | ||
|
||
return getManager(obj); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Owner } from '@ember/-internals/owner'; | ||
import { Dict, Opaque } from '@glimmer/interfaces'; | ||
import { CapturedArguments } from '@glimmer/runtime'; | ||
|
||
const MANAGERS: WeakMap<any, ManagerFactory<Opaque>> = new WeakMap(); | ||
|
||
const getPrototypeOf = Object.getPrototypeOf; | ||
|
||
export type ManagerFactory<ManagerDelegate> = (owner: Owner) => ManagerDelegate; | ||
|
||
export function setManager<ManagerDelegate>(factory: ManagerFactory<ManagerDelegate>, obj: any) { | ||
MANAGERS.set(obj, factory); | ||
return obj; | ||
} | ||
|
||
export function getManager<T>(obj: any): undefined | ManagerFactory<T> { | ||
let pointer = obj; | ||
while (pointer !== undefined && pointer !== null) { | ||
if (MANAGERS.has(pointer)) { | ||
return MANAGERS.get(pointer) as ManagerFactory<T>; | ||
} | ||
|
||
pointer = getPrototypeOf(pointer); | ||
} | ||
|
||
return; | ||
} | ||
|
||
export function valueForCapturedArgs(args: CapturedArguments): ManagerArgs { | ||
return { | ||
named: args.named.value(), | ||
positional: args.positional.value(), | ||
}; | ||
} | ||
|
||
export interface ManagerArgs { | ||
named: Dict<Opaque>; | ||
positional: Opaque[]; | ||
} |
Oops, something went wrong.