Added the base Pin class
This commit is contained in:
parent
a9bc28478e
commit
259786ad98
43
src/modules/simulation/classes/Pin.ts
Normal file
43
src/modules/simulation/classes/Pin.ts
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
6
src/modules/simulation/types/SubscriptionData.ts
Normal file
6
src/modules/simulation/types/SubscriptionData.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { Subscription } from 'rxjs'
|
||||||
|
|
||||||
|
export interface SubscriptionData<T> {
|
||||||
|
subscription: Subscription
|
||||||
|
data: T
|
||||||
|
}
|
Loading…
Reference in a new issue