Skip to content

Add support for dynamic activity name #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/decorators/activity.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SetMetadata } from '@nestjs/common';
import { TEMPORAL_MODULE_ACTIVITY } from '../temporal.constants';

export interface ActivityOptions {
name?: string;
name?: string | ((instance: any) => string | Promise<string>);
}

export function Activity(): MethodDecorator;
Expand Down
34 changes: 24 additions & 10 deletions lib/temporal.explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import {
TEMPORAL_WORKER_CONFIG,
} from './temporal.constants';
import { TemporalMetadataAccessor } from './temporal-metadata.accessors';
import { ActivityOptions } from './decorators';

@Injectable()
export class TemporalExplorer
implements OnModuleInit, OnModuleDestroy, OnApplicationBootstrap
{
implements OnModuleInit, OnModuleDestroy, OnApplicationBootstrap {
private readonly logger = new Logger(TemporalExplorer.name);
private readonly injector = new Injector();
private worker: Worker;
Expand All @@ -38,7 +38,7 @@ export class TemporalExplorer
private readonly discoveryService: DiscoveryService,
private readonly metadataAccessor: TemporalMetadataAccessor,
private readonly metadataScanner: MetadataScanner,
) {}
) { }

clearInterval() {
this.timerId && clearInterval(this.timerId);
Expand Down Expand Up @@ -128,7 +128,7 @@ export class TemporalExplorer
),
);

activities.forEach((wrapper: InstanceWrapper) => {
const activitiesLoader = activities.flatMap((wrapper: InstanceWrapper) => {
const { instance, metatype } = wrapper;
const isRequestScoped = !wrapper.isDependencyTreeStatic();

Expand All @@ -137,25 +137,39 @@ export class TemporalExplorer
instance.constructor || metatype,
);

this.metadataScanner.scanFromPrototype(
return this.metadataScanner.scanFromPrototype(
instance,
Object.getPrototypeOf(instance),
async (key: string) => {
if (this.metadataAccessor.isActivity(instance[key])) {
const metadata = this.metadataAccessor.getActivity(instance[key]);

const args: unknown[] = [metadata?.name];
const metadata = this.metadataAccessor.getActivity(instance[key]) as ActivityOptions;

let activityName = key;
if (metadata?.name) {
if (typeof metadata.name === 'string') {
activityName = metadata.name
}
else {
const activityNameResult = metadata.name(instance);
if (typeof activityNameResult === 'string') {
activityName = activityNameResult
}
else {
activityName = await activityNameResult;
}
}
}

if (isRequestScoped) {
// TODO: handle request scoped
} else {
activitiesMethod[key] = instance[key].bind(instance);
activitiesMethod[activityName] = instance[key].bind(instance);
}
}
},
);
});

await Promise.all(activitiesLoader);
return activitiesMethod;
}
}