lemoncloud-engine-js
Version:
lemoncloud.io - Lemon Engine Modle of Node for Robust Micro-service based on DynamoDB + ElastiCache + Elasticsearch.
1 lines • 3.67 kB
JavaScript
var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r};module.exports=function(r,e,t){e=e||"NS";var f=r.U,i=r._;if(!f)throw new Error("$U is required!");if(!i)throw new Error("$_ is required!");function n(r){throw new Error("NOT_IMPLEMENTED - "+c+":"+JSON.stringify(r))}var d=r.log,o=(r.inf,r.err),c=f.NS(e,"magenta"),a=t||{};a.do_notify=n,a.do_subscribe=n,e.startsWith("!")||e.startsWith("_")||r(e,a);function s(r,e){return void 0===a[r]?e:a[r]}var y=s("NS_NAME",""),u=s("MASTER",null),l=s("CHILDS",[]),b=["create","update","delete"];a.$notify={};var m=function(t){if(!y)return Promise.resolve(t);if(!t._id)return Promise.reject(new Error(c+"notify:_id is required!"));var r=""+t._id;if(d(c,"- my_notify_trigger("+r+")...."),!r.startsWith(y+":"))return d(c,"! WARN - ignored due to id="+r+" by NS="+y),t;var e=a.$notify[r];return e?Promise.all(e.map(function(r,e){try{return r.handle?r.handle(t):r.promise?r.promise:null}catch(r){return o(c,">> action["+e+"] :",r),r}})).then(function(){return d(c,"! finished call subscriptions!!! count="+e.length),t}).catch(function(r){throw o(c,"actions.error=",r),r}):t};if(u){var _=":record:*";d(c,">>>>> subscribe master ("+_+") ... "),u.do_subscribe(":record:*",function(r){d(c,"- on-notified-record ("+r._id+").... that=",f.json(r));var e=r._id||"",t=e.split(":");if(t.length<2)return r;t.shift(),t.unshift(y);var i=t.join(":"),n=f.extend({},r);return n._id=i,n._id_org=e,m(n)}).then(function(r){d(c,"! subscribed master. res=",r)}),d(c,"<<<<< subscribe master ("+_+") ... ")}function h(r){return r}return a.do_notify=function(r,e){return function(r,e,t){r=r||"",t=t||"",y&&d(c,"do_"+t+"()... id=",r&&r._id||r&&r.records&&"record#:"+r.records.length||r);var i=e="object"===(void 0===r?"undefined":_typeof(r))?r:{_id:r,_current_mode:t,data:e};return f.promise(i)}(r,e,"notify").then(m).then(h)},a.do_subscribe=function(r,e){var t=e="object"===(void 0===r?"undefined":_typeof(r))?r:f.extend(void 0!==e&&("object"!==(void 0===e?"undefined":_typeof(e))||e instanceof Promise)?{params:e}:e||{},{_id:r});if(!y)return Promise.resolve(t);if(!t._id)return Promise.reject(new Error(c+"notify:_id is required!"));if(!t.params)return Promise.reject(new Error(c+"notify:params is required!"));var i=""+t._id,n=t.params;if(d(c,"- my_notify_subscribe_sync("+i+").... params=",void 0===n?"undefined":_typeof(n)),i.startsWith(":")&&(i=y+i,t._id=i),!i.startsWith(y+":")){if(!l||!l.length)return d(c,"! ignored subscribe due to NS-NAME:"+y),Promise.resolve(t);d(c,"> bubble down for target notifier!!! id=",i);var o=l.map(function(r){return r.do_subscribe(t)});return Promise.all(o).then(function(){return t})}d(c,"> INFO! - register subscriber by id ="+i);var s={handle:null,promise:null};if("function"==typeof n)s.callee=n,s.handle=function(t){var i=this.callee;return new Promise(function(r,e){try{r(i(t))}catch(r){e(r)}})};else{if(!("object"===(void 0===n?"undefined":_typeof(n))&&n instanceof Promise))return Promise.reject(new Error(c+"invalid params type:"+(void 0===n?"undefined":_typeof(n))));s.promise=n}if(s)if(i.endsWith(":*")){var u=i.substring(0,i.length-1);b.forEach(function(r){var e=u+r;a.$notify[e]||(a.$notify[e]=[]),a.$notify[e].push(s),d(c,"> notify-subscribe("+e+") count=",a.$notify[e].length)}),d(c,"! notify-subscribe("+i+") ... modes=",b.join("/"))}else a.$notify[i]||(a.$notify[i]=[]),a.$notify[i].push(s),d(c,"! notify-subscribe("+i+") count=",a.$notify[i].length);return d(c,"! subscribed-notify("+i+")... that=",f.json(t)),Promise.resolve(t)},a};
;