-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep generated event handlers, attach with WeakMap to host element Add tests for events Support multi-dispatch unlisten (must give event name and handler function name). Fixes #1639
- Loading branch information
Showing
7 changed files
with
435 additions
and
26 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
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,95 @@ | ||
<!-- | ||
@license | ||
Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
|
||
<link rel="import" href="../../polymer.html"> | ||
|
||
<script> | ||
var EventLoggerImpl = { | ||
created: function() { | ||
this._handled = {}; | ||
this._warned = []; | ||
this._removed = []; | ||
}, | ||
handle: function(e) { | ||
this._handled[e.currentTarget.localName] = e.type; | ||
}, | ||
_warn: function(array) { | ||
var hasColor = array[0].indexOf('%c') === 0; | ||
var funIdx = hasColor ? 3 : 2; | ||
var messageIdx = hasColor ? 4 : 3; | ||
if (array[funIdx] === '_createEventHandler') { | ||
this._warned.push(array[messageIdx]); | ||
} else { | ||
Polymer.Base._warn(array); | ||
} | ||
}, | ||
_unlisten: function(node, eventName, handler) { | ||
this._removed.push({target: node.localName, event: eventName}); | ||
Polymer.Base._unlisten(node, eventName, handler); | ||
} | ||
}; | ||
</script> | ||
|
||
<dom-module id="x-listeners"> | ||
<script> | ||
Polymer({ | ||
is: 'x-listeners', | ||
behaviors: [EventLoggerImpl], | ||
listeners: { | ||
foo: 'handle', | ||
bar: 'missing' | ||
}, | ||
teardown: function() { | ||
this.unlisten(this, 'foo', 'handle'); | ||
this.unlisten(this, 'bar', 'missing'); | ||
} | ||
}); | ||
</script> | ||
</dom-module> | ||
|
||
<dom-module id="x-on"> | ||
<template> | ||
<div id="inner" on-foo="handle" on-bar="missing"></div> | ||
</template> | ||
<script> | ||
Polymer({ | ||
is: 'x-on', | ||
behaviors: [EventLoggerImpl], | ||
teardown: function() { | ||
this.unlisten(this.$.inner, 'foo', 'handle'); | ||
this.unlisten(this.$.inner, 'bar', 'missing'); | ||
} | ||
}); | ||
</script> | ||
</dom-module> | ||
|
||
<dom-module id="x-dynamic"> | ||
<template> | ||
<div id="inner"></div> | ||
</template> | ||
<script> | ||
Polymer({ | ||
is: 'x-dynamic', | ||
behaviors: [EventLoggerImpl], | ||
setup: function() { | ||
this.listen(this, 'foo', 'handle'); | ||
this.listen(this.$.inner, 'foo', 'handle'); | ||
this.listen(this, 'bar', 'missing'); | ||
this.listen(this.$.inner, 'bar', 'missing'); | ||
}, | ||
teardown: function() { | ||
this.unlisten(this, 'foo', 'handle'); | ||
this.unlisten(this.$.inner, 'foo', 'handle'); | ||
this.unlisten(this, 'bar', 'missing'); | ||
this.unlisten(this.$.inner, 'bar', 'missing'); | ||
} | ||
}); | ||
</script> | ||
</dom-module> |
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,148 @@ | ||
<!DOCTYPE html> | ||
<!-- | ||
@license | ||
Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
|
||
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script> | ||
<script src="../../../web-component-tester/browser.js"></script> | ||
|
||
<link rel="import" href="../../polymer.html"> | ||
<link rel="import" href="events-elements.html"> | ||
</head> | ||
<body> | ||
|
||
<script> | ||
suite('Event Listeners', function() { | ||
var el; | ||
|
||
suite('listeners block', function() { | ||
suiteSetup(function() { | ||
el = document.createElement('x-listeners'); | ||
document.body.appendChild(el); | ||
}); | ||
|
||
suiteTeardown(function() { | ||
document.body.removeChild(el); | ||
}); | ||
|
||
test('setup', function() { | ||
var boundListeners = el.__boundListeners; | ||
assert(boundListeners, 'listeners are bound'); | ||
var map = boundListeners.get(el); | ||
assert(map, 'events have handlers'); | ||
Object.keys(el.listeners).forEach(function(l) { | ||
var key = Polymer.Base._boundListenerKey(l, el.listeners[l]); | ||
assert(map[key], 'listener is in event map'); | ||
}); | ||
}); | ||
|
||
test('fire', function() { | ||
el.fire('foo'); | ||
assert.equal(el._handled[el.localName], 'foo'); | ||
el.fire('bar'); | ||
assert.equal(el._warned.length, 1); | ||
assert.equal(el._warned[0], 'listener method `missing` not defined'); | ||
}); | ||
|
||
test('teardown', function() { | ||
el.teardown(); | ||
assert.deepEqual(el._removed[0], {target: el.localName, event: 'foo'}); | ||
assert.deepEqual(el._removed[1], {target: el.localName, event: 'bar'}); | ||
}); | ||
}); | ||
|
||
suite('on-*', function() { | ||
|
||
suiteSetup(function() { | ||
el = document.createElement('x-on'); | ||
document.body.appendChild(el); | ||
}); | ||
|
||
suiteTeardown(function() { | ||
document.body.removeChild(el); | ||
}); | ||
|
||
test('setup', function() { | ||
var boundListeners = el.__boundListeners; | ||
assert(boundListeners, 'listeners are bound'); | ||
var map = boundListeners.get(el.$.inner); | ||
assert(map, 'events have handlers'); | ||
assert(el._notes[0].events.length, 2); | ||
This comment has been minimized.
Sorry, something went wrong.
leodido
|
||
el._notes[0].events.forEach(function(l) { | ||
var key = Polymer.Base._boundListenerKey(l.name, l.value); | ||
assert(map[key], 'listener is in event map'); | ||
}); | ||
}); | ||
|
||
test('fire', function() { | ||
var options = {node: el.$.inner}; | ||
el.fire('foo', {}, options); | ||
assert.equal(el._handled.div, 'foo'); | ||
assert(!el._handled[el.localName]); | ||
el.fire('bar', {}, options); | ||
assert.equal(el._warned.length, 1); | ||
assert.equal(el._warned[0], 'listener method `missing` not defined'); | ||
}); | ||
|
||
test('teardown', function() { | ||
el.teardown(); | ||
assert.deepEqual(el._removed[0], {target: 'div', event: 'foo'}); | ||
assert.deepEqual(el._removed[1], {target: 'div', event: 'bar'}); | ||
}); | ||
}); | ||
|
||
suite('dynamic', function() { | ||
|
||
suiteSetup(function() { | ||
el = document.createElement('x-dynamic'); | ||
document.body.appendChild(el); | ||
}); | ||
|
||
suiteTeardown(function() { | ||
document.body.removeChild(el); | ||
}); | ||
|
||
test('setup', function() { | ||
assert(!el.__boundListeners, 'listeners are not bound'); | ||
el.setup(); | ||
var boundListeners = el.__boundListeners; | ||
assert(boundListeners, 'listeners are bound'); | ||
var outerMap = boundListeners.get(el); | ||
assert(outerMap, 'element events have handlers'); | ||
var innerMap = boundListeners.get(el.$.inner); | ||
assert(innerMap, 'inner div events have handlers'); | ||
}); | ||
|
||
test('fire', function() { | ||
var options = {node: el.$.inner}; | ||
el.fire('foo', {}, options); | ||
assert.equal(el._handled.div, 'foo', 'inner handler'); | ||
assert.equal(el._handled[el.localName], 'foo', 'outer handler'); | ||
el.fire('bar', {}, options); | ||
assert.equal(el._warned.length, 2); | ||
assert.equal(el._warned[0], 'listener method `missing` not defined'); | ||
assert.equal(el._warned[1], 'listener method `missing` not defined'); | ||
}); | ||
|
||
test('teardown', function() { | ||
el.teardown(); | ||
assert.deepEqual(el._removed[0], {target: el.localName, event: 'foo'}); | ||
assert.deepEqual(el._removed[1], {target: 'div', event: 'foo'}); | ||
assert.deepEqual(el._removed[2], {target: el.localName, event: 'bar'}); | ||
assert.deepEqual(el._removed[3], {target: 'div', event: 'bar'}); | ||
}); | ||
}); | ||
}); | ||
</script> | ||
|
||
</body> | ||
</html> |
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
Oops, something went wrong.
FYI: if I'm reading the code correctly,
listen()
could [semi-validly?] be called withtarget === null
here, which would cause this to throw withI'm currently tracking down a typechecking issue and was wondering whether
listen
/unlisten
can truly be called withnull
as the actual code (^ this) implies they can, whereas the externs disallownull
./cc @rictic @azakus