This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
[FEAT] Modifier Managers #245
Merged
+774
−144
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,6 +1,20 @@ | ||
import { assert } from '@glimmer/util'; | ||
import { DEBUG } from '@glimmer/env'; | ||
import { | ||
ModifierManager as VMModifierManager, | ||
VMArguments, | ||
CapturedArguments, | ||
Destroyable, | ||
DynamicScope, | ||
} from '@glimmer/interfaces'; | ||
import { Tag, createUpdatableTag, track, untrack, combine, update } from '@glimmer/validator'; | ||
import { assert, debugToString } from '@glimmer/util'; | ||
import { SimpleElement } from '@simple-dom/interface'; | ||
import { Args } from '../interfaces'; | ||
import debugRenderMessage from '../utils/debug'; | ||
import { argsProxyFor } from './util'; | ||
import { getModifierManager } from '.'; | ||
import { OWNER_KEY } from '../owner'; | ||
import { VMModifierDefinitionWithHandle } from '../render-component/vm-definitions'; | ||
|
||
/////////// | ||
|
||
|
@@ -10,7 +24,7 @@ export interface Capabilities { | |
|
||
export type OptionalCapabilities = Partial<Capabilities>; | ||
|
||
export type ManagerAPIVersion = '3.4' | '3.13'; | ||
export type ManagerAPIVersion = '3.13'; | ||
|
||
export function capabilities( | ||
managerAPI: ManagerAPIVersion, | ||
|
@@ -25,12 +39,152 @@ export function capabilities( | |
|
||
/////////// | ||
|
||
export interface ModifierManager<ModifierInstance> { | ||
export interface ModifierManager<ModifierStateBucket> { | ||
capabilities: Capabilities; | ||
createModifier(definition: unknown, args: Args): ModifierInstance; | ||
installModifier(instance: ModifierInstance, element: SimpleElement, args: Args): void; | ||
updateModifier(instance: ModifierInstance, args: Args): void; | ||
destroyModifier(instance: ModifierInstance, args: Args): void; | ||
createModifier(definition: unknown, args: Args): ModifierStateBucket; | ||
installModifier(instance: ModifierStateBucket, element: Element, args: Args): void; | ||
updateModifier(instance: ModifierStateBucket, args: Args): void; | ||
destroyModifier(instance: ModifierStateBucket, args: Args): void; | ||
} | ||
|
||
export type ModifierDefinition<_Instance = unknown> = {}; | ||
|
||
export type SimpleModifier = (element: Element, ...args: unknown[]) => undefined | (() => void); | ||
|
||
interface SimpleModifierStateBucket { | ||
definition: SimpleModifier; | ||
destructor?(): void; | ||
element?: Element; | ||
} | ||
|
||
class SimpleModifierManager implements ModifierManager<SimpleModifierStateBucket> { | ||
capabilities = capabilities('3.13'); | ||
|
||
createModifier(definition: SimpleModifier, args: Args): SimpleModifierStateBucket { | ||
if (DEBUG) { | ||
assert(Object.keys(args.named).length === 0, `You used named arguments with the ${definition.name} modifier, but it is a standard function. Normal functions cannot receive named arguments when used as modifiers.`); | ||
} | ||
|
||
return { definition }; | ||
} | ||
|
||
installModifier(bucket: SimpleModifierStateBucket, element: Element, args: Args): void { | ||
bucket.destructor = bucket.definition(element, ...args.positional); | ||
bucket.element = element; | ||
} | ||
|
||
updateModifier(bucket: SimpleModifierStateBucket, args: Args): void { | ||
this.destroyModifier(bucket); | ||
this.installModifier(bucket, bucket.element!, args); | ||
} | ||
|
||
destroyModifier(bucket: SimpleModifierStateBucket): void { | ||
const { destructor } = bucket; | ||
|
||
if (destructor !== undefined) { | ||
destructor(); | ||
} | ||
} | ||
} | ||
|
||
const SIMPLE_MODIFIER_MANAGER = new SimpleModifierManager(); | ||
|
||
/////////// | ||
|
||
export class CustomModifierState<ModifierStateBucket> { | ||
public tag = createUpdatableTag(); | ||
|
||
constructor( | ||
public element: SimpleElement, | ||
public delegate: ModifierManager<ModifierStateBucket>, | ||
public modifier: ModifierStateBucket, | ||
public argsProxy: Args, | ||
public capturedArgs: CapturedArguments | ||
) {} | ||
|
||
destroy(): void { | ||
const { delegate, modifier, argsProxy } = this; | ||
delegate.destroyModifier(modifier, argsProxy); | ||
} | ||
} | ||
|
||
export class CustomModifierManager<ModifierStateBucket> | ||
implements | ||
VMModifierManager< | ||
CustomModifierState<ModifierStateBucket>, | ||
ModifierDefinition<ModifierStateBucket> | ||
> { | ||
create( | ||
element: SimpleElement, | ||
definition: ModifierDefinition<ModifierStateBucket>, | ||
args: VMArguments, | ||
dynamicScope: DynamicScope | ||
): CustomModifierState<ModifierStateBucket> { | ||
const owner = dynamicScope.get(OWNER_KEY).value() as object; | ||
let delegate = getModifierManager(owner, definition); | ||
|
||
if (delegate === undefined) { | ||
if (DEBUG) { | ||
assert( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious, do these get stripped properly on their own or do they have to be within an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe the import should be tree-shaken, which is why I figured this pattern would be alright for now. We currently only replace |
||
typeof definition === 'function', | ||
`No modifier manager found for ${definition}, and it was not a plain function, so it could not be used as a modifier` | ||
); | ||
} | ||
|
||
delegate = (SIMPLE_MODIFIER_MANAGER as unknown) as ModifierManager<ModifierStateBucket>; | ||
} | ||
|
||
const capturedArgs = args.capture(); | ||
const argsProxy = argsProxyFor(capturedArgs, 'modifier'); | ||
|
||
const instance = delegate.createModifier(definition, argsProxy); | ||
|
||
return new CustomModifierState(element, delegate, instance, argsProxy, capturedArgs); | ||
} | ||
|
||
getTag({ tag, capturedArgs }: CustomModifierState<ModifierStateBucket>): Tag { | ||
return combine([tag, capturedArgs.tag]); | ||
} | ||
|
||
install(state: CustomModifierState<ModifierStateBucket>): void { | ||
const { element, argsProxy, delegate, modifier, tag } = state; | ||
|
||
if (delegate.capabilities.disableAutoTracking === true) { | ||
untrack(() => delegate.installModifier(modifier, element as Element, argsProxy)); | ||
} else { | ||
const combinedTrackingTag = track( | ||
() => delegate.installModifier(modifier, element as Element, argsProxy), | ||
DEBUG && debugRenderMessage!(`(instance of a \`${debugToString!(modifier)}\` modifier)`) | ||
); | ||
|
||
update(tag, combinedTrackingTag); | ||
} | ||
} | ||
|
||
update(state: CustomModifierState<ModifierStateBucket>): void { | ||
const { argsProxy, delegate, modifier, tag } = state; | ||
|
||
if (delegate.capabilities.disableAutoTracking === true) { | ||
untrack(() => delegate.updateModifier(modifier, argsProxy)); | ||
} else { | ||
const combinedTrackingTag = track( | ||
() => delegate.updateModifier(modifier, argsProxy), | ||
DEBUG && debugRenderMessage!(`(instance of a \`${debugToString!(modifier)}\` modifier)`) | ||
); | ||
update(tag, combinedTrackingTag); | ||
} | ||
} | ||
|
||
getDestructor(state: CustomModifierState<ModifierStateBucket>): Destroyable { | ||
return state; | ||
} | ||
} | ||
|
||
export const CUSTOM_MODIFIER_MANAGER = new CustomModifierManager(); | ||
|
||
export class VMCustomModifierDefinition<ModifierStateBucket> | ||
implements VMModifierDefinitionWithHandle { | ||
public manager = CUSTOM_MODIFIER_MANAGER; | ||
|
||
constructor(public handle: number, public state: ModifierDefinition<ModifierStateBucket>) {} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { DEBUG } from '@glimmer/env'; | ||
|
||
let debugRenderMessage: undefined | ((renderingStack: string) => string); | ||
|
||
if (DEBUG) { | ||
debugRenderMessage = (renderingStack: string): string => { | ||
return `While rendering:\n----------------\n${renderingStack.replace(/^/gm, ' ')}`; | ||
}; | ||
} | ||
|
||
export default debugRenderMessage; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for not using
export *
👯♂