Use EventEmitter instead of Emitter in Box

... so that we have one less dependency on `Tappable`
This commit is contained in:
Aria Minaei 2022-12-01 15:14:54 +01:00
parent d2876a7c9a
commit 391958f5cf

View file

@ -1,6 +1,6 @@
import type {Prism} from './prism/Interface'
import prism from './prism/prism'
import Emitter from './utils/Emitter'
import EventEmitter from './utils/EventEmitter'
/**
* Common interface for Box types. Boxes wrap a single value.
@ -39,7 +39,7 @@ export interface IBox<V> {
*/
export default class Box<V> implements IBox<V> {
private _publicPrism: Prism<V>
private _emitter = new Emitter<V>()
private _emitter = new EventEmitter()
/**
* @param _value - The initial value of the Box.
@ -50,8 +50,11 @@ export default class Box<V> implements IBox<V> {
*/
protected _value: V,
) {
const subscribe = (listener: (val: V) => void) =>
this._emitter.tappable.tap(listener)
const subscribe = (listener: (val: V) => void) => {
this._emitter.addEventListener('change', listener)
return () => this._emitter.removeEventListener('change', listener)
}
const getValue = () => this._value
this._publicPrism = prism(() => {
return prism.source(subscribe, getValue)
@ -66,7 +69,7 @@ export default class Box<V> implements IBox<V> {
set(v: V) {
if (v === this._value) return
this._value = v
this._emitter.emit(v)
this._emitter.emit('change', v)
}
/**