@simplito/privmx-webendpoint
Version:
PrivMX Web Endpoint library
49 lines (46 loc) • 1.4 kB
JavaScript
;
/*!
PrivMX Web Endpoint.
Copyright © 2024 Simplito sp. z o.o.
This file is part of the PrivMX Platform (https://privmx.dev).
This software is Licensed under the PrivMX Free License.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Api = void 0;
const IdGenerator_1 = require("./IdGenerator");
class Api {
lib;
promises;
taskIdGenerator;
constructor(lib) {
this.lib = lib;
this.taskIdGenerator = new IdGenerator_1.IdGenerator();
this.promises = new Map();
this.setResultsCallback();
}
async runAsync(func) {
return new Promise((resolve, reject) => {
const taskId = this.generateId();
this.promises.set(taskId, { resolve, reject });
func(taskId);
});
}
resolveResult(result) {
if (result.status == true) {
this.promises.get(result.taskId).resolve(result.result);
}
else {
this.promises.get(result.taskId).reject(result.error);
}
this.promises.delete(result.taskId);
}
generateId() {
return this.taskIdGenerator.generateId();
}
setResultsCallback() {
this.lib.setResultsCallback((result) => this.resolveResult(result));
}
}
exports.Api = Api;