-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(event-display): create an active observable variable
- Loading branch information
Showing
7 changed files
with
46 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/phoenix-event-display/src/helpers/active-variable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** Callback function type. */ | ||
export type CallbackFunction<T = any> = (updatedValue: T) => void; | ||
|
||
/** | ||
* An active variable whose value can be changed and the change can be observed. | ||
*/ | ||
export class ActiveVariable<T = any> { | ||
/** | ||
* Create the observable active variable. | ||
* @param value Initial value. | ||
*/ | ||
constructor(public value?: T) {} | ||
|
||
/** | ||
* Callbacks to call on update. | ||
*/ | ||
private callbacks: CallbackFunction<T>[] = []; | ||
|
||
/** | ||
* Update the value of variable. | ||
* @param updatedValue New updated value. | ||
*/ | ||
public update(updatedValue: T) { | ||
this.value = updatedValue; | ||
this.callbacks.forEach((callback) => callback(updatedValue)); | ||
} | ||
|
||
/** | ||
* Call a function on updating the value of variable. | ||
* @param callback Callback to call with updated value when the variable is updated. | ||
*/ | ||
public onUpdate(callback: CallbackFunction<T>) { | ||
this.callbacks.push(callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters