Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cqrs

Package Overview
Dependencies
Maintainers
2
Versions
183
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cqrs - npm Package Compare versions

Comparing version 2.10.23 to 2.10.24

32

dist/ActorEventEmitter.js
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -19,16 +11,14 @@ const Actor_1 = require("./Actor");

}
publish(event) {
return __awaiter(this, void 0, void 0, function* () {
let json = this.json;
let map = json[event.actorType];
for (let listenerId in map) {
let listener = map[listenerId];
let { listenerType, handleMethodName } = listener;
listener = yield this.service.get(listenerType, listenerId);
if (listener) {
yield listener[handleMethodName](event);
}
async publish(event) {
let json = this.json;
let map = json[event.actorType];
for (let listenerId in map) {
let listener = map[listenerId];
let { listenerType, handleMethodName } = listener;
listener = await this.service.get(listenerType, listenerId);
if (listener) {
await listener[handleMethodName](event);
}
this.service.unbind();
});
}
this.service.unbind();
}

@@ -35,0 +25,0 @@ subscribe(actorType, listenerType, listenerId, handleMethodName) {

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -18,7 +10,5 @@ class IDManager {

}
unbind(id) {
return __awaiter(this, void 0, void 0, function* () {
this.holdIds.delete(id);
this.socket.emit("unbind", { domainId: this.domainId, id });
});
async unbind(id) {
this.holdIds.delete(id);
this.socket.emit("unbind", { domainId: this.domainId, id });
}

@@ -28,21 +18,19 @@ isHold(id) {

}
bind(id) {
return __awaiter(this, void 0, void 0, function* () {
if (this.isHold(id))
return;
var that = this;
return new Promise(resolve => {
let timeout = false;
// timeout
let t = setTimeout(() => {
timeout = true;
resolve("timeout");
}, 1000);
this.socket.emit("bind", { domainId: this.domainId, id }, (err, result) => {
clearTimeout(t);
this.holdIds.add(id);
if (!timeout) {
resolve();
}
});
async bind(id) {
if (this.isHold(id))
return;
var that = this;
return new Promise(resolve => {
let timeout = false;
// timeout
let t = setTimeout(() => {
timeout = true;
resolve("timeout");
}, 1000);
this.socket.emit("bind", { domainId: this.domainId, id }, (err, result) => {
clearTimeout(t);
this.holdIds.add(id);
if (!timeout) {
resolve();
}
});

@@ -49,0 +37,0 @@ });

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -22,90 +14,64 @@ const Event_1 = require("./Event");

}
existSaga(sagaId) {
return __awaiter(this, void 0, void 0, function* () {
return !!(yield this.getSaga(sagaId));
});
async existSaga(sagaId) {
return !!await this.getSaga(sagaId);
}
beginSaga(sagaId) {
return __awaiter(this, void 0, void 0, function* () {
const exist = yield this.existSaga(sagaId);
if (!exist) {
return this.sagas.insert({ sagaId, done: false, alive: true });
}
});
async beginSaga(sagaId) {
const exist = await this.existSaga(sagaId);
if (!exist) {
return this.sagas.insert({ sagaId, done: false, alive: true });
}
}
getSaga(sagaId) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.sagas.cfindOne({ sagaId, alive: true }).exec();
});
async getSaga(sagaId) {
return await this.sagas.cfindOne({ sagaId, alive: true }).exec();
}
killSaga(sagaId) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.sagas.update({ sagaId }, { alive: false });
});
async killSaga(sagaId) {
return await this.sagas.update({ sagaId }, { alive: false });
}
endSaga(sagaId) {
return __awaiter(this, void 0, void 0, function* () {
const exist = yield this.existSaga(sagaId);
if (exist) {
return yield this.sagas.update({ sagaId }, { done: true });
}
});
async endSaga(sagaId) {
const exist = await this.existSaga(sagaId);
if (exist) {
return await this.sagas.update({ sagaId }, { done: true });
}
}
findUndoneSaga() {
return __awaiter(this, void 0, void 0, function* () {
return yield this.sagas.find({ done: false });
});
async findUndoneSaga() {
return await this.sagas.find({ done: false });
}
createSnap(snap) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.snaps.insert(snap.json);
});
async createSnap(snap) {
return await this.snaps.insert(snap.json);
}
saveEvents(events) {
return __awaiter(this, void 0, void 0, function* () {
events = [].concat(events);
const eventsJSONArr = events.map(event => {
return event.json || event;
});
yield this.events.insert(eventsJSONArr);
this.emit('saved events', events);
async saveEvents(events) {
events = [].concat(events);
const eventsJSONArr = events.map(event => {
return event.json || event;
});
await this.events.insert(eventsJSONArr);
this.emit('saved events', events);
}
getLatestSnapshot(actorId) {
return __awaiter(this, void 0, void 0, function* () {
let data = yield this.snaps.cfindOne({ actorId }).sort({ index: -1, date: -1 }).exec();
if (data) {
return Snap_1.default.parse(data);
}
});
async getLatestSnapshot(actorId) {
let data = await this.snaps.cfindOne({ actorId }).sort({ index: -1, date: -1 }).exec();
if (data) {
return Snap_1.default.parse(data);
}
}
getEvents(actorId) {
return __awaiter(this, void 0, void 0, function* () {
let events = yield this.events.cfind({ actorId }).sort({ index: 1, date: 1 }).exec();
return events.map(event => Event_1.default.parse(event));
});
async getEvents(actorId) {
let events = await this.events.cfind({ actorId }).sort({ index: 1, date: 1 }).exec();
return events.map(event => Event_1.default.parse(event));
}
getLatestEvent(actorId) {
return __awaiter(this, void 0, void 0, function* () {
let event = yield this.events.cfind({ actorId }).sort({ index: -1, date: -1 }).limit(1).exec();
return event.length ? Event_1.default.parse(event[0]) : null;
});
async getLatestEvent(actorId) {
let event = await this.events.cfind({ actorId }).sort({ index: -1, date: -1 }).limit(1).exec();
return event.length ? Event_1.default.parse(event[0]) : null;
}
getEventsBySnapshot(snapId) {
return __awaiter(this, void 0, void 0, function* () {
const snap = yield this.getSnapshotById(snapId);
if (snap) {
let events = yield this.events.cfind({
actorId: snap.actorId,
index: { '$gt': snap.latestEventIndex }
}).sort({ date: 1, index: 1 }).exec();
return events.map(event => Event_1.default.parse(event));
}
});
async getEventsBySnapshot(snapId) {
const snap = await this.getSnapshotById(snapId);
if (snap) {
let events = await this.events.cfind({
actorId: snap.actorId,
index: { '$gt': snap.latestEventIndex }
}).sort({ date: 1, index: 1 }).exec();
return events.map(event => Event_1.default.parse(event));
}
}
getSnapshotByIndex(actorId, index) {
return __awaiter(this, void 0, void 0, function* () {
let snap = yield this.snaps.cfindOne({ actorId, index }).exec();
return Snap_1.default.parse(snap);
});
async getSnapshotByIndex(actorId, index) {
let snap = await this.snaps.cfindOne({ actorId, index }).exec();
return Snap_1.default.parse(snap);
}

@@ -122,36 +88,26 @@ // async getSnapshotByLastIndex(actorId, index) {

// }
getSnapshotById(id) {
return __awaiter(this, void 0, void 0, function* () {
let snap = yield this.snaps.cfindOne({ id }).exec();
return Snap_1.default.parse(snap);
});
async getSnapshotById(id) {
let snap = await this.snaps.cfindOne({ id }).exec();
return Snap_1.default.parse(snap);
}
getEventById(id) {
return __awaiter(this, void 0, void 0, function* () {
let event = yield this.events.cfindOne({ id }).exec();
if (event) {
return Event_1.default.parse(event);
}
else {
return null;
}
});
async getEventById(id) {
let event = await this.events.cfindOne({ id }).exec();
if (event) {
return Event_1.default.parse(event);
}
else {
return null;
}
}
findEventsBySagaId(sagaId) {
return __awaiter(this, void 0, void 0, function* () {
let events = yield this.events.cfind({ sagaId }).sort({ index: -1, date: -1 }).exec();
return events.map(event => Event_1.default.parse(event));
});
async findEventsBySagaId(sagaId) {
let events = await this.events.cfind({ sagaId }).sort({ index: -1, date: -1 }).exec();
return events.map(event => Event_1.default.parse(event));
}
removeEventsBySagaId(sagaId) {
return __awaiter(this, void 0, void 0, function* () {
yield this.killSaga(sagaId);
yield this.events.remove({ sagaId });
});
async removeEventsBySagaId(sagaId) {
await this.killSaga(sagaId);
await this.events.remove({ sagaId });
}
findFollowEvents(actorId, index) {
return __awaiter(this, void 0, void 0, function* () {
let events = yield this.events.cfind({ actorId, index: { $gt: index } }).sort({ index: 1, date: 1 }).exec();
return events.map(event => Event_1.default.parse(event));
});
async findFollowEvents(actorId, index) {
let events = await this.events.cfind({ actorId, index: { $gt: index } }).sort({ index: 1, date: 1 }).exec();
return events.map(event => Event_1.default.parse(event));
}

@@ -158,0 +114,0 @@ }

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -59,8 +51,8 @@ const Service_1 = require("./Service");

// TODO: clear undone saga!
(() => __awaiter(this, void 0, void 0, function* () {
const sagas = yield this.eventstore.findUndoneSaga();
(async () => {
const sagas = await this.eventstore.findUndoneSaga();
for (let saga of sagas) {
this.eventbus.rollback(saga.sagaId);
}
}))();
})();
this.repositorieMap = new Map();

@@ -96,222 +88,214 @@ this.eventbus = options.EventBus ?

}
getNativeActor(type, id) {
return __awaiter(this, void 0, void 0, function* () {
const roles = type.split(".");
const actorType = roles.shift();
let repo = this.repositorieMap.get(this.ActorClassMap.get(actorType));
const actor = yield repo.get(id);
let result;
if (roles.length) {
for (let role of roles) {
result = this.roleMap.get(role).wrap(result || actor);
}
async getNativeActor(type, id) {
const roles = type.split(".");
const actorType = roles.shift();
let repo = this.repositorieMap.get(this.ActorClassMap.get(actorType));
const actor = await repo.get(id);
let result;
if (roles.length) {
for (let role of roles) {
result = this.roleMap.get(role).wrap(result || actor);
}
return result || actor;
});
}
return result || actor;
}
nativeCreateActor(type, data) {
return __awaiter(this, void 0, void 0, function* () {
const actorType = type.split(".").shift();
const ActorClass = this.ActorClassMap.get(actorType);
const repo = this.repositorieMap.get(ActorClass);
let uniqueValidator = yield this.get('UniqueValidator', ActorClass.getType());
if (!uniqueValidator && ActorClass.uniqueFields) {
uniqueValidator = yield this.create("UniqueValidator", { actotType: ActorClass.getType(), uniqueFields: ActorClass.uniqueFields });
}
if (ActorClass.beforeCreate) {
try {
let uniqueValidatedOk = true;
let holded = [];
// unique field value validate
if (ActorClass.uniqueFields) {
let arr = [];
ActorClass.uniqueFields.forEach(key => {
let value = data[key];
if (value && ['string', 'number'].includes(typeof (value))) {
arr.push({ key, value });
}
});
if (arr.length) {
try {
uniqueValidatedOk = yield uniqueValidator.hold(arr);
}
catch (err) {
holded = err.holded;
uniqueValidatedOk = false;
}
uniqueValidator.unbind();
async nativeCreateActor(type, data) {
const actorType = type.split(".").shift();
const ActorClass = this.ActorClassMap.get(actorType);
const repo = this.repositorieMap.get(ActorClass);
let uniqueValidator = await this.get('UniqueValidator', ActorClass.getType());
if (!uniqueValidator && ActorClass.uniqueFields) {
uniqueValidator = await this.create("UniqueValidator", { actotType: ActorClass.getType(), uniqueFields: ActorClass.uniqueFields });
}
if (ActorClass.beforeCreate) {
try {
let uniqueValidatedOk = true;
let holded = [];
// unique field value validate
if (ActorClass.uniqueFields) {
let arr = [];
ActorClass.uniqueFields.forEach(key => {
let value = data[key];
if (value && ['string', 'number'].includes(typeof (value))) {
arr.push({ key, value });
}
});
if (arr.length) {
try {
uniqueValidatedOk = await uniqueValidator.hold(arr);
}
catch (err) {
holded = err.holded;
uniqueValidatedOk = false;
}
uniqueValidator.unbind();
}
data = (yield ActorClass.beforeCreate(data, this, uniqueValidatedOk, holded)) || data;
}
catch (err) {
throw err;
}
data = (await ActorClass.beforeCreate(data, this, uniqueValidatedOk, holded)) || data;
}
const actorId = (yield repo.create(data)).json.id;
const actor = yield this[exports.getActorProxy](type, actorId);
if (ActorClass.created) {
yield ActorClass.created(actor, this);
catch (err) {
throw err;
}
return actor;
});
}
const actorId = (await repo.create(data)).json.id;
const actor = await this[exports.getActorProxy](type, actorId);
if (ActorClass.created) {
await ActorClass.created(actor, this);
}
return actor;
}
[exports.getActorProxy](type, id, sagaId, key, parents) {
return __awaiter(this, void 0, void 0, function* () {
parents = parents || [];
let actor = yield this.getNativeActor(type, id);
if (!actor) {
return null;
}
// cluster support
// if (this.isCluster) {
// if (!this.idManager.isHold(id)) {
// // if timeout , then try loop bind .
// let looptry = async () => {
// const result = await this.idManager.bind(id);
// if (result === 'timeout') { // timeout
// if (parents) {
// for (let parent of parents) {
// await this.idManager.unbind(parent.id); // unbind parent actor
// const p = await this[getActorProxy](parent.type, parent.id); // rebind parent actor
// // parent is removed
// if (!p) {
// throw new Error(`type=${parent.type} id=${parent.id} 's actor is removed!`);
// }
// }
// }
// await looptry();
// }
// }
// await looptry();
// if (Array.isArray(actor)) {
// let events = await this.eventstore.findFollowEvents(actor[0].id, actor[latestEventIndex]);
// actor[0][loadEvents](events);
// if (!actor[0].json.isAlive) {
// return null;
// }
// } else {
// let events = await this.eventstore.findFollowEvents(actor.id, actor[latestEventIndex]);
// actor[loadEvents](events);
// if (!actor.json.isAlive) {
// return null;
// }
// }
// }
// }
const that = this;
let roles;
if (Array.isArray(actor)) {
roles = actor[1];
actor = actor[0];
}
const proxy = new Proxy(actor, {
get(target, prop) {
if (prop === "then") {
return proxy;
}
;
let member = actor[prop];
let roleName;
let role;
if ("data" === prop || "lock" === prop || "lockData" === prop || prop === "json" || prop === "id" || typeof prop === 'symbol') {
return Reflect.get(target, prop);
}
else {
if (!member) {
if (roles) {
for (let rn in roles) {
role = roles[rn];
member = role.methods[prop];
roleName = rn;
if (member)
break;
}
async [exports.getActorProxy](type, id, sagaId, key, parents) {
parents = parents || [];
let actor = await this.getNativeActor(type, id);
if (!actor) {
return null;
}
// cluster support
// if (this.isCluster) {
// if (!this.idManager.isHold(id)) {
// // if timeout , then try loop bind .
// let looptry = async () => {
// const result = await this.idManager.bind(id);
// if (result === 'timeout') { // timeout
// if (parents) {
// for (let parent of parents) {
// await this.idManager.unbind(parent.id); // unbind parent actor
// const p = await this[getActorProxy](parent.type, parent.id); // rebind parent actor
// // parent is removed
// if (!p) {
// throw new Error(`type=${parent.type} id=${parent.id} 's actor is removed!`);
// }
// }
// }
// await looptry();
// }
// }
// await looptry();
// if (Array.isArray(actor)) {
// let events = await this.eventstore.findFollowEvents(actor[0].id, actor[latestEventIndex]);
// actor[0][loadEvents](events);
// if (!actor[0].json.isAlive) {
// return null;
// }
// } else {
// let events = await this.eventstore.findFollowEvents(actor.id, actor[latestEventIndex]);
// actor[loadEvents](events);
// if (!actor.json.isAlive) {
// return null;
// }
// }
// }
// }
const that = this;
let roles;
if (Array.isArray(actor)) {
roles = actor[1];
actor = actor[0];
}
const proxy = new Proxy(actor, {
get(target, prop) {
if (prop === "then") {
return proxy;
}
;
let member = actor[prop];
let roleName;
let role;
if ("data" === prop || "lock" === prop || "lockData" === prop || prop === "json" || prop === "id" || typeof prop === 'symbol') {
return Reflect.get(target, prop);
}
else {
if (!member) {
if (roles) {
for (let rn in roles) {
role = roles[rn];
member = role.methods[prop];
roleName = rn;
if (member)
break;
}
else
return;
}
if (typeof member === "function") {
if (prop in Object.prototype)
return undefined;
return new Proxy(member, {
apply(target, cxt, args) {
return new Promise(function (resolve, reject) {
function run() {
return __awaiter(this, void 0, void 0, function* () {
for (let i = 0; i < that.beforeCallHandles.length; i++) {
yield that.beforeCallHandles[i]({ actor, prop });
else
return;
}
if (typeof member === "function") {
if (prop in Object.prototype)
return undefined;
return new Proxy(member, {
apply(target, cxt, args) {
return new Promise(function (resolve, reject) {
async function run() {
for (let i = 0; i < that.beforeCallHandles.length; i++) {
await that.beforeCallHandles[i]({ actor, prop });
}
const islock = actor[isLock](key);
if (islock) {
setTimeout(run, 2000);
}
else {
const iservice = new Service_1.default(actor, that.eventbus, that.repositorieMap.get(that.ActorClassMap.get(actor.type)), that, (type, id, sagaId, key, parent) => that[exports.getActorProxy](type, id, sagaId, key, parent), (type, data) => that.nativeCreateActor(type, data), prop, sagaId, roleName, role, [...parents, { type: actor.type, id: actor.id }]);
const service = function (type, data) {
if (arguments.length === 0) {
type = prop;
data = null;
}
const islock = actor[isLock](key);
if (islock) {
setTimeout(run, 2000);
else if (arguments.length === 1) {
data = type;
type = prop;
}
return iservice.apply(type, data);
};
service.__proto__ = iservice;
cxt = { service, $: service, proxy };
cxt.__proto__ = actor;
let result;
try {
result = target.call(cxt, ...args);
}
catch (err) {
if (service.isRootSaga) {
that.eventbus.rollback(sagaId || service.sagaId).then(r => reject(err));
}
else {
const iservice = new Service_1.default(actor, that.eventbus, that.repositorieMap.get(that.ActorClassMap.get(actor.type)), that, (type, id, sagaId, key, parent) => that[exports.getActorProxy](type, id, sagaId, key, parent), (type, data) => that.nativeCreateActor(type, data), prop, sagaId, roleName, role, [...parents, { type: actor.type, id: actor.id }]);
const service = function (type, data) {
if (arguments.length === 0) {
type = prop;
data = null;
}
else if (arguments.length === 1) {
data = type;
type = prop;
}
return iservice.apply(type, data);
};
service.__proto__ = iservice;
cxt = { service, $: service, proxy };
cxt.__proto__ = actor;
let result;
try {
result = target.call(cxt, ...args);
reject(err);
}
return;
}
if (result instanceof Promise) {
result.then(result => {
resolve(result);
if (!service.unbindCalled) {
service.unbind();
}
catch (err) {
if (service.isRootSaga) {
that.eventbus.rollback(sagaId || service.sagaId).then(r => reject(err));
}
else {
reject(err);
}
return;
}).catch(err => {
if (!service.unbindCalled) {
service.unbind();
}
if (result instanceof Promise) {
result.then(result => {
resolve(result);
if (!service.unbindCalled) {
service.unbind();
}
}).catch(err => {
if (!service.unbindCalled) {
service.unbind();
}
if (service.isRootSaga) {
that.eventbus.rollback(sagaId || service.sagaId).then(r => reject(err));
}
else {
reject(err);
}
});
if (service.isRootSaga) {
that.eventbus.rollback(sagaId || service.sagaId).then(r => reject(err));
}
else {
resolve(result);
if (service.unbindCalled) {
service.unbind();
}
reject(err);
}
});
}
else {
resolve(result);
if (service.unbindCalled) {
service.unbind();
}
});
}
}
run();
});
}
});
}
else
return undefined;
}
run();
});
}
});
}
else
return undefined;
}
});
return proxy;
}
});
return proxy;
}

@@ -322,5 +306,5 @@ register(Classes) {

}
(() => __awaiter(this, void 0, void 0, function* () {
(async () => {
if (this.isCluster) {
yield this.waitInited();
await this.waitInited();
}

@@ -335,6 +319,6 @@ for (let Class of Classes) {

this.repositorieMap.set(Class, repo);
(() => __awaiter(this, void 0, void 0, function* () {
(async () => {
this.waitInited();
if (type !== 'ActorEventEmitter' && type !== 'UniqueValidator') {
const emitter = yield this.get('ActorEventEmitter', "ActorEventEmitter" + type);
const emitter = await this.get('ActorEventEmitter', "ActorEventEmitter" + type);
if (!emitter && (!this.isCluster || this._isMaster)) {

@@ -356,16 +340,12 @@ this.create("ActorEventEmitter", { id: "ActorEventEmitter" + type });

});
}))();
})();
}
}))();
})();
return this;
}
create(type, data) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.nativeCreateActor(type, data);
});
async create(type, data) {
return await this.nativeCreateActor(type, data);
}
get(type, id) {
return __awaiter(this, void 0, void 0, function* () {
return yield this[exports.getActorProxy](type, id);
});
async get(type, id) {
return await this[exports.getActorProxy](type, id);
}

@@ -372,0 +352,0 @@ on(event, handle) {

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -69,43 +61,39 @@ const events_1 = require("events");

}
publish(actor) {
return __awaiter(this, void 0, void 0, function* () {
if (this.lockSet.has(actor.id)) {
return;
}
else {
this.lockSet.add(actor.id);
}
const event = yield this.eventstore.getLatestEvent(actor.id);
let startIndex = event ? event.index + 1 : 0;
let events = actor[uncommittedEvents].map(function (evt, index) {
actor[latestEventIndex] = evt.index = index + startIndex;
return evt;
});
yield this.eventstore.saveEvents(events);
actor[uncommittedEvents] = [];
let snap = yield this.eventstore.getLatestSnapshot(actor.id);
let lastEvent = events[events.length - 1];
if (lastEvent.index - snap.lastEventId > 10) {
let latestEventIndex = lastEvent.index;
let index = snap.index + 1;
let newSnap = new Snap_1.default(actor, index, latestEventIndex);
yield this.eventstore.createSnap(newSnap);
}
this.lockSet.delete(actor.id);
if (actor[uncommittedEvents].length) {
yield this.publish(actor);
}
async publish(actor) {
if (this.lockSet.has(actor.id)) {
return;
}
else {
this.lockSet.add(actor.id);
}
const event = await this.eventstore.getLatestEvent(actor.id);
let startIndex = event ? event.index + 1 : 0;
let events = actor[uncommittedEvents].map(function (evt, index) {
actor[latestEventIndex] = evt.index = index + startIndex;
return evt;
});
await this.eventstore.saveEvents(events);
actor[uncommittedEvents] = [];
let snap = await this.eventstore.getLatestSnapshot(actor.id);
let lastEvent = events[events.length - 1];
if (lastEvent.index - snap.lastEventId > 10) {
let latestEventIndex = lastEvent.index;
let index = snap.index + 1;
let newSnap = new Snap_1.default(actor, index, latestEventIndex);
await this.eventstore.createSnap(newSnap);
}
this.lockSet.delete(actor.id);
if (actor[uncommittedEvents].length) {
await this.publish(actor);
}
}
rollback(sagaId) {
return __awaiter(this, void 0, void 0, function* () {
yield sleep_1.default(100);
yield this.eventstore.killSaga(sagaId);
const events = yield this.eventstore.findEventsBySagaId(sagaId);
yield this.eventstore.removeEventsBySagaId(sagaId);
events.forEach(event => {
const Class = this.ActorClassMap.get(event.actorType);
const repo = this.repositorieMap.get(Class);
repo.clear(event.actorId);
});
async rollback(sagaId) {
await sleep_1.default(100);
await this.eventstore.killSaga(sagaId);
const events = await this.eventstore.findEventsBySagaId(sagaId);
await this.eventstore.removeEventsBySagaId(sagaId);
events.forEach(event => {
const Class = this.ActorClassMap.get(event.actorType);
const repo = this.repositorieMap.get(Class);
repo.clear(event.actorId);
});

@@ -112,0 +100,0 @@ }

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -24,11 +16,9 @@ const Snap_1 = require("./Snap");

}
create(data) {
return __awaiter(this, void 0, void 0, function* () {
const actor = new this.ActorClass(data);
const snap = new Snap_1.default(actor);
yield this.eventstore.createSnap(snap);
this.cache.set(actor.id, actor);
setImmediate(() => this.emit("create", actor.json));
return actor;
});
async create(data) {
const actor = new this.ActorClass(data);
const snap = new Snap_1.default(actor);
await this.eventstore.createSnap(snap);
this.cache.set(actor.id, actor);
setImmediate(() => this.emit("create", actor.json));
return actor;
}

@@ -44,33 +34,29 @@ clear(id) {

}
getHistory(actorId, actorType) {
return __awaiter(this, void 0, void 0, function* () {
const snap = yield this.eventstore.getSnapshotByIndex(actorId, 0);
const events = yield this.eventstore.getEvents(actorId);
if (snap) {
return new History_1.default(this.ActorClass, snap, events, actorType);
}
throw new Error("no actor by " + actorId);
});
async getHistory(actorId, actorType) {
const snap = await this.eventstore.getSnapshotByIndex(actorId, 0);
const events = await this.eventstore.getEvents(actorId);
if (snap) {
return new History_1.default(this.ActorClass, snap, events, actorType);
}
throw new Error("no actor by " + actorId);
}
get(id) {
return __awaiter(this, void 0, void 0, function* () {
let actor = this.getFromCache(id);
if (actor) {
if (actor.json.isAlive) {
return actor;
}
else {
this.cache.delete(id);
return null;
}
async get(id) {
let actor = this.getFromCache(id);
if (actor) {
if (actor.json.isAlive) {
return actor;
}
else {
this.emit("reborn", id);
let snap = yield this.eventstore.getLatestSnapshot(id);
if (snap) {
const events = yield this.eventstore.getEventsBySnapshot(snap.id);
return reborn_1.default(this.ActorClass, snap, events);
}
this.cache.delete(id);
return null;
}
});
}
else {
this.emit("reborn", id);
let snap = await this.eventstore.getLatestSnapshot(id);
if (snap) {
const events = await this.eventstore.getEventsBySnapshot(snap.id);
return reborn_1.default(this.ActorClass, snap, events);
}
}
}

@@ -77,0 +63,0 @@ exist(id) {

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -44,75 +36,73 @@ const Event_1 = require("./Event");

}
apply(type, data, direct) {
return __awaiter(this, void 0, void 0, function* () {
const event = new Event_1.default(this.actor, data, type, this.method, this.sagaId, direct || false, this.roleName);
let updater;
if (type === "remove") {
updater = () => ({ isAlive: false });
}
else if (type === "subscribe") {
updater = (json, _event) => {
const listeners = json.listeners;
let { event, listenerType, listenerId, handleMethodName } = _event.data;
if (listeners[event]) {
listeners[event][listenerId] = { handleMethodName, listenerType };
}
else {
listeners[event] = { [listenerId]: { handleMethodName, listenerType } };
}
return { listeners };
};
}
else if (type === "unsubscribe") {
updater = (json, _event) => {
const listeners = json.listeners;
let { event, listenerId } = _event.data;
if (listeners[event]) {
delete listeners[event][listenerId];
}
return { listeners };
};
}
else {
updater = (this.actor.updater[type] ||
this.actor.updater[this.method + "Update"] ||
(this.role ? this.role.updater[type] || this.role.updater[this.method] : null));
}
if (updater) {
const updatedData = updater(this.actor[datakey], event);
this.actor[datakey] = Object.assign({}, this.actor[datakey], direct ? data : {}, updatedData);
event.updatedData = _.pick(this.actor.refreshJSON(), Object.keys(updatedData));
}
this.actor[uncommittedEvents] = this.actor[uncommittedEvents] || [];
this.actor[uncommittedEvents].push(event);
++this.actor[exports.latestEventIndex];
// this.actor.refreshJSON();
yield this.bus.publish(this.actor);
this.applied = true;
if (!["subscribe", "unsubscribe", "_subscribe", "_unsubscribe"].includes(type)) {
const actorType = this.actor.type;
setImmediate(() => __awaiter(this, void 0, void 0, function* () {
const emitter = yield this.get("ActorEventEmitter", "ActorEventEmitter" + actorType);
if (emitter) {
emitter.publish(event);
}
}));
let listeners = this.actor.json.listeners;
let handles = listeners[type];
let emit = (handles) => __awaiter(this, void 0, void 0, function* () {
if (handles) {
for (let id in handles) {
let { handleMethodName, listenerType } = handles[id];
let actor = yield this.get(listenerType, id);
if (actor) {
actor[handleMethodName](event);
}
async apply(type, data, direct) {
const event = new Event_1.default(this.actor, data, type, this.method, this.sagaId, direct || false, this.roleName);
let updater;
if (type === "remove") {
updater = () => ({ isAlive: false });
}
else if (type === "subscribe") {
updater = (json, _event) => {
const listeners = json.listeners;
let { event, listenerType, listenerId, handleMethodName } = _event.data;
if (listeners[event]) {
listeners[event][listenerId] = { handleMethodName, listenerType };
}
else {
listeners[event] = { [listenerId]: { handleMethodName, listenerType } };
}
return { listeners };
};
}
else if (type === "unsubscribe") {
updater = (json, _event) => {
const listeners = json.listeners;
let { event, listenerId } = _event.data;
if (listeners[event]) {
delete listeners[event][listenerId];
}
return { listeners };
};
}
else {
updater = (this.actor.updater[type] ||
this.actor.updater[this.method + "Update"] ||
(this.role ? this.role.updater[type] || this.role.updater[this.method] : null));
}
if (updater) {
const updatedData = updater(this.actor[datakey], event);
this.actor[datakey] = Object.assign({}, this.actor[datakey], direct ? data : {}, updatedData);
event.updatedData = _.pick(this.actor.refreshJSON(), Object.keys(updatedData));
}
this.actor[uncommittedEvents] = this.actor[uncommittedEvents] || [];
this.actor[uncommittedEvents].push(event);
++this.actor[exports.latestEventIndex];
// this.actor.refreshJSON();
await this.bus.publish(this.actor);
this.applied = true;
if (!["subscribe", "unsubscribe", "_subscribe", "_unsubscribe"].includes(type)) {
const actorType = this.actor.type;
setImmediate(async () => {
const emitter = await this.get("ActorEventEmitter", "ActorEventEmitter" + actorType);
if (emitter) {
emitter.publish(event);
}
});
let listeners = this.actor.json.listeners;
let handles = listeners[type];
let emit = async (handles) => {
if (handles) {
for (let id in handles) {
let { handleMethodName, listenerType } = handles[id];
let actor = await this.get(listenerType, id);
if (actor) {
actor[handleMethodName](event);
}
}
});
emit(handles);
handles = listeners["*"];
emit(handles);
}
this.unbind();
});
}
};
emit(handles);
handles = listeners["*"];
emit(handles);
}
this.unbind();
}

@@ -132,30 +122,24 @@ lock(timeout) {

}
sagaBegin() {
return __awaiter(this, void 0, void 0, function* () {
if (this.sagaId && !this.sagaMode) {
throw new Error("Cannot include child Saga");
}
this.sagaMode = true;
this.sagaId = uuid();
yield this._domain.eventstore.beginSaga(this.sagaId);
});
async sagaBegin() {
if (this.sagaId && !this.sagaMode) {
throw new Error("Cannot include child Saga");
}
this.sagaMode = true;
this.sagaId = uuid();
await this._domain.eventstore.beginSaga(this.sagaId);
}
sagaEnd() {
return __awaiter(this, void 0, void 0, function* () {
if (this.sagaMode) {
this.sagaMode = false;
this.sagaId = null;
yield this._domain.eventstore.endSaga(this.sagaId);
}
});
async sagaEnd() {
if (this.sagaMode) {
this.sagaMode = false;
this.sagaId = null;
await this._domain.eventstore.endSaga(this.sagaId);
}
}
rollback() {
return __awaiter(this, void 0, void 0, function* () {
if (this.sagaMode) {
return yield this.bus.rollback(this.sagaId);
}
else {
throw new Error("no saga");
}
});
async rollback() {
if (this.sagaMode) {
return await this.bus.rollback(this.sagaId);
}
else {
throw new Error("no saga");
}
}

@@ -166,71 +150,59 @@ actorLock(actor) {

tryLock();
function tryLock() {
return __awaiter(this, void 0, void 0, function* () {
var isLock = yield actor.lock({ key: that.key, timeout: that.timeout });
if (isLock)
resolve();
else {
setTimeout(tryLock, 300);
}
});
async function tryLock() {
var isLock = await actor.lock({ key: that.key, timeout: that.timeout });
if (isLock)
resolve();
else {
setTimeout(tryLock, 300);
}
}
});
}
get(type, id) {
return __awaiter(this, void 0, void 0, function* () {
if (id === this.actor.id)
throw new Error("Don't be get self");
this.subIds.push(id);
let proxy = yield this.getActor(type, id, this.sagaId || null, this.key, this.parents || []);
if (!proxy)
return null;
if (this.lockMode) {
yield this.actorLock(proxy);
}
return proxy;
});
async get(type, id) {
if (id === this.actor.id)
throw new Error("Don't be get self");
this.subIds.push(id);
let proxy = await this.getActor(type, id, this.sagaId || null, this.key, this.parents || []);
if (!proxy)
return null;
if (this.lockMode) {
await this.actorLock(proxy);
}
return proxy;
}
create(type, data) {
return __awaiter(this, void 0, void 0, function* () {
return this.createActor(type, data, this.sagaId);
});
async create(type, data) {
return this.createActor(type, data, this.sagaId);
}
subscribe(event, handleMethodName) {
return __awaiter(this, void 0, void 0, function* () {
let { actorId, actorType, type } = event;
if (actorId && actorType && type) {
let actor = yield this.get(actorType, actorId);
if (actor) {
actor.subscribe(type, this.actor.type, this.actor.id, handleMethodName);
}
async subscribe(event, handleMethodName) {
let { actorId, actorType, type } = event;
if (actorId && actorType && type) {
let actor = await this.get(actorType, actorId);
if (actor) {
actor.subscribe(type, this.actor.type, this.actor.id, handleMethodName);
}
else if (actorType) {
let actor = yield this.get("ActorEventEmitter", "ActorEventEmitter" + actorType);
if (actor) {
yield actor.subscribe(actorType, this.actor.type, this.actor.id, handleMethodName);
}
}
else if (actorType) {
let actor = await this.get("ActorEventEmitter", "ActorEventEmitter" + actorType);
if (actor) {
await actor.subscribe(actorType, this.actor.type, this.actor.id, handleMethodName);
}
});
}
}
unsubscribe(event) {
return __awaiter(this, void 0, void 0, function* () {
let { actorId, actorType, type } = event;
if (actorId && actorType && type) {
let actor = yield this.get(actorType, actorId);
if (actor) {
actor.unsubscribe(type, this.actor.id);
}
async unsubscribe(event) {
let { actorId, actorType, type } = event;
if (actorId && actorType && type) {
let actor = await this.get(actorType, actorId);
if (actor) {
actor.unsubscribe(type, this.actor.id);
}
else if (actorType) {
let actor = yield this.get("ActorEventEmitter", "ActorEventEmitter" + actorType);
if (actor) {
yield actor.unsubscribe(actorType, this.actor.id);
}
}
else if (actorType) {
let actor = await this.get("ActorEventEmitter", "ActorEventEmitter" + actorType);
if (actor) {
await actor.unsubscribe(actorType, this.actor.id);
}
});
}
}
getHistory(actorType, actorId, eventType) {
return __awaiter(this, void 0, void 0, function* () {
return yield this._domain.getHistory(actorType, actorId, eventType);
});
async getHistory(actorType, actorId, eventType) {
return await this._domain.getHistory(actorType, actorId, eventType);
}

@@ -237,0 +209,0 @@ }

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
function sleep(time) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise(resolve => setImmediate(() => setTimeout(resolve, time)));
});
async function sleep(time) {
return new Promise(resolve => setImmediate(() => setTimeout(resolve, time)));
}
exports.default = sleep;
//# sourceMappingURL=sleep.js.map
{
"name": "cqrs",
"version": "2.10.23",
"version": "2.10.24",
"description": "cqrs for javascript",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

{
"compilerOptions": {
"target": "es2015",
"target": "es2017",
"module": "commonjs",

@@ -5,0 +5,0 @@ "outDir": "dist",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc