Skip to content

Commit

Permalink
Move lazy event setup from component manager to component class after…
Browse files Browse the repository at this point in the history
… rebase
  • Loading branch information
simonihmig committed Nov 17, 2020
1 parent dd429e3 commit 9846858
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { privatize as P } from '@ember/-internals/container';
import { getOwner } from '@ember/-internals/owner';
import { guidFor } from '@ember/-internals/utils';
import {
addChildView,
EventDispatcher,
setElementView,
setViewElement,
} from '@ember/-internals/views';
import { addChildView, setElementView, setViewElement } from '@ember/-internals/views';
import { assert, debugFreeze } from '@ember/debug';
import { EMBER_COMPONENT_IS_VISIBLE } from '@ember/deprecated-features';
import { _instrumentStart } from '@ember/instrumentation';
Expand Down Expand Up @@ -119,21 +114,6 @@ const EMPTY_POSITIONAL_ARGS: Reference[] = [];

debugFreeze(EMPTY_POSITIONAL_ARGS);

function _setupLazyEventsForComponent(dispatcher: EventDispatcher | undefined, component: object) {
// non-interactive rendering (e.g. SSR) has no event dispatcher
if (dispatcher === undefined) {
return;
}

let lazyEvents = dispatcher.lazyEvents;

lazyEvents.forEach((mappedEventName: string, event: string) => {
if (mappedEventName !== null && typeof component[mappedEventName] === 'function') {
dispatcher.setupHandlerForBrowserEvent(event);
}
});
}

export default class CurlyComponentManager
extends BaseInternalComponentManager<ComponentStateBucket, DefinitionState>
implements
Expand Down Expand Up @@ -338,8 +318,6 @@ export default class CurlyComponentManager
}
}

_setupLazyEventsForComponent(environment.extra.eventDispatcher, component);

// Track additional lifecycle metadata about this component in a state bucket.
// Essentially we're saving off all the state we'll need in the future.
let bucket = new ComponentStateBucket(
Expand Down
34 changes: 21 additions & 13 deletions packages/@ember/-internals/glimmer/lib/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { isUpdatableRef, updateRef } from '@glimmer/reference';
import { normalizeProperty } from '@glimmer/runtime';
import { createTag, dirtyTag } from '@glimmer/validator';
import { Namespace } from '@simple-dom/interface';
import { EmberVMEnvironment } from './environment';
import { Environment } from '@glimmer/interfaces';

export const ARGS = enumerableSymbol('ARGS');
export const HAS_BLOCK = enumerableSymbol('HAS_BLOCK');
Expand Down Expand Up @@ -726,10 +726,20 @@ const Component = CoreView.extend(
this[DIRTY_TAG] = createTag();
this[BOUNDS] = null;

if (DEBUG && this.renderer._destinedForDOM && this.tagName === '') {
let eventDispatcher = this._dispatcher;
if (eventDispatcher) {
let lazyEvents = eventDispatcher.lazyEvents;

lazyEvents.forEach((mappedEventName: string, event: string) => {
if (mappedEventName !== null && typeof this[mappedEventName] === 'function') {
eventDispatcher.setupHandlerForBrowserEvent(event);
}
});
}

if (DEBUG && eventDispatcher && this.tagName === '') {
let eventNames = [];
let eventDispatcher = getOwner(this).lookup<EventDispatcher>('event_dispatcher:main');
let events = (eventDispatcher && eventDispatcher.finalEventNameMapping) || {};
let events = eventDispatcher.finalEventNameMapping;

// tslint:disable-next-line:forin
for (let key in events) {
Expand Down Expand Up @@ -788,24 +798,22 @@ const Component = CoreView.extend(
);
},

get setupEventHandler(): (eventName: string) => void | null {
if (this._setupEventHandler === undefined) {
get _dispatcher(): EventDispatcher | null {
if (this.__dispatcher === undefined) {
let owner = getOwner(this);
if (owner.lookup<EmberVMEnvironment>('-environment:main')!.isInteractive) {
let dispatcher = owner.lookup<EventDispatcher>('event_dispatcher:main');
this._setupEventHandler = (eventName: string) =>
dispatcher!.setupHandlerForEmberEvent(eventName);
if (owner.lookup<Environment>('-environment:main')!.isInteractive) {
this.__dispatcher = owner.lookup<EventDispatcher>('event_dispatcher:main');
} else {
// In FastBoot we have no EventDispatcher. Set to null to not try again to look it up.
this._setupEventHandler = null;
this.__dispatcher = null;
}
}

return this._setupEventHandler;
return this.__dispatcher;
},

on(eventName: string) {
this.setupEventHandler?.(eventName);
this._dispatcher?.setupHandlerForEmberEvent(eventName);
return this._super(...arguments);
},

Expand Down

0 comments on commit 9846858

Please sign in to comment.