@constructorfleet/ultimate-govee
Version:
Library for interacting with Govee devices written in Typescript.
53 lines • 1.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChannelService = void 0;
const common_1 = require("@nestjs/common");
const rxjs_1 = require("rxjs");
const areSameConfig = (data1, data2) => {
if (data1 === undefined && data2 === undefined) {
return true;
}
if (data1 === undefined || data2 === undefined) {
return false;
}
return Object.entries(data1).every(([k, v]) => data2[k] === v);
};
class ChannelService {
constructor(eventBus, initialState, initialConfig) {
this.eventBus = eventBus;
this.initialState = initialState;
this.initialConfig = initialConfig;
this.subscriptions = [];
this.logger = new common_1.Logger(this.constructor.name);
this.state = {
enabled: new rxjs_1.BehaviorSubject(undefined),
config: new rxjs_1.BehaviorSubject(undefined),
};
this.onEnabledChanged$ = this.state.enabled.pipe((0, rxjs_1.map)((e) => e === true), (0, rxjs_1.share)());
this.onConfigChanged$ = this.state.config.pipe((0, rxjs_1.filter)((config) => config !== undefined), (0, rxjs_1.map)((config) => config), (0, rxjs_1.distinctUntilChanged)((previous, current) => !areSameConfig(previous, current)), (0, rxjs_1.share)());
}
onModuleInit() {
this.state.enabled.next(this.initialState);
this.state.config.next(this.initialConfig);
}
onModuleDestroy() {
this.closeSubscriptions();
}
getConfig() {
return this.state.config.getValue();
}
setConfig(config) {
this.state.config.next(config);
}
setEnabled(enabled) {
this.state.enabled.next(enabled);
}
get isEnabled() {
return this.state.enabled.value === true;
}
closeSubscriptions() {
this.subscriptions.forEach((sub) => sub.unsubscribe());
}
}
exports.ChannelService = ChannelService;
//# sourceMappingURL=channel.service.js.map