2
0
mirror of https://github.com/esiur/esiur-js.git synced 2026-04-03 21:48:21 +00:00
This commit is contained in:
2019-08-07 05:00:25 +03:00
parent 399a096c58
commit 7d2bcc60f7
20 changed files with 126 additions and 106 deletions

79
src/Core/AsyncBag.js Normal file
View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 25/07/2017.
*/
"use strict";
import AsyncReply from './AsyncReply.js';
export default class AsyncBag extends AsyncReply
{
constructor() {
super();
this.replies = [];
this.results = [];
this.count = 0;
this.sealedBag = false;
}
seal()
{
this.sealedBag = true;
if (this.results.length == 0)
this.trigger([]);
var self = this;
var singleTaskCompleted = function(taskIndex)
{
return function(results, reply){
self.results[taskIndex] = results;
self.count++;
if (self.count == self.results.length)
self.trigger(self.results);
};
};
for(var i = 0; i < this.results.length; i++)
this.replies[i].then(singleTaskCompleted(i));
/*
this.replies[i].then(function(r, reply){
self.results[self.replies.indexOf(reply)] = r;
self.count++;
if (self.count == self.results.length)
self.trigger(self.results);
});
*/
}
add(reply)
{
if (!this.sealedBag) {
this.replies.push(reply);
this.results.push(null);
}
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 18/11/2017.
*/
"use strict";
import ExceptionCode from './ExceptionCode.js';
export default class AsyncException extends Error
{
constructor()
{
super();
this.raised = false;
}
raise(type, code, message)
{
this.type = (type == 0 ? "Management" : "Execusion");
this.code = code;
if (type == 0)
{
for(var i in ExceptionCode)
if (ExceptionCode[i] == code)
{
this.message = i;
break;
}
}
else
this.message = message;
this.raised = true;
}
toString()
{
return this.type + " (" + this.code + ") : " + this.message;
}
}

71
src/Core/AsyncQueue.js Normal file
View File

@@ -0,0 +1,71 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 25/07/2017.
*/
"use strict";
import AsyncReply from './AsyncReply.js';
export default class AsyncQueue extends AsyncReply
{
constructor()
{
super();
this.list = [];
var self = this;
this.processQueue = function ()
{
for (var i = 0; i < self.list.length; i++)
if (self.list[i].ready)
{
self.trigger(self.list[i].result);
self.list.splice(i, 1);
i--;
}
else
break;
self.ready = (self.list.length == 0);
}
}
add(reply)
{
this.list.push(reply);
this.ready = false;
reply.then(this.processQueue);
}
remove(reply)
{
this.list.splice(this.list.indexOf(reply), 1);
this.processQueue();
}
}

160
src/Core/AsyncReply.js Normal file
View File

@@ -0,0 +1,160 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 25/07/2017.
*/
"use strict";
import AsyncException from './AsyncException.js';
export default class AsyncReply extends Promise
{
then(callback)
{
this.callbacks.push(callback);
if (this.ready)
callback(this.result, this);
return this;
}
// Alias for then()
done(callback)
{
this.then(callback);
}
// Alias for error()
catch(callback)
{
return this.error(callback);
}
error(callback)
{
this.errorCallbacks.push(callback);
if (this.exception.raised)
{
callback(this.exception);
}
return this;
}
progress(callback)
{
this.progressCallbacks.push(callback);
return this;
}
chunk(callback)
{
this.chunkCallbacks.push(callback);
return this;
}
// Alias for chunk()
next(callback)
{
this.chunk(callback);
}
trigger(result)
{
this.result = result;
this.ready = true;
for(var i = 0; i < this.callbacks.length; i++)
this.callbacks[i](result, this);
}
triggerError(type, code, message)
{
if (this.ready)
return;
if (type instanceof AsyncException)
this.exception.raise(type.type, type.code, type.message);
else
this.exception.raise(type, code, message);
if (this.errorCallbacks.length == 0)
throw this.exception;
else
for(var i = 0; i < this.errorCallbacks.length; i++)
this.errorCallbacks[i](this.exception, this);
}
triggerProgress(type, value, max)
{
if (this.ready)
return;
for(var i = 0; i < this.progressCallbacks.length; i++)
this.progressCallbacks[i](type, value, max, this);
}
triggerChunk(value)
{
if (this.ready)
return;
for(var i = 0; i < this.chunkCallbacks.length; i++)
this.chunkCallbacks[i](value, this);
}
constructor(result)
{
if (result instanceof Function)
{
super(result);
this.awaiter = result;
}
else
super(()=>{});
this.callbacks = [];
this.errorCallbacks = [];
this.progressCallbacks = [];
this.chunkCallbacks = [];
this.exception = new AsyncException();// null;
var self = this;
if (result !== undefined && !(result instanceof Function)) {
this.result = result;
this.ready = true;
}
else
{
this.ready = false;
this.result = null;
}
}
}

4
src/Core/ErrorType.js Normal file
View File

@@ -0,0 +1,4 @@
export default {
Management: 0,
Exception: 1
};

30
src/Core/ExceptionCode.js Normal file
View File

@@ -0,0 +1,30 @@
export default //const ExceptionCode =
{
HostNotReachable: 0,
AccessDenied: 1,
ResourceNotFound: 2,
AttachDenied: 3,
InvalidMethod: 4,
InvokeDenied: 5,
CreateDenied: 6,
AddParentDenied: 7,
AddChildDenied: 8,
ViewAttributeDenied: 9,
UpdateAttributeDenied: 10,
StoreNotFound: 11,
ParentNotFound: 12,
ChildNotFound: 13,
ResourceIsNotStore: 14,
DeleteDenied: 15,
DeleteFailed: 16,
UpdateAttributeFailed: 17,
GetAttributesFailed: 18,
ClearAttributesFailed: 19,
TemplateNotFound: 20,
RenameDenied: 21,
ClassNotFound: 22,
MethodNotFound: 23,
PropertyNotFound: 24,
SetPropertyDenied: 25,
ReadOnlyProperty: 26
};

42
src/Core/IDestructible.js Normal file
View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 31/08/2017.
*/
"use strict";
import IEventHandler from './IEventHandler.js';
export default class IDestructible extends IEventHandler
{
destroy()
{
this._emit("destroy", this);
}
constructor()
{
super();
}
}

96
src/Core/IEventHandler.js Normal file
View File

@@ -0,0 +1,96 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 30/08/2017.
*/
"use strict";
export default class IEventHandler
{
_register(event)
{
this._events[event] = [];
}
constructor()
{
this._events = {};
}
_emit(event)
{
event = event.toLowerCase();
var args = Array.prototype.slice.call(arguments, 1);
if (this._events[event])
for(var i = 0; i < this._events[event].length; i++)
if (this._events[event][i].f.apply(this._events[event][i].i, args))
return true;
return false;
}
_emitArgs(event, args)
{
event = event.toLowerCase();
if (this._events[event])
for(var i = 0; i < this._events[event].length; i++)
if (this._events[event][i].f.apply(this._events[event][i].i, args))
return true;
return this;
}
on(event, fn, issuer)
{
if (!(fn instanceof Function))
return this;
event = event.toLowerCase();
// add
if (!this._events[event])
this._events[event] = [];
this._events[event].push({f: fn, i: issuer == null ? this: issuer});
return this;
}
off(event, fn)
{
event = event.toLocaleString();
if (this._events[event])
{
if (fn)
{
for(var i = 0; i < this._events[event].length; i++)
if (this._events[event][i].f == fn)
this._events[event].splice(i--, 1);
//var index = this._events[event].indexOf(fn);
//if (index > -1)
//this._events[event].splice(index, 1);
}
else
{
this._events[event] = [];
}
}
}
}

4
src/Core/ProgressType.js Normal file
View File

@@ -0,0 +1,4 @@
export default {
Execution: 0,
Network: 1
};