@connectv/core
Version:
agent-based reactive programming library for typescript/javascript
99 lines • 2.53 kB
JavaScript
import { Subject } from 'rxjs';
import { Tracker } from '../shared/tracker';
import { Pin } from './pin';
/**
*
* Represents a map of labelled pins. The labelled pins are created
* first time they are requested, allowing for possibly huge
* maps without high memory cost.
*
*/
export class PinMap extends Tracker {
/**
*
* @param factory will be used to create each new pin.
*
*/
constructor(factory = () => new Pin()) {
super();
this.factory = factory;
this._pins = {};
}
/**
*
* Fetches the pin with the given label, and create it if not
* created already.
*
* @param label
*
*/
get(label) {
if (!(label in this._pins)) {
let _pin = this.factory(label);
this._pins[label] = _pin;
if (this._subject)
this._subject.next([label, _pin]);
return _pin;
}
return this._pins[label];
}
/**
*
* Checks if a pin with given label is created, without
* creating the pin.
*
* @param label
*
*/
instantiated(label) {
return label in this._pins;
}
/**
*
* @returns an array of all created pins.
*
*/
get pins() {
return Object.values(this._pins);
}
/**
*
* @returns an entry list (pairs of `[string, Pin]`) of created pins.
*
*/
get entries() {
return Object.entries(this._pins);
}
/**
*
* Subscribes to the event of creation of a new pin. The subscriber function
* will also be invoked on all of the already created pairs.
*
* @param subscriber
* @returns a [`Subscription`](https://rxjs-dev.firebaseapp.com/guide/subscription) object
* that you can later unsubscribe from using `.unsubscribe()`
*
*/
subscribe(subscriber) {
if (!this._subject)
this._subject = new Subject();
this.entries.forEach(entry => subscriber(...entry));
return this.track(this._subject.subscribe(entry => subscriber(...entry)));
}
/**
*
* Clears all the created pins and remove references to them,
* also will remove all subscriptions.
*
*/
clear() {
this.pins.forEach(pin => pin.clear());
this._pins = {};
if (this._subject) {
this._subject.complete();
this._subject = undefined;
}
return super.clear();
}
}
//# sourceMappingURL=pin-map.js.map