Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: Small EventSource additions #5978

Merged
merged 1 commit into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions ui-v2/app/utils/dom/event-source/blocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ export default function(EventSource, backoff = create5xxBackoff()) {
*/
return class extends EventSource {
constructor(source, configuration = {}) {
const { currentEvent, ...config } = configuration;
super(configuration => {
const { createEvent, ...superConfiguration } = configuration;
return source
.apply(this, [superConfiguration])
.apply(this, [superConfiguration, this])
.catch(backoff)
.then(result => {
if (result instanceof Error) {
Expand Down Expand Up @@ -103,7 +104,19 @@ export default function(EventSource, backoff = create5xxBackoff()) {
this.previousEvent = this.currentEvent;
return throttledResolve(result);
});
}, configuration);
}, config);
if (typeof currentEvent !== 'undefined') {
this.currentEvent = currentEvent;
}
// only on initialization
// if we already have an currentEvent set via configuration
// dispatch the event so things are populated immediately
this.addEventListener('open', e => {
const currentEvent = e.target.getCurrentEvent();
if (typeof currentEvent !== 'undefined') {
this.dispatchEvent(currentEvent);
}
});
}
// if we are having these props, at least make getters
getCurrentEvent() {
Expand Down
11 changes: 6 additions & 5 deletions ui-v2/app/utils/dom/event-source/callable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const defaultRunner = function(target, configuration, isClosed) {
}
// TODO Consider wrapping this is a promise for none thenable returns
return target.source
.bind(target)(configuration)
.bind(target)(configuration, target)
.then(function(res) {
return defaultRunner(target, configuration, isClosed);
});
Expand Down Expand Up @@ -36,7 +36,7 @@ export default function(
this.readyState = 2;
this.source =
typeof source !== 'function'
? function(configuration) {
? function(configuration, target) {
this.close();
return P.resolve();
}
Expand All @@ -45,7 +45,7 @@ export default function(
P.resolve()
.then(() => {
// if we are already closed, don't do anything
if (this.readyState !== 0) {
if (this.readyState > 1) {
return;
}
this.readyState = 1; // open
Expand All @@ -68,13 +68,14 @@ export default function(
close() {
// additional readyState 3 = CLOSING
switch (this.readyState) {
case 0: // CONNECTING
case 2: // CLOSED
this.readyState = 2; // CLOSED
// it's already CLOSED , do nothing
break;
default:
this.readyState = 3; // CLOSING
}
// non-standard
return this;
}
};
}