110 lines
1.8 KiB
JavaScript
110 lines
1.8 KiB
JavaScript
|
|
/*
|
||
|
|
|
||
|
|
Copyright (c) 2020, 2023, The Unified Company.
|
||
|
|
|
||
|
|
This code is part of Unify.
|
||
|
|
|
||
|
|
This program is free software; you can redistribute it and/or modify
|
||
|
|
it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
|
||
|
|
as published by the ESA.
|
||
|
|
See the ESA Software Community License - Strong Copyleft LICENSE, for more details.
|
||
|
|
|
||
|
|
https://unifyjs.org
|
||
|
|
|
||
|
|
*/
|
||
|
|
|
||
|
|
|
||
|
|
export default class promiseManager{
|
||
|
|
|
||
|
|
promises = new Array();
|
||
|
|
|
||
|
|
messages = new Array();
|
||
|
|
|
||
|
|
socketManager;
|
||
|
|
|
||
|
|
addPromise( promiseObject ) {
|
||
|
|
|
||
|
|
this.promises.push( promiseObject );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
addMessage( message ) {
|
||
|
|
|
||
|
|
this.messages.push( message );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
getPromiseByID( id ) {
|
||
|
|
|
||
|
|
var promises = this.promises;
|
||
|
|
|
||
|
|
for( var c = 0; c < promises.length; c++ ) {
|
||
|
|
|
||
|
|
var currentPromise = promises[c];
|
||
|
|
|
||
|
|
if( currentPromise.id == id ) {
|
||
|
|
|
||
|
|
return currentPromise;
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
console.error( "Promise with id " + id + " not found", this );
|
||
|
|
|
||
|
|
return false;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
getMessageByID( id ) {
|
||
|
|
|
||
|
|
var messages = this.messages;
|
||
|
|
|
||
|
|
for( var c = 0; c < messages.length; c++ ) {
|
||
|
|
|
||
|
|
var message = messages[c];
|
||
|
|
|
||
|
|
if( message.id == id ) {
|
||
|
|
|
||
|
|
return message;
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
createPromise( messageID, resolveFunction, object ) {
|
||
|
|
|
||
|
|
var promiseObject = new Object();
|
||
|
|
|
||
|
|
promiseObject.id = messageID;
|
||
|
|
|
||
|
|
promiseObject.resolve = resolveFunction;
|
||
|
|
|
||
|
|
if( object ) {
|
||
|
|
|
||
|
|
promiseObject.object = object;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
createPromiseFunction( messageID, object ) {
|
||
|
|
|
||
|
|
var promiseManager = this;
|
||
|
|
|
||
|
|
function promiseFunction( resolveFunction ){
|
||
|
|
|
||
|
|
var promiseObject = this.createPromise( messageID, resolveFunction, object );
|
||
|
|
|
||
|
|
promiseManager.addPromise( promiseObject );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
return promiseFunction;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|