2019-02-21 10:36:15 +00:00
|
|
|
export const defaultRunner = function(target, configuration, isClosed) {
|
|
|
|
if (isClosed(target)) {
|
2019-05-01 18:09:29 +00:00
|
|
|
target.dispatchEvent({ type: 'close' });
|
2019-02-21 10:36:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO Consider wrapping this is a promise for none thenable returns
|
|
|
|
return target.source
|
2019-06-20 09:23:36 +00:00
|
|
|
.bind(target)(configuration, target)
|
2019-02-21 10:36:15 +00:00
|
|
|
.then(function(res) {
|
|
|
|
return defaultRunner(target, configuration, isClosed);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const errorEvent = function(e) {
|
|
|
|
return new ErrorEvent('error', {
|
|
|
|
error: e,
|
|
|
|
message: e.message,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const isClosed = function(target) {
|
|
|
|
switch (target.readyState) {
|
|
|
|
case 2: // CLOSED
|
|
|
|
case 3: // CLOSING
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
export default function(
|
|
|
|
EventTarget,
|
|
|
|
P = Promise,
|
|
|
|
run = defaultRunner,
|
|
|
|
createErrorEvent = errorEvent
|
|
|
|
) {
|
|
|
|
return class extends EventTarget {
|
|
|
|
constructor(source, configuration = {}) {
|
|
|
|
super();
|
|
|
|
this.readyState = 2;
|
|
|
|
this.source =
|
|
|
|
typeof source !== 'function'
|
2019-06-20 09:23:36 +00:00
|
|
|
? function(configuration, target) {
|
2019-02-21 10:36:15 +00:00
|
|
|
this.close();
|
|
|
|
return P.resolve();
|
|
|
|
}
|
|
|
|
: source;
|
2019-05-15 13:59:59 +00:00
|
|
|
this.readyState = 0; // CONNECTING
|
2019-02-21 10:36:15 +00:00
|
|
|
P.resolve()
|
|
|
|
.then(() => {
|
2019-05-15 13:59:59 +00:00
|
|
|
// if we are already closed, don't do anything
|
2019-06-20 09:23:36 +00:00
|
|
|
if (this.readyState > 1) {
|
2019-05-15 13:59:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-02-21 10:36:15 +00:00
|
|
|
this.readyState = 1; // open
|
2019-05-15 13:59:59 +00:00
|
|
|
// the connection _was just_ opened
|
2019-02-21 10:36:15 +00:00
|
|
|
this.dispatchEvent({ type: 'open' });
|
|
|
|
return run(this, configuration, isClosed);
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.dispatchEvent(createErrorEvent(e));
|
|
|
|
// close after the dispatch so we can tell if it was an error whilst closed or not
|
|
|
|
// but make sure its before the promise tick
|
|
|
|
this.readyState = 2; // CLOSE
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// This only gets called when the promise chain completely finishes
|
|
|
|
// so only when its completely closed.
|
|
|
|
this.readyState = 2; // CLOSE
|
|
|
|
});
|
|
|
|
}
|
|
|
|
close() {
|
|
|
|
// additional readyState 3 = CLOSING
|
2019-05-15 13:59:59 +00:00
|
|
|
switch (this.readyState) {
|
2019-07-01 20:20:21 +00:00
|
|
|
case 0: // CONNECTING
|
2019-05-15 13:59:59 +00:00
|
|
|
case 2: // CLOSED
|
2019-07-01 20:20:21 +00:00
|
|
|
this.readyState = 2;
|
2019-05-15 13:59:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
2019-07-01 20:20:21 +00:00
|
|
|
// OPEN
|
2019-05-15 13:59:59 +00:00
|
|
|
this.readyState = 3; // CLOSING
|
2019-02-21 10:36:15 +00:00
|
|
|
}
|
2019-06-20 09:23:36 +00:00
|
|
|
// non-standard
|
|
|
|
return this;
|
2019-02-21 10:36:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|