celerichain-ember-cor
Version:
Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.
65 lines (50 loc) • 1.84 kB
JavaScript
import Service from '@ember/service';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { isArray } from '@ember/array';
import { dasherize } from '@ember/string';
import { storageFor } from 'ember-local-storage';
import autoSerialize from '../utils/auto-serialize';
export default class AppCacheService extends Service {
currentUser;
store;
localCache;
get cachePrefix() {
const userId = this.currentUser.id ?? 'anon';
return `${userId}:${this.currentUser.companyId}:`;
}
setEmberData(key, value, except = []) {
value = autoSerialize(value, except);
return this.set(key, value);
}
getEmberData(key, modelName) {
const data = this.get(key);
if (isArray(data)) {
return data.map((instance) => this.store.push(this.store.normalize(modelName, instance)));
}
return this.store.push(this.store.normalize(modelName, data));
}
set(key, value) {
this.localCache.set(`${this.cachePrefix}${dasherize(key)}`, value);
return this;
}
get(key, defaultValue = null) {
const value = this.localCache.get(`${this.cachePrefix}${dasherize(key)}`);
if (value === undefined) {
return defaultValue;
}
return value;
}
has(key) {
if (isArray(key)) {
return key.every((k) => this.get(k) !== undefined);
}
return this.get(key) !== undefined;
}
doesntHave(key) {
if (isArray(key)) {
return key.every((k) => this.get(k) === undefined);
}
return this.get(key) === undefined;
}
}