RocketChat/Rocket.Chat

View on GitHub
apps/meteor/app/settings/client/lib/settings.ts

Summary

Maintainability
A
0 mins
Test Coverage
import type { SettingValue } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';
import { ReactiveDict } from 'meteor/reactive-dict';

import { PublicSettingsCachedCollection } from '../../../../client/lib/settings/PublicSettingsCachedCollection';
import { SettingsBase } from '../../lib/settings';

class Settings extends SettingsBase {
    cachedCollection = PublicSettingsCachedCollection;

    collection = PublicSettingsCachedCollection.collection;

    dict = new ReactiveDict('settings');

    get<TValue = any>(_id: string | RegExp, ...args: []): TValue {
        if (_id instanceof RegExp) {
            throw new Error('RegExp Settings.get(RegExp)');
        }
        if (args.length > 0) {
            throw new Error('settings.get(String, callback) only works on backend');
        }
        return this.dict.get(_id) as TValue;
    }

    private _storeSettingValue(record: { _id: string; value: SettingValue }, initialLoad: boolean): void {
        Meteor.settings[record._id] = record.value;
        this.dict.set(record._id, record.value);
        this.load(record._id, record.value, initialLoad);
    }

    init(): void {
        let initialLoad = true;
        this.collection.find().observe({
            added: (record: { _id: string; value: SettingValue }) => this._storeSettingValue(record, initialLoad),
            changed: (record: { _id: string; value: SettingValue }) => this._storeSettingValue(record, initialLoad),
            removed: (record: { _id: string }) => {
                delete Meteor.settings[record._id];
                this.dict.set(record._id, null);
                this.load(record._id, undefined, initialLoad);
            },
        });
        initialLoad = false;
    }
}

export const settings = new Settings();

settings.init();