Skip to content

Commit

Permalink
Merge pull request #8126 from lengxuegang/master
Browse files Browse the repository at this point in the history
feat(cache): Add cache option isGlobal
  • Loading branch information
kamilmysliwiec authored Sep 28, 2021
2 parents 4cf5a56 + a195cfe commit 6485231
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 20 deletions.
2 changes: 2 additions & 0 deletions packages/common/cache/cache.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class CacheModule {
static register(options: CacheModuleOptions = {}): DynamicModule {
return {
module: CacheModule,
global: options.isGlobal,
providers: [{ provide: CACHE_MODULE_OPTIONS, useValue: options }],
};
}
Expand All @@ -45,6 +46,7 @@ export class CacheModule {
static registerAsync(options: CacheModuleAsyncOptions): DynamicModule {
return {
module: CacheModule,
global: options.isGlobal,
imports: options.imports,
providers: [
...this.createAsyncProviders(options),
Expand Down
8 changes: 8 additions & 0 deletions packages/common/cache/interfaces/cache-module.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { ModuleMetadata, Provider, Type } from '../../interfaces';
import { CacheManagerOptions } from './cache-manager.interface';

export interface CacheModuleOptions extends CacheManagerOptions {
/**
* If "true', register `CacheModule` as a global module.
*/
isGlobal?: boolean;
[key: string]: any;
}

Expand Down Expand Up @@ -48,4 +52,8 @@ export interface CacheModuleAsyncOptions
*/
inject?: any[];
extraProviders?: Provider[];
/**
* If "true', register `CacheModule` as a global module.
*/
isGlobal?: boolean;
}
8 changes: 4 additions & 4 deletions packages/core/helpers/external-context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@ export class ExternalContextCreator {
: this.pipesConsumer.apply(value, { metatype, type, data }, pipes);
}

public async transformToResult(resultOrDeffered: any) {
if (resultOrDeffered && isFunction(resultOrDeffered.subscribe)) {
return lastValueFrom(resultOrDeffered);
public async transformToResult(resultOrDeferred: any) {
if (resultOrDeferred && isFunction(resultOrDeferred.subscribe)) {
return lastValueFrom(resultOrDeferred);
}
return resultOrDeffered;
return resultOrDeferred;
}

public createGuardsFn<TContext extends string = ContextType>(
Expand Down
8 changes: 4 additions & 4 deletions packages/core/interceptors/interceptors-consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class InterceptorsConsumer {
const context = this.createContext(args, instance, callback);
context.setType<TContext>(type);

const start$ = defer(() => this.transformDeffered(next));
const start$ = defer(() => this.transformDeferred(next));
const nextFn =
(i = 0) =>
async () => {
Expand All @@ -51,11 +51,11 @@ export class InterceptorsConsumer {
);
}

public transformDeffered(next: () => Promise<any>): Observable<any> {
public transformDeferred(next: () => Promise<any>): Observable<any> {
return fromPromise(next()).pipe(
switchMap(res => {
const isDeffered = res instanceof Promise || res instanceof Observable;
return isDeffered ? res : Promise.resolve(res);
const isDeferred = res instanceof Promise || res instanceof Observable;
return isDeferred ? res : Promise.resolve(res);
}),
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/pipes/pipes-consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class PipesConsumer {
{ metatype, type, data }: { metatype: any; type?: any; data?: any },
transforms: PipeTransform[],
) {
return transforms.reduce(async (defferedValue, pipe) => {
const val = await defferedValue;
return transforms.reduce(async (deferredValue, pipe) => {
const val = await deferredValue;
const result = pipe.transform(val, { metatype, type, data });
return result;
}, Promise.resolve(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('ExternalContextCreator', () => {
});

describe('transformToResult', () => {
describe('when resultOrDeffered', () => {
describe('when resultOrDeferred', () => {
describe('is Promise', () => {
it('should return Promise', async () => {
const value = 100;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/test/interceptors/interceptors-consumer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ describe('InterceptorsConsumer', () => {
expect(context.getHandler()).to.be.eql(callback);
});
});
describe('transformDeffered', () => {
describe('transformDeferred', () => {
describe('when next() result is plain value', () => {
it('should return Observable', async () => {
const val = 3;
const next = async () => val;
expect(await lastValueFrom(consumer.transformDeffered(next))).to.be.eql(
expect(await lastValueFrom(consumer.transformDeferred(next))).to.be.eql(
val,
);
});
Expand All @@ -103,7 +103,7 @@ describe('InterceptorsConsumer', () => {
it('should return Observable', async () => {
const val = 3;
const next = async () => val;
expect(await lastValueFrom(consumer.transformDeffered(next))).to.be.eql(
expect(await lastValueFrom(consumer.transformDeferred(next))).to.be.eql(
val,
);
});
Expand All @@ -113,7 +113,7 @@ describe('InterceptorsConsumer', () => {
const val = 3;
const next = async () => of(val);
expect(
await await lastValueFrom(consumer.transformDeffered(next) as any),
await await lastValueFrom(consumer.transformDeferred(next) as any),
).to.be.eql(val);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('RouterResponseController', () => {
});

describe('transformToResult', () => {
describe('when resultOrDeffered', () => {
describe('when resultOrDeferred', () => {
describe('is Promise', () => {
it('should return Promise', async () => {
const value = 100;
Expand Down
2 changes: 1 addition & 1 deletion packages/microservices/test/listeners-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('ListenersController', () => {
expect(addSpyTCP.calledTwice).to.be.true;
});
describe('when request scoped', () => {
it(`should call "addHandler" with deffered proxy`, () => {
it(`should call "addHandler" with deferred proxy`, () => {
explorer.expects('explore').returns(handlers);
instance.registerPatternHandlers(
new InstanceWrapper({ scope: Scope.REQUEST }),
Expand Down
2 changes: 1 addition & 1 deletion packages/websockets/test/web-sockets-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ describe('WebSocketsController', () => {
});
});
describe('pickResult', () => {
describe('when defferedResult contains value which', () => {
describe('when deferredResult contains value which', () => {
describe('is a Promise', () => {
it('should return Promise<Observable>', async () => {
const value = 100;
Expand Down
4 changes: 2 additions & 2 deletions packages/websockets/web-sockets-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ export class WebSocketsController {
}

public async pickResult(
defferedResult: Promise<any>,
deferredResult: Promise<any>,
): Promise<Observable<any>> {
const result = await defferedResult;
const result = await deferredResult;
if (result && isFunction(result.subscribe)) {
return result;
}
Expand Down

0 comments on commit 6485231

Please sign in to comment.