mirror of
https://github.com/esiur/esiur-js.git
synced 2025-05-06 12:32:58 +00:00
q
This commit is contained in:
parent
154385b899
commit
e05c729cd3
@ -50,7 +50,8 @@ export class WH extends IEventHandler
|
|||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.stores = new AutoList();
|
//this.stores = new AutoList();
|
||||||
|
this.stores = new KeyList();
|
||||||
this.resources = new KeyList();
|
this.resources = new KeyList();
|
||||||
this.resourceCounter = 0;
|
this.resourceCounter = 0;
|
||||||
this.templates = new KeyList();
|
this.templates = new KeyList();
|
||||||
@ -209,17 +210,62 @@ export class WH extends IEventHandler
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
put(name, resource, store, parent, customTemplate = null, age = 0, manager = null, attributes = null){
|
async put(name, resource, store, parent, customTemplate = null, age = 0, manager = null, attributes = null){
|
||||||
|
|
||||||
if (resource.instance != null)
|
if (resource.instance != null)
|
||||||
throw new Error("Resource has a store.");
|
throw new Error("Resource has a store.");
|
||||||
|
|
||||||
let path = (name.startsWith("/") ? name.substr(1) : name).split("/");
|
let path = name.replace(/^\\/g, "").split("/");
|
||||||
|
|
||||||
var rt = new AsyncReply();
|
if (path.length > 1)
|
||||||
|
{
|
||||||
|
if (parent != null)
|
||||||
|
throw new Error("Parent can't be set when using path in instance name");
|
||||||
|
|
||||||
resource.instance = new Instance(this.resourceCounter++, name, resource, store, customTemplate, age);
|
parent = await Warehouse.get(path.slice(0, path.length - 1).join("/"));
|
||||||
|
|
||||||
|
if (parent == null)
|
||||||
|
throw new Error("Can't find parent");
|
||||||
|
|
||||||
|
store = store ?? parent.instance.store;
|
||||||
|
}
|
||||||
|
|
||||||
|
let instanceName = path[path.length - 1];
|
||||||
|
|
||||||
|
let resourceReference = new WekRef(resource);
|
||||||
|
|
||||||
|
if (store == null)
|
||||||
|
{
|
||||||
|
// assign parent's store as a store
|
||||||
|
if (parent != null)
|
||||||
|
{
|
||||||
|
// assign parent as a store
|
||||||
|
if (parent instanceof IStore)
|
||||||
|
{
|
||||||
|
store = parent;
|
||||||
|
let list = Warehouse.stores.get(store);
|
||||||
|
if (list)
|
||||||
|
list.add(resourceReference);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
store = parent.instance.store;
|
||||||
|
|
||||||
|
let list = Warehouse.stores.get(store);
|
||||||
|
if (list)
|
||||||
|
list.add(resourceReference);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// assign self as a store (root store)
|
||||||
|
else if (resource instanceof IStore)
|
||||||
|
{
|
||||||
|
store = resource;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw new Error("Can't find a store for the resource.");
|
||||||
|
}
|
||||||
|
|
||||||
|
resource.instance = new Instance(Warehouse.resourceCounter++, instanceName, resource, store, customTemplate, age);
|
||||||
|
|
||||||
if (attributes != null)
|
if (attributes != null)
|
||||||
resource.instance.setAttributes(attributes);
|
resource.instance.setAttributes(attributes);
|
||||||
@ -227,63 +273,117 @@ export class WH extends IEventHandler
|
|||||||
if (manager != null)
|
if (manager != null)
|
||||||
resource.instance.managers.add(manager);
|
resource.instance.managers.add(manager);
|
||||||
|
|
||||||
if (parent)
|
if (store == parent)
|
||||||
{
|
parent = null;
|
||||||
parent.instance.children.add(resource);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!(resource instanceof IStore))
|
|
||||||
store.instance.children.add(resource);
|
|
||||||
}
|
|
||||||
|
|
||||||
let self = this;
|
try
|
||||||
|
|
||||||
const initResource = ()=>{
|
|
||||||
|
|
||||||
self.resources.add(resource.instance.id, resource);
|
|
||||||
|
|
||||||
if (self.warehouseIsOpen)
|
|
||||||
{
|
|
||||||
resource.trigger(ResourceTrigger.Initialize).then(()=>{
|
|
||||||
if (resource instanceof IStore)
|
|
||||||
resource.trigger(ResourceTrigger.Open).then(()=>{ rt.trigger(true);
|
|
||||||
self._emit("connected", resource)
|
|
||||||
}).error(ex => {
|
|
||||||
Warehouse.remove(resource);
|
|
||||||
rt.triggerError(ex)
|
|
||||||
});
|
|
||||||
else
|
|
||||||
rt.trigger(true);
|
|
||||||
|
|
||||||
}).error(ex => {
|
|
||||||
Warehouse.remove(resource);
|
|
||||||
rt.triggerError(ex)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
if (resource instanceof IStore)
|
if (resource instanceof IStore)
|
||||||
self._emit("connected", resource);
|
stores.add(resource, []);
|
||||||
rt.trigger(true);
|
|
||||||
|
|
||||||
|
if (!await store.put(resource))
|
||||||
|
throw new Error("Store failed to put the resource");
|
||||||
|
|
||||||
|
|
||||||
|
if (parent != null)
|
||||||
|
{
|
||||||
|
await parent.instance.store.addChild(parent, resource);
|
||||||
|
await store.addParent(resource, parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Warehouse.resources.add(resource.instance.Id, resourceReference);
|
||||||
|
|
||||||
|
if (Warehouse.warehouseIsOpen)
|
||||||
|
{
|
||||||
|
await resource.trigger(ResourceTrigger.Initialize);
|
||||||
|
if (resource instanceof IStore)
|
||||||
|
await resource.trigger(ResourceTrigger.Open);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resource instanceof IStore)
|
if (resource instanceof IStore)
|
||||||
{
|
Warehouse._emit("StoreConnected", resource);
|
||||||
this.stores.add(resource);
|
|
||||||
initResource();
|
|
||||||
}
|
}
|
||||||
else
|
catch (ex)
|
||||||
store.put(resource).then(()=>{
|
{
|
||||||
initResource();
|
|
||||||
}).error(ex=> {
|
|
||||||
// failed to put
|
|
||||||
Warehouse.remove(resource);
|
Warehouse.remove(resource);
|
||||||
rt.triggerError(ex);
|
throw ex;
|
||||||
});
|
}
|
||||||
|
|
||||||
return rt;
|
return resource;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// var rt = new AsyncReply();
|
||||||
|
|
||||||
|
// resource.instance = new Instance(this.resourceCounter++, name, resource, store, customTemplate, age);
|
||||||
|
|
||||||
|
|
||||||
|
// if (attributes != null)
|
||||||
|
// resource.instance.setAttributes(attributes);
|
||||||
|
|
||||||
|
// if (manager != null)
|
||||||
|
// resource.instance.managers.add(manager);
|
||||||
|
|
||||||
|
// if (parent)
|
||||||
|
// {
|
||||||
|
// parent.instance.children.add(resource);
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// if (!(resource instanceof IStore))
|
||||||
|
// store.instance.children.add(resource);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// let self = this;
|
||||||
|
|
||||||
|
// const initResource = ()=>{
|
||||||
|
|
||||||
|
// self.resources.add(resource.instance.id, resource);
|
||||||
|
|
||||||
|
// if (self.warehouseIsOpen)
|
||||||
|
// {
|
||||||
|
// resource.trigger(ResourceTrigger.Initialize).then(()=>{
|
||||||
|
// if (resource instanceof IStore)
|
||||||
|
// resource.trigger(ResourceTrigger.Open).then(()=>{ rt.trigger(true);
|
||||||
|
// self._emit("connected", resource)
|
||||||
|
// }).error(ex => {
|
||||||
|
// Warehouse.remove(resource);
|
||||||
|
// rt.triggerError(ex)
|
||||||
|
// });
|
||||||
|
// else
|
||||||
|
// rt.trigger(true);
|
||||||
|
|
||||||
|
// }).error(ex => {
|
||||||
|
// Warehouse.remove(resource);
|
||||||
|
// rt.triggerError(ex)
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// if (resource instanceof IStore)
|
||||||
|
// self._emit("connected", resource);
|
||||||
|
// rt.trigger(true);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (resource instanceof IStore)
|
||||||
|
// {
|
||||||
|
// this.stores.add(resource);
|
||||||
|
// initResource();
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// store.put(resource).then(()=>{
|
||||||
|
// initResource();
|
||||||
|
// }).error(ex=> {
|
||||||
|
// // failed to put
|
||||||
|
// Warehouse.remove(resource);
|
||||||
|
// rt.triggerError(ex);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// return rt;
|
||||||
}
|
}
|
||||||
|
|
||||||
_onParentsRemove(value)
|
_onParentsRemove(value)
|
||||||
@ -417,41 +517,32 @@ export class WH extends IEventHandler
|
|||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
|
|
||||||
query(path)
|
async query(path)
|
||||||
{
|
{
|
||||||
|
let p = path.replace(/^\\/g, "").split("/");
|
||||||
|
let resource;
|
||||||
|
|
||||||
var p = path.trim().split('/');
|
for(let store of Warehouse.stores.keys)
|
||||||
var resource;
|
|
||||||
|
|
||||||
for(var i = 0; i < this.stores.length; i++)
|
|
||||||
{
|
{
|
||||||
let store = this.stores.at(i);
|
|
||||||
|
|
||||||
if (p[0] == store.instance.name)
|
if (p[0] == store.instance.name)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (p.length == 1)
|
if (p.length == 1)
|
||||||
return new AsyncReply([store]);
|
return new AsyncReply([store]);
|
||||||
|
|
||||||
var rt = new AsyncReply();
|
let res = await store.get(p.slice(1).join("/"));
|
||||||
|
|
||||||
store.get(p.splice(1).join("/")).then(res=>{
|
|
||||||
if (res != null)
|
if (res != null)
|
||||||
rt.trigger([res]);
|
return new AsyncReply([res]);
|
||||||
else
|
|
||||||
{
|
|
||||||
resource = store;
|
resource = store;
|
||||||
|
|
||||||
for (var i = 1; i < p.length; i++)
|
for (var i = 1; i < p.length; i++)
|
||||||
{
|
{
|
||||||
var children = resource.instance.children.list.filter(x=>x.instance.name == p[i]);// <IResource>(p[i]);
|
var children = await resource.instance.children(p[i]);
|
||||||
if (children != null && children.length > 0)
|
if (children != null && children.length > 0)
|
||||||
{
|
{
|
||||||
if (i == p.length - 1)
|
if (i == p.length - 1)
|
||||||
{
|
return new AsyncReply(children);
|
||||||
rt.trigger(children);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
resource = children[0];
|
resource = children[0];
|
||||||
}
|
}
|
||||||
@ -459,11 +550,7 @@ export class WH extends IEventHandler
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
rt.trigger(null);
|
return new AsyncReply(null);
|
||||||
}
|
|
||||||
}).error(ex => rt.triggerError(ex));
|
|
||||||
|
|
||||||
return rt;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user