Skip to content

Commit

Permalink
[BUGFIX beta] Ensure {{on}} does not run in FastBoot / SSR contexts.
Browse files Browse the repository at this point in the history
Since `{{on}}` is a "special" built-in modifier manager, the generic
fixes for custom modifier managers do not fix `on`. This applies the
same general fix, and ensures that `{{on}}` is a no-op in FastBoot mode.
  • Loading branch information
rwjblue committed Jun 3, 2019
1 parent a0c1013 commit f0a7e24
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
31 changes: 24 additions & 7 deletions packages/@ember/-internals/glimmer/lib/modifiers/on.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import { Opaque, Simple } from '@glimmer/interfaces';
import { Tag } from '@glimmer/reference';
import { Tag, CONSTANT_TAG } from '@glimmer/reference';
import { Arguments, CapturedArguments, ModifierManager } from '@glimmer/runtime';
import { Destroyable } from '@glimmer/util';
import buildUntouchableThis from '../utils/untouchable-this';
Expand Down Expand Up @@ -210,24 +210,37 @@ function addEventListener(
}
}

export default class OnModifierManager implements ModifierManager<OnModifierState, Opaque> {
export default class OnModifierManager implements ModifierManager<OnModifierState | null, Opaque> {
public SUPPORTS_EVENT_OPTIONS: boolean = SUPPORTS_EVENT_OPTIONS;
public isInteractive: boolean;

constructor(isInteractive: boolean) {
this.isInteractive = isInteractive;
}

get counters() {
return { adds, removes };
}

create(element: Simple.Element | Element, _state: Opaque, args: Arguments) {
if (!this.isInteractive) { return null; }

const capturedArgs = args.capture();

return new OnModifierState(<Element>element, capturedArgs);
}

getTag({ tag }: OnModifierState): Tag {
return tag;
getTag(state: OnModifierState | null): Tag {
if (state === null) { return CONSTANT_TAG; }

return state.tag;
}

install(state: OnModifierState) {
install(state: OnModifierState | null) {
if (state === null) {
return;
}

state.updateFromArgs();

let { element, eventName, callback, options } = state;
Expand All @@ -237,7 +250,11 @@ export default class OnModifierManager implements ModifierManager<OnModifierStat
state.shouldUpdate = false;
}

update(state: OnModifierState) {
update(state: OnModifierState | null) {
if (state === null) {
return;
}

// stash prior state for el.removeEventListener
let { element, eventName, callback, options } = state;

Expand All @@ -256,7 +273,7 @@ export default class OnModifierManager implements ModifierManager<OnModifierStat
state.shouldUpdate = false;
}

getDestructor(state: Destroyable) {
getDestructor(state: Destroyable | null) {
return state;
}
}
17 changes: 9 additions & 8 deletions packages/@ember/-internals/glimmer/lib/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,7 @@ if (EMBER_GLIMMER_FN_HELPER) {
interface IBuiltInModifiers {
[name: string]: ModifierDefinition | undefined;
}
const BUILTIN_MODIFIERS: IBuiltInModifiers = {
action: { manager: new ActionModifierManager(), state: null },
on: undefined,
};

if (EMBER_GLIMMER_ON_MODIFIER) {
BUILTIN_MODIFIERS.on = { manager: new OnModifierManager(), state: null };
}
export default class RuntimeResolver implements IRuntimeResolver<OwnedTemplateMeta> {
public isInteractive: boolean;
public compiler: LazyCompiler<OwnedTemplateMeta>;
Expand All @@ -119,7 +112,7 @@ export default class RuntimeResolver implements IRuntimeResolver<OwnedTemplateMe

private builtInHelpers: IBuiltInHelpers = BUILTINS_HELPERS;

private builtInModifiers: IBuiltInModifiers = BUILTIN_MODIFIERS;
private builtInModifiers: IBuiltInModifiers;

// supports directly imported late bound layouts on component.prototype.layout
private templateCache: Map<Owner, Map<TemplateFactory, OwnedTemplate>> = new Map();
Expand All @@ -136,6 +129,14 @@ export default class RuntimeResolver implements IRuntimeResolver<OwnedTemplateMe
populateMacros(macros);
this.compiler = new LazyCompiler<OwnedTemplateMeta>(new CompileTimeLookup(this), this, macros);
this.isInteractive = isInteractive;

this.builtInModifiers = {
action: { manager: new ActionModifierManager(), state: null },
};

if (EMBER_GLIMMER_ON_MODIFIER) {
this.builtInModifiers.on = { manager: new OnModifierManager(isInteractive), state: null };
}
}

/*** IRuntimeResolver ***/
Expand Down

0 comments on commit f0a7e24

Please sign in to comment.