UNPKG

eventric

Version:

behavior-first application development

2 lines 41.3 kB
!function(){"use strict";var t="undefined"==typeof window?global:window;if("function"!=typeof t.require){var e={},n={_load:function(t,r){var i=n._resolveFilename(t,r),s=e[i];if(!s)throw new Error('Cannot find module "'+i+'" from "'+r+'"');if(n._cache[i])return n._cache[i].exports;var u=o(i),a={id:i,exports:{}};return n._cache[i]=a,s.call(a.exports,a.exports,u,a),a.exports},_cache:{},_resolveFilename:function(t,n){var r=i(s(n),t);return e.hasOwnProperty(r)?r:(r=i(r,"./index"),e.hasOwnProperty(r)?r:t)}},r=function(t,e){return n._load(t,e)},i=function(){var t=/^\.\.?(\/|$)/;return function(e,n){var r,i,o=[];r=(t.test(n)?e+"/"+n:n).split("/");for(var s=0,u=r.length;u>s;s++)i=r[s],".."===i?o.pop():"."!==i&&""!==i&&o.push(i);return o.join("/")}}(),o=function(e){return function(n){return t.require(n,e)}},s=function(t){return t?t.split("/").slice(0,-1).join("/"):""};r.register=r.define=function(t,n){if("object"==typeof t)for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r]);else e[t]=n},r.list=function(){var t=[];for(var n in e)e.hasOwnProperty(n)&&t.push(n);return t},t.require=r,r.define("module",function(t,e,r){r.exports=n})}}(),require.register("eventric/eventric",function(t,e,n){var r,i=function(t,e){return function(){return t.apply(e,arguments)}},o=[].slice;r=function(){function t(){this._handleRemoteRPCRequest=i(this._handleRemoteRPCRequest,this),this.PubSub=e("./pub_sub"),this.EventBus=e("./event_bus"),this.Remote=e("./remote"),this.Context=e("./context"),this.DomainEvent=e("./domain_event"),this.Aggregate=e("./aggregate"),this.Repository=e("./repository"),this.Projection=e("./projection"),this.Logger=e("./logger"),this.RemoteInMemory=e("./remote/inmemory"),this.StoreInMemory=e("./store/inmemory"),this.log=this.Logger,this._contexts={},this._params={},this._domainEventHandlers={},this._domainEventHandlersAll=[],this._storeClasses={},this._remoteEndpoints=[],this.addRemoteEndpoint("inmemory",this.RemoteInMemory.endpoint),this.addStore("inmemory",this.StoreInMemory),this.set("default domain events store","inmemory")}return t.prototype.set=function(t,e){return this._params[t]=e},t.prototype.get=function(t){return t?this._params[t]:this._params},t.prototype.addStore=function(t,e,n){return null==n&&(n={}),this._storeClasses[t]={Class:e,options:n}},t.prototype.getStores=function(){return this._storeClasses},t.prototype.context=function(t){var e,n,r;if(!t)throw n="Contexts must have a name",this.log.error(n),new Error(n);return r=new this.PubSub,e=new this.Context(t,this),this.mixin(e,r),this._delegateAllDomainEventsToGlobalHandlers(e),this._delegateAllDomainEventsToRemoteEndpoints(e),this._contexts[t]=e,e},t.prototype.getContext=function(t){return this._contexts[t]},t.prototype.remote=function(t){var e,n,r;if(!t)throw e="Missing context name",this.log.error(e),new Error(e);return n=new this.PubSub,r=new this.Remote(t,this),this.mixin(r,n),r},t.prototype.addRemoteEndpoint=function(t,e){return this._remoteEndpoints.push(e),e.setRPCHandler(this._handleRemoteRPCRequest)},t.prototype._handleRemoteRPCRequest=function(t,e){var n,r;return n=this.getContext(t.contextName),n?-1===this.Remote.ALLOWED_RPC_OPERATIONS.indexOf(t.method)?(r="RPC operation '"+t.method+"' not allowed",this.log.error(r),e(r,null)):t.method in n?n[t.method].apply(n,t.params).then(function(t){return e(null,t)})["catch"](function(t){return e(t)}):(r="Remote RPC method "+t.method+" not found on Context "+t.contextName,this.log.error(r),e(r,null)):(r="Tried to handle Remote RPC with not registered context "+t.contextName,this.log.error(r),e(r,null))},t.prototype._delegateAllDomainEventsToGlobalHandlers=function(t){return t.subscribeToAllDomainEvents(function(e){return function(n){var r,i,o,s,u;for(i=e.getDomainEventHandlers(t.name,n.name),u=[],o=0,s=i.length;s>o;o++)r=i[o],u.push(r(n));return u}}(this))},t.prototype._delegateAllDomainEventsToRemoteEndpoints=function(t){return t.subscribeToAllDomainEvents(function(e){return function(n){return e._remoteEndpoints.forEach(function(e){return e.publish(t.name,n.name,n),n.aggregate?e.publish(t.name,n.name,n.aggregate.id,n):void 0})}}(this))},t.prototype.subscribeToDomainEvent=function(){var t,e,n,r,i,s,u;return r=2<=arguments.length?o.call(arguments,0,u=arguments.length-1):(u=0,[]),e=arguments[u++],t=r[0],n=r[1],null==t&&(t="all"),null==n&&(n="all"),"all"===t&&"all"===n?this._domainEventHandlersAll.push(e):(null==(i=this._domainEventHandlers)[t]&&(i[t]={}),null==(s=this._domainEventHandlers[t])[n]&&(s[n]=[]),this._domainEventHandlers[t][n].push(e))},t.prototype.getDomainEventHandlers=function(t,e){var n,r,i,o,s;return[].concat(null!=(n=null!=(r=this._domainEventHandlers[t])?r[e]:void 0)?n:[],null!=(i=null!=(o=this._domainEventHandlers[t])?o.all:void 0)?i:[],null!=(s=this._domainEventHandlersAll)?s:[])},t.prototype.generateUid=function(t){var e,n;return e=function(){return(65536*(1+Math.random())|0).toString(16).substring(1)},n=t||"-",e()+e()+n+e()+n+e()+n+e()+n+e()+e()+e()},t.prototype.defaults=function(t,e){var n,r,i,o;for(n=[].concat(Object.keys(t),Object.keys(e)),i=0,o=n.length;o>i;i++)r=n[i],!t[r]&&e[r]&&(t[r]=e[r]);return t},t.prototype.eachSeries=function(t,e,n){var r,i;return n=n||function(){},Array.isArray(t)&&t.length?(r=0,(i=function(){e(t[r],function(e){e?n(e):(++r,r>=t.length?n():i())})})()):n()},t.prototype.mixin=function(t,e){var n,r;r=[];for(n in e)r.push(t[n]=e[n]);return r},t}(),n.exports=r}),require.register("es6-promise",function(t,e,n){(function(){"use strict";function t(t){return"function"==typeof t||"object"==typeof t&&null!==t}function e(t){return"function"==typeof t}function r(t){return"object"==typeof t&&null!==t}function i(){}function o(){return function(){process.nextTick(c)}}function s(){var t=0,e=new O(c),n=document.createTextNode("");return e.observe(n,{characterData:!0}),function(){n.data=t=++t%2}}function u(){var t=new MessageChannel;return t.port1.onmessage=c,function(){t.port2.postMessage(0)}}function a(){return function(){setTimeout(c,1)}}function c(){for(var t=0;z>t;t+=2){var e=M[t],n=M[t+1];e(n),M[t]=void 0,M[t+1]=void 0}z=0}function l(){}function h(){return new TypeError("You cannot resolve a promise with itself")}function f(){return new TypeError("A promises callback cannot return that same promise.")}function p(t){try{return t.then}catch(e){return W.error=e,W}}function m(t,e,n,r){try{t.call(e,n,r)}catch(i){return i}}function g(t,e,n){H(function(t){var r=!1,i=m(n,e,function(n){r||(r=!0,e!==n?_(t,n):b(t,n))},function(e){r||(r=!0,E(t,e))},"Settle: "+(t._label||" unknown promise"));!r&&i&&(r=!0,E(t,i))},t)}function d(t,e){e._state===k?b(t,e._result):t._state===$?E(t,e._result):j(e,void 0,function(e){_(t,e)},function(e){E(t,e)})}function v(t,n){if(n.constructor===t.constructor)d(t,n);else{var r=p(n);r===W?E(t,W.error):void 0===r?b(t,n):e(r)?g(t,n,r):b(t,n)}}function _(e,n){e===n?E(e,h()):t(n)?v(e,n):b(e,n)}function y(t){t._onerror&&t._onerror(t._result),P(t)}function b(t,e){t._state===L&&(t._result=e,t._state=k,0===t._subscribers.length||H(P,t))}function E(t,e){t._state===L&&(t._state=$,t._result=e,H(y,t))}function j(t,e,n,r){var i=t._subscribers,o=i.length;t._onerror=null,i[o]=e,i[o+k]=n,i[o+$]=r,0===o&&t._state&&H(P,t)}function P(t){var e=t._subscribers,n=t._state;if(0!==e.length){for(var r,i,o=t._result,s=0;s<e.length;s+=3)r=e[s],i=e[s+n],r?D(n,r,i,o):i(o);t._subscribers.length=0}}function x(){this.error=null}function w(t,e){try{return t(e)}catch(n){return G.error=n,G}}function D(t,n,r,i){var o,s,u,a,c=e(r);if(c){if(o=w(r,i),o===G?(a=!0,s=o.error,o=null):u=!0,n===o)return void E(n,f())}else o=i,u=!0;n._state!==L||(c&&u?_(n,o):a?E(n,s):t===k?b(n,o):t===$&&E(n,o))}function S(t,e){try{e(function(e){_(t,e)},function(e){E(t,e)})}catch(n){E(t,n)}}function A(t,e,n,r){this._instanceConstructor=t,this.promise=new t(l,r),this._abortOnReject=n,this._validateInput(e)?(this._input=e,this.length=e.length,this._remaining=e.length,this._init(),0===this.length?b(this.promise,this._result):(this.length=this.length||0,this._enumerate(),0===this._remaining&&b(this.promise,this._result))):E(this.promise,this._validationError())}function C(){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}function I(){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}function R(t){this._id=V++,this._state=void 0,this._result=void 0,this._subscribers=[],l!==t&&(e(t)||C(),this instanceof R||I(),S(this,t))}var T;T=Array.isArray?Array.isArray:function(t){return"[object Array]"===Object.prototype.toString.call(t)};var N,q=T,z=(Date.now||function(){return(new Date).getTime()},Object.create||function(t){if(arguments.length>1)throw new Error("Second argument not supported");if("object"!=typeof t)throw new TypeError("Argument must be an object");return i.prototype=t,new i},0),H=function(t,e){M[z]=t,M[z+1]=e,z+=2,2===z&&N()},F="undefined"!=typeof window?window:{},O=F.MutationObserver||F.WebKitMutationObserver,B="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel,M=new Array(1e3);N="undefined"!=typeof process&&"[object process]"==={}.toString.call(process)?o():O?s():B?u():a();var L=void 0,k=1,$=2,W=new x,G=new x;A.prototype._validateInput=function(t){return q(t)},A.prototype._validationError=function(){return new Error("Array Methods must be provided an Array")},A.prototype._init=function(){this._result=new Array(this.length)};var U=A;A.prototype._enumerate=function(){for(var t=this.length,e=this.promise,n=this._input,r=0;e._state===L&&t>r;r++)this._eachEntry(n[r],r)},A.prototype._eachEntry=function(t,e){var n=this._instanceConstructor;r(t)?t.constructor===n&&t._state!==L?(t._onerror=null,this._settledAt(t._state,e,t._result)):this._willSettleAt(n.resolve(t),e):(this._remaining--,this._result[e]=this._makeResult(k,e,t))},A.prototype._settledAt=function(t,e,n){var r=this.promise;r._state===L&&(this._remaining--,this._abortOnReject&&t===$?E(r,n):this._result[e]=this._makeResult(t,e,n)),0===this._remaining&&b(r,this._result)},A.prototype._makeResult=function(t,e,n){return n},A.prototype._willSettleAt=function(t,e){var n=this;j(t,void 0,function(t){n._settledAt(k,e,t)},function(t){n._settledAt($,e,t)})};var Q=function(t,e){return new U(this,t,!0,e).promise},Y=function(t,e){function n(t){_(o,t)}function r(t){E(o,t)}var i=this,o=new i(l,e);if(!q(t))return E(o,new TypeError("You must pass an array to race.")),o;for(var s=t.length,u=0;o._state===L&&s>u;u++)j(i.resolve(t[u]),void 0,n,r);return o},K=function(t,e){var n=this;if(t&&"object"==typeof t&&t.constructor===n)return t;var r=new n(l,e);return _(r,t),r},J=function(t,e){var n=this,r=new n(l,e);return E(r,t),r},V=0,X=R;R.all=Q,R.race=Y,R.resolve=K,R.reject=J,R.prototype={constructor:R,then:function(t,e){var n=this,r=n._state;if(r===k&&!t||r===$&&!e)return this;var i=new this.constructor(l),o=n._result;if(r){var s=arguments[r-1];H(function(){D(r,i,s,o)})}else j(n,i,t,e);return i},"catch":function(t){return this.then(null,t)}};var Z=function(){var t;t="undefined"!=typeof global?global:"undefined"!=typeof window&&window.document?window:self;var n="Promise"in t&&"resolve"in t.Promise&&"reject"in t.Promise&&"all"in t.Promise&&"race"in t.Promise&&function(){var n;return new t.Promise(function(t){n=t}),e(n)}();n||(t.Promise=X)},te={Promise:X,polyfill:Z};"function"==typeof define&&define.amd?define(function(){return te}):"undefined"!=typeof n&&n.exports?n.exports=te:"undefined"!=typeof this&&(this.ES6Promise=te)}).call(this)}),require.register("eventric/index",function(t,e,n){e("es6-promise").polyfill(),n.exports=new(e("./eventric"))}),require.register("eventric/aggregate/aggregate",function(t,e,n){var r,i=function(t,e){return function(){return t.apply(e,arguments)}};r=function(){function t(t,e,n,r){this._context=t,this._eventric=e,this._name=n,this.getDomainEvents=i(this.getDomainEvents,this),this.emitDomainEvent=i(this.emitDomainEvent,this),this._domainEvents=[],this.root=r?new r:{},this.root.$emitDomainEvent=this.emitDomainEvent}return t.prototype.emitDomainEvent=function(t,e){var n,r,i;if(n=this._context.getDomainEvent(t),!n)throw i="Tried to emitDomainEvent '"+t+"' which is not defined",this._eventric.log.error(i),new Error(i);return r=this._createDomainEvent(t,n,e),this._domainEvents.push(r),this._handleDomainEvent(t,r),this._eventric.log.debug("Created and Handled DomainEvent in Aggregate",r)},t.prototype._createDomainEvent=function(t,e,n){var r;return r={},e.apply(r,[n]),new this._eventric.DomainEvent({id:this._eventric.generateUid(),name:t,aggregate:{id:this.id,name:this._name},context:this._context.name,payload:r})},t.prototype._handleDomainEvent=function(t,e){return this.root["handle"+t]?this.root["handle"+t](e,function(){}):this._eventric.log.debug("Tried to handle the DomainEvent '"+t+"' without a matching handle method")},t.prototype.getDomainEvents=function(){return this._domainEvents},t.prototype.applyDomainEvents=function(t){var e,n,r,i;for(i=[],n=0,r=t.length;r>n;n++)e=t[n],i.push(this._applyDomainEvent(e));return i},t.prototype._applyDomainEvent=function(t){return this._handleDomainEvent(t.name,t)},t}(),n.exports=r}),require.register("eventric/aggregate/index",function(t,e,n){n.exports=e("./aggregate")}),require.register("eventric/context/context",function(t,e,n){var r,i=function(t,e){return function(){return t.apply(e,arguments)}},o=[].slice;r=function(){function t(t,e){this.name=t,this._eventric=e,this.clearProjectionStore=i(this.clearProjectionStore,this),this.getProjectionStore=i(this.getProjectionStore,this),this._getAggregateRepository=i(this._getAggregateRepository,this),this.publishDomainEvent=i(this.publishDomainEvent,this),this.emitDomainEvent=i(this.emitDomainEvent,this),this._initialized=!1,this._params=this._eventric.get(),this._di={},this._aggregateRootClasses={},this._commandHandlers={},this._queryHandlers={},this._domainEventClasses={},this._domainEventHandlers={},this._projectionClasses={},this._domainEventStreamClasses={},this._domainEventStreamInstances={},this._repositoryInstances={},this._storeClasses={},this._storeInstances={},this._eventBus=new this._eventric.EventBus(this._eventric),this.projectionService=new this._eventric.Projection(this._eventric),this.log=this._eventric.log}return t.prototype.set=function(t,e){return this._params[t]=e,this},t.prototype.get=function(t){return this._params[t]},t.prototype.emitDomainEvent=function(t,e){var n,r;if(n=this.getDomainEvent(t),!n)throw new Error("Tried to emitDomainEvent '"+t+"' which is not defined");return r=this._createDomainEvent(t,n,e),this.saveAndPublishDomainEvent(r).then(function(t){return function(){return t._eventric.log.debug("Created and Handled DomainEvent in Context",r)}}(this))},t.prototype.publishDomainEvent=function(t){return this._eventBus.publishDomainEvent(t)},t.prototype._createDomainEvent=function(t,e,n){return new this._eventric.DomainEvent({id:this._eventric.generateUid(),name:t,context:this.name,payload:new e(n)})},t.prototype.addStore=function(t,e,n){return null==n&&(n={}),this._storeClasses[t]={Class:e,options:n},this},t.prototype.defineDomainEvent=function(t,e){return this._domainEventClasses[t]=e,this},t.prototype.defineDomainEvents=function(t){var e,n;for(n in t)e=t[n],this.defineDomainEvent(n,e);return this},t.prototype.addCommandHandler=function(t,e){return this._commandHandlers[t]=e,this},t.prototype._getAggregateRepository=function(t,e){var n,r,i;return r||(r={}),r[t]||(n=this._aggregateRootClasses[t],i=new this._eventric.Repository({aggregateName:t,AggregateRoot:n,context:this,eventric:this._eventric}),r[t]=i),r[t].setCommand(e),r[t]},t.prototype.addCommandHandlers=function(t){var e,n;for(n in t)e=t[n],this.addCommandHandler(n,e);return this},t.prototype.addQueryHandler=function(t,e){return this._queryHandlers[t]=e,this},t.prototype.addQueryHandlers=function(t){var e,n;for(n in t)e=t[n],this.addQueryHandler(n,e);return this},t.prototype.addAggregate=function(t,e){return this._aggregateRootClasses[t]=e,this},t.prototype.addAggregates=function(t){var e,n;for(n in t)e=t[n],this.addAggregate(n,e);return this},t.prototype.subscribeToDomainEvent=function(t,e){var n;return n=function(t){return function(){return e.apply(t._di,arguments)}}(this),this._eventBus.subscribeToDomainEvent(t,n)},t.prototype.subscribeToDomainEvents=function(t){var e,n,r;r=[];for(e in t)n=t[e],r.push(this.subscribeToDomainEvent(e,n));return r},t.prototype.subscribeToDomainEventWithAggregateId=function(t,e,n){var r;return r=function(t){return function(){return n.apply(t._di,arguments)}}(this),this._eventBus.subscribeToDomainEventWithAggregateId(t,e,r)},t.prototype.subscribeToAllDomainEvents=function(t){var e;return e=function(e){return function(){return t.apply(e._di,arguments)}}(this),this._eventBus.subscribeToAllDomainEvents(e)},t.prototype.addProjection=function(t,e){return this._projectionClasses[t]=e,this},t.prototype.addProjections=function(t){var e,n;for(n in t)e=t[n],this.addProjection(n,e);return this},t.prototype.getProjectionInstance=function(t){return this.projectionService.getInstance(t)},t.prototype.destroyProjectionInstance=function(t){return this.projectionService.destroyInstance(t,this)},t.prototype.initializeProjectionInstance=function(t,e){var n;return this._projectionClasses[t]?this.projectionService.initializeInstance(t,this._projectionClasses[t],e,this):(n="Given projection "+t+" not registered on context",this._eventric.log.error(n),n=new Error(n))},t.prototype.initialize=function(){return new Promise(function(t){return function(e,n){return t.log.debug("["+t.name+"] Initializing"),t.log.debug("["+t.name+"] Initializing Store"),t._initializeStores().then(function(){return t.log.debug("["+t.name+"] Finished initializing Store"),t._di={$query:function(){return t.query.apply(t,arguments)},$projectionStore:function(){return t.getProjectionStore.apply(t,arguments)},$emitDomainEvent:function(){return t.emitDomainEvent.apply(t,arguments)}}}).then(function(){return t.log.debug("["+t.name+"] Initializing Projections"),t._initializeProjections()}).then(function(){return t.log.debug("["+t.name+"] Finished initializing Projections"),t.log.debug("["+t.name+"] Finished initializing"),t._initialized=!0,e()})["catch"](function(t){return n(t)})}}(this))},t.prototype._initializeStores=function(){return new Promise(function(t){return function(e,n){var r,i,o,s;o=[],s=t._eventric.defaults(t._storeClasses,t._eventric.getStores());for(i in s)r=s[i],o.push({name:i,Class:r.Class,options:r.options});return t._eventric.eachSeries(o,function(e,n){return t.log.debug("["+t.name+"] Initializing Store "+e.name),t._storeInstances[e.name]=new e.Class,t._storeInstances[e.name].initialize(t,e.options).then(function(){return t.log.debug("["+t.name+"] Finished initializing Store "+e.name),n()})["catch"](function(t){return n(t)})},function(t){return t?n(t):e()})}}(this))},t.prototype._initializeProjections=function(){return new Promise(function(t){return function(e,n){var r,i,o,s;o=[],s=t._projectionClasses;for(i in s)r=s[i],o.push({name:i,"class":r});return t._eventric.eachSeries(o,function(e,r){var i;return i=null,t.log.debug("["+t.name+"] Initializing Projection "+e.name),t.projectionService.initializeInstance(e.name,e["class"],{},t).then(function(){return t.log.debug("["+t.name+"] Finished initializing Projection "+e.name),r()})["catch"](function(t){return n(t)})},function(t){return t?n(t):e()})}}(this))},t.prototype.getProjection=function(t){return this.projectionService.getInstance(t)},t.prototype.getDomainEvent=function(t){return this._domainEventClasses[t]},t.prototype.getDomainEventsStore=function(){var t;return t=this.get("default domain events store"),this._storeInstances[t]},t.prototype.saveAndPublishDomainEvent=function(t){return new Promise(function(e){return function(n,r){return e.getDomainEventsStore().saveDomainEvent(t).then(function(){return e.publishDomainEvent(t)}).then(function(e){return e?r(e):n(t)})}}(this))},t.prototype.findDomainEventsByName=function(){var t;return t=1<=arguments.length?o.call(arguments,0):[],new Promise(function(e){return function(n,r){var i;return(i=e.getDomainEventsStore()).findDomainEventsByName.apply(i,o.call(t).concat([function(t,e){return t?r(t):n(e)}]))}}(this))},t.prototype.findDomainEventsByNameAndAggregateId=function(){var t;return t=1<=arguments.length?o.call(arguments,0):[],new Promise(function(e){return function(n,r){var i;return(i=e.getDomainEventsStore()).findDomainEventsByNameAndAggregateId.apply(i,o.call(t).concat([function(t,e){return t?r(t):n(e)}]))}}(this))},t.prototype.getProjectionStore=function(t,e){return new Promise(function(n){return function(r,i){var o;return n._storeInstances[t]?n._storeInstances[t].getProjectionStore(e).then(function(t){return r(t)})["catch"](function(t){return i(t)}):(o="Requested Store with name "+t+" not found",n.log.error(o),i(o))}}(this))},t.prototype.clearProjectionStore=function(t,e){return new Promise(function(n){return function(r,i){var o;return n._storeInstances[t]?n._storeInstances[t].clearProjectionStore(e).then(function(){return r()})["catch"](function(t){return i(t)}):(o="Requested Store with name "+t+" not found",n.log.error(o),i(o))}}(this))},t.prototype.getEventBus=function(){return this._eventBus},t.prototype.command=function(t,e){return new Promise(function(n){return function(r,i){var s,u,a,c,l,h,f,p;if(s={id:n._eventric.generateUid(),name:t,params:e},n.log.debug("Got Command",s),!n._initialized)return l="Context not initialized yet",n.log.error(l),l=new Error(l),i(l);if(!n._commandHandlers[t])return l="Given command "+t+" not registered on context",n.log.error(l),l=new Error(l),i(l);f={},p=n._di;for(c in p)a=p[c],f[c]=a;return f.$aggregate={create:function(){var t,e,r;return t=arguments[0],e=2<=arguments.length?o.call(arguments,1):[],r=n._getAggregateRepository(t,s),r.create.apply(r,e)},load:function(t,e){var r;return r=n._getAggregateRepository(t,s),r.findById(e)}},h=null,u=n._commandHandlers[t],h=u.apply(f,[e]),Promise.all([h]).then(function(e){var i;return i=e[0],n.log.debug("Completed Command",t),r(i)})["catch"](function(t){return i(t)})}}(this))},t.prototype.query=function(t,e){return new Promise(function(n){return function(r,i){var o,s;return n.log.debug("Got Query",t),n._initialized?n._queryHandlers[t]?(s=n._queryHandlers[t].apply(n._di,[e]),Promise.all([s]).then(function(e){var i;return i=e[0],n.log.debug("Completed Query "+t+" with Result "+i),r(i)})["catch"](function(t){return i(t)})):(o="Given query "+t+" not registered on context",n.log.error(o),o=new Error(o),i(o)):(o="Context not initialized yet",n.log.error(o),o=new Error(o),void i(o))}}(this))},t}(),n.exports=r}),require.register("eventric/context/index",function(t,e,n){n.exports=e("./context")}),require.register("eventric/domain_event/domain_event",function(t,e,n){var r;r=function(){function t(t){this.id=t.id,this.name=t.name,this.payload=t.payload,this.aggregate=t.aggregate,this.context=t.context,this.timestamp=(new Date).getTime()}return t}(),n.exports=r}),require.register("eventric/domain_event/index",function(t,e,n){n.exports=e("./domain_event")}),require.register("eventric/event_bus/event_bus",function(t,e,n){var r;r=function(){function t(t){this._eventric=t,this._pubSub=new this._eventric.PubSub}return t.prototype.subscribeToDomainEvent=function(t,e,n){return null==n&&(n={}),this._pubSub.subscribe(t,e)},t.prototype.subscribeToDomainEventWithAggregateId=function(t,e,n,r){return null==r&&(r={}),this.subscribeToDomainEvent(""+t+"/"+e,n,r)},t.prototype.subscribeToAllDomainEvents=function(t){return this._pubSub.subscribe("DomainEvent",t)},t.prototype.publishDomainEvent=function(t){return new Promise(function(e){return function(n,r){return e._pubSub.publish("DomainEvent",t).then(function(){return e._pubSub.publish(t.name,t)}).then(function(){return t.aggregate&&t.aggregate.id?e._pubSub.publish(""+t.name+"/"+t.aggregate.id,t).then(function(){return n()}):n()})["catch"](function(t){return r(t)})}}(this))},t}(),n.exports=r}),require.register("eventric/event_bus/index",function(t,e,n){n.exports=e("./event_bus")}),require.register("eventric/logger/index",function(t,e,n){n.exports=e("./logger")}),require.register("eventric/logger/logger",function(t,e,n){n.exports={_logLevel:1,setLogLevel:function(t){return this._logLevel=function(){switch(t){case"debug":return 0;case"warn":return 1;case"info":return 2;case"error":return 3}}()},debug:function(){return this._logLevel>0?void 0:console.log.apply(console,arguments)},warn:function(){return this._logLevel>1?void 0:console.log.apply(console,arguments)},info:function(){return this._logLevel>2?void 0:console.log.apply(console,arguments)},error:function(){return this._logLevel>3?void 0:console.log.apply(console,arguments)}}}),require.register("eventric/projection/index",function(t,e,n){n.exports=e("./projection")}),require.register("eventric/projection/projection",function(t,e,n){var r,i=function(t,e){return function(){return t.apply(e,arguments)}};r=function(){function t(t){this._eventric=t,this._applyDomainEventToProjection=i(this._applyDomainEventToProjection,this),this.log=this._eventric.log,this._handlerFunctions={},this._projectionInstances={},this._domainEventsApplied={}}return t.prototype.initializeInstance=function(t,e,n,r){return this._context=r,new Promise(function(r){return function(i,o){var s,u,a,c,l,h;if(c="function"==typeof e?new e:e,r._context._di){h=r._context._di;for(a in h)u=h[a],c[a]=u}return l=r._eventric.generateUid(),s=null,c.$subscribeHandlersWithAggregateId=function(t){return s=t},r.log.debug("["+r._context.name+"] Clearing ProjectionStores "+c.stores+" of "+t),r._clearProjectionStores(c.stores,t).then(function(){return r.log.debug("["+r._context.name+"] Finished clearing ProjectionStores of "+t),r._injectStoresIntoProjection(t,c)}).then(function(){return r._callInitializeOnProjection(t,c,n)}).then(function(){return r.log.debug("["+r._context.name+"] Replaying DomainEvents against Projection "+t),r._parseEventNamesFromProjection(c)}).then(function(t){return r._applyDomainEventsFromStoreToProjection(l,c,t,s)}).then(function(e){return r.log.debug("["+r._context.name+"] Finished Replaying DomainEvents against Projection "+t),r._subscribeProjectionToDomainEvents(l,t,c,e,s)}).then(function(){var e;return r._projectionInstances[l]=c,e={id:l,projection:c},r._context.publish("projection:"+t+":initialized",e),r._context.publish("projection:"+l+":initialized",e),i(l)})["catch"](function(t){return o(t)})}}(this))},t.prototype._callInitializeOnProjection=function(t,e,n){return new Promise(function(r){return function(i){return e.initialize?(r.log.debug("["+r._context.name+"] Calling initialize on Projection "+t),e.initialize(n,function(){return r.log.debug("["+r._context.name+"] Finished initialize call on Projection "+t),i(e)})):(r.log.debug("["+r._context.name+"] No initialize function on Projection "+t+" given, skipping"),i(e))}}(this))},t.prototype._injectStoresIntoProjection=function(t,e){return new Promise(function(n){return function(r,i){return e.stores?(null==e.$store&&(e.$store={}),n._eventric.eachSeries(e.stores,function(r,i){return n.log.debug("["+n._context.name+"] Injecting ProjectionStore "+r+" into Projection "+t),n._context.getProjectionStore(r,t).then(function(o){return o?(e.$store[r]=o,n.log.debug("["+n._context.name+"] Finished Injecting ProjectionStore "+r+" into Projection "+t),i()):void 0})["catch"](function(t){return i(t)})},function(t){return t?i(t):r()})):r()}}(this))},t.prototype._clearProjectionStores=function(t,e){return new Promise(function(n){return function(r){return t?n._eventric.eachSeries(t,function(t,r){return n.log.debug("["+n._context.name+"] Clearing ProjectionStore "+t+" for "+e),n._context.clearProjectionStore(t,e).then(function(){return n.log.debug("["+n._context.name+"] Finished clearing ProjectionStore "+t+" for "+e),r()})["catch"](function(t){return r(t)})},function(){return r()}):r()}}(this))},t.prototype._parseEventNamesFromProjection=function(t){return new Promise(function(){return function(e){var n,r,i,o;r=[];for(i in t)o=t[i],0===i.indexOf("handle")&&"function"==typeof o&&(n=i.replace(/^handle/,""),r.push(n));return e(r)}}(this))},t.prototype._applyDomainEventsFromStoreToProjection=function(t,e,n,r){return new Promise(function(i){return function(o,s){var u;return i._domainEventsApplied[t]={},u=r?i._context.findDomainEventsByNameAndAggregateId(n,r):i._context.findDomainEventsByName(n),u.then(function(r){return r&&0!==r.length?i._eventric.eachSeries(r,function(n,r){return i._applyDomainEventToProjection(n,e).then(function(){return i._domainEventsApplied[t][n.id]=!0,r()})},function(t){return t?s(t):o(n)}):o(n)})["catch"](function(t){return s(t)})}}(this))},t.prototype._subscribeProjectionToDomainEvents=function(t,e,n,r,i){return new Promise(function(o){return function(s,u){var a;return a=function(r,i){return null==i&&(i=function(){}),o._domainEventsApplied[t][r.id]?i():o._applyDomainEventToProjection(r,n).then(function(){var s;return o._domainEventsApplied[t][r.id]=!0,s={id:t,projection:n,domainEvent:r},o._context.publish("projection:"+e+":changed",s),o._context.publish("projection:"+t+":changed",s),i()})["catch"](function(t){return i(t)})},o._eventric.eachSeries(r,function(e,n){var r;return r=i?o._context.subscribeToDomainEventWithAggregateId(e,i,a):o._context.subscribeToDomainEvent(e,a),r.then(function(e){var r;return null==(r=o._handlerFunctions)[t]&&(r[t]=[]),o._handlerFunctions[t].push(e),n()})["catch"](function(t){return n(t)})},function(t){return t?u(t):s()})}}(this))},t.prototype._applyDomainEventToProjection=function(t,e){return new Promise(function(n){return function(r){var i;return e["handle"+t.name]?(i=e["handle"+t.name](t),Promise.all([i]).then(function(t){var e;return e=t[0],r(e)})):(n.log.debug("Tried to apply DomainEvent '"+t.name+"' to Projection without a matching handle method"),void r())}}(this))},t.prototype.getInstance=function(t){return this._projectionInstances[t]},t.prototype.destroyInstance=function(t){var e,n,r,i,o;if(!this._handlerFunctions[t])return this._eventric.log.error("Missing attribute projectionId");for(n=[],o=this._handlerFunctions[t],r=0,i=o.length;i>r;r++)e=o[r],n.push(this._context.unsubscribeFromDomainEvent(e));return delete this._handlerFunctions[t],delete this._projectionInstances[t],Promise.all(n)},t}(),n.exports=r}),require.register("eventric/pub_sub/index",function(t,e,n){n.exports=e("./pub_sub")}),require.register("eventric/pub_sub/pub_sub",function(t,e,n){var r,i=[].slice;r=function(){function t(){this._subscribers=[],this._subscriberId=0,this._nextTick=function(){var t;return t=1<=arguments.length?i.call(arguments,0):[],setTimeout.apply(null,t)}}return t.prototype.subscribe=function(t,e){return new Promise(function(n){return function(r){var i;return i={eventName:t,subscriberFn:e,subscriberId:n._getNextSubscriberId()},n._subscribers.push(i),r(i.subscriberId)}}(this))},t.prototype.publish=function(t,e){return new Promise(function(n){return function(r){var i,o;return o=n._getRelevantSubscribers(t),i=function(){return 0===o.length?r():(o.shift().subscriberFn(e,function(){}),n._nextTick(i,0))},n._nextTick(i,0)}}(this))},t.prototype._getRelevantSubscribers=function(t){return t?this._subscribers.filter(function(e){return e.eventName===t}):this._subscribers},t.prototype.unsubscribe=function(t){return new Promise(function(e){return function(n){return e._subscribers=e._subscribers.filter(function(e){return e.subscriberId!==t}),n()}}(this))},t.prototype._getNextSubscriberId=function(){return this._subscriberId++},t}(),n.exports=r}),require.register("eventric/remote/index",function(t,e,n){n.exports=e("./remote")}),require.register("eventric/remote/remote",function(t,e,n){var r;r=function(){function t(t,n){this._contextName=t,this._eventric=n,this.name=this._contextName,this.InMemoryRemote=e("./inmemory"),this._params={},this._clients={},this._projectionClasses={},this._projectionInstances={},this._handlerFunctions={},this.projectionService=new this._eventric.Projection(this._eventric),this.addClient("inmemory",this.InMemoryRemote.client),this.set("default client","inmemory"),this._exposeRpcOperationsAsMemberFunctions() }return t.ALLOWED_RPC_OPERATIONS=["command","query","findDomainEventsByName","findDomainEventsByNameAndAggregateId"],t.prototype._exposeRpcOperationsAsMemberFunctions=function(){return t.ALLOWED_RPC_OPERATIONS.forEach(function(t){return function(e){return t[e]=function(){return t._rpc(e,arguments)}}}(this))},t.prototype.set=function(t,e){return this._params[t]=e,this},t.prototype.get=function(t){return this._params[t]},t.prototype.subscribeToAllDomainEvents=function(t){var e,n;return n=this.get("default client"),e=this.getClient(n),e.subscribe(this._contextName,t)},t.prototype.subscribeToDomainEvent=function(t,e){var n,r;return r=this.get("default client"),n=this.getClient(r),n.subscribe(this._contextName,t,e)},t.prototype.subscribeToDomainEventWithAggregateId=function(t,e,n){var r,i;return i=this.get("default client"),r=this.getClient(i),r.subscribe(this._contextName,t,e,n)},t.prototype.unsubscribeFromDomainEvent=function(t){var e,n;return n=this.get("default client"),e=this.getClient(n),e.unsubscribe(t)},t.prototype._rpc=function(t,e){var n,r;return r=this.get("default client"),n=this.getClient(r),n.rpc({contextName:this._contextName,method:t,params:Array.prototype.slice.call(e)})},t.prototype.addClient=function(t,e){return this._clients[t]=e,this},t.prototype.getClient=function(t){return this._clients[t]},t.prototype.addProjection=function(t,e){return this._projectionClasses[t]=e,this},t.prototype.initializeProjection=function(t,e){return this.projectionService.initializeInstance("",t,e,this)},t.prototype.initializeProjectionInstance=function(t,e){var n;return this._projectionClasses[t]?this.projectionService.initializeInstance(t,this._projectionClasses[t],e,this):(n="Given projection "+t+" not registered on remote",this._eventric.log.error(n),n=new Error(n))},t.prototype.getProjectionInstance=function(t){return this.projectionService.getInstance(t)},t.prototype.destroyProjectionInstance=function(t){return this.projectionService.destroyInstance(t,this)},t}(),n.exports=r}),require.register("eventric/repository/index",function(t,e,n){n.exports=e("./repository")}),require.register("eventric/repository/repository",function(t,e,n){var r,i=function(t,e){return function(){return t.apply(e,arguments)}};r=function(){function t(t,e){this._eventric=e,this.save=i(this.save,this),this.create=i(this.create,this),this.findById=i(this.findById,this),this._aggregateName=t.aggregateName,this._AggregateRoot=t.AggregateRoot,this._context=t.context,this._eventric=t.eventric,this._command={},this._aggregateInstances={},this._store=this._context.getDomainEventsStore()}return t.prototype.findById=function(t,e){return null==e&&(e=function(){}),new Promise(function(n){return function(r,i){return n._findDomainEventsForAggregate(t,function(o,s){var u,a,c,l;return o?(e(o,null),void i(o)):s.length?(u=new n._eventric.Aggregate(n._context,n._eventric,n._aggregateName,n._AggregateRoot),u.applyDomainEvents(s),u.id=t,u.root.$id=t,u.root.$save=function(){return n.save(u.id)},a=null!=(l=n._command.id)?l:"nocommand",null==(c=n._aggregateInstances)[a]&&(c[a]={}),n._aggregateInstances[a][t]=u,e(null,u.root),r(u.root)):(o="No domainEvents for "+n._aggregateName+" Aggregate with "+t+" available",n._eventric.log.error(o),e(o,null),void i(o))})}}(this))},t.prototype._findDomainEventsForAggregate=function(t,e){return this._store.findDomainEventsByAggregateId(t,function(){return function(t,n){return t?e(t,null):0===n.length?e(null,[]):e(null,n)}}(this))},t.prototype.create=function(t){return new Promise(function(e){return function(n,r){var i,o,s,u,a,c;return i=new e._eventric.Aggregate(e._context,e._eventric,e._aggregateName,e._AggregateRoot),i.id=e._eventric.generateUid(),"function"!=typeof i.root.create&&(u="No create function on aggregate",e._eventric.log.error(u),r(new Error(u))),i.root.$id=i.id,i.root.$save=function(){return e.save(i.id)},o=null!=(c=e._command.id)?c:"nocommand",null==(a=e._aggregateInstances)[o]&&(a[o]={}),e._aggregateInstances[o][i.id]=i,s=i.root.create(t),Promise.all([s]).then(function(){return n(i.root)})["catch"](function(t){var e;return e=t[0],r(e)})}}(this))},t.prototype.save=function(t,e){return null==e&&(e=function(){}),new Promise(function(n){return function(r,i){var o,s,u,a,c;return s=null!=(c=n._command.id)?c:"nocommand",(o=n._aggregateInstances[s][t])?(u=o.getDomainEvents(),u.length<1?(a="Tried to save 0 DomainEvents from Aggregate "+n._aggregateName,n._eventric.log.debug(a,n._command),a=new Error(a),"function"==typeof e&&e(a,null),void i(a)):(n._eventric.log.debug("Going to Save and Publish "+u.length+" DomainEvents from Aggregate "+n._aggregateName),n._eventric.eachSeries(u,function(t,e){return n._store.saveDomainEvent(t).then(function(){return n._eventric.log.debug("Saved DomainEvent",t),e(null)})},function(t){return t?(e(t,null),i(t)):n._eventric.eachSeries(u,function(t,e){return n._eventric.log.debug("Publishing DomainEvent",t),n._context.getEventBus().publishDomainEvent(t).then(function(){return e()})},function(t){return t?(e(t,null),i(t)):(r(o.id),e(null,o.id))})}))):(a="Tried to save unknown aggregate "+n._aggregateName,n._eventric.log.error(a),a=new Error(a),"function"==typeof e&&e(a,null),void i(a))}}(this))},t.prototype.setCommand=function(t){return this._command=t},t}(),n.exports=r}),require.register("eventric/remote/inmemory/index",function(t,e,n){n.exports=e("./remote_inmemory")}),require.register("eventric/remote/inmemory/remote_inmemory",function(t,e,n){var r,i,o,s,u,a,c=[].slice;o=e("../../pub_sub"),s=null,a=new o,i=function(){function t(){s=function(t){return function(e){return new Promise(function(n,r){return t._handleRPCRequest(e,function(t,e){return t?r(t):n(e)})})}}(this)}return t.prototype.setRPCHandler=function(t){this._handleRPCRequest=t},t.prototype.publish=function(){var t,e,n,r,i,o,s;return e=arguments[0],o=3<=arguments.length?c.call(arguments,1,s=arguments.length-1):(s=1,[]),i=arguments[s++],n=o[0],t=o[1],r=u(e,n,t),a.publish(r,i,function(){})},t}(),n.exports.endpoint=new i,r=function(){function t(){}return t.prototype.rpc=function(t){if(!s)throw new Error("No Remote Endpoint available for in memory client");return s(t)},t.prototype.subscribe=function(){var t,e,n,r,i,o,s;return e=arguments[0],o=3<=arguments.length?c.call(arguments,1,s=arguments.length-1):(s=1,[]),i=arguments[s++],n=o[0],t=o[1],r=u(e,n,t),a.subscribe(r,i)},t.prototype.unsubscribe=function(t){return a.unsubscribe(t)},t}(),n.exports.client=new r,u=function(t,e,n){var r;return r=t,e&&(r+="/"+e),n&&(r+="/"+n),r}}),require.register("eventric/store/inmemory/index",function(t,e,n){n.exports=e("./store_inmemory")}),require.register("eventric/store/inmemory/store_inmemory",function(t,e,n){var r,i,o=[].slice;i=["domain_events","projections"],r=function(){function t(){}return t.prototype._domainEvents={},t.prototype._projections={},t.prototype.initialize=function(){var t,e,n;return n=arguments[0],e=2<=arguments.length?o.call(arguments,1):[],this._context=n,t=e[0],new Promise(function(t){return function(e){return t._domainEventsCollectionName=""+t._context.name+".DomainEvents",t._projectionCollectionName=""+t._context.name+".Projections",t._domainEvents[t._domainEventsCollectionName]=[],e()}}(this))},t.prototype.saveDomainEvent=function(t){return new Promise(function(e){return function(n){return e._domainEvents[e._domainEventsCollectionName].push(t),n(t)}}(this))},t.prototype.findDomainEventsByName=function(t,e){var n,r;return n=t instanceof Array?function(e){return t.indexOf(e)>-1}:function(e){return e===t},r=this._domainEvents[this._domainEventsCollectionName].filter(function(t){return n(t.name)}),e(null,r)},t.prototype.findDomainEventsByNameAndAggregateId=function(t,e,n){var r,i,o;return i=t instanceof Array?function(e){return t.indexOf(e)>-1}:function(e){return e===t},r=e instanceof Array?function(t){return e.indexOf(t)>-1}:function(t){return t===e},o=this._domainEvents[this._domainEventsCollectionName].filter(function(t){var e;return i(t.name)&&r(null!=(e=t.aggregate)?e.id:void 0)}),n(null,o)},t.prototype.findDomainEventsByAggregateId=function(t,e){var n,r;return n=t instanceof Array?function(e){return t.indexOf(e)>-1}:function(e){return e===t},r=this._domainEvents[this._domainEventsCollectionName].filter(function(t){var e;return n(null!=(e=t.aggregate)?e.id:void 0)}),e(null,r)},t.prototype.getProjectionStore=function(t){return new Promise(function(e){return function(n){var r,i,o;return null==(r=e._projections)[o=e._projectionCollectionName]&&(r[o]={}),null==(i=e._projections[e._projectionCollectionName])[t]&&(i[t]={}),n(e._projections[e._projectionCollectionName][t])}}(this))},t.prototype.clearProjectionStore=function(t){return new Promise(function(e){return function(n){var r,i,o;return null==(r=e._projections)[o=e._projectionCollectionName]&&(r[o]={}),null==(i=e._projections[e._projectionCollectionName])[t]&&(i[t]={}),delete e._projections[e._projectionCollectionName][t],n()}}(this))},t.prototype.checkSupport=function(t){return i.indexOf(t)>-1},t}(),n.exports=r});