import type { EventEmitter } from 'events' ; import type { StoreInstance } from '../typings' ; export class Subscription { stores : EventEmitter [ ] ; unsubscribe ? : Array < ( ) => void > ; constructor ( stores ) { this . stores = stores ; this . unsubscribe = undefined ; } onStateChange ? : ( ) => void ; handleStateChange = ( ) => { this . onStateChange && this . onStateChange ( ) ; } ; isSubscribed ( ) { return Boolean ( this . unsubscribe ) ; } setOnStateChange ( onStateChange ) { this . onStateChange = onStateChange ; } trySubscribe ( ) { if ( ! this . unsubscribe ) { this . unsubscribe = this . stores . filter ( Boolean ) . map ( ( store ) => { store . on ( 'change' , this . handleStateChange ) ; return ( ) => { store . removeListener ( 'change' , this . handleStateChange ) ; } ; } ) ; } } tryUnsubscribe ( ) { if ( this . unsubscribe ) { this . unsubscribe . forEach ( ( f ) => f ( ) ) ; this . unsubscribe = undefined ; } this . onStateChange = undefined ; } }