inital #2
This commit is contained in:
85
out/helpers/resolvable.js
Normal file
85
out/helpers/resolvable.js
Normal file
@@ -0,0 +1,85 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.WaitForSync = exports.Resolvable = void 0;
|
||||
var ResolvableState;
|
||||
(function (ResolvableState) {
|
||||
ResolvableState[ResolvableState["WAITING"] = 0] = "WAITING";
|
||||
ResolvableState[ResolvableState["PENDING"] = 1] = "PENDING";
|
||||
ResolvableState[ResolvableState["ERROR"] = 2] = "ERROR";
|
||||
ResolvableState[ResolvableState["DONE"] = 3] = "DONE";
|
||||
})(ResolvableState || (ResolvableState = {}));
|
||||
class FetchOnce {
|
||||
constructor(fetchMethod) {
|
||||
this.fetchMethod = fetchMethod;
|
||||
this.state = ResolvableState.WAITING;
|
||||
this.pendings = [];
|
||||
}
|
||||
resolve(...args) {
|
||||
// eslint-disable-next-line promise/avoid-new
|
||||
return new Promise((resolve, reject) => {
|
||||
switch (this.state) {
|
||||
case ResolvableState.WAITING:
|
||||
this.state = ResolvableState.PENDING;
|
||||
this.pendings.push([resolve, reject]);
|
||||
if (this.fetchMethod)
|
||||
this.parsePromise(this.fetchMethod(...args));
|
||||
break;
|
||||
case ResolvableState.PENDING:
|
||||
this.pendings.push([resolve, reject]);
|
||||
break;
|
||||
case ResolvableState.DONE:
|
||||
resolve(this.data);
|
||||
break;
|
||||
case ResolvableState.ERROR:
|
||||
reject(this.error);
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
isFinished() {
|
||||
return this.state === ResolvableState.DONE || this.state === ResolvableState.ERROR;
|
||||
}
|
||||
parsePromise(promise) {
|
||||
promise.then((data) => {
|
||||
this.data = data;
|
||||
this.state = ResolvableState.DONE;
|
||||
this.pendings.forEach(pending => pending[0](data));
|
||||
}).catch(err => {
|
||||
this.error = err;
|
||||
this.state = ResolvableState.ERROR;
|
||||
this.pendings.forEach(pending => pending[1](err));
|
||||
});
|
||||
}
|
||||
}
|
||||
class Resolvable extends FetchOnce {
|
||||
constructor(fetchMethod) {
|
||||
super(fetchMethod);
|
||||
}
|
||||
}
|
||||
exports.Resolvable = Resolvable;
|
||||
class WaitForSync extends FetchOnce {
|
||||
constructor() {
|
||||
super(undefined);
|
||||
this.state = ResolvableState.PENDING;
|
||||
}
|
||||
setData(data) {
|
||||
if (!this.isFinished()) {
|
||||
this.parsePromise((() => __awaiter(this, void 0, void 0, function* () { return data; }))());
|
||||
}
|
||||
}
|
||||
setError(error) {
|
||||
if (!this.isFinished()) {
|
||||
this.parsePromise((() => __awaiter(this, void 0, void 0, function* () { throw error; }))());
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.WaitForSync = WaitForSync;
|
||||
Reference in New Issue
Block a user