From 6e4348a62920c13857eafc12905e8092776e7a36 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 29 Sep 2024 20:06:37 +0100 Subject: [PATCH] add saving the last value --- src/event.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/event.ts b/src/event.ts index ba245b3..7c227a8 100644 --- a/src/event.ts +++ b/src/event.ts @@ -145,7 +145,9 @@ export interface ISubOptions { name?: string; /** - * Make `subscribe` immediately receive the last emitted event, if there was any. + * Make `subscribe` callback receive the last emitted event (if there was any), + * immediately (before `subscribe` function returns). + * * It is to allow for stateful subscribers that need to know the last event-state. */ emitLast?: boolean; @@ -232,6 +234,12 @@ export class SubEvent { */ protected _subs: ISubscriber[] = []; + /** + * Last emitted value, if there was any. + * @private + */ + private lastValue; + /** * Event constructor. * @@ -334,7 +342,7 @@ export class SubEvent { * which is synchronous by default. * * @param data - * Data to be sent, according to the template type. + * Data/value to be sent, according to the template type. * * @param options * Event-emitting options. @@ -366,8 +374,12 @@ export class SubEvent { } else { sub.cb && sub.cb(data); } - if (onFinished && index === r.length - 1) { - onFinished(r.length); // finished sending + if (index === r.length - 1) { + // the end of emission reached; + if (onFinished) { + onFinished(r.length); // notify + } + this.lastValue = data; // save the last emitted value } })); });