feat: 使用 PubSub 进行消息广播。
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common';
|
||||
import { sanitize } from '@neuralegion/class-sanitizer/dist';
|
||||
import { plainToClass } from 'class-transformer';
|
||||
|
||||
@Injectable()
|
||||
export class SanitizePipe implements PipeTransform {
|
||||
@ -12,13 +12,11 @@ export class SanitizePipe implements PipeTransform {
|
||||
return value;
|
||||
}
|
||||
const constructorFunction = metadata.metatype;
|
||||
if (!constructorFunction) {
|
||||
if (!constructorFunction || value instanceof constructorFunction) {
|
||||
return value;
|
||||
}
|
||||
value = Object.assign(new constructorFunction(), value);
|
||||
try {
|
||||
sanitize(value);
|
||||
return value;
|
||||
return plainToClass(constructorFunction, value);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
|
@ -0,0 +1,5 @@
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { getPubSubToken } from '../utils/token';
|
||||
|
||||
export const InjectPubSub = (name?: string): ParameterDecorator =>
|
||||
Inject(getPubSubToken(name));
|
@ -1,5 +1,5 @@
|
||||
import { ModuleMetadata } from '@nestjs/common';
|
||||
import { PubSubOptions } from 'graphql-subscriptions';
|
||||
import { PubSubOptions } from './pub-sub-options.interface';
|
||||
|
||||
export interface PubSubAsyncConfig extends Pick<ModuleMetadata, 'imports'> {
|
||||
name?: string;
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { RedisOptions } from 'ioredis';
|
||||
|
||||
export interface PubSubOptions {
|
||||
name: string;
|
||||
name?: string;
|
||||
redis: RedisOptions;
|
||||
}
|
||||
|
@ -1,2 +1 @@
|
||||
export const DEFAULT_PUB_SUB_NAME = 'default';
|
||||
export const PUB_SUB_CONFIG_TOKEN = 'pub_sub_config_token';
|
||||
|
@ -1,47 +1,48 @@
|
||||
import { DynamicModule, Module, Provider } from '@nestjs/common';
|
||||
import { DynamicModule, Module } from '@nestjs/common';
|
||||
import { PubSubService } from './pub-sub.service';
|
||||
import {
|
||||
createOptionsProvider,
|
||||
createAsyncPubSubProviders,
|
||||
createPubSubProvider,
|
||||
} from './pub-sub.providers';
|
||||
import { PubSubOptions } from './interfaces/pub-sub-options.interface';
|
||||
import { PubSubAsyncConfig } from './interfaces/pub-sub-async-config.interface';
|
||||
import { getPubSubToken } from './utils/token';
|
||||
import { PUB_SUB_CONFIG_TOKEN } from './pub-sub.constants';
|
||||
import { getPubSubConfigToken } from './utils/token';
|
||||
|
||||
@Module({
|
||||
providers: [PubSubService],
|
||||
})
|
||||
export class PubSubModule {
|
||||
public static forRoot(options: PubSubOptions): DynamicModule {
|
||||
const providers = [createPubSubProvider(options)];
|
||||
return {
|
||||
global: true,
|
||||
module: PubSubModule,
|
||||
providers,
|
||||
exports: providers,
|
||||
};
|
||||
}
|
||||
public static forRootAsync(config: PubSubAsyncConfig) {
|
||||
const providers: Provider[] = [
|
||||
createOptionsProvider(config),
|
||||
const providers = [
|
||||
{
|
||||
provide: getPubSubToken(config.name),
|
||||
inject: [PUB_SUB_CONFIG_TOKEN],
|
||||
useFactory: (options: PubSubOptions) => {
|
||||
return createPubSubProvider({
|
||||
name: config.name,
|
||||
...options,
|
||||
});
|
||||
},
|
||||
provide: getPubSubConfigToken(options.name),
|
||||
useValue: options,
|
||||
},
|
||||
createPubSubProvider(options.name),
|
||||
];
|
||||
return {
|
||||
global: true,
|
||||
module: PubSubModule,
|
||||
imports: config.imports,
|
||||
providers,
|
||||
exports: providers,
|
||||
};
|
||||
}
|
||||
public static forRootAsync(...configs: PubSubAsyncConfig[]): DynamicModule {
|
||||
const providers = createAsyncPubSubProviders(configs);
|
||||
return {
|
||||
global: true,
|
||||
module: PubSubModule,
|
||||
imports: configs
|
||||
.map((config) => config.imports)
|
||||
.flat()
|
||||
.filter((o, i, a) => a.indexOf(o) === i),
|
||||
providers,
|
||||
exports: providers,
|
||||
};
|
||||
}
|
||||
public static forFeature(): DynamicModule {
|
||||
return {
|
||||
module: PubSubModule,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -2,22 +2,30 @@ import { Provider } from '@nestjs/common';
|
||||
import { PubSubAsyncConfig } from './interfaces/pub-sub-async-config.interface';
|
||||
import { PubSubOptions } from './interfaces/pub-sub-options.interface';
|
||||
import { PubSub } from './pub-sub';
|
||||
import { PUB_SUB_CONFIG_TOKEN } from './pub-sub.constants';
|
||||
import { getPubSubToken } from './utils/token';
|
||||
import { getPubSubConfigToken, getPubSubToken } from './utils/token';
|
||||
|
||||
export function createPubSubProvider(options: PubSubOptions): Provider {
|
||||
export function createPubSubProvider(name: string): Provider {
|
||||
return {
|
||||
provide: getPubSubToken(options.name),
|
||||
useFactory: () => {
|
||||
return new PubSub(options);
|
||||
},
|
||||
provide: getPubSubToken(name),
|
||||
useFactory: (option: PubSubOptions) => new PubSub(option),
|
||||
inject: [getPubSubConfigToken(name)],
|
||||
};
|
||||
}
|
||||
|
||||
export function createOptionsProvider(config: PubSubAsyncConfig): Provider {
|
||||
return {
|
||||
provide: PUB_SUB_CONFIG_TOKEN,
|
||||
provide: getPubSubConfigToken(config.name),
|
||||
useFactory: config.useFactory,
|
||||
inject: config.inject || [],
|
||||
};
|
||||
}
|
||||
export function createAsyncPubSubProviders(
|
||||
configs: PubSubAsyncConfig[],
|
||||
): Provider[] {
|
||||
return configs
|
||||
.map((config) => [
|
||||
createOptionsProvider(config),
|
||||
createPubSubProvider(config.name),
|
||||
])
|
||||
.flat();
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import {
|
||||
PubSubRawNextMessage,
|
||||
} from './interfaces/pub-sub-raw-message.interface';
|
||||
|
||||
const log = debug('app:pubsub:instance');
|
||||
const log = debug('fennec:pubsub:instance');
|
||||
export class PubSub extends EventEmitter {
|
||||
pubRedis: Redis;
|
||||
pSubRedis: Redis;
|
||||
|
@ -3,3 +3,6 @@ import { DEFAULT_PUB_SUB_NAME } from '../pub-sub.constants';
|
||||
export function getPubSubToken(name) {
|
||||
return `app:pub-usb:${name || DEFAULT_PUB_SUB_NAME}`;
|
||||
}
|
||||
export function getPubSubConfigToken(name) {
|
||||
return `app:pub-usb:config:${name || DEFAULT_PUB_SUB_NAME}`;
|
||||
}
|
||||
|
Reference in New Issue
Block a user