eventric
Version:
Build JavaScript applications with Behaviour-driven Domain Design. Based on DDD, BDD, CQRS and EventSourcing.
169 lines (139 loc) • 5.29 kB
JavaScript
var PubSub, Remote, eventric, projectionService,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
eventric = require('eventric');
PubSub = require('eventric/src/pub_sub');
projectionService = require('eventric/src/projection');
Remote = (function(_super) {
__extends(Remote, _super);
function Remote(_contextName) {
this._contextName = _contextName;
Remote.__super__.constructor.apply(this, arguments);
this.name = this._contextName;
this._params = {};
this._clients = {};
this._projectionClasses = {};
this._projectionInstances = {};
this._handlerFunctions = {};
this.addClient('inmemory', (require('eventric/src/remote/inmemory')).client);
this.set('default client', 'inmemory');
}
Remote.prototype.set = function(key, value) {
this._params[key] = value;
return this;
};
Remote.prototype.get = function(key) {
return this._params[key];
};
Remote.prototype.command = function() {
return this._rpc('command', arguments);
};
Remote.prototype.query = function() {
return this._rpc('query', arguments);
};
Remote.prototype.findAllDomainEvents = function() {
return this._rpc('findAllDomainEvents', arguments);
};
Remote.prototype.findDomainEventsByName = function() {
return this._rpc('findDomainEventsByName', arguments);
};
Remote.prototype.findDomainEventsByAggregateId = function() {
return this._rpc('findDomainEventsByAggregateId', arguments);
};
Remote.prototype.findDomainEventsByAggregateName = function() {
return this._rpc('findDomainEventsByAggregateName', arguments);
};
Remote.prototype.findDomainEventsByNameAndAggregateId = function() {
return this._rpc('findDomainEventsByNameAndAggregateId', arguments);
};
Remote.prototype.subscribeToAllDomainEvents = function(handlerFn, options) {
var client, clientName;
if (options == null) {
options = {};
}
clientName = this.get('default client');
client = this.getClient(clientName);
return client.subscribe(this._contextName, handlerFn);
};
Remote.prototype.subscribeToDomainEvent = function(domainEventName, handlerFn, options) {
var client, clientName;
if (options == null) {
options = {};
}
clientName = this.get('default client');
client = this.getClient(clientName);
return client.subscribe(this._contextName, domainEventName, handlerFn);
};
Remote.prototype.subscribeToDomainEventWithAggregateId = function(domainEventName, aggregateId, handlerFn, options) {
var client, clientName;
if (options == null) {
options = {};
}
clientName = this.get('default client');
client = this.getClient(clientName);
return client.subscribe(this._contextName, domainEventName, aggregateId, handlerFn);
};
Remote.prototype.unsubscribeFromDomainEvent = function(subscriberId) {
var client, clientName;
clientName = this.get('default client');
client = this.getClient(clientName);
return client.unsubscribe(subscriberId);
};
Remote.prototype._rpc = function(method, params) {
return new Promise((function(_this) {
return function(resolve, reject) {
var client, clientName;
clientName = _this.get('default client');
client = _this.getClient(clientName);
return client.rpc({
contextName: _this._contextName,
method: method,
params: Array.prototype.slice.call(params)
}, function(err, result) {
if (err) {
return reject(err);
} else {
return resolve(result);
}
});
};
})(this));
};
Remote.prototype.addClient = function(clientName, client) {
this._clients[clientName] = client;
return this;
};
Remote.prototype.getClient = function(clientName) {
return this._clients[clientName];
};
Remote.prototype.addProjection = function(projectionName, projectionClass) {
this._projectionClasses[projectionName] = projectionClass;
return this;
};
Remote.prototype.initializeProjection = function(projectionObject) {
return projectionService.initializeInstance({
object: projectionObject
}, null, this);
};
Remote.prototype.initializeProjectionInstance = function(projectionName, params) {
var err;
if (!this._projectionClasses[projectionName]) {
err = "Given projection " + projectionName + " not registered on remote";
eventric.log.error(err);
err = new Error(err);
return err;
}
return projectionService.initializeInstance({
name: projectionName,
"class": this._projectionClasses[projectionName]
}, params, this);
};
Remote.prototype.getProjectionInstance = function(projectionId) {
return projectionService.getInstance(projectionId);
};
Remote.prototype.destroyProjectionInstance = function(projectionId) {
return projectionService.destroyInstance(projectionId, this);
};
return Remote;
})(PubSub);
module.exports = Remote;