Skip to content

Commit

Permalink
style(project-wide): lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Burak Tasci committed Dec 24, 2018
1 parent ec046c5 commit 3eb0798
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 220 deletions.
31 changes: 14 additions & 17 deletions packages/@ngx-cache/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// angular
import {
InjectionToken, Injector, ModuleWithProviders, NgModule, Optional, PLATFORM_ID, SkipSelf
} from '@angular/core';
import { InjectionToken, Injector, ModuleWithProviders, NgModule, Optional, PLATFORM_ID, SkipSelf } from '@angular/core';

// module
import { CacheLoader, CacheStaticLoader } from './src/cache.loader';
import { CacheService } from './src/cache.service';
import { Storage } from './src/storage';
Expand All @@ -20,39 +16,40 @@ export * from './src/storage';
export const STORAGE = new InjectionToken<Storage>('STORAGE');

// for AoT compilation
// tslint:disable-next-line
export function cacheFactory(): CacheLoader {
return new CacheStaticLoader();
}

// tslint:disable-next-line
export function cacheServiceFactory(loader: CacheLoader, platformId: any, injector: Injector): CacheService {
return new CacheService(loader, platformId, injector);
}

@NgModule()
export class CacheModule {
static forRoot(configuredProvider: any = {
provide: CacheLoader,
useFactory: (cacheFactory)
}): ModuleWithProviders {
static forRoot(
configuredProvider: any = {
provide: CacheLoader,
useFactory: cacheFactory
}
): ModuleWithProviders {
return {
ngModule: CacheModule,
providers: [
configuredProvider,
{
provide: CacheService,
useFactory: (cacheServiceFactory),
deps: [
CacheLoader,
PLATFORM_ID,
Injector
]
useFactory: cacheServiceFactory,
deps: [CacheLoader, PLATFORM_ID, Injector]
}
]
};
}

constructor(@Optional() @SkipSelf() parentModule: CacheModule) {
if (parentModule)
constructor(@Optional() @SkipSelf() parentModule?: CacheModule) {
if (parentModule) {
throw new Error('CacheModule already loaded; import in root module only.');
}
}
}
16 changes: 8 additions & 8 deletions packages/@ngx-cache/core/src/cache.loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// module
import { CacheSettings } from './models/cache-settings';
import { LifeSpan } from './models/life-span';

Expand All @@ -17,12 +16,13 @@ export class CacheStaticLoader implements CacheLoader {
return this.providedSettings.lifeSpan;
}

constructor(private readonly providedSettings: CacheSettings = {
key: 'NGX_CACHE',
lifeSpan: {
expiry: Number.MAX_VALUE,
TTL: Number.MAX_VALUE
constructor(
private readonly providedSettings: CacheSettings = {
key: 'NGX_CACHE',
lifeSpan: {
expiry: Number.MAX_VALUE,
TTL: Number.MAX_VALUE
}
}
}) {
}
) {}
}
89 changes: 42 additions & 47 deletions packages/@ngx-cache/core/src/cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// angular
import { Inject, Injectable, InjectionToken, Injector, PLATFORM_ID } from '@angular/core';

// module
import { LifeSpan } from './models/life-span';
import { CacheValue } from './models/cache-value';
import { ReturnType } from './models/return-type';
import { Cache } from './cache';
import { CacheLoader } from './cache.loader';
import { CacheValue } from './models/cache-value';
import { LifeSpan } from './models/life-span';
import { ReturnType } from './models/return-type';

export const CACHE = new InjectionToken<Cache>('CACHE');

Expand All @@ -22,25 +20,22 @@ export class CacheService {
}

static normalizeKey(key: string | number): string {
if (CacheService.validateKey(key))
if (CacheService.validateKey(key)) {
throw new Error('Please provide a valid key to save in the CacheService');
}

return `${key}`;
}

private static validateKey(key: string | number): boolean {
return !key
|| typeof key === 'boolean'
|| Number.isNaN(key as number);
return !key || typeof key === 'boolean' || Number.isNaN(key as number);
}

private static validateValue(value: CacheValue): boolean {
return value.lifeSpan.expiry && value.lifeSpan.expiry > Date.now();
}

constructor(readonly loader: CacheLoader,
@Inject(PLATFORM_ID) private readonly platformId: any,
private readonly injector: Injector) {
constructor(readonly loader: CacheLoader, @Inject(PLATFORM_ID) private readonly platformId: any, private readonly injector: Injector) {
CacheService.instance = this;

this.cache = this.injector.get(CACHE);
Expand All @@ -52,62 +47,63 @@ export class CacheService {
}

has(key: string | number): boolean {
key = CacheService.normalizeKey(key);
const normalized = CacheService.normalizeKey(key);

return this.cache.keys.indexOf(key) !== -1;
return this.cache.keys.indexOf(normalized) !== -1;
}

set(key: string | number, value: any, returnType: ReturnType = ReturnType.Scalar, lifeSpan?: LifeSpan): boolean {
key = CacheService.normalizeKey(key);
lifeSpan = lifeSpan || this.lifeSpan;
const normalized = CacheService.normalizeKey(key);

return this.cache.setItem(key, {
return this.cache.setItem(normalized, {
data: value,
returnType,
lifeSpan: this.parseLifeSpan(lifeSpan)
lifeSpan: this.parseLifeSpan(lifeSpan ? lifeSpan : this.lifeSpan)
});
}

get(key: string | number): any {
key = CacheService.normalizeKey(key);
const cached = this.cache.getItem(key);
let res;

if (cached)
if (CacheService.validateValue(cached))
res = cached.data;
else
this.remove(key);

return res;
const normalized = CacheService.normalizeKey(key);
const cached = this.cache.getItem(normalized);

if (cached) {
if (CacheService.validateValue(cached)) {
return cached.data;
} else {
this.remove(normalized);
}
}

return undefined;
}

getWithMetadata(key: string | number): CacheValue {
key = CacheService.normalizeKey(key);
const cached = this.cache.getItem(key);
let res;
getWithMetadata(key: string | number): CacheValue | undefined {
const normalized = CacheService.normalizeKey(key);
const cached = this.cache.getItem(normalized);

if (cached)
if (CacheService.validateValue(cached))
res = cached;
else
if (cached) {
if (CacheService.validateValue(cached)) {
return cached;
} else {
this.remove(key);
}
}

return res;
return undefined;
}

remove(key: string | number, wild = false): void {
key = CacheService.normalizeKey(key);
const normalized = CacheService.normalizeKey(key);

this.cache.removeItem(key, wild);
this.cache.removeItem(normalized, wild);
}

clear(): void {
this.cache.clear();
}

dehydrate(): any {
const keys = this.cache.keys || [];
const keys = this.cache.keys.length ? this.cache.keys : [];
const res = {};

keys.forEach((key: string) => {
Expand All @@ -118,16 +114,15 @@ export class CacheService {
}

rehydrate(json: any): void {
Object.keys(json)
.forEach((key: string) => {
key = CacheService.normalizeKey(key);
this.cache.setItem(key, json[key]);
});
Object.keys(json).forEach((key: string) => {
const normalized = CacheService.normalizeKey(key);
this.cache.setItem(normalized, json[normalized]);
});
}

private parseLifeSpan(lifeSpan: LifeSpan): LifeSpan {
return {
expiry: lifeSpan.expiry || (lifeSpan.TTL ? Date.now() + (lifeSpan.TTL * 1000) : this.lifeSpan.expiry),
expiry: lifeSpan.expiry || (lifeSpan.TTL ? Date.now() + lifeSpan.TTL * 1000 : this.lifeSpan.expiry),
TTL: lifeSpan.TTL || this.lifeSpan.TTL
};
}
Expand Down
5 changes: 2 additions & 3 deletions packages/@ngx-cache/core/src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// module
import { CacheValue } from './models/cache-value';

export abstract class Cache {
abstract get keys(): Array<string>;
abstract get keys(): Array<string> | undefined;

abstract getItem(key: string): CacheValue;
abstract getItem(key: string): CacheValue | undefined;

abstract setItem(key: string, value: CacheValue): boolean;

Expand Down
55 changes: 28 additions & 27 deletions packages/@ngx-cache/core/src/cached.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
// libs
import { Observable, of as observableOf } from 'rxjs';
import { map } from 'rxjs/operators';

// module
import { ReturnType } from './models/return-type';
import { CacheService } from './cache.service';
import { ReturnType } from './models/return-type';
import { isObservable, isPromise } from './util';

// tslint:disable-next-line
export function CacheKey(target: any, propertyKey: string, index: number): void {
const metadataKey = `__cache_${propertyKey}_keys`;

Array.isArray(target[metadataKey])
? target[metadataKey].push(index)
: target[metadataKey] = [index];
Array.isArray(target[metadataKey]) ? target[metadataKey].push(index) : (target[metadataKey] = [index]);
}

// tslint:disable-next-line
export function Cached(key: string): any | Observable<any> | Promise<any> {
// tslint:disable-next-line
return function (target: Function, propertyKey: string, descriptor: TypedPropertyDescriptor<any>): any | Observable<any> | Promise<any> {
return function(target: Function, propertyKey: string, descriptor: TypedPropertyDescriptor<any>): any | Observable<any> | Promise<any> {
const method: Function = descriptor.value;
descriptor.value = function(...args: Array<any>): any | Observable<any> | Promise<any> {
const cache = CacheService.getInstance();
Expand All @@ -27,27 +25,28 @@ export function Cached(key: string): any | Observable<any> | Promise<any> {

let keyParts = '';

if (Array.isArray(indices))
for (let i = 0; i < args.length; i++)
if (indices.indexOf(i) !== -1)
keyParts = !keyParts
? String(args[i])
: `${keyParts}_${String(args[i])}`;
if (Array.isArray(indices)) {
for (let i = 0; i < args.length; i++) {
if (indices.indexOf(i) !== -1) {
keyParts = !keyParts ? String(args[i]) : `${keyParts}_${String(args[i])}`;
}
}
}

let cacheKey = !keyParts
? key
: `${key}_${keyParts}`;
let cacheKey = !keyParts ? key : `${key}_${keyParts}`;

cacheKey = CacheService.normalizeKey(cacheKey);

if (!cacheKey || !cache)
// tslint:disable-next-line
if (!cache || !cacheKey) {
// tslint:disable-next-line
return method.apply(this, args);
}

if (cache.has(cacheKey)) {
const cached = cache.getWithMetadata(cacheKey);

if (cached && cached.data)
if (cached && cached.data) {
switch (cached.returnType) {
case ReturnType.Observable:
return observableOf(cached.data);
Expand All @@ -56,25 +55,27 @@ export function Cached(key: string): any | Observable<any> | Promise<any> {
default:
return cached.data;
}
}
}

// tslint:disable-next-line
const value = method.apply(this, args);

if (isObservable(value))
return value
.pipe(
map((res: any) => {
cache.set(cacheKey, res, ReturnType.Observable);
if (isObservable(value)) {
return value.pipe(
map((res: any) => {
cache.set(cacheKey, res, ReturnType.Observable);

return res;
}));
else if (isPromise(value))
return (value as any).then((res: any) => {
return res;
})
);
} else if (isPromise(value)) {
return (value).then((res: any) => {
cache.set(cacheKey, res, ReturnType.Promise);

return res;
});
}

cache.set(cacheKey, value);

Expand Down
1 change: 0 additions & 1 deletion packages/@ngx-cache/core/src/models/cache-settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// module
import { LifeSpan } from './life-span';

export interface CacheSettings {
Expand Down
1 change: 0 additions & 1 deletion packages/@ngx-cache/core/src/models/cache-value.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// module
import { LifeSpan } from './life-span';
import { ReturnType } from './return-type';

Expand Down
1 change: 0 additions & 1 deletion packages/@ngx-cache/core/src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// libs
import { EventEmitter } from 'events';

export abstract class Storage extends EventEmitter {
Expand Down
Loading

0 comments on commit 3eb0798

Please sign in to comment.