Skip to content

Commit

Permalink
fixing #33
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Sep 29, 2024
1 parent 6588884 commit 2e994de
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ export class SubEvent<T = unknown> {
const onError = typeof options?.onError === 'function' && options.onError;
const start = schedule === EmitSchedule.sync ? Stat.callNow : Stat.callNext;
const middle = schedule === EmitSchedule.async ? Stat.callNext : Stat.callNow;
const onLast = (count: number) => {
this._lastEvent = data; // save the last event
if (onFinished) {
onFinished(count); // notify
}
};
start(() => {
const r = this._getRecipients();
r.forEach((sub, index) => middle(() => {
Expand All @@ -378,12 +384,12 @@ export class SubEvent<T = unknown> {
}
if (index === r.length - 1) {
// the end of emission reached;
this._lastEvent = data; // save the last event
if (onFinished) {
onFinished(r.length); // notify
}
onLast(r.length);
}
}));
if (!r.length) {
onLast(0);
}
});
return this;
}
Expand Down
5 changes: 2 additions & 3 deletions test/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,8 @@ describe('toConsumer', () => {
describe('lastEvent', () => {
it('must be set once emission finished', done => {
const e = new SubEvent<number>();
e.subscribe(dummy); // TODO: Should not be needed with #33 is resolved?

const onFinished = (count: number) => {
const onFinished = () => {
expect(e.lastEvent).to.eq(123);
};

Expand All @@ -472,7 +471,7 @@ describe('lastEvent', () => {
expect(e.lastEvent).to.be.undefined;

setTimeout(() => {
expect(handler).to.have.been.called.with(1);
expect(handler).to.have.been.called.with(0);
done();
});
});
Expand Down

0 comments on commit 2e994de

Please sign in to comment.