convex-pixel
Version:
The library for creating pseudo 3d interactive scenes based on webgl
81 lines • 2.94 kB
JavaScript
import { __extends } from "tslib";
import * as PIXI from "pixi.js";
import { SonarEventTypes, Sonar } from "./sonar";
var SonarDetector = /** @class */ (function (_super) {
__extends(SonarDetector, _super);
function SonarDetector(_object, _detectedProps, _eventType) {
if (_eventType === void 0) { _eventType = SonarEventTypes.CHANGE; }
var _this = _super.call(this) || this;
_this._object = _object;
_this._detectedProps = _detectedProps;
_this._eventType = _eventType;
_this._snapshot = {};
_this._detectChangesHandler = function (nextStep) {
if (nextStep === void 0) { nextStep = false; }
_this.detectChanges(nextStep);
};
_this.startDetection();
return _this;
}
SonarDetector.prototype.detectChanges = function (nextStep) {
if (nextStep === void 0) { nextStep = false; }
if (nextStep) {
this._detectChangesNextFrame();
return;
}
if (!this._object) {
this.emit(SonarEventTypes.LOST_CONTEXT);
return;
}
if (!this.detectedProps) {
throw Error("Property \"detectedProps\" is not defined.");
}
for (var _i = 0, _a = this.detectedProps; _i < _a.length; _i++) {
var prop = _a[_i];
var current = this._object;
var previous = this._snapshot;
if (current[prop] !== previous[prop]) {
previous[prop] = current[prop];
this.emit(this._eventType);
break;
}
}
};
SonarDetector.prototype.destroy = function () {
this.stopDetection();
this.removeAllListeners();
this._object = undefined;
this._snapshot = undefined;
this._detectedProps = undefined;
};
SonarDetector.prototype.startDetection = function () {
Sonar.instance.add(this);
};
SonarDetector.prototype.stopDetection = function () {
clearTimeout(this._nextTimeoutId);
if (Sonar.instance) {
Sonar.instance.remove(this);
}
};
Object.defineProperty(SonarDetector.prototype, "detectedProps", {
get: function () {
return this._detectedProps;
},
enumerable: true,
configurable: true
});
Object.defineProperty(SonarDetector.prototype, "eventType", {
get: function () {
return this._eventType;
},
enumerable: true,
configurable: true
});
SonarDetector.prototype._detectChangesNextFrame = function () {
clearTimeout(this._nextTimeoutId);
this._nextTimeoutId = setTimeout(this._detectChangesHandler, 0);
};
return SonarDetector;
}(PIXI.utils.EventEmitter));
export { SonarDetector };
//# sourceMappingURL=sonar-detector.js.map