Skip to content

Commit

Permalink
feat: allow logging state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiksta committed Jun 25, 2024
1 parent 16483b2 commit 13c9395
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/core/src/rx/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ export default class State<T> extends MulticastStream<T> {
/** The current value/state */
get value() { return this._value }

constructor(private initialValue: T) {
public static loggingCallback?:
(name: string | undefined, previous: any, next: any) => void = undefined;

constructor(
private initialValue: T,
private loggingName?: string,
private loggingCallback = State.loggingCallback,
) {
super();
this._value = initialValue;
}
Expand All @@ -50,6 +57,8 @@ export default class State<T> extends MulticastStream<T> {
if (this.completed) return;
const newValue = valueOrSetter instanceof Function ?
valueOrSetter(this._value) : valueOrSetter;
if (this.loggingCallback !== undefined && this.loggingName !== undefined)
this.loggingCallback(this.loggingName, this._value, newValue);
this._value = newValue;
super.next(newValue);
}
Expand Down

0 comments on commit 13c9395

Please sign in to comment.