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:
parent
466c3c6899
commit
3a14360da4
|
@ -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
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue