Skip to content

Commit

Permalink
Fix events not firing in Firefox. Provide new CustomEvent to each ele…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
ernsheong committed Dec 21, 2017
1 parent de4ea8e commit bd88799
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lite-signal",
"version": "1.0.0",
"version": "1.0.1",
"description": "Basic publish-subscribe functionality",
"keywords": [
"web-component",
Expand Down
33 changes: 20 additions & 13 deletions lite-signal.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,32 @@
<script>
(function() {
// private list of subscribers
var signals = []
var subscribers = []

// signal dispatcher
function notify(name, data) {
function getNewSignal(name, data) {
// convert generic-signal event to named-signal event
var signal = new CustomEvent('lite-signal-' + name, {
// if signals bubble, it's easy to get confusing duplicates
return new CustomEvent('lite-signal-' + name, {
// if subscribers bubble, it's easy to get confusing duplicates
// (1) listen on a container on behalf of local child
// (2) some deep child ignores the event and it bubbles
// up to said container
// (3) local child event bubbles up to container
// also, for performance, we avoid signals flying up the
// also, for performance, we avoid subscribers flying up the
// tree from all over the place
bubbles: false,
detail: data
detail: data,
});
// dispatch named-signal to all 'signals' instances,
}

// signal dispatcher
function notify(name, data) {
// dispatch named-signal to all 'subscribers' instances,
// only interested listeners will react
signals.forEach(function(s) {
s.dispatchEvent(signal);
subscribers.forEach(function(sub) {
// Generate a new CustomEvent for each instance:
// Once an event is consumed, for some reason in FF it does not trigger anything in another subscriber
// listening on the same event (https://github.com/ernsheong/lite-signal/issues/4)
sub.dispatchEvent(getNewSignal(name, data));
});
}

Expand All @@ -41,15 +47,15 @@
connectedCallback() {
super.connectedCallback();

signals.push(this);
subscribers.push(this);
}

disconnectedCallback() {
super.disconnectedCallback();

var i = signals.indexOf(this);
var i = subscribers.indexOf(this);
if (i >= 0) {
signals.splice(i, 1);
subscribers.splice(i, 1);
}
}
}
Expand All @@ -59,5 +65,6 @@
document.addEventListener('lite-signal', function(e) {
notify(e.detail.name, e.detail.data);
});

})();
</script>

0 comments on commit bd88799

Please sign in to comment.