@finos/legend-application
Version:
Legend application core
84 lines • 2.89 kB
JavaScript
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* 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 { guaranteeIsNumber, guaranteeIsString, guaranteeIsBoolean, guaranteeIsObject, isNonNullable, returnUndefOnError, isEmpty, } from '@finos/legend-shared';
import { LocalStorage } from './ApplicationStorage.js';
export class StorageStore {
storeIndex;
storageService;
data;
constructor(storageService, storeIndex) {
this.storageService = storageService;
this.storeIndex = storeIndex;
this.data = this.storageService.getIndex(this.storeIndex) ?? {};
}
getValue(key) {
return this.data[key];
}
getNumericValue(key) {
const value = this.getValue(key);
return value !== undefined ? guaranteeIsNumber(value) : undefined;
}
getStringValue(key) {
const value = this.getValue(key);
return value !== undefined ? guaranteeIsString(value) : undefined;
}
getBooleanValue(key) {
const value = this.getValue(key);
return value !== undefined ? guaranteeIsBoolean(value) : undefined;
}
getObjectValue(key) {
const value = this.getValue(key);
return value !== undefined ? guaranteeIsObject(value) : undefined;
}
hasValue(key) {
return isNonNullable(this.data[key]);
}
persistValue(key, value) {
if (value !== undefined) {
this.data[key] = value;
}
else {
delete this.data[key];
}
this.storageService.updateIndex(this.storeIndex, this.data);
}
}
export class StorageService {
applicationStore;
storage;
data;
constructor(applicationStore) {
this.applicationStore = applicationStore;
this.storage = new LocalStorage();
const data = this.storage.getItem(applicationStore.config.applicationStorageKey);
this.data = data
? (returnUndefOnError(() => JSON.parse(data)) ?? {})
: {};
}
getIndex(index) {
return this.data[index];
}
updateIndex(index, value) {
if (isEmpty(value)) {
delete this.data[index];
}
else {
this.data[index] = value;
}
this.storage.setItem(this.applicationStore.config.applicationStorageKey, JSON.stringify(this.data));
}
}
//# sourceMappingURL=StorageService.js.map