Skip to content

Allow the components of an AsyncEvent to be correlated in reductions #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/event-reduce/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ export interface IAsyncEvent<Result = void, ContextIn = void, ContextOut = Conte
}

export interface AsyncStart<Result, Context> {
id: number;
promise: PromiseLike<Result>;
context: Context;
}

export interface AsyncResult<Result, Context> {
id: number;
result: Result;
context: Context;
}

export interface AsyncError<Context> {
id: number;
error: any;
context: Context;
}
Expand Down Expand Up @@ -128,15 +131,18 @@ class AsyncEvent<Result, Context> extends AsyncEventBase<Result, Context> {
readonly resolved = new Subject<AsyncResult<Result, Context>>(() => `${this.displayName}.resolved`);
readonly rejected = new Subject<AsyncError<Context>>(() => `${this.displayName}.rejected`);

private _nextEventId = 0;

next(promise: PromiseLike<Result>, context: Context) {
const id = this._nextEventId++;
fireEvent('⚡⌚ (async event)', this.displayName + '.started', { promise, context }, () => ({ Promise: promise, Container: this.container }),
() => this.started.next({ promise, context }));
() => this.started.next({ id, promise, context }));

promise.then(
result => fireEvent('⚡✔ (async result)', this.displayName + '.resolved', { result, context }, () => ({ Promise: promise, Container: this.container }),
() => this.resolved.next({ result, context })),
() => this.resolved.next({ id, result, context })),
error => fireEvent('⚡❌ (async error)', this.displayName + '.rejected', { error, context }, () => ({ Promise: promise, Container: this.container }),
() => this.rejected.next({ error, context })));
() => this.rejected.next({ id, error, context })));
}
}

Expand Down
121 changes: 116 additions & 5 deletions tests/EventTests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { asyncEvent, event } from 'event-reduce';
import { ChainedEventsError } from "event-reduce/lib/events";
import { AsyncError, AsyncResult, AsyncStart, ChainedEventsError } from "event-reduce/lib/events";
import * as sinon from 'sinon';
import { SynchronousPromise } from 'synchronous-promise';
import { describe, it, test, then, when } from 'wattle';
Expand Down Expand Up @@ -68,9 +68,9 @@ describe(asyncEvent.name, function () {
type Context = { bar: string; };
let sut = asyncEvent<Result, Context>();

let started = sinon.stub();
let resolved = sinon.stub();
let rejected = sinon.stub();
let started = sinon.stub<[AsyncStart<Result, Context>]>();
let resolved = sinon.stub<[AsyncResult<Result, Context>]>();
let rejected = sinon.stub<[AsyncError<Context>]>();
sut.started.subscribe(started);
sut.resolved.subscribe(resolved);
sut.rejected.subscribe(rejected);
Expand All @@ -96,4 +96,115 @@ describe(asyncEvent.name, function () {
then("rejected event fired", () => rejected.should.have.been.calledWith(sinon.match({ error, context })));
});
});
});

test("correlation id", () => {
started.resetHistory();
resolved.resetHistory();
rejected.resetHistory();
let context = { bar: 'context' };

when("async event called twice", () => {
let firstPromise = SynchronousPromise.unresolved<Result>();
let secondPromise = SynchronousPromise.unresolved<Result>();

sut(firstPromise, context);
sut(secondPromise, context);

then("started event fired with different ids", () => {
started.should.have.been.calledTwice;
started.firstCall.args[0].id.should.not.equal(started.secondCall.args[0].id);
});

test("first promise completes first", () => {

when("first promise resolved", () => {
let firstResult = { foo: 'first' };
firstPromise.resolve(firstResult);

then("resolved event fired with id for first promise", () => resolved.should.have.been.calledWith(sinon.match({ id: started.firstCall.args[0].id, result: firstResult })));

when("second promise resolved", () => {
let secondResult = { foo: 'second' };
secondPromise.resolve(secondResult);

then("resolved event fired with id for second promise", () => resolved.should.have.been.calledWith(sinon.match({ id: started.secondCall.args[0].id, result: secondResult })));
});

when("second promise rejected", () => {
let error = { message: 'second promise rejected' };
secondPromise.reject(error);

then("rejected event fired with id for second promise", () => rejected.should.have.been.calledWith(sinon.match({ id: started.secondCall.args[0].id, error })));
});
});

when("first promise rejected", () => {
let firstError = { message: 'first promise rejected' };
firstPromise.reject(firstError);

then("rejected event fired with id for first promise", () => rejected.should.have.been.calledWith(sinon.match({ id: started.firstCall.args[0].id, error: firstError })));

when("second promise resolved", () => {
let secondResult = { foo: 'second' };
secondPromise.resolve(secondResult);

then("resolved event fired with id for second promise", () => resolved.should.have.been.calledWith(sinon.match({ id: started.secondCall.args[0].id, result: secondResult })));
});

when("second promise rejected", () => {
let secondError = { message: 'second promise rejected' };
secondPromise.reject(secondError);

then("rejected event fired with id for second promise", () => rejected.should.have.been.calledWith(sinon.match({ id: started.secondCall.args[0].id, error: secondError })));
});
});
});

test("second promise completes first", () => {

when("second promise resolved", () => {
let secondResult = { foo: 'second' };
secondPromise.resolve(secondResult);

then("resolved event fired with id for second promise", () => resolved.should.have.been.calledWith(sinon.match({ id: started.secondCall.args[0].id, result: secondResult })));

when("first promise resolved", () => {
let firstResult = { foo: 'first' };
firstPromise.resolve(firstResult);

then("resolved event fired with id for first promise", () => resolved.should.have.been.calledWith(sinon.match({ id: started.firstCall.args[0].id, result: firstResult })));
});

when("first promise rejected", () => {
let error = { message: 'first promise rejected' };
firstPromise.reject(error);

then("rejected event fired with id for first promise", () => rejected.should.have.been.calledWith(sinon.match({ id: started.firstCall.args[0].id, error })));
});
});

when("second promise rejected", () => {
let secondError = { message: 'second promise rejected' };
secondPromise.reject(secondError);

then("rejected event fired with id for second promise", () => rejected.should.have.been.calledWith(sinon.match({ id: started.secondCall.args[0].id, error: secondError })));

when("first promise resolved", () => {
let firstResult = { foo: 'first' };
firstPromise.resolve(firstResult);

then("resolved event fired with id for first promise", () => resolved.should.have.been.calledWith(sinon.match({ id: started.firstCall.args[0].id, result: firstResult })));
});

when("first promise rejected", () => {
let firstError = { message: 'first promise rejected' };
firstPromise.reject(firstError);

then("rejected event fired with id for first promise", () => rejected.should.have.been.calledWith(sinon.match({ id: started.firstCall.args[0].id, error: firstError })));
});
});
});
});
});
});