@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
55 lines (54 loc) • 1.89 kB
JavaScript
;
import { computed, ref, watch } from "../../core/reactivity/CoreReactivity";
import { NamedFunction3 } from "./_Base";
export var PerformanceChangeEvent = /* @__PURE__ */ ((PerformanceChangeEvent2) => {
PerformanceChangeEvent2["aboveThreshold"] = "aboveThreshold";
PerformanceChangeEvent2["belowThreshold"] = "belowThreshold";
return PerformanceChangeEvent2;
})(PerformanceChangeEvent || {});
export const PERFORMANCE_CHANGE_EVENTS = [
"aboveThreshold" /* aboveThreshold */,
"belowThreshold" /* belowThreshold */
];
export class onPerformanceChange extends NamedFunction3 {
static type() {
return "onPerformanceChange";
}
func(threshold, callbacks, evaluator) {
const performanceRef = this.scene.perfMonitor.ref();
const watchFallsBelow = () => {
const isAboveThresholdRef = ref(true);
const _isBelowThreshold = computed(() => performanceRef.value < threshold);
const stopWatch1 = watch(_isBelowThreshold, () => {
if (_isBelowThreshold.value == true) {
isAboveThresholdRef.value = false;
}
});
const stopWatch2 = watch(isAboveThresholdRef, () => {
callbacks.belowThreshold();
});
evaluator.onDispose(() => {
stopWatch1();
stopWatch2();
});
};
const watchFallsAbove = () => {
const isBelowThresholdRef = ref(true);
const _isAboveThreshold = computed(() => performanceRef.value > threshold);
const stopWatch1 = watch(_isAboveThreshold, (newVal, oldVal) => {
if (_isAboveThreshold.value == true) {
isBelowThresholdRef.value = false;
}
});
const stopWatch2 = watch(isBelowThresholdRef, () => {
callbacks.aboveThreshold();
});
evaluator.onDispose(() => {
stopWatch1();
stopWatch2();
});
};
watchFallsBelow();
watchFallsAbove();
}
}