2
0
mirror of https://github.com/esiur/esiur-js.git synced 2025-05-06 12:32:58 +00:00
This commit is contained in:
Ahmed Zamil 2021-02-20 00:09:06 +03:00
parent e6a28da68d
commit 0efb266c2a
14 changed files with 1458 additions and 905 deletions

View File

@ -5117,7 +5117,7 @@ var DistributedConnection = /*#__PURE__*/function (_IStore) {
key: "IIPReportProgress", key: "IIPReportProgress",
value: function IIPReportProgress(callbackId, type, value, max) { value: function IIPReportProgress(callbackId, type, value, max) {
var req = this.requests.item(callbackId); var req = this.requests.item(callbackId);
req === null || req === void 0 ? void 0 : req.triggerProgress(type, value, max); if (req != null) req.triggerProgress(type, value, max);
} }
}, { }, {
key: "IIPReportChunk", key: "IIPReportChunk",
@ -5805,7 +5805,7 @@ var DistributedConnection = /*#__PURE__*/function (_IStore) {
this.resourceRequests.set(id, reply); this.resourceRequests.set(id, reply);
var self = this; var self = this;
this.sendRequest(_IIPPacketAction["default"].AttachResource).addUint32(id).done().then(function (rt) { this.sendRequest(_IIPPacketAction["default"].AttachResource).addUint32(id).done().then(function (rt) {
var dr = resource !== null && resource !== void 0 ? resource : new _DistributedResource["default"](self, id, rt[1], rt[2]); var dr = resource || new _DistributedResource["default"](self, id, rt[1], rt[2]);
self.getTemplate(rt[0]).then(function (tmp) { self.getTemplate(rt[0]).then(function (tmp) {
// ClassId, ResourceAge, ResourceLink, Content // ClassId, ResourceAge, ResourceLink, Content
if (resource == null) _Warehouse["default"].put(dr, id.toString(), self, null, tmp); if (resource == null) _Warehouse["default"].put(dr, id.toString(), self, null, tmp);

1899
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "esiur", "name": "esiur",
"version": "1.5.0", "version": "1.5.1",
"description": "Distributed Object Framework", "description": "Distributed Object Framework",
"main": "esiur.js", "main": "esiur.js",
"scripts": { "scripts": {
@ -19,17 +19,17 @@
}, },
"homepage": "https://github.com/esiur/esiur-js#readme", "homepage": "https://github.com/esiur/esiur-js#readme",
"dependencies": { "dependencies": {
"@babel/polyfill": "^7.8.3", "@babel/polyfill": "^7.12.1",
"@babel/runtime": "^7.8.4", "@babel/runtime": "^7.12.5",
"ws": "^6.2.1" "ws": "^6.2.1"
}, },
"devDependencies": { "devDependencies": {
"@babel/cli": "^7.8.4", "@babel/cli": "^7.12.1",
"@babel/core": "^7.8.6", "@babel/core": "^7.12.3",
"@babel/plugin-transform-runtime": "^7.8.3", "@babel/plugin-transform-runtime": "^7.12.1",
"@babel/preset-env": "^7.8.6", "@babel/preset-env": "^7.12.1",
"babel-cli": "^6.0.0", "babel-cli": "^6.0.0",
"babelify": "^10.0.0", "babelify": "^10.0.0",
"browserify": "^16.5.0" "browserify": "^16.5.2"
} }
} }

View File

@ -845,7 +845,8 @@ export default class DistributedConnection extends IStore {
IIPReportProgress(callbackId, type, value, max) { IIPReportProgress(callbackId, type, value, max) {
var req = this.requests.item(callbackId); var req = this.requests.item(callbackId);
req?.triggerProgress(type, value, max); if (req != null)
req.triggerProgress(type, value, max);
} }
IIPReportChunk(callbackId, data) { IIPReportChunk(callbackId, data) {
@ -1655,7 +1656,7 @@ export default class DistributedConnection extends IStore {
.done() .done()
.then(function (rt) { .then(function (rt) {
let dr = resource ?? new DistributedResource(self, id, rt[1], rt[2]); let dr = resource || new DistributedResource(self, id, rt[1], rt[2]);
self.getTemplate(rt[0]).then(function (tmp) { self.getTemplate(rt[0]).then(function (tmp) {

View File

@ -48,6 +48,11 @@ export default class DistributedResource extends IResource
this._p.attached = false; this._p.attached = false;
} }
trigger(type)
{
return new AsyncReply(true);
}
constructor(connection, instanceId, age, link) constructor(connection, instanceId, age, link)
{ {
super(); super();

View File

@ -0,0 +1,41 @@
import Warehouse from "../Resource/Warehouse.js";
export default class ResourceProxy
{
static cache = {};
static getProxy(type)
{
var template = Warehouse.getTemplateByType(type);
var className = type.prototype.constructor.name;
if (ResourceProxy.cache[className])
return ResourceProxy.cache[className];
var code = `return (class E_${className} extends b { constructor() {super();} `;
// generate class
for(var i = 0; i < template.properties.length; i++)
{
let pt = template.properties[i];
let desc = Object.getOwnPropertyDescriptor(Payment.prototype, pt.name);
if (desc)
{
code += `\r\n set ${pt.name}(v) { \r\n if (this.instance) this.instance.emitModification(this.instance.template.properties[${i}], v); \r\n super.${pt.name} = v; } \r\n get ${pt.name}() { \r\n return super.${pt.name};}`;
}
else
{
code += `\r\n set ${pt.name}(v) { \r\n if (this.instance) this.instance.emitModification(this.instance.template.properties[${i}], v); \r\n super._${pt.name} = v; } \r\n get ${pt.name}() { \r\n return super._${pt.name};}`;
}
}
var func = new Function("b", code + "})");
var proxyType = func.call(type /* this */, type);
ResourceProxy.cache[className] = proxyType;
return proxyType;
}
}

View File

@ -51,7 +51,7 @@ export default class IResource extends IDestructible
super(); super();
} }
static getTemplate() static get template()
{ {
return { return {
namespace: "Esiur", namespace: "Esiur",

View File

@ -149,7 +149,7 @@ export default class Instance extends IEventHandler
var val = {}; var val = {};
if (this.getPropertyValue(propertyName, val)) if (this.getPropertyValue(propertyName, val))
{ {
var pt = this.template.getPropertyTemplateByName(propertyName); var pt = this.template.getPropertyTemplateByName(propertyName);
this.emitModification(pt, val.value) this.emitModification(pt, val.value)
} }
} }

View File

@ -99,7 +99,7 @@ export default class ResourceTemplate {
if (type === undefined) if (type === undefined)
return; return;
var template = type.getTemplate(); var template = type.template;
// set guid // set guid
this.className = template.namespace + "." + type.prototype.constructor.name; this.className = template.namespace + "." + type.prototype.constructor.name;
@ -122,7 +122,7 @@ export default class ResourceTemplate {
var et = new EventTemplate(); var et = new EventTemplate();
et.name = template.events[i].name; et.name = template.events[i].name;
et.index = i; et.index = i;
et.expansion = template.events[i].expansion; et.expansion = template.events[i].help;
this.events.push(et); this.events.push(et);
} }
@ -131,7 +131,7 @@ export default class ResourceTemplate {
ft.name = template.functions[i].name; ft.name = template.functions[i].name;
ft.index = i; ft.index = i;
ft.isVoid = template.functions[i].void; ft.isVoid = template.functions[i].void;
ft.expansion = template.functions[i].expansion; ft.expansion = template.functions[i].help;
this.functions.push(ft); this.functions.push(ft);
} }

View File

@ -36,6 +36,8 @@ import MemoryStore from '../Stores/MemoryStore.js';
import Instance from '../Resource/Instance.js'; import Instance from '../Resource/Instance.js';
import IStore from './IStore.js'; import IStore from './IStore.js';
import { ResourceTrigger } from './IResource.js'; import { ResourceTrigger } from './IResource.js';
import IndexDBStore from '../Stores/IndexDBStore.js';
import ResourceProxy from '../Proxy/ResourceProxy.js';
export class WH extends IEventHandler export class WH extends IEventHandler
@ -55,10 +57,16 @@ export class WH extends IEventHandler
this._urlRegex = /^(?:([^\s|:]*):\/\/([^\/]*)\/?)/; this._urlRegex = /^(?:([^\s|:]*):\/\/([^\/]*)\/?)/;
} }
new(type, name, store = null, parent = null, manager = null, attributes = null) async new(type, name, store = null, parent = null, manager = null, attributes = null, properties = null)
{ {
var res = new type(); var proxyType = ResourceProxy.getProxy(type);
this.put(res, name, store, parent, null, 0, manager, attributes);
var res = new proxyType();
if (properties != null)
Object.assign(res, properties);
await this.put(res, name, store, parent, null, 0, manager, attributes);
return res; return res;
} }
@ -67,14 +75,12 @@ export class WH extends IEventHandler
return new AsyncReply(this.resources.item(id)); return new AsyncReply(this.resources.item(id));
} }
get(path, attributes = null, parent = null, manager = null) async get(path, attributes = null, parent = null, manager = null)
{ {
var rt = new AsyncReply(); //var rt = new AsyncReply();
var self = this; //var self = this;
// Should we create a new store ? // Should we create a new store ?
if (path.match(this._urlRegex)) if (path.match(this._urlRegex))
//if (path.includes("://")) //if (path.includes("://"))
{ {
@ -82,118 +88,69 @@ export class WH extends IEventHandler
//var url = path.split(/(?:):\/\/([^:\/]*):?(\d*)/); //var url = path.split(/(?:):\/\/([^:\/]*):?(\d*)/);
// without port // without port
let url = path.split(this._urlRegex); let url = path.split(this._urlRegex);
//var url = path.split("://", 2);
//var hostname = url[1];// url[1].split("/", 2)[0];
//var pathname = url[2];// url[1].split("/").splice(1).join("/");
var handler; var handler;
if (handler = this.protocols.item(url[1])) if (handler = this.protocols.item(url[1]))
{ {
if (!this.warehouseIsOpen)
await this.open();
var store = await handler(url[2], attributes);
var store = handler(); //await this.put(store, url[2], null, parent, null, 0, manager, attributes);
this.put(store, url[2], null, parent, null, 0, manager, attributes); //await store.trigger(ResourceTrigger.Open);
//this.warehouseIsOpen = true;
try
store.trigger(ResourceTrigger.Open).then(x => {
this.warehouseIsOpen = true;
if (url[3].length > 0 && url[3] != "")
store.get(url[3]).then(r => {
rt.trigger(r);
}).error(e => rt.triggerError(e));
else
rt.trigger(store);
}).error(e => {
rt.triggerError(e);
self.remove(store);
});
return rt;
}
}
this.query(path).then(rs =>
{
if (rs != null && rs.length > 0)
rt.trigger(rs[0]);
else
rt.trigger(null);
});
return rt;
/*
var p = id.split('/');
var res = null;
for(var s = 0; s < this.stores.length; s++)
{
var d = this.stores.at(s);
if (p[0] == d.instance.name)
{
var i = 1;
res = d;
while(p.length > i)
{ {
var si = i; if (url[3].length > 0 && url[3] != "")
return await store.get(url[3]);
for (var r = 0; r < res.instance.children.length; r++) else
if (res.instance.children.item(r).instance.name == p[i]) return store;
{ } catch(ex)
i++; {
res = res.instance.children.item(r); this.remove(resource);
break; throw ex;
}
if (si == i)
// not found, ask the store
return d.get(id.substring(p[0].length + 1));
} }
return new AsyncReply(res); // store.trigger(ResourceTrigger.Open).then(x => {
// this.warehouseIsOpen = true;
// if (url[3].length > 0 && url[3] != "")
// store.get(url[3]).then(r => {
// rt.trigger(r);
// }).error(e => rt.triggerError(e));
// else
// rt.trigger(store);
// }).error(e => {
// rt.triggerError(e);
// self.remove(store);
// });
// return rt;
} }
} }
var rs = await this.query(path);
if (rs != null && rs.length > 0)
return rs[0];
else
return null;
// Should we create a new store ? // .then(rs =>
if (id.includes("://")) // {
{ // if (rs != null && rs.length > 0)
var url = id.split("://", 2); // rt.trigger(rs[0]);
var hostname = url[1].split("/", 2)[0]; // else
var pathname = url[1].split("/").splice(1).join("/"); // rt.trigger(null);
var handler; // });
//return rt;
var rt = new AsyncReply();
var self = this;
if (handler = this.protocols.item(url[0]))
{
var store = handler();
this.put(store, url[0] + "://" + hostname, null, parent, null, 0, manager, attributes);
store.trigger(ResourceTrigger.Open).then(x=>{
if (pathname.length > 0 && pathname != "")
store.get(pathname).then(r=>{
rt.trigger(r);
}).error(e => rt.triggerError(e));
else
rt.trigger(store);
}).error(e => {
rt.triggerError(e);
self.remove(store);
});
}
return rt;
}
return new AsyncReply(null);
*/
} }
@ -237,7 +194,7 @@ export class WH extends IEventHandler
return true; return true;
} }
put(resource, name, store, parent, customTemplate = null, age = 0, manager = null, attributes = null){ async put(resource, name, store, parent, customTemplate = null, age = 0, manager = null, attributes = null){
resource.instance = new Instance(this.resourceCounter++, name, resource, store, customTemplate, age); resource.instance = new Instance(this.resourceCounter++, name, resource, store, customTemplate, age);
//resource.instance.children.on("add", Warehouse._onChildrenAdd).on("remove", Warehouse._onChildrenRemove); //resource.instance.children.on("add", Warehouse._onChildrenAdd).on("remove", Warehouse._onChildrenRemove);
@ -265,9 +222,19 @@ export class WH extends IEventHandler
this._emit("connected", resource); this._emit("connected", resource);
} }
else else
store.put(resource); await store.put(resource);
this.resources.add(resource.instance.id, resource); this.resources.add(resource.instance.id, resource);
if (this.warehouseIsOpen)
{
await resource.trigger(ResourceTrigger.Initialize);
if (resource instanceof IStore)
await resource.trigger(ResourceTrigger.Open);
}
return new AsyncReply(true);
} }
_onParentsRemove(value) _onParentsRemove(value)
@ -301,9 +268,18 @@ export class WH extends IEventHandler
getTemplateByType(type) getTemplateByType(type)
{ {
//debugger;
let className = type.prototype.constructor.name;
if (className.startsWith("E_"))
className = className.substr(2);
className = type.template.namespace + "." + className;
// loaded ? // loaded ?
for (var i = 0; i < this.templates.length; i++) for (var i = 0; i < this.templates.length; i++)
if (this.templates.at(i).className == typeof(type)) if (this.templates.at(i).className == className)
return this.templates.at(i); return this.templates.at(i);
var template = new ResourceTemplate(type); var template = new ResourceTemplate(type);
@ -400,12 +376,47 @@ export class WH extends IEventHandler
return null; return null;
} }
async open()
{
if (this.warehouseIsOpen)
return new AsyncReply(false);
this.warehouseIsOpen = true;
for (var i = 0; i < this.resources.length; i++)
{
var r = this.resources.at(i);
console.log("init ", r);
var rt = await r.trigger(ResourceTrigger.Initialize);
console.log("init done", r);
if (!rt)
console.log(`Resource failed at Initialize ${r.Instance.Name} [${r.Instance.Template.ClassName}]`);
}
for (var i = 0; i < this.resources.length; i++)
{
var r = this.resources.at(i);
var rt = await r.trigger(ResourceTrigger.SystemInitialized);
if (!rt)
console.log(`Resource failed at SystemInitialized ${r.Instance.Name} [${r.Instance.Template.ClassName}]`);
}
return new AsyncReply(true);
}
} }
let Warehouse = new WH(); let Warehouse = new WH();
Warehouse.protocols.add("iip", () => new DistributedConnection()); Warehouse.protocols.add("iip", (name, attributes) => Warehouse.new(DistributedConnection, name, null, null, null, attributes));
Warehouse.protocols.add("mem", () => new MemoryStore()); Warehouse.protocols.add("mem", (name, attributes) => Warehouse.new(MemoryStore, name, null, null, null, attributes));
Warehouse.protocols.add("db", (name, attributes) => Warehouse.new(IndexDBStore, name, null, null, null, attributes));
export default Warehouse; export default Warehouse;

139
src/Stores/IndexDBStore.js Normal file
View File

@ -0,0 +1,139 @@
/*
* Copyright (c) 2017-2021 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 2/18/2021.
*/
"use strict";
import { ResourceTrigger } from '../Resource/IResource.js';
import IStore from '../Resource/IStore.js'
import AsyncReply from '../Core/AsyncReply.js';
export default class IndexDBStore extends IStore
{
constructor()
{
super();
}
put(resource)
{
let rt = new AsyncReply();
var transaction = this.db.transaction(["resources"], "readwrite");
var objectStore = transaction.objectStore("resources");
// copy resource data
let props = resource.instance.template.properties;
var snap = {};
for(var i = 0; i < props.length; i++)
snap[props[i].name] = resource[props[i].name];
// debugger;
var request = objectStore.add(snap);
request.onerror = function(event) {
rt.trigger(null);
};
request.onsuccess = function(event) {
rt.trigger(request.result);
};
return rt;
}
retrive(id)
{
let rt = new AsyncReply();
var transaction = this.db.transaction(["resources"]);
var objectStore = transaction.objectStore("resources");
var request = objectStore.get(id);
request.onerror = function(event) {
rt.trigger(null);
};
request.onsuccess = function(event) {
rt.trigger(request.result);
};
return rt;
}
get(resource)
{
return new AsyncReply(null);
}
link(resource)
{
if (resource.instance.store == this)
return this.instance.name + "/" + resource.id;
}
trigger(trigger)
{
if (trigger == ResourceTrigger.Initialize)
{
let dbName = this.instance.attributes.item("db") ?? "esiur";
let request = indexedDB.open(dbName, 3);
let self = this;
let rt = new AsyncReply();
request.onupgradeneeded = function(event) {
self._store = request.result.createObjectStore("resources", {keyPath: "id", autoIncrement: true});
console.log(self._store);
};
request.onerror = function(event) {
console.error("Database error: " + event.target.errorCode);
rt.trigger(false);
};
request.onsuccess = function(event) {
console.log(event);
self.db = request.result;
rt.trigger(true);
};
return rt;
}
return new AsyncReply(true);
}
record(resource, propertyName, value, age, dateTime)
{
}
getRecord(resource, fromDate, toDate)
{
}
}

View File

@ -27,6 +27,7 @@
"use strict"; "use strict";
import IStore from '../Resource/IStore.js' import IStore from '../Resource/IStore.js'
import AsyncReply from '../Core/AsyncReply.js';
export default class MemoryStore extends IStore export default class MemoryStore extends IStore
{ {

View File

@ -3,14 +3,18 @@ import wh from './Resource/Warehouse.js';
import Structure from './Data/Structure.js'; import Structure from './Data/Structure.js';
import DistributedResource from './Net/IIP/DistributedResource.js' import DistributedResource from './Net/IIP/DistributedResource.js'
import MemoryStore from './Stores/MemoryStore.js'; import MemoryStore from './Stores/MemoryStore.js';
import IndexedDBStore from './Stores/IndexDBStore.js';
import IResource from './Resource/IResource.js'; import IResource from './Resource/IResource.js';
import ResourceProxy from './Proxy/ResourceProxy.js';
if (window) { if (window) {
window.wh = wh; window.wh = wh;
window.Structure = Structure; window.Structure = Structure;
window.DistributedResource = DistributedResource; window.DistributedResource = DistributedResource;
window.MemoryStore = MemoryStore; window.MemoryStore = MemoryStore;
window.IndexedDBStore = IndexedDBStore;
window.IResource = IResource; window.IResource = IResource;
window.ResourceProxy = ResourceProxy;
} }
else if (global) else if (global)
{ {
@ -18,8 +22,8 @@ else if (global)
global.Structure = Structure; global.Structure = Structure;
global.DistributedResource = DistributedResource; global.DistributedResource = DistributedResource;
global.MemoryStore = MemoryStore; global.MemoryStore = MemoryStore;
global.IndexedDBStore = IndexedDBStore;
global.IResource = IResource; global.IResource = IResource;
} }
export default wh; export default wh;

View File

@ -4,7 +4,7 @@ var demo = null;
class JSResource extends IResource class JSResource extends IResource
{ {
static getTemplate() static get template()
{ {
return { return {
namespace: "JS", namespace: "JS",