Skip to content

Commit

Permalink
ui: Ensure an EventSource isn't opened if closed before first tick (#…
Browse files Browse the repository at this point in the history
…5703)

EventSources will wait for 1 tick before 'opening'. There is always the
chance that the EventSource is '.close()'ed before that tick. We
therefore check the 'readyState' before opening the EventSource
  • Loading branch information
johncowen authored and John Cowen committed May 24, 2019
1 parent e35f213 commit e6a7e1c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
17 changes: 13 additions & 4 deletions ui-v2/app/utils/dom/event-source/callable.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ export default function(
return P.resolve();
}
: source;
this.readyState = 0; // connecting
this.readyState = 0; // CONNECTING
P.resolve()
.then(() => {
// if we are already closed, don't do anything
if (this.readyState !== 0) {
return;
}
this.readyState = 1; // open
// ...that the connection _was just_ opened
// the connection _was just_ opened
this.dispatchEvent({ type: 'open' });
return run(this, configuration, isClosed);
})
Expand All @@ -63,8 +67,13 @@ export default function(
}
close() {
// additional readyState 3 = CLOSING
if (this.readyState !== 2) {
this.readyState = 3;
switch (this.readyState) {
case 0: // CONNECTING
case 2: // CLOSED
this.readyState = 2; // CLOSED
break;
default:
this.readyState = 3; // CLOSING
}
}
};
Expand Down
15 changes: 15 additions & 0 deletions ui-v2/tests/integration/utils/dom/event-source/callable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,19 @@ module('Integration | Utility | dom/event-source/callable', function(hooks) {
});
});
});
test("it can be closed before the first tick, and therefore doesn't run", function(assert) {
assert.expect(3);
const EventSource = domEventSourceCallable(EventTarget);
const listener = this.stub();
const source = new EventSource();
source.close();
assert.equal(source.readyState, 2);
source.addEventListener('open', function(e) {
listener();
});
return Promise.resolve().then(function() {
assert.notOk(listener.called);
assert.equal(source.readyState, 2);
});
});
});

0 comments on commit e6a7e1c

Please sign in to comment.