2
0
mirror of https://github.com/esiur/esiur-js.git synced 2025-06-27 15:23:11 +00:00

ES6 modules

This commit is contained in:
2019-07-15 14:22:40 +03:00
parent 1e522af044
commit fdfa690b23
50 changed files with 474 additions and 244 deletions

113
src/Data/AutoList.js Normal file
View File

@ -0,0 +1,113 @@
/*
* 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 05/09/2017.
*/
"use strict";
import IEventHandler from '../Engine/IEventHandler.js';
import IDestructible from '../Engine/IDestructible.js';
export default class AutoList extends IEventHandler
{
constructor()
{
super();
this.list = [];
}
get length()
{
return this.list.length;
}
add(value)
{
if (value instanceof IDestructible)
value.on("destroy", this._item_destroyed, this);
this.list.push(value);
this._emit("add", value);
}
set(index, value)
{
if (index >= this.list.length || index < 0)
return;
if (value instanceof IDestructible)
value.on("destroy", this._item_destroyed, this);
if (this.list[index] instanceof IDestructible)
this.list[index].off("destroy", this._item_destroyed);
this.list[index] = value;
}
at(index)
{
return this.list[index];
}
item(index)
{
return this.list[index];
}
remove(value)
{
this.removeAt(this.list.indexOf(value));
}
contains(value)
{
return this.list.indexOf(value) > -1;
}
toArray()
{
return this.list.slice(0);
}
removeAt(index)
{
if (index >= this.list.length || index < 0)
return;
var item = this.list[index];
if (item instanceof IDestructible)
item.off("destroy", this._item_destroyed);
this.list.splice(index, 1);
this._emit("remove", item);
}
_item_destroyed(sender)
{
this.remove(sender);
}
}

71
src/Data/AutoMap.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 05/09/2017.
*/
"use strict";
import IEventHandler from '../Engine/IEventHandler.js';
import IDestructible from '../Engine/IDestructible.js';
export default class AutoMap extends IEventHandler
{
constructor()
{
super();
this.dic = {};
}
add(key, value)
{
if (value instanceof IDestructible)
value.on("destroy", this._item_destroyed);
this.dic[key] = value;
this._emit("add", key, value);
}
set(key, value)
{
if (this.dic[key] !== undefined)
this.remove(key);
this.add(key, value);
}
remove(key)
{
if (this.dic[key] !== undefined) {
if (this.dic[key] instanceof IDestructible)
this.dic[key].off("destroy", this._item_destroyed);
delete this.dic[key];
}
}
_item_destroyed(sender)
{
this.remove(sender);
}
}

225
src/Data/BinaryList.js Normal file
View File

@ -0,0 +1,225 @@
/*
* 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/08/2017.
*/
"use strict";
import DataType from './DataType.js';
import DC from './DataConverter.js';
export default class BinaryList
{
constructor()
{
this.list = [];
}
addRange(bl)
{
for(var i = 0; i < bl.list.length; i++)
this.list.push(bl.list[i]);
return this;
}
add(typedValue, position)
{
if (position !== undefined)
this.list.splice(position, 0, typedValue);
else
this.list.push(typedValue);
return this;
}
get length()
{
return this.toArray().length;
}
toArray()
{
var ars = [];
// calculate length
for(var i = 0; i < this.list.length; i++)
{
switch (this.list[i].type)
{
case DataType.Bool:
ars.push(DC.boolToBytes(this.list[i].value));
break;
case DataType.UInt8:
ars.push(DC.uint8ToBytes(this.list[i].value));
break;
case DataType.Int8:
ars.push(DC.int8ToBytes(this.list[i].value));
break;
case DataType.Char:
ars.push(DC.charToBytes(this.list[i].value));
break;
case DataType.UInt16:
ars.push(DC.uint16ToBytes(this.list[i].value));
break;
case DataType.Int16:
ars.push(DC.int16ToBytes(this.list[i].value));
break;
case DataType.UInt32:
ars.push(DC.uint32ToBytes(this.list[i].value));
break;
case DataType.Int32:
ars.push(DC.int32ToBytes(this.list[i].value));
break;
case DataType.UInt64:
ars.push(DC.uint64ToBytes(this.list[i].value));
break;
case DataType.Int64:
ars.push(DC.int64ToBytes(this.list[i].value));
break;
case DataType.Float32:
ars.push(DC.float32ToBytes(this.list[i].value));
break;
case DataType.Float64:
ars.push(DC.float64ToBytes(this.list[i].value));
break;
case DataType.String:
ars.push(DC.stringToBytes(this.list[i].value));
break;
case DataType.DateTime:
ars.push(DC.dateTimeToBytes(this.list[i].value));
break;
case DataType.UInt8Array:
ars.push(this.list[i].value);
//case DataType.Resource:
// ars.push(DC.uint32ToBytes(this.list[i].value.instance.id));
// break;
//case DataType.DistributedResource:
// ars.push(DC.int8ToBytes(this.list[i].value));
// break;
}
}
var length = 0;
ars.forEach(function(a){
length += a.length;
});
var rt = new Uint8Array(length);
var offset = 0;
for(var i = 0; i < ars.length; i++) {
rt.set(ars[i], offset);
offset+=ars[i].length;
}
return rt;
}
toDC()
{
return new DC(this.toArray());
}
addDateTime(value, position)
{
return this.add({type: DataType.DateTime, value: value}, position);
}
addUint8Array(value, position)
{
return this.add({type: DataType.UInt8Array, value: value}, position);
}
addHex(value, position)
{
return this.addUint8Array(DC.hexToBytes(value), position);
}
addString(value, position)
{
return this.add({type: DataType.String, value: value}, position);
}
addUint8(value, position)
{
return this.add({type: DataType.UInt8, value: value}, position);
}
addInt8(value, position)
{
return this.add({type: DataType.Int8, value: value}, position);
}
addChar(value, position)
{
return this.add({type: DataType.Char, value: value}, position);
}
addUint16(value, position)
{
return this.add({type: DataType.UInt16, value: value}, position);
}
addInt16(value, position)
{
return this.add({type: DataType.Int16, value: value}, position);
}
addUint32(value, position)
{
return this.add({type: DataType.UInt32, value: value}, position);
}
addInt32(value, position)
{
return this.add({type: DataType.Int32, value: value}, position);
}
addUint64(value, position)
{
return this.add({type: DataType.UInt64, value: value}, position);
}
addInt64(value, position)
{
return this.add({type: DataType.Int64, value: value}, position);
}
addFloat32(value, position)
{
return this.add({type: DataType.Float32, value: value}, position);
}
addFloat64(value, position)
{
return this.add({type: DataType.Float64, value: value}, position);
}
}

925
src/Data/Codec.js Normal file
View File

@ -0,0 +1,925 @@
/*
* 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 DataType from './DataType.js';
import ResourceComparisonResult from './ResourceComparisionResult.js';
import StructureComparisonResult from './StructureComparisonResult.js';
export default class Codec {
static parse(data, offset, sizeObject, connection, dataType = DataType.Unspecified) {
var size;
var reply = new AsyncReply();
var isArray;
var t;
if (dataType == DataType.Unspecified) {
size = 1;
dataType = data[offset++];
}
else
size = 0;
t = dataType & 0x7F;
isArray = (dataType & 0x80) == 0x80;
var payloadSize = DataType.sizeOf(dataType);
var contentLength = 0;
// check if we have the enough data
if (payloadSize == -1) {
contentLength = data.getUint32(offset);
offset += 4;
size += 4 + contentLength;
}
else
size += payloadSize;
sizeObject.size = size;
if (isArray) {
switch (t) {
// VarArray ?
case DataType.Void:
return Codec.parseVarArray(data, offset, contentLength, connection);
case DataType.Bool:
return new AsyncReply(data.getBooleanArray(offset, contentLength));
case DataType.UInt8:
return new AsyncReply(data.getUint8Array(offset, contentLength));
case DataType.Int8:
return new AsyncReply(data.getInt8Array(offset, contentLength));
case DataType.Char:
return new AsyncReply(data.getCharArray(offset, contentLength));
case DataType.Int16:
return new AsyncReply(data.getInt16Array(offset, contentLength));
case DataType.UInt16:
return new AsyncReply(data.getUint16Array(offset, contentLength));
case DataType.Int32:
return new AsyncReply(data.getInt32Array(offset, contentLength));
case DataType.UInt32:
return new AsyncReply(data.getUint32Array(offset, contentLength));
case DataType.Int64:
return new AsyncReply(data.getInt64Array(offset, contentLength));
case DataType.UInt64:
return new AsyncReply(data.getUint64Array(offset, contentLength));
case DataType.Float32:
return new AsyncReply(data.getFloat32Array(offset, contentLength));
case DataType.Float64:
return new AsyncReply(data.getFloat64Array(offset, contentLength));
case DataType.String:
return new AsyncReply(data.getStringArray(offset, contentLength));
case DataType.Resource:
case DataType.DistributedResource:
return Codec.parseResourceArray(data, offset, contentLength, connection);
case DataType.DateTime:
return new AsyncReply(data.getDateTimeArray(offset, contentLength));
case DataType.Structure:
return Codec.parseStructureArray(data, offset, contentLength, connection);
}
}
else {
switch (t) {
case DataType.NotModified:
return new AsyncReply(new NotModified());
case DataType.Void:
return new AsyncReply(null);
case DataType.Bool:
return new AsyncReply(data.getBoolean(offset));
case DataType.UInt8:
return new AsyncReply(data[offset]);
case DataType.Int8:
return new AsyncReply(data.getInt8(offset));
case DataType.Char:
return new AsyncReply(data.getChar(offset));
case DataType.Int16:
return new AsyncReply(data.getInt16(offset));
case DataType.UInt16:
return new AsyncReply(data.getUint16(offset));
case DataType.Int32:
return new AsyncReply(data.getInt32(offset));
case DataType.UInt32:
return new AsyncReply(data.getUint32(offset));
case DataType.Int64:
return new AsyncReply(data.getInt64(offset));
case DataType.UInt64:
return new AsyncReply(data.getUint64(offset));
case DataType.Float32:
return new AsyncReply(data.getFloat32(offset));
case DataType.Float64:
return new AsyncReply(data.getFloat64(offset));
case DataType.String:
return new AsyncReply(data.getString(offset, contentLength));
case DataType.Resource:
return Codec.parseResource(data, offset);
case DataType.DistributedResource:
return Codec.parseDistributedResource(data, offset, connection);
case DataType.DateTime:
return new AsyncReply(data.getDateTime(offset));
case DataType.Structure:
return Codec.parseStructure(data, offset, contentLength, connection);
}
}
return null;
}
static parseResource(data, offset) {
return Warehouse.get(data.getUint32(offset));
}
static parseDistributedResource(data, offset, connection) {
//var g = data.getGuid(offset);
//offset += 16;
// find the object
var iid = data.getUint32(offset);
return connection.fetch(iid);// Warehouse.Get(iid);
}
/// <summary>
/// Parse an array of bytes into array of resources
/// </summary>
/// <param name="data">Array of bytes.</param>
/// <param name="length">Number of bytes to parse.</param>
/// <param name="offset">Zero-indexed offset.</param>
/// <param name="connection">DistributedConnection is required to fetch resources.</param>
/// <returns>Array of resources.</returns>
static parseResourceArray(data, offset, length, connection)
{
var reply = new AsyncBag();
if (length == 0)
{
reply.seal();
return reply;
}
var end = offset + length;
//
var result = data[offset++];
var previous = null;
if (result == ResourceComparisonResult.Null)
previous = new AsyncReply(null);
else if (result == ResourceComparisonResult.Local)
{
previous = Warehouse.get(data.getUint32(offset));
offset += 4;
}
else if (result == ResourceComparisonResult.Distributed)
{
previous = connection.fetch(data.getUint32(offset));
offset += 4;
}
reply.add(previous);
while (offset < end)
{
result = data[offset++];
var current = null;
if (result == ResourceComparisonResult.Null)
{
current = new AsyncReply(null);
}
else if (result == ResourceComparisonResult.Same)
{
current = previous;
}
else if (result == ResourceComparisonResult.Local)
{
current = Warehouse.get(data.getUint32(offset));
offset += 4;
}
else if (result == ResourceComparisonResult.Distributed)
{
current = connection.fetch(data.getUint32(offset));
offset += 4;
}
reply.add(current);
previous = current;
}
reply.seal();
return reply;
}
/// <summary>
/// Compose an array of property values.
/// </summary>
/// <param name="array">PropertyValue array.</param>
/// <param name="connection">DistributedConnection is required to check locality.</param>
/// <param name="prependLength">If True, prepend the length as UInt32 at the beginning of the output.</param>
/// <returns>Array of bytes in the network byte order.</returns>
static composePropertyValueArray(array, connection, prependLength = false)
{
var rt = BL();
for (var i = 0; i < array.Length; i++)
rt.addUint8Array(Codec.composePropertyValue(array[i], connection));
if (prependLength)
rt.addUint32(rt.length, 0);
return rt.toArray();
}
/// <summary>
/// Compose a property value.
/// </summary>
/// <param name="propertyValue">Property value</param>
/// <param name="connection">DistributedConnection is required to check locality.</param>
/// <returns>Array of bytes in the network byte order.</returns>
static composePropertyValue(propertyValue, connection)
{
// age, date, value
return BL().addUint64(propertyValue.age)
.addDateTime(propertyValue.date)
.addUint8Array(Codec.compose(propertyValue.value, connection))
.toArray();
}
/// <summary>
/// Parse property value.
/// </summary>
/// <param name="data">Array of bytes.</param>
/// <param name="offset">Zero-indexed offset.</param>
/// <param name="connection">DistributedConnection is required to fetch resources.</param>
/// <param name="cs">Output content size.</param>
/// <returns>PropertyValue.</returns>
static parsePropertyValue(data, offset, sizeObject, connection)
{
var reply = new AsyncReply();
var age = data.getUint64(offset);
offset += 8;
var date = data.getDateTime(offset);
offset += 8;
var cs = {};
Codec.parse(data, offset, cs, connection).then(function(value)
{
reply.trigger(new PropertyValue(value, age, date));
});
sizeObject.size = 16 + cs.size;
return reply;
}
/// <summary>
/// Parse resource history
/// </summary>
/// <param name="data">Array of bytes.</param>
/// <param name="offset">Zero-indexed offset.</param>
/// <param name="length">Number of bytes to parse.</param>
/// <param name="resource">Resource</param>
/// <param name="fromAge">Starting age.</param>
/// <param name="toAge">Ending age.</param>
/// <param name="connection">DistributedConnection is required to fetch resources.</param>
/// <returns></returns>
static parseHistory(data, offset, length, resource, connection)
{
var list = new KeyList();
var reply = new AsyncReply();
var bagOfBags = new AsyncBag();
var ends = offset + length;
while (offset < ends)
{
var index = data[offset++];
var pt = resource.instance.template.getPropertyTemplateByIndex(index);
list.add(pt, null);
var cs = data.getUint32(offset);
offset += 4;
bagOfBags.add(Codec.parsePropertyValueArray(data, offset, cs, connection));
offset += cs;
}
bagOfBags.seal();
bagOfBags.then(x =>
{
for(var i = 0; i < list.length; i++)
list.values[i] = x[i];
reply.trigger(list);
});
return reply;
}
/// <summary>
/// Compose resource history
/// </summary>
/// <param name="history">History</param>
/// <param name="connection">DistributedConnection is required to fetch resources.</param>
/// <returns></returns>
static composeHistory(history, connection, prependLength = false)
{
var rt = new BinaryList();
for (var i = 0; i < history.length; i++)
rt.addUint8(history.keys[i].index).addUint8Array(Codec.composePropertyValueArray(history.values[i], connection, true));
if (prependLength)
rt.addUint32(rt.length, 0);
return rt.toArray();
}
/// <summary>
/// Parse an array of ProperyValue.
/// </summary>
/// <param name="data">Array of bytes.</param>
/// <param name="offset">Zero-indexed offset.</param>
/// <param name="length">Number of bytes to parse.</param>
/// <param name="connection">DistributedConnection is required to fetch resources.</param>
/// <returns></returns>
static parsePropertyValueArray(data, offset, length, connection)
{
var rt = new AsyncBag();
while (length > 0)
{
var cs = {};
rt.add(Codec.parsePropertyValue(data, offset, cs, connection));
if (cs.size > 0)
{
offset += cs.size;
length -= cs.size;
}
else
throw new Exception("Error while parsing ValueInfo structured data");
}
rt.seal();
return rt;
}
static parseStructure(data, offset, contentLength, connection, metadata = null, keys = null, types = null)
{
var reply = new AsyncReply();
var bag = new AsyncBag();
var keylist = [];
var typelist = [];
if (keys == null) {
while (contentLength > 0) {
var len = data[offset++];
keylist.push(data.getString(offset, len));
offset += len;
typelist.push(data[offset]);
var rt = {};
bag.add(Codec.parse(data, offset, rt, connection));
contentLength -= rt.size + len + 1;
offset += rt.size;
}
}
else if (types == null) {
for (var i = 0; i < keys.length; i++)
keylist.push(keys[i]);
while (contentLength > 0) {
typelist.push(data[offset]);
var rt = {};
bag.add(Codec.parse(data, offset, rt, connection));
contentLength -= rt.size;
offset += rt.size;
}
}
else {
for (var i = 0; i < keys.length; i++) {
keylist.push(keys[i]);
typelist.push(types[i]);
}
var i = 0;
while (contentLength > 0) {
var rt = {};
bag.add(Codec.parse(data, offset, rt, connection, types[i]));
contentLength -= rt.size;
offset += rt.size;
i++;
}
}
bag.seal();
bag.then(function (res) {
// compose the list
var s = new Structure();
for (var i = 0; i < keylist.length; i++)
s[keylist[i]] = res[i];
reply.trigger(s);
});
if (metadata != null)
{
metadata.keys = keylist;
metadata.types = typelist;
}
return reply;
}
static parseVarArray(data, offset, contentLength, connection) {
var rt = new AsyncBag();
while (contentLength > 0) {
var cs = {};
rt.add(Codec.parse(data, offset, cs, connection));
if (cs.size > 0) {
offset += cs.size;
contentLength -= cs.size;
}
else
throw new Exception("Error while parsing structured data");
}
rt.seal();
return rt;
}
static compose(value, connection, prependType = true) {
if (value instanceof Function)
value = value(connection);
else if (value instanceof DistributedPropertyContext)
value = value.method(this);
var type = Codec.getDataType(value, connection);
var rt = new BinaryList();
switch (type) {
case DataType.Void:
// nothing to do;
break;
case DataType.String:
var st = DC.stringToBytes(value);
rt.addUint32(st.length).addUint8Array(st);
break;
case DataType.Resource:
rt.addUint32(value._p.instanceId);
break;
case DataType.DistributedResource:
// rt.addUint8Array(DC.stringToBytes(value.instance.template.classId)).addUint32(value.instance.id);
rt.addUint32(value.instance.id);
break;
case DataType.Structure:
rt.addUint8Array(Codec.composeStructure(value, connection, true, true, true));
break;
case DataType.VarArray:
rt.addUint8Array(Codec.composeVarArray(value, connection, true));
break;
case DataType.ResourceArray:
rt.addUint8Array(Codec.composeResourceArray(value, connection, true));
break;
case DataType.StructureArray:
rt.addUint8Array(Codec.composeStructureArray(value, connection, true));
break;
default:
rt.add({type: type, value: value});
if (DataType.isArray(type))
rt.addUint32(rt.length, 0);
break;
}
if (prependType)
rt.addUint8(type, 0);
return rt.toArray();
}
static composeVarArray(array, connection, prependLength = false) {
var rt = new BinaryList();
for (var i = 0; i < array.length; i++)
rt.addUint8Array(Codec.compose(array[i], connection));
if (prependLength)
rt.addUint32(rt.length, 0);
return rt.toArray();
}
static composeStructure(value, connection, includeKeys = true, includeTypes = true, prependLength = false) {
var rt = new BinaryList();
var keys = value.getKeys();
if (includeKeys) {
for (var i = 0; i < keys.length; i++) {
var key = DC.stringToBytes(keys[i]);
rt.addUint8(key.length).addUint8Array(key).addUint8Array(Codec.compose(value[keys[i]], connection));
}
}
else {
for (var i = 0; i < keys.length; i++)
rt.addUint8Array(Codec.compose(value[keys[i]], connection, includeTypes));
}
if (prependLength)
rt.addUint32(rt.length, 0);
return rt.toArray();
}
static composeStructureArray(structures, connection, prependLength = false) {
if (structures == null || structures.length == 0 || !(structures instanceof StructureArray))
return new DC(0);
var rt = new BinaryList();
var comparision = StructureComparisonResult.Structure;
rt.addUint8(comparision);
rt.addUint8Array(Codec.composeStructure(structures[0], connection));
for (var i = 1; i < structures.Length; i++) {
comparision = Codec.compareStructure(structures[i - 1], structures[i], connection);
rt.addUint8(comparision);
if (comparision == StructureComparisonResult.Structure)
rt.addUint8Array(Codec.composeStructure(structures[i], connection));
else if (comparision == StructureComparisonResult.StructureSameKeys)
rt.addUint8Array(Codec.composeStructure(structures[i], connection, false));
else if (comparision == StructureComparisonResult.StructureSameTypes)
rt.addUint8Array(Codec.composeStructure(structures[i], connection, false, false));
}
if (prependLength)
rt.addUint32(rt.length, 0);
return rt.toArray();
}
static compareStructure(previous, next, connection) {
if (next == null)
return StructureComparisonResult.Null;
if (previous == null)
return StructureComparisonResult.Structure;
if (next == previous)
return StructureComparisonResult.Same;
if (previous.length != next.length)
return StructureComparisonResult.Structure;
var previousKeys = previous.getKeys();
var nextKeys = next.getKeys();
for (var i = 0; i < previousKeys.length; i++)
if (previousKeys[i] != nextKeys[i])
return StructureComparisonResult.Structure;
var previousTypes = Codec.getStructureDateTypes(previous, connection);
var nextTypes = Codec.getStructureDateTypes(next, connection);
for (var i = 0; i < previousTypes.length; i++)
if (previousTypes[i] != nextTypes[i])
return StructureComparisonResult.StructureSameKeys;
return StructureComparisonResult.StructureSameTypes;
}
static getStructureDateTypes(structure, connection) {
var keys = structure.getKeys();
var types = [];
for (var i = 0; i < keys.length; i++)
types.push(Codec.getDataType(structure[keys[i]], connection));
return types;
}
static isLocalResource(resource, connection) {
if (resource instanceof DistributedResource)
if (resource._p.connection == connection)
return true;
return false;
}
static composeResource(resource, connection) {
if (Codec.isLocalResource(resource, connection))
return BL().addUint32(resource.id);
else {
return BL().addUint8Array(resource.instance.template.classId.value).addUint32(resource.instance.id);
}
}
static compareResource(previous, next, connection) {
if (next == null)
return ResourceComparisonResult.Null;
else if (next == previous)
return ResourceComparisonResult.Same;
else if (Codec.isLocalResource(next, connection))
return ResourceComparisonResult.Local;
else
return ResourceComparisonResult.Distributed;
}
static composeResourceArray(resources, connection, prependLength = false) {
if (resources == null || resources.length == 0)// || !(resources instanceof ResourceArray))
return prependLength ? new DC(4) : new DC(0);
var rt = new BinaryList();
var comparsion = Codec.compareResource(null, resources[0], connection);
rt.addUint8(comparsion);
if (comparsion == ResourceComparisonResult.Local)
rt.addUint32(resources[0]._p.instanceId);
else if (comparsion == ResourceComparisonResult.Distributed)
rt.addUint32(resources[0].instance.id);
for (var i = 1; i < resources.Length; i++)
{
comparsion = Codec.compareResource(resources[i - 1], resources[i], connection);
rt.addUint8(comparsion);
if (comparsion == ResourceComparisonResult.Local)
rt.addUint32(resources[i]._p.instanceId);
else if (comparsion == ResourceComparisonResult.Distributed)
rt.addUint32(resources[i].instance.id);
}
if (prependLength)
rt.addUint32(rt.length, 0);
return rt.toArray();
}
static getDataType(value) {
switch (typeof value) {
case "number":
// float or ?
if (Math.floor(value) == value) {
if (value > 0) {
// larger than byte ?
if (value > 0xFF) {
// larger than short ?
if (value > 0xFFFF) {
// larger than int ?
if (value > 0xFFFFFFFF) {
return DataType.UInt64;
}
else {
return DataType.UInt32;
}
}
else {
return DataType.UInt16;
}
}
else {
return DataType.UInt8;
}
}
else {
if (value < -128) {
if (value < -32768) {
if (value < -2147483648) {
return DataType.Int64;
}
else {
return DataType.Int32;
}
}
else {
return DataType.Int16;
}
}
else {
return DataType.Int8;
}
}
}
else {
// float or double
return DataType.Float64;
}
break;
case "string":
return DataType.String;
case "boolean":
return DataType.Bool;
case "object":
if (value instanceof Array) {
return DataType.VarArray;
}
else if (value instanceof IResource) {
return Codec.isLocalResource(value, connection) ? DataType.Resource : DataType.DistributedResource;
}
else if (value instanceof Date) {
return DataType.DateTime;
}
else if (value instanceof Uint8Array
|| value instanceof ArrayBuffer) {
return DataType.UInt8Array;
}
else if (value instanceof Number) {
// JS numbers are always 64-bit float
return DataType.Float64;
}
else if (value instanceof Structure) {
return DataType.Structure;
}
else {
return DataType.Void
}
break;
default:
return DataType.Void;
}
}
/// <summary>
/// Parse an array of structures
/// </summary>
/// <param name="data">Bytes array</param>
/// <param name="offset">Zero-indexed offset</param>
/// <param name="length">Number of bytes to parse</param>
/// <param name="connection">DistributedConnection is required in case a structure in the array holds items at the other end</param>
/// <returns>Array of structures</returns>
static parseStructureArray(data, offset, length, connection)
{
var reply = new AsyncBag();
if (length == 0)
{
reply.seal();
return reply;
}
var end = offset + length;
var result = data[offset++];
var previous = null;
//var previousKeys = [];
//var previousTypes = [];
var metadata = {keys: null, types: null};
if (result == StructureComparisonResult.Null)
previous = new AsyncReply(null);
else if (result == StructureComparisonResult.Structure)
{
var cs = data.getUint32(offset);
offset += 4;
previous = this.parseStructure(data, offset, cs, connection, metadata);
offset += cs;
}
reply.add(previous);
while (offset < end)
{
result = data[offset++];
if (result == StructureComparisonResult.Null)
previous = new AsyncReply(null);
else if (result == StructureComparisonResult.Structure)
{
var cs = data.getUint32(offset);
offset += 4;
previous = this.parseStructure(data, offset, cs, connection, metadata);
offset += cs;
}
else if (result == StructureComparisonResult.StructureSameKeys)
{
var cs = data.getUint32(offset);
offset += 4;
previous = this.parseStructure(data, offset, cs, connection, metadata, metadata.keys);
offset += cs;
}
else if (result == StructureComparisonResult.StructureSameTypes)
{
var cs = data.getUint32(offset);
offset += 4;
previous = this.parseStructure(data, offset, cs, connection, metadata, metadata.keys, metadata.types);
offset += cs;
}
reply.add(previous);
}
reply.seal();
return reply;
}
}

509
src/Data/DataConverter.js Normal file
View File

@ -0,0 +1,509 @@
/*
* 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 BinaryList from './BinaryList.js';
import Guid from './Guid.js';
export const UNIX_EPOCH = 621355968000000000;
export const TWO_PWR_32 = (1 << 16) * (1 << 16);
export default class DC extends Uint8Array
{
constructor(bufferOrSize) {
super(bufferOrSize);
//if (bufferOrSize instanceof ArrayBuffer) {
// this.buffer = bufferOrSize;
//}
//else
//{
// this.buffer = new Uint8Array(bufferOrSize);
//}
this.dv = new DataView(this.buffer);
}
static boolToBytes(value)
{
var rt = new DC(1);
rt.setBoolean(0, value);
return rt;
}
static int8ToBytes(value)
{
var rt = new DC(1);
rt.setInt8(0, value);
return rt;
}
static hexToBytes(value)
{
// convert hex to Uint8Array
var rt = new DC(value.length/2);
for(var i = 0; i < ar.length; i++)
rt[i] = parseInt(value.substr(i*2, 2), 16);
return rt;
}
static uint8ToBytes(value)
{
var rt = new DC(1);
rt.setUint8(0, value);
return rt;
}
static charToBytes(value)
{
var rt = new DC(2);
rt.setChar(0, value);
return rt;
}
static int16ToBytes(value)
{
var rt = new DC(2);
rt.setInt16(0, value);
return rt;
}
static uint16ToBytes(value)
{
var rt = new DC(2);
rt.setUint16(0, value);
return rt;
}
static int32ToBytes(value)
{
var rt = new DC(4);
rt.setInt32(0, value);
return rt;
}
static uint32ToBytes(value)
{
var rt = new DC(4);
rt.setUint32(0, value);
return rt;
}
static float32ToBytes(value)
{
var rt = new DC(4);
rt.setFloat32(0, value);
return rt;
}
static int64ToBytes(value)
{
var rt = new DC(8);
rt.setInt64(0, value);
return rt;
}
static uint64ToBytes(value)
{
var rt = new DC(8);
rt.setUint64(0, value);
return rt;
}
static float64ToBytes(value)
{
var rt = new DC(8);
rt.setFloat64(0, value);
return rt;
}
static dateTimeToBytes(value)
{
var rt = new DC(8);
rt.setDateTime(0, value);
return rt;
}
static stringToBytes(value)
{
var utf8 = unescape(encodeURIComponent(value));
var rt = [];
for (var i = 0; i < utf8.length; i++)
rt.push(utf8.charCodeAt(i));
return new DC(rt);
}
static stringArrayToBytes(values)
{
var list = new BinaryList();
for(var i = 0; i < values.length; i++)
{
var s = DC.stringToBytes(values[i]);
list.addUint32(s.length).addUint8Array(s);
}
return list.toArray();
}
append(src, offset, length)
{
if (!(src instanceof DC))
src = new DC(src);
var appendix = src.clip(offset, length);
var rt = new DC(this.length + appendix.length);
rt.set(this, 0);
rt.set(appendix, this.length);
return rt;
}
static combine(a, aOffset, aLength, b, bOffset, bLength)
{
if (!(a instanceof DC))
a = new DC(a);
if (!(b instanceof DC))
b = new DC(b);
a = a.clip(aOffset, aLength);
b = b.clip(bOffset, bLength);
var rt = new DC(a.length + b.length);
rt.set(a, 0);
rt.set(b, a.length);
return rt;
}
clip(offset, length)
{
return this.slice(offset, offset+length);
}
getInt8(offset)
{
return this.dv.getUint8(offset);
}
getUint8(offset)
{
return this[offset];// this.dv.getUint8(offset);
}
getInt16(offset)
{
return this.dv.getInt16(offset);
}
getUint16(offset)
{
return this.dv.getUint16(offset);
}
getInt32(offset)
{
return this.dv.getInt32(offset);
}
getUint32(offset)
{
return this.dv.getUint32(offset);
}
getFloat32(offset)
{
return this.dv.getFloat32(offset);
}
getFloat64(offset)
{
return this.dv.getFloat64(offset);
}
setInt8(offset, value)
{
return this.dv.setInt8(offset, value);
}
setUint8(offset, value)
{
return this.dv.setUint8(offset, value);
}
setInt16(offset, value)
{
return this.dv.setInt16(offset, value);
}
setUint16(offset, value)
{
return this.dv.setUint16(offset, value);
}
setInt32(offset, value)
{
return this.dv.setInt32(offset, value);
}
setUint32(offset, value)
{
return this.dv.setUint32(offset, value);
}
setFloat32(offset, value)
{
return this.dv.setFloat32(offset, value);
}
setFloat64(offset, value)
{
return this.dv.setFloat64(offset, value);
}
getInt8Array(offset, length)
{
return new Int8Array(this.buffer, offset, length);
}
getUint8Array(offset, length)
{
return new Uint8Array(this.buffer, offset, length);
}
getInt16Array(offset, length)
{
return new Int16Array(this.buffer, offset, length);
}
getUint16Array(offset, length)
{
return new Uint16Array(this.buffer, offset, length);
}
getInt32Array(offset, length)
{
return new Int32Array(this.buffer, offset, length);
}
getUint32Array(offset, length)
{
return new Uint32Array(this.buffer, offset, length);
}
getFloat32Array(offset, length)
{
return new Float32Array(this.buffer, offset, length);
}
getFloat64Array(offset, length)
{
return new Float64Array(this.buffer, offset, length);
}
getInt64Array(offset, length)
{
return new Int64Array(this.buffer, offset, length);
}
getUint64Array(offset, length)
{
return new Uint64Array(this.buffer, offset, length);
}
getBoolean(offset)
{
return this.getUint8(offset) > 0;
}
setBoolean(offset, value)
{
this.setUint8(offset, value ? 1: 0);
}
getBooleanArray(offset, length)
{
var rt = [];
for(var i = 0; i < length; i++)
rt.push(this.getBoolean(offset+i));
return rt;
}
getChar(offset)
{
return String.fromCharCode(this.getUint16(offset));
}
setChar(offset, value)
{
this.setUint16(offset, value.charCodeAt(0));
}
getCharArray(offset, length)
{
var rt = [];
for(var i = 0; i < length; i+=2)
rt.push(this.getChar(offset+i));
return rt;
}
getHex(offset, length)
{
var rt = "";
for(var i = offset; i < offset + length; i++) {
var h = this[i].toString(16);
rt += h.length == 1 ? "0" + h : h;
}
return rt;
}
getString(offset, length)
{
if (typeof StringView != "undefined")
return new StringView(this.buffer, "UTF-8", offset, length);
else
{
var bytes = this.getUint8Array(offset, length);
var encodedString = String.fromCharCode.apply(null, bytes),
decodedString = decodeURIComponent(escape(encodedString));
return decodedString;
}
}
getStringArray(offset, length)
{
var rt = [];
var i = 0;
while (i < length)
{
var cl = this.getUint32(offset + i);
i += 4;
rt.push(this.getString(offset + i, cl));
i += cl;
}
return rt;
}
getInt64(offset)
{
var h = this.getInt32(offset);
var l = this.getInt32(offset + 4);
return h * TWO_PWR_32 + ((l >= 0) ? l : TWO_PWR_32 + l);
}
getUint64(offset)
{
var h = this.getUint32(offset);
var l = this.getUint32(offset + 4);
return h * TWO_PWR_32 + ((l >= 0) ? l : TWO_PWR_32 + l);
}
setInt64(offset, value)
{
var l = (value % TWO_PWR_32) | 0;
var h = (value / TWO_PWR_32) | 0;
this.setInt32(offset, h);
this.setInt32(offset + 4, l);
}
setUint64(offset, value)
{
var l = (value % TWO_PWR_32) | 0;
var h = (value / TWO_PWR_32) | 0;
this.setInt32(offset, h);
this.setInt32(offset + 4, l);
}
setDateTime(offset, value)
{
// Unix Epoch
var ticks = 621355968000000000 + (value.getTime() * 10000);
this.setUint64(offset, ticks);
}
getDateTime(offset)
{
var ticks = this.getUint64(offset);
return new Date(Math.round((ticks-UNIX_EPOCH)/10000));
}
getDateTimeArray(offset)
{
var rt = [];
for(var i = 0; i < length; i+=8)
rt.push(this.getDateTime(offset+i));
return rt;
}
getGuid(offset)
{
return new Guid(this.clip(offset, 16));
/*
var d = this.getUint8Array(offset, 16);
var rt = "";
for (var i = 0; i < 16; i++) {
rt += String.fromCharCode(d[i]);
}
return btoa(rt);
*/
}
getGuidArray(offset, length)
{
var rt = [];
for(var i = 0; i < length; i+=16)
rt.push(this.getGuid(offset+i));
return rt;
}
sequenceEqual(ar)
{
if (ar.length != this.length)
return false;
else
{
for(var i = 0; i < this.length; i++)
if (ar[i] != this[i])
return false;
}
return true;
}
}
export function BL(){
return new BinaryList();
};
export {DC};

114
src/Data/DataType.js Normal file
View File

@ -0,0 +1,114 @@
/*
* 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";
export default //const DataType =
{
Void: 0x0,
//Variant,
Bool: 1,
Int8: 2,
UInt8: 3,
Char: 4,
Int16: 5,
UInt16: 6,
Int32: 7,
UInt32: 8,
Int64: 9,
UInt64: 10,
Float32: 11,
Float64: 12,
Decimal: 13,
DateTime: 14,
Resource: 15,
DistributedResource: 16,
ResourceLink: 17,
String: 18,
Structure: 19,
//Stream,
//Array = 0x80,
VarArray: 0x80,
BoolArray: 0x81,
UInt8Array: 0x82,
Int8Array: 0x83,
CharArray: 0x84,
Int16Array: 0x85,
UInt16Array: 0x86,
Int32Array: 0x87,
UInt32Array: 0x88,
Int64Array: 0x89,
UInt64Array: 0x8A,
Float32Array: 0x8B,
Float64Array: 0x8C,
DecimalArray: 0x8D,
DateTimeArray: 0x8E,
ResourceArray: 0x8F,
DistributedResourceArray: 0x90,
ResourceLinkArray: 0x91,
StringArray: 0x92,
StructureArray: 0x93,
NotModified: 0x7f,
Unspecified: 0xff,
isArray: function(type)
{
return ((type & 0x80) == 0x80) && (type != DataType.NotModified);
},
sizeOf: function(type)
{
switch (type)
{
case DataType.Void:
case DataType.NotModified:
return 0;
case DataType.Bool:
case DataType.Int8:
case DataType.UInt8:
return 1;
case DataType.Char:
case DataType.Int16:
case DataType.UInt16:
return 2;
case DataType.Int32:
case DataType.UInt32:
case DataType.Float32:
case DataType.Resource:
return 4;
case DataType.Int64:
case DataType.UInt64:
case DataType.Float64:
case DataType.DateTime:
return 8;
case DataType.DistributedResource:
return 4;
default:
return -1;
}
}
};

40
src/Data/Guid.js Normal file
View File

@ -0,0 +1,40 @@
/*
* 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 02/09/2017.
*/
"use strict";
export default class Guid
{
constructor(dc)
{
this.value = dc;
}
valueOf()
{
return this.value.getHex(0, 16);
}
}

116
src/Data/KeyList.js Normal file
View File

@ -0,0 +1,116 @@
/*
* 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 06/11/2017.
*/
"use strict";
import IDestructible from '../Engine/IDestructible.js';
export default class KeyList
{
constructor()
{
this.keys = [];
this.values = [];
}
at(index)
{
return this.values[index];
}
item(key)
{
for(var i = 0; i < this.keys.length; i++)
if (this.keys[i] == key)
return this.values[i];
}
get(key)
{
for(var i = 0; i < this.keys.length; i++)
if (this.keys[i] == key)
return this.values[i];
}
_item_destroyed(sender)
{
for(var i = 0; i < this.values.length; i++)
if (sender == this.values[i])
{
this.removeAt(i);
break;
}
}
add(key, value)
{
this.remove(key);
if (value instanceof IDestructible)
value.on("destroy", this._item_destroyed, this);
this.keys.push(key);
this.values.push(value);
}
contains(key)
{
for(var i = 0; i < this.keys.length; i++)
if (this.keys[i] == key)
return true;
return false;
}
set(key, value)
{
this.remove(key);
this.add(key, value);
}
remove(key)
{
for(var i = 0; i < this.keys.length; i++)
if (key == this.keys[i])
{
this.removeAt(i);
break;
}
}
removeAt(index)
{
if (this.values[index] instanceof IDestructible)
this.values[index].off("destroy", this._item_destroyed);
this.keys.splice(index, 1);
this.values.splice(index, 1);
}
get length()
{
return this.keys.length;
}
}

32
src/Data/NotModified.js Normal file
View File

@ -0,0 +1,32 @@
/*
* 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 26/08/2017.
*/
"use strict";
export default class NotModified
{
}

37
src/Data/PropertyValue.js Normal file
View File

@ -0,0 +1,37 @@
/*
* 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 06/11/2017.
*/
"use strict";
export default class PropertyValue
{
constructor(value, age, date)
{
this.value = value;
this.age = age;
this.date = date;
}
}

38
src/Data/ResourceArray.js Normal file
View File

@ -0,0 +1,38 @@
/*
* 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 26/08/2017.
*/
"use strict";
export default class ResourceArray extends Array
{
push(value)
{
if (value instanceof IResource)
super.push(value);
else
return;
}
}

View File

@ -0,0 +1,7 @@
export default // const ResourceComparisonResult =
{
Null: 0,
Distributed: 1,
Local: 2,
Same: 3
};

46
src/Data/Structure.js Normal file
View File

@ -0,0 +1,46 @@
/*
* 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 26/08/2017.
*/
"use strict";
export default class Structure
{
getKeys() {
var rt = [];
for (var i in this)
if (!(this[i] instanceof Function))
rt.push(i);
return rt;
}
constructor(data)
{
if (data instanceof Object)
for(var i in data)
this[i] = data[i];
}
}

View File

@ -0,0 +1,39 @@
/*
* 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 06/09/2017.
*/
"use strict";
export default class StructureArray extends Array
{
push(value)
{
if (value instanceof Structure)
super.push(value);
else
return;
}
}

View File

@ -0,0 +1,9 @@
export default //const StructureComparisonResult =
{
Null: 0,
Structure: 1,
StructureSameKeys: 2,
StructureSameTypes: 3,
Same: 4
};