@intuitionrobotics/ts-common
Version:
70 lines • 2.13 kB
JavaScript
/*
* ts-common is the basic building blocks of our typescript projects
*
* Copyright (C) 2020 Intuition Robotics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { addItemToArray, removeItemFromArray } from "../utils/array-tools.js";
export class DebugFlag {
key;
constructor(key) {
this.key = key;
DebugFlags.add(this);
}
rename(newKey) {
DebugFlags.rename(this.key, newKey);
}
getKey() {
return this.key;
}
enable(enable = true) {
if (this.isEnabled() === enable)
return;
if (enable)
this._enable();
else
this._disable();
}
_enable() {
addItemToArray(DebugFlags.instance.ActiveDebugFlags, this.key);
}
_disable() {
removeItemFromArray(DebugFlags.instance.ActiveDebugFlags, this.key);
}
isEnabled() {
return DebugFlags.instance.ActiveDebugFlags.includes(this.key);
}
}
export class DebugFlags {
static instance = new DebugFlags();
AllDebugFlags = {};
ActiveDebugFlags = [];
constructor() {
}
static createFlag(key) {
// @ts-expect-error TS struggles with this dynamic typing
return new DebugFlag(key);
}
static add(flag) {
this.instance.AllDebugFlags[flag.getKey()] = flag;
}
static rename(previousKey, newKey) {
const flag = this.instance.AllDebugFlags[previousKey];
if (!flag)
return;
delete this.instance.AllDebugFlags[previousKey];
this.instance.AllDebugFlags[newKey] = flag;
}
}
//# sourceMappingURL=debug-flags.js.map