convex-pixel
Version:
The library for creating pseudo 3d interactive scenes based on webgl
58 lines • 1.86 kB
JavaScript
const DEFAULT_INTERVAL = 500;
export var SonarEventTypes;
(function (SonarEventTypes) {
SonarEventTypes["CHANGE"] = "change";
SonarEventTypes["LOST_CONTEXT"] = "lost-context";
})(SonarEventTypes || (SonarEventTypes = {}));
export class Sonar {
constructor(detectionInterval = DEFAULT_INTERVAL) {
this._poolDetectors = new Array();
this._timerId = undefined;
this._tickHandler = () => {
for (let i = 0, l = this._poolDetectors.length; i < l; i++) {
this._poolDetectors[i].detectChanges();
}
};
this._interval = detectionInterval;
}
static create() {
if (!Sonar.instance) {
Sonar.instance = new Sonar();
}
return Sonar.instance;
}
run() {
this._timerId = setInterval(this._tickHandler, this._interval);
}
stop() {
if (this._timerId) {
clearInterval(this._timerId);
this._timerId = undefined;
}
}
add(detector) {
if (this._poolDetectors.indexOf(detector) > -1)
return; // throw new Error('The detector is already added to pool');
this._poolDetectors.push(detector);
}
remove(detector) {
const index = this._poolDetectors.indexOf(detector);
if (index === -1)
return; // throw new Error('The detector is already removed');
this._poolDetectors.splice(index, 1);
}
removeAll() {
while (this._poolDetectors.length > 0) {
const detector = this._poolDetectors.pop();
if (detector) {
detector.destroy();
}
}
}
destroy() {
this.stop();
this.removeAll();
this._poolDetectors.length = 0;
}
}
//# sourceMappingURL=sonar.js.map