From dd88085cc267c87fa772ad37a1478422752a4c6c Mon Sep 17 00:00:00 2001 From: ahmed Date: Sun, 23 Nov 2025 00:31:09 +0300 Subject: [PATCH] Replace .error with .catch --- build/esiur.js | 170 +++++++++++++----------- package.json | 2 +- src/Core/AsyncReply.js | 3 + src/Net/IIP/DistributedConnection.js | 10 +- src/Resource/Warehouse.js | 192 ++++++++++++++++++++++----- src/Stores/IndexedDBStore.js | 6 +- src/Stores/MongoDBStore.js | 6 +- 7 files changed, 270 insertions(+), 119 deletions(-) diff --git a/build/esiur.js b/build/esiur.js index 2c7a9e5..fbc2ea6 100644 --- a/build/esiur.js +++ b/build/esiur.js @@ -1073,6 +1073,11 @@ var AsyncReply = exports["default"] = /*#__PURE__*/function (_Promise) { value: function done(callback) { this.then(callback); } + }, { + key: "catch", + value: function _catch(callback) { + this.error(callback); + } }, { key: "error", value: function error(callback) { @@ -6356,7 +6361,7 @@ var DistributedConnection = exports["default"] = /*#__PURE__*/function (_IStore) var resource = new (Function.prototype.bind.apply(type, values))(); _Warehouse["default"].put(name, resource, store, parent).then(function (ok) { _classPrivateMethodGet(self, _sendReply, _sendReply2).call(self, _IIPPacketAction["default"].CreateResource, callback).addUint32(resource.Instance.Id).done(); - }).error(function (ex) { + })["catch"](function (ex) { // send some error _classPrivateMethodGet(self, _sendError, _sendError2).call(self, _ErrorType["default"].Management, callback, _ExceptionCode["default"].AddToStoreFailed); }); @@ -6973,7 +6978,7 @@ var DistributedConnection = exports["default"] = /*#__PURE__*/function (_IStore) if (resource == null) { _Warehouse["default"].put(id.toString(), dr, self, null, tmp).then(function (ok) { initResource(ok); - }).error(function (ex) { + })["catch"](function (ex) { reply.triggerError(ex); }); } else { @@ -6984,7 +6989,7 @@ var DistributedConnection = exports["default"] = /*#__PURE__*/function (_IStore) }); } else { if (resource == null) { - _Warehouse["default"].put(id.toString(), dr, self, null, template).then(initResource).error(function (ex) { + _Warehouse["default"].put(id.toString(), dr, self, null, template).then(initResource)["catch"](function (ex) { return reply.triggerError(ex); }); } else { @@ -7632,7 +7637,7 @@ function _processClientAuth2(data) { (_classPrivateFieldGet5 = (0, _classPrivateFieldGet25["default"])(_this5, _openReply)) === null || _classPrivateFieldGet5 === void 0 || _classPrivateFieldGet5.trigger(true); _this5._emit("ready", _this5); (0, _classPrivateFieldSet2["default"])(_this5, _openReply, null); - }).error(function (x) { + })["catch"](function (x) { var _classPrivateFieldGet6; (_classPrivateFieldGet6 = (0, _classPrivateFieldGet25["default"])(_this5, _openReply)) === null || _classPrivateFieldGet6 === void 0 || _classPrivateFieldGet6.triggerError(x); (0, _classPrivateFieldSet2["default"])(_this5, _openReply, null); @@ -7857,7 +7862,7 @@ function _processAuthorization2(results) { _this7._emit("ready", _this7); (_classPrivateFieldGet19 = (0, _classPrivateFieldGet25["default"])(_this7, _server)) === null || _classPrivateFieldGet19 === void 0 || (_classPrivateFieldGet19 = _classPrivateFieldGet19.membership) === null || _classPrivateFieldGet19 === void 0 || _classPrivateFieldGet19.login((0, _classPrivateFieldGet25["default"])(_this7, _session)); (0, _classPrivateFieldSet2["default"])(_this7, _loginDate, new Date()); - }).error(function (x) { + })["catch"](function (x) { var _classPrivateFieldGet20; (_classPrivateFieldGet20 = (0, _classPrivateFieldGet25["default"])(_this7, _openReply)) === null || _classPrivateFieldGet20 === void 0 || _classPrivateFieldGet20.triggerError(x); (0, _classPrivateFieldSet2["default"])(_this7, _openReply, null); @@ -12516,7 +12521,7 @@ var WH = exports.WH = /*#__PURE__*/function (_IEventHandler) { var rt = new _AsyncReply["default"](); this.put(name, res, store, parent, null, 0, manager, attributes).then(function () { return rt.trigger(res); - }).error(function (ex) { + })["catch"](function (ex) { return rt.triggerError(ex); }); return rt; @@ -12527,7 +12532,9 @@ var WH = exports.WH = /*#__PURE__*/function (_IEventHandler) { }, { key: "getById", value: function getById(id) { - return new _AsyncReply["default"](this.resources.item(id)); + var _this$resources$item; + var r = (_this$resources$item = this.resources.item(id)) === null || _this$resources$item === void 0 ? void 0 : _this$resources$item.deref(); + return new _AsyncReply["default"](r); } }, { key: "get", @@ -12627,120 +12634,127 @@ var WH = exports.WH = /*#__PURE__*/function (_IEventHandler) { _context.next = 8; break; } - rt.triggerError(new _AsyncException["default"](0, _ExceptionCode["default"].GeneralFailure, "Resource has a store")); + rt.triggerError(0, _ExceptionCode["default"].GeneralFailure, "Resource has a store"); return _context.abrupt("return", rt); case 8: - path = name.replace(/^\\/g, "").split("/"); + // trim '/' character + path = name.replace(/^[\/]+/, '').split('/'); if (!(path.length > 1)) { - _context.next = 18; + _context.next = 20; break; } if (!(parent != null)) { - _context.next = 12; + _context.next = 13; break; } - throw new Error("Parent can't be set when using path in instance name"); - case 12: - _context.next = 14; - return Warehouse.get(path.slice(0, path.length - 1).join("/")); - case 14: + rt.triggerError(0, _ExceptionCode["default"].GeneralFailure, "Parent can't be set when using path in instance name"); + return _context.abrupt("return", rt); + case 13: + _context.next = 15; + return Warehouse.get(path.slice(0, -1).join("/")); + case 15: parent = _context.sent; if (!(parent == null)) { - _context.next = 17; + _context.next = 19; break; } - throw new Error("Can't find parent"); - case 17: - store = (_store = store) !== null && _store !== void 0 ? _store : parent.instance.store; - case 18: - instanceName = path[path.length - 1]; + rt.triggerError(0, _ExceptionCode["default"].GeneralFailure, "Can't find parent."); + return _context.abrupt("return", rt); + case 19: + store = (_store = store) !== null && _store !== void 0 ? _store : parent.Instance.Store; + case 20: + instanceName = path.at(-1); resourceReference = new WeakRef(resource); if (!(store == null)) { - _context.next = 30; + _context.next = 33; break; } if (!(parent != null)) { - _context.next = 25; + _context.next = 27; break; } // assign parent as a store if (parent instanceof _IStore["default"]) { store = parent; - list = Warehouse.stores.get(store); + list = this.stores.get(store); if (list) list.add(resourceReference); } else { store = parent.instance.store; - _list = Warehouse.stores.get(store); + _list = this.stores.get(store); if (_list) _list.add(resourceReference); } - _context.next = 30; + _context.next = 33; break; - case 25: + case 27: if (!(resource instanceof _IStore["default"])) { - _context.next = 29; + _context.next = 31; break; } store = resource; - _context.next = 30; + _context.next = 33; break; - case 29: - throw new Error("Can't find a store for the resource."); - case 30: - resource.instance = new _Instance["default"](Warehouse.resourceCounter++, instanceName, resource, store, customTemplate, age); + case 31: + rt.triggerError(0, _ExceptionCode["default"].GeneralFailure, "Can't find a store for the resource."); + return _context.abrupt("return", rt); + case 33: + resource.instance = new _Instance["default"](this.resourceCounter++, instanceName, resource, store, customTemplate, age); if (attributes != null) resource.instance.setAttributes(attributes); if (manager != null) resource.instance.managers.add(manager); if (store == parent) parent = null; - _context.prev = 34; - if (resource instanceof _IStore["default"]) stores.add(resource, []); - _context.next = 38; + _context.prev = 37; + if (resource instanceof _IStore["default"]) this.stores.add(resource, []); + _context.next = 41; return store.put(resource); - case 38: + case 41: if (_context.sent) { - _context.next = 40; + _context.next = 44; break; } - throw new Error("Store failed to put the resource"); - case 40: + rt.triggerError(0, _ExceptionCode["default"].GeneralFailure, "Store failed to put the resource."); + return _context.abrupt("return", rt); + case 44: if (!(parent != null)) { - _context.next = 45; + _context.next = 49; break; } - _context.next = 43; + _context.next = 47; return parent.instance.store.addChild(parent, resource); - case 43: - _context.next = 45; - return store.addParent(resource, parent); - case 45: - Warehouse.resources.add(resource.instance.Id, resourceReference); - if (!Warehouse.warehouseIsOpen) { - _context.next = 52; - break; - } + case 47: _context.next = 49; - return resource.trigger(_IResource.ResourceTrigger.Initialize); + return store.addParent(resource, parent); case 49: - if (!(resource instanceof _IStore["default"])) { - _context.next = 52; + this.resources.add(resource.instance.id, resourceReference); + if (!this.warehouseIsOpen) { + _context.next = 56; break; } - _context.next = 52; + _context.next = 53; + return resource.trigger(_IResource.ResourceTrigger.Initialize); + case 53: + if (!(resource instanceof _IStore["default"])) { + _context.next = 56; + break; + } + _context.next = 56; return resource.trigger(_IResource.ResourceTrigger.Open); - case 52: - if (resource instanceof _IStore["default"]) Warehouse._emit("StoreConnected", resource); - _context.next = 59; + case 56: + if (resource instanceof _IStore["default"]) this._emit("StoreConnected", resource); + _context.next = 64; break; - case 55: - _context.prev = 55; - _context.t0 = _context["catch"](34); - Warehouse.remove(resource); - throw _context.t0; case 59: - return _context.abrupt("return", resource); - case 60: + _context.prev = 59; + _context.t0 = _context["catch"](37); + this.remove(resource); + rt.triggerError(_context.t0); + return _context.abrupt("return", rt); + case 64: + rt.trigger(resource); + return _context.abrupt("return", rt); + case 66: case "end": return _context.stop(); } - }, _callee, null, [[34, 55]]); + }, _callee, this, [[37, 59]]); })); function put(_x, _x2, _x3, _x4) { return _put.apply(this, arguments); @@ -12855,7 +12869,7 @@ var WH = exports.WH = /*#__PURE__*/function (_IEventHandler) { while (1) switch (_context2.prev = _context2.next) { case 0: p = path.replace(/^\\/g, "").split("/"); - _iterator = _createForOfIteratorHelper(Warehouse.stores.keys); + _iterator = _createForOfIteratorHelper(this.stores.keys); _context2.prev = 2; _iterator.s(); case 4: @@ -12937,7 +12951,7 @@ var WH = exports.WH = /*#__PURE__*/function (_IEventHandler) { case "end": return _context2.stop(); } - }, _callee2, null, [[2, 37, 40, 43]]); + }, _callee2, this, [[2, 37, 40, 43]]); })); function query(_x5) { return _query.apply(this, arguments); @@ -12953,8 +12967,10 @@ var WH = exports.WH = /*#__PURE__*/function (_IEventHandler) { var rt = new _AsyncReply["default"](); var self = this; for (var i = 0; i < this.resources.length; i++) { - var r = this.resources.at(i); - initBag.add(r.trigger(_IResource.ResourceTrigger.Initialize)); + var r = this.resources.at(i).deref(); + if (r) { + initBag.add(r.trigger(_IResource.ResourceTrigger.Initialize)); + } //if (!rt) // console.log(`Resource failed at Initialize ${r.Instance.Name} [${r.Instance.Template.ClassName}]`); } @@ -12963,8 +12979,10 @@ var WH = exports.WH = /*#__PURE__*/function (_IEventHandler) { for (var _i4 = 0; _i4 < ar.length; _i4++) if (!ar[_i4]) console.log("Resource failed at Initialize ".concat(self.resources.at(_i4).instance.name, " [").concat(self.resources.at(_i4).instance.template.className, "]")); var sysBag = new _AsyncBag["default"](); for (var _i5 = 0; _i5 < _this2.resources.length; _i5++) { - var r = _this2.resources.at(_i5); - sysBag.add(r.trigger(_IResource.ResourceTrigger.SystemInitialized)); + var r = _this2.resources.at(_i5).deref(); + if (r) { + sysBag.add(r.trigger(_IResource.ResourceTrigger.SystemInitialized)); + } } sysBag.seal(); sysBag.then(function (ar2) { @@ -13896,13 +13914,13 @@ var IndexedDBStore = exports["default"] = /*#__PURE__*/function (_IStore) { resource.instance.loadProperty(_v.name, _v.age, _v.modification, ar[i]); } rt.trigger(resource); - }).error(function (ex) { + })["catch"](function (ex) { return rt.triggerError(ex); }); - }).error(function (ex) { + })["catch"](function (ex) { return rt.triggerError(ex); }); - }).error(function (ex) { + })["catch"](function (ex) { return rt.triggerError(ex); }); }; diff --git a/package.json b/package.json index 1367098..aa4a4f6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "esiur", - "version": "2.3.5", + "version": "2.3.6", "description": "Distributed Object Framework", "main": "esiur.js", "type": "module", diff --git a/src/Core/AsyncReply.js b/src/Core/AsyncReply.js index a744e2f..953f571 100644 --- a/src/Core/AsyncReply.js +++ b/src/Core/AsyncReply.js @@ -56,6 +56,9 @@ export default class AsyncReply extends Promise this.then(callback); } + catch(callback){ + this.error(callback); + } error(callback) { diff --git a/src/Net/IIP/DistributedConnection.js b/src/Net/IIP/DistributedConnection.js index da462c0..1e16fff 100644 --- a/src/Net/IIP/DistributedConnection.js +++ b/src/Net/IIP/DistributedConnection.js @@ -634,7 +634,7 @@ export default class DistributedConnection extends IStore { this._emit("ready", this); this.#openReply = null; - }).error((x) => + }).catch((x) => { this.#openReply?.triggerError(x); this.#openReply = null; @@ -1104,7 +1104,7 @@ export default class DistributedConnection extends IStore { this.#server?.membership?.login(this.#session); this.#loginDate = new Date(); - }).error((x) => + }).catch((x) => { this.#openReply?.triggerError(x); this.#openReply = null; @@ -2153,7 +2153,7 @@ export default class DistributedConnection extends IStore { self.#sendReply(IIPPacketAction.CreateResource, callback) .addUint32(resource.Instance.Id) .done(); - }).error(function(ex){ + }).catch(function(ex){ // send some error self.#sendError(ErrorType.Management, callback, ExceptionCode.AddToStoreFailed); }); @@ -3052,7 +3052,7 @@ export default class DistributedConnection extends IStore { if (resource == null) { Warehouse.put(id.toString(), dr, self, null, tmp).then(function(ok){ initResource(ok); - }).error(function(ex){ + }).catch(function(ex){ reply.triggerError(ex); }); } @@ -3067,7 +3067,7 @@ export default class DistributedConnection extends IStore { if (resource == null) { Warehouse.put(id.toString(), dr, self, null, template) .then(initResource) - .error((ex) => reply.triggerError(ex)); + .catch((ex) => reply.triggerError(ex)); } else { initResource(resource); } diff --git a/src/Resource/Warehouse.js b/src/Resource/Warehouse.js index 4aa08b2..3118b7f 100644 --- a/src/Resource/Warehouse.js +++ b/src/Resource/Warehouse.js @@ -99,7 +99,7 @@ export class WH extends IEventHandler this.put(name, res, store, parent, null, 0, manager, attributes) .then(()=>rt.trigger(res)) - .error((ex)=>rt.triggerError(ex)); + .catch((ex)=>rt.triggerError(ex)); return rt; } @@ -111,7 +111,8 @@ export class WH extends IEventHandler getById(id) { - return new AsyncReply(this.resources.item(id)); + let r = this.resources.item(id)?.deref(); + return new AsyncReply(r); } get(path, attributes = null)//, parent = null, manager = null) @@ -214,29 +215,36 @@ export class WH extends IEventHandler async put(name, resource, store, parent, customTemplate = null, age = 0, manager = null, attributes = null){ + let rt = new AsyncReply(); if (resource.instance != null){ - rt.triggerError(new AsyncException(0, ExceptionCode.GeneralFailure, "Resource has a store")); + rt.triggerError(0, ExceptionCode.GeneralFailure, "Resource has a store"); return rt; } - let path = name.replace(/^\\/g, "").split("/"); + // trim '/' character + let path = name.replace(/^[\/]+/, '').split('/'); if (path.length > 1) { - if (parent != null) - throw new Error("Parent can't be set when using path in instance name"); + if (parent != null){ + rt.triggerError(0, ExceptionCode.GeneralFailure, "Parent can't be set when using path in instance name"); + return rt; + } - parent = await Warehouse.get(path.slice(0, path.length - 1).join("/")); + parent = await Warehouse.get(path.slice(0, -1).join("/")); - if (parent == null) - throw new Error("Can't find parent"); + if (parent == null){ + rt.triggerError(0, ExceptionCode.GeneralFailure, "Can't find parent."); + return rt; + } - store = store ?? parent.instance.store; + store = store ?? parent.Instance.Store; } - let instanceName = path[path.length - 1]; + let instanceName = path.at(-1); + let resourceReference = new WeakRef(resource); @@ -249,7 +257,7 @@ export class WH extends IEventHandler if (parent instanceof IStore) { store = parent; - let list = Warehouse.stores.get(store); + let list = this.stores.get(store); if (list) list.add(resourceReference); } @@ -257,7 +265,7 @@ export class WH extends IEventHandler { store = parent.instance.store; - let list = Warehouse.stores.get(store); + let list = this.stores.get(store); if (list) list.add(resourceReference); } @@ -268,10 +276,13 @@ export class WH extends IEventHandler store = resource; } else - throw new Error("Can't find a store for the resource."); + { + rt.triggerError(0, ExceptionCode.GeneralFailure, "Can't find a store for the resource."); + return rt; + } } - resource.instance = new Instance(Warehouse.resourceCounter++, instanceName, resource, store, customTemplate, age); + resource.instance = new Instance(this.resourceCounter++, instanceName, resource, store, customTemplate, age); if (attributes != null) resource.instance.setAttributes(attributes); @@ -285,22 +296,25 @@ export class WH extends IEventHandler try { if (resource instanceof IStore) - stores.add(resource, []); + this.stores.add(resource, []); - if (!await store.put(resource)) - throw new Error("Store failed to put the resource"); - + if (!await store.put(resource)){ + rt.triggerError(0, ExceptionCode.GeneralFailure, "Store failed to put the resource."); + return rt; + } 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) + + this.resources.add(resource.instance.id, resourceReference); + + if (this.warehouseIsOpen) { await resource.trigger(ResourceTrigger.Initialize); if (resource instanceof IStore) @@ -308,15 +322,127 @@ export class WH extends IEventHandler } if (resource instanceof IStore) - Warehouse._emit("StoreConnected", resource); + this._emit("StoreConnected", resource); } catch (ex) { - Warehouse.remove(resource); - throw ex; + this.remove(resource); + rt.triggerError(ex); + return rt; } - return resource; + rt.trigger(resource); + return rt; + + + + + + + + // let rt = new AsyncReply(); + + // if (resource.instance != null){ + // rt.triggerError(new AsyncException(0, ExceptionCode.GeneralFailure, "Resource has a store")); + // return rt; + // } + + // let path = name.replace(/^\\/g, "").split("/"); + + // if (path.length > 1) + // { + // if (parent != null) + // throw new Error("Parent can't be set when using path in instance name"); + + // 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 WeakRef(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) + // resource.instance.setAttributes(attributes); + + // if (manager != null) + // resource.instance.managers.add(manager); + + // if (store == parent) + // parent = null; + + // try + // { + // if (resource instanceof IStore) + // this.stores.add(resource, []); + + + // 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) + // Warehouse._emit("StoreConnected", resource); + // } + // catch (ex) + // { + // Warehouse.remove(resource); + // throw ex; + // } + + // return resource; @@ -528,7 +654,7 @@ export class WH extends IEventHandler let p = path.replace(/^\\/g, "").split("/"); let resource; - for(let store of Warehouse.stores.keys) + for(let store of this.stores.keys) { if (p[0] == store.instance.name) { @@ -576,8 +702,10 @@ export class WH extends IEventHandler for (var i = 0; i < this.resources.length; i++) { - var r = this.resources.at(i); - initBag.add(r.trigger(ResourceTrigger.Initialize)); + let r = this.resources.at(i).deref(); + if (r) { + initBag.add(r.trigger(ResourceTrigger.Initialize)); + } //if (!rt) // console.log(`Resource failed at Initialize ${r.Instance.Name} [${r.Instance.Template.ClassName}]`); } @@ -594,8 +722,10 @@ export class WH extends IEventHandler for (let i = 0; i < this.resources.length; i++) { - var r = this.resources.at(i); - sysBag.add(r.trigger(ResourceTrigger.SystemInitialized)); + var r = this.resources.at(i).deref(); + if (r){ + sysBag.add(r.trigger(ResourceTrigger.SystemInitialized)); + } } sysBag.seal(); diff --git a/src/Stores/IndexedDBStore.js b/src/Stores/IndexedDBStore.js index 4dfd5a0..bf537c6 100644 --- a/src/Stores/IndexedDBStore.js +++ b/src/Stores/IndexedDBStore.js @@ -211,9 +211,9 @@ export default class IndexedDBStore extends IStore } rt.trigger(resource); - }).error(ex => rt.triggerError(ex)); - }).error(ex => rt.triggerError(ex)); - }).error(ex => rt.triggerError(ex)); + }).catch(ex => rt.triggerError(ex)); + }).catch(ex => rt.triggerError(ex)); + }).catch(ex => rt.triggerError(ex)); }; return rt; diff --git a/src/Stores/MongoDBStore.js b/src/Stores/MongoDBStore.js index e5ccfd0..4618048 100644 --- a/src/Stores/MongoDBStore.js +++ b/src/Stores/MongoDBStore.js @@ -210,9 +210,9 @@ } rt.trigger(resource); - }).error(ex => rt.triggerError(ex)); - }).error(ex => rt.triggerError(ex)); - }).error(ex => rt.triggerError(ex)); + }).catch(ex => rt.triggerError(ex)); + }).catch(ex => rt.triggerError(ex)); + }).catch(ex => rt.triggerError(ex)); } });