Skip to content

Commit b6e09d0

Browse files
authored
Merge pull request #109 from glandjs/development
feat(core): introduce saga-based module execution and NestJS-compatible dynamic module support
2 parents ba0d63f + 87e1406 commit b6e09d0

13 files changed

+64
-55
lines changed

examples/very-simple.ts

-47
This file was deleted.

packages/common/constant.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const PATH_METADATA = 'path';
22
export const METHOD_METADATA = 'method';
33
export const MODULE_METADATA = '__module__';
4+
export const SAGA_METADATA = '__saga__';
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { PATH_METADATA } from '../../constant';
2+
import type { EventType } from '../../types';
23

34
/**
45
* @publicApi
56
*/
6-
export function Channel(stream?: string): ClassDecorator {
7+
export function Channel(event?: EventType): ClassDecorator {
78
return (target: Function) => {
8-
Reflect.defineMetadata(PATH_METADATA, stream, target);
9+
Reflect.defineMetadata(PATH_METADATA, event, target);
910
};
1011
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { METHOD_METADATA } from '../../constant';
2+
import type { EventType } from '../../types';
3+
4+
/**
5+
* @publicApi
6+
*/
7+
export function Emit(event: EventType): MethodDecorator {
8+
return (target: object, key: string | symbol) => {
9+
Reflect.defineMetadata(METHOD_METADATA, event, target, key);
10+
};
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import 'reflect-metadata';
22
export * from './on.decorator';
33
export * from './channel.decorator';
4+
export * from './saga';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { METHOD_METADATA } from '../../../constant';
2+
3+
/**
4+
* @publicApi
5+
*/
6+
export function Compensate(): MethodDecorator {
7+
return (target: object, key: string | symbol) => {
8+
Reflect.defineMetadata(METHOD_METADATA, {}, target, key);
9+
};
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import 'reflect-metadata';
2+
export * from './saga.decorator';
3+
export * from './step.decorator';
4+
export * from './compensate.decorator';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { SAGA_METADATA } from '../../../constant';
2+
import type { EventType } from '../../../types';
3+
4+
/**
5+
* @publicApi
6+
*/
7+
export function Saga(opts: { event: EventType; compensation: EventType }): ClassDecorator {
8+
return (target: Function) => {
9+
Reflect.defineMetadata(SAGA_METADATA, opts, target);
10+
};
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { METHOD_METADATA } from '../../../constant';
2+
3+
/**
4+
* @publicApi
5+
*/
6+
export function Step(opts: { index: number }): MethodDecorator {
7+
return (target: object, key: string | symbol) => {
8+
Reflect.defineMetadata(METHOD_METADATA, opts, target, key);
9+
};
10+
}

packages/common/interfaces/events/event.interface.ts renamed to packages/common/interfaces/events/channel.interface.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export interface EventOptions {
77
export interface EventChannel {
88
channel(type: EventType): EventChannel;
99

10-
request<R>(type: EventType, data: any, strategy?: 'first' | 'last'): R | undefined;
11-
request<R>(type: EventType, data: any, strategy?: 'all'): R[];
10+
call<R>(type: EventType, data: any, strategy?: 'first' | 'last'): R | undefined;
11+
call<R>(type: EventType, data: any, strategy?: 'all'): R[];
1212

1313
respond<T, R>(listener: (data: T, respond: (result: R) => void) => void): Noop;
1414
respond<T, R>(type: EventType, listener: (data: T, respond: (result: R) => void) => void): Noop;
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './event.interface';
1+
export * from './channel.interface';

packages/common/interfaces/modules.interfaces.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ export interface ModuleMetadata<T = any> {
55
imports?: ImportableModule<T>[];
66
controllers?: ApiController<T>[];
77
channels?: ApiChannel<T>[];
8-
exports?: InjectionToken<T>[];
8+
sagas?: Constructor<T>[];
99
}
10-
export interface DynamicModule<T = any> extends ModuleMetadata<T> {
10+
11+
export interface DynamicModule<T = any> {
12+
imports?: ImportableModule<T>[];
13+
exports?: InjectionToken<T>[];
1114
module: Constructor<any>;
15+
/**
16+
* @default false
17+
*/
18+
global?: boolean;
1219
}

packages/common/types/modules.type.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DynamicModule, ModuleMetadata } from '../interfaces';
22
import { Constructor } from '@medishn/toolkit';
3-
export type ImportableModule<T = any> = Constructor<T> | DynamicModule<T>;
3+
export type ImportableModule<T = any> = Constructor<T> | DynamicModule | Promise<DynamicModule>;
44

55
export type InjectionToken<T = any> = string | symbol | Constructor<T> | Function;
66

0 commit comments

Comments
 (0)