This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
export class EventBus<T>
|
||||
{
|
||||
private value: T
|
||||
private subscribers: ((val: T) => void)[] = []
|
||||
|
||||
constructor (
|
||||
initialValue: T)
|
||||
{
|
||||
this.value = initialValue
|
||||
}
|
||||
|
||||
get ()
|
||||
: T
|
||||
{
|
||||
return this.value
|
||||
}
|
||||
|
||||
set (
|
||||
val: T)
|
||||
: void
|
||||
{
|
||||
this.value = val
|
||||
this.subscribers.forEach (f => f (val))
|
||||
}
|
||||
|
||||
subscribe (
|
||||
func: (val: T) => void)
|
||||
: () => void
|
||||
{
|
||||
this.subscribers.push (func)
|
||||
return () => {
|
||||
this.subscribers = this.subscribers.filter (sub => sub !== func)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import { EventBus } from './EventBus'
|
||||
|
||||
export const WikiIdBus = new EventBus<number | null> (null)
|
||||
Reference in New Issue
Block a user