Skip to content

Commit

Permalink
add sendign last event
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Sep 29, 2024
1 parent 6e4348a commit 5bfc73f
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ export interface ISubOptions {
* immediately (before `subscribe` function returns).
*
* It is to allow for stateful subscribers that need to know the last event-state.
*
* If the last emitted event was `undefined`, it won't be sent to the subscriber.
*/
emitLast?: boolean;

Expand Down Expand Up @@ -235,10 +237,10 @@ export class SubEvent<T = unknown> {
protected _subs: ISubscriber<T>[] = [];

/**
* Last emitted value, if there was any.
* Last emitted event, if there was any.
* @private
*/
private lastValue;
private lastEvent?: T;

/**
* Event constructor.
Expand Down Expand Up @@ -307,6 +309,9 @@ export class SubEvent<T = unknown> {
this.options.onSubscribe(ctx);
sub.data = ctx.data;
}
if (this.lastEvent !== undefined) {
cb(this.lastEvent);
}
this._subs.push(sub);
return new Subscription({cancel: this._createCancel(sub), sub});
}
Expand Down Expand Up @@ -379,7 +384,7 @@ export class SubEvent<T = unknown> {
if (onFinished) {
onFinished(r.length); // notify
}
this.lastValue = data; // save the last emitted value
this.lastEvent = data; // save the last emitted value
}
}));
});
Expand Down

0 comments on commit 5bfc73f

Please sign in to comment.