ファイル
btrc-hub/frontend/src/lib/eventBus/EventBus.ts
T
2025-06-15 14:24:41 +09:00

36 行
533 B
TypeScript

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)
}
}
}