ui: Ensure an EventSource isn't opened if closed before first tick (#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
This commit is contained in:
John Cowen 2019-05-15 14:59:59 +01:00 committed by John Cowen
parent 466c3c6899
commit 3a14360da4
2 changed files with 28 additions and 4 deletions

View file

@ -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);
})
@ -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
}
}
};

View file

@ -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);
});
});
});