-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable global event dispatcher listeners to be lazily created. #19227
Changes from all commits
eedd575
1731a3a
b2f89ef
828c7aa
5f0621c
d53070f
1393ab7
71bed42
dd429e3
229e3cb
f4c84c7
876f91c
f61af5e
54c3122
5963428
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { RenderingTestCase, moduleFor, runTask } from 'internal-test-helpers'; | ||
|
||
import { Component } from '../utils/helpers'; | ||
import { _getCurrentRunLoop, run } from '@ember/runloop'; | ||
import { _getCurrentRunLoop } from '@ember/runloop'; | ||
import { | ||
subscribe as instrumentationSubscribe, | ||
reset as instrumentationReset, | ||
|
@@ -20,9 +20,108 @@ function fireNativeWithDataTransfer(node, type, dataTransfer) { | |
node.dispatchEvent(event); | ||
} | ||
|
||
function triggerEvent(node, event) { | ||
switch (event) { | ||
case 'focusin': | ||
return node.focus(); | ||
case 'focusout': | ||
return node.blur(); | ||
default: | ||
return node.trigger(event); | ||
} | ||
} | ||
|
||
const SUPPORTED_EMBER_EVENTS = { | ||
touchstart: 'touchStart', | ||
touchmove: 'touchMove', | ||
touchend: 'touchEnd', | ||
touchcancel: 'touchCancel', | ||
keydown: 'keyDown', | ||
keyup: 'keyUp', | ||
keypress: 'keyPress', | ||
mousedown: 'mouseDown', | ||
mouseup: 'mouseUp', | ||
contextmenu: 'contextMenu', | ||
click: 'click', | ||
dblclick: 'doubleClick', | ||
focusin: 'focusIn', | ||
focusout: 'focusOut', | ||
submit: 'submit', | ||
input: 'input', | ||
change: 'change', | ||
dragstart: 'dragStart', | ||
drag: 'drag', | ||
dragenter: 'dragEnter', | ||
dragleave: 'dragLeave', | ||
dragover: 'dragOver', | ||
drop: 'drop', | ||
dragend: 'dragEnd', | ||
}; | ||
|
||
moduleFor( | ||
'EventDispatcher', | ||
class extends RenderingTestCase { | ||
['@test event handler methods are called when event is triggered'](assert) { | ||
let receivedEvent; | ||
let browserEvent; | ||
|
||
this.registerComponent('x-button', { | ||
ComponentClass: Component.extend( | ||
{ | ||
tagName: 'button', | ||
}, | ||
Object.keys(SUPPORTED_EMBER_EVENTS) | ||
.map((browerEvent) => ({ | ||
[SUPPORTED_EMBER_EVENTS[browerEvent]](event) { | ||
receivedEvent = event; | ||
}, | ||
})) | ||
.reduce((result, singleEventHandler) => ({ ...result, ...singleEventHandler }), {}) | ||
), | ||
}); | ||
|
||
this.render(`{{x-button}}`); | ||
|
||
let elementNode = this.$('button'); | ||
let element = elementNode[0]; | ||
|
||
for (browserEvent in SUPPORTED_EMBER_EVENTS) { | ||
receivedEvent = null; | ||
runTask(() => triggerEvent(elementNode, browserEvent)); | ||
assert.ok(receivedEvent, `${browserEvent} event was triggered`); | ||
assert.strictEqual(receivedEvent.target, element); | ||
} | ||
} | ||
|
||
['@test event listeners are called when event is triggered'](assert) { | ||
let receivedEvent; | ||
let browserEvent; | ||
|
||
this.registerComponent('x-button', { | ||
ComponentClass: Component.extend({ | ||
tagName: 'button', | ||
init() { | ||
this._super(); | ||
Object.keys(SUPPORTED_EMBER_EVENTS).forEach((browserEvent) => { | ||
this.on(SUPPORTED_EMBER_EVENTS[browserEvent], (event) => (receivedEvent = event)); | ||
}); | ||
}, | ||
}), | ||
}); | ||
|
||
this.render(`{{x-button}}`); | ||
|
||
let elementNode = this.$('button'); | ||
let element = elementNode[0]; | ||
|
||
for (browserEvent in SUPPORTED_EMBER_EVENTS) { | ||
receivedEvent = null; | ||
runTask(() => triggerEvent(elementNode, browserEvent)); | ||
assert.ok(receivedEvent, `${browserEvent} event was triggered`); | ||
assert.strictEqual(receivedEvent.target, element); | ||
} | ||
} | ||
|
||
['@test events bubble view hierarchy for form elements'](assert) { | ||
let receivedEvent; | ||
|
||
|
@@ -420,12 +519,15 @@ moduleFor( | |
constructor() { | ||
super(...arguments); | ||
|
||
let dispatcher = this.owner.lookup('event_dispatcher:main'); | ||
run(dispatcher, 'destroy'); | ||
this.owner.__container__.reset('event_dispatcher:main'); | ||
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 was not able to keep this test unchanged, as the reference to the But I think this affects only our internal testing here, and not imply any publicly visible changes. |
||
this.dispatcher = this.owner.lookup('event_dispatcher:main'); | ||
} | ||
|
||
getBootOptions() { | ||
return { | ||
skipEventDispatcher: true, | ||
}; | ||
} | ||
|
||
['@test additional events can be specified'](assert) { | ||
this.dispatcher.setup({ myevent: 'myEvent' }); | ||
|
||
|
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.
FWIW, we have a WeakSet polyfill thingy that we use (which lets us treat it like a
WeakSet
, but on IE11 is implemented as aWeakMap
).https://github.com/glimmerjs/glimmer-vm/blob/master/packages/@glimmer/util/lib/weak-set.ts