Added the base Pin class

This commit is contained in:
Matei Adriel 2019-07-16 12:48:38 +03:00
parent a9bc28478e
commit 259786ad98
2 changed files with 49 additions and 0 deletions

View file

@ -0,0 +1,43 @@
import { SubscriptionData } from '../types/SubscriptionData'
import { BehaviorSubject } from 'rxjs'
export class Pin {
public state = new BehaviorSubject(false)
public connectedTo = new Set<Pin>()
private pairs = new Set<Pin>()
private subscriptions: SubscriptionData<Pin>[] = []
public addPair(pin: Pin) {
this.pairs.add(pin)
const rawSubscription = pin.state.subscribe(state => {
this.state.next(state)
})
this.subscriptions.push({
data: pin,
subscription: rawSubscription
})
}
public removePair(pin: Pin) {
this.pairs.delete(pin)
for (const subscription of this.subscriptions) {
if (subscription.data === pin) {
subscription.subscription.unsubscribe()
}
}
this.subscriptions = this.subscriptions.filter(
subscription => subscription.data !== pin
)
}
public dispose() {
for (const subscription of this.subscriptions) {
subscription.subscription.unsubscribe()
}
}
}

View file

@ -0,0 +1,6 @@
import { Subscription } from 'rxjs'
export interface SubscriptionData<T> {
subscription: Subscription
data: T
}