Azure / azure-functions-durable-js

JavaScript library for using the Durable Functions bindings
https://www.npmjs.com/package/durable-functions
MIT License
128 stars 47 forks source link

Add overload to improve the way to call orchestrations, activities, and entities #461

Closed hossam-nasr closed 1 year ago

hossam-nasr commented 1 year ago

Resolves #445 in a non-breaking way:

Before: Activities/Orchestrations:

const activityName = 'helloActivity';
const subOrchestratorName = 'sayHelloWithActivity';

const orchestrator: OrchestrationHandler = function* (context: OrchestrationContext) {
    const outputs = [];
    outputs.push(yield context.df.callSubOrchestrator(subOrchestratorName, 'Tokyo'));
    outputs.push(yield context.df.callSubOrchestrator(subOrchestratorName, 'Seattle'));
    outputs.push(yield context.df.callSubOrchestrator(subOrchestratorName, 'Cairo'));

    return outputs;
};
df.app.orchestration('durableOrchestrator1', orchestrator);

const sayHelloWithActivityHandler: OrchestrationHandler = function* (context: OrchestrationContext) {
    const input = context.df.getInput();
    return yield context.df.callActivity(activityName, input);
};
 df.app.orchestration(subOrchestratorName, sayHelloWithActivityHandler);

const helloActivityHandler: ActivityHandler = (input: string): string => {
    return `Hello, ${input}`;
};
df.app.activity(activityName, { handler: helloActivityHandler });

Entities:

const entityName = 'counterEntity';

const counterEntityHandler: EntityHandler<number> = (context: EntityContext<number>) => {
    // entity implementation
};
df.app.entity(entityName, counterEntityHandler);

const httpStart: HttpHandler = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponse> => {
    const id: string = req.params.id;
    const entityId: df.EntityId = new df.EntityId(entityName, id);
    const client = df.getClient(context);
    // use entityId for management operations
    await client.signalEntity(entityId, 'add', 1);
    const stateResponse = await client.readEntityState(entityId);
    // ...etc.
};

After:

Orchestrations/Activities:

const orchestrator: OrchestrationHandler = function* (context: OrchestrationContext) {
    const outputs = [];
    outputs.push(yield context.df.callSubOrchestrator(sayHelloWithActivity, 'Tokyo'));
    outputs.push(yield context.df.callSubOrchestrator(sayHelloWithActivity, 'Seattle'));
    outputs.push(yield context.df.callSubOrchestrator(sayHelloWithActivity, 'Cairo'));

    return outputs;
};
df.app.orchestration('durableOrchestrator1', orchestrator);

const sayHelloWithActivityHandler: OrchestrationHandler = function* (context: OrchestrationContext) {
    const input = context.df.getInput();
    return yield context.df.callActivity(helloActivity, input);
};
const sayHelloWithActivity: CallSubOrchestratorInput = df.app.orchestration(
    'sayHelloWithActivity',
    sayHelloWithActivityHandler
);

const helloActivityHandler: ActivityHandler = (input: string): string => {
    return `Hello, ${input}`;
};
const helloActivity: CallActivityInput = df.app.activity('hello', { handler: helloActivityHandler });

Entities:

const counterEntityHandler: EntityHandler<number> = (context: EntityContext<number>) => {
    // entity implementation
};
const counterEntity: RegisterEntityResult = df.app.entity('counterEntity', counterEntityHandler);

const httpStart: HttpHandler = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponse> => {
    const id: string = req.params.id;
    const entityId: df.EntityId = counterEntity.getEntityId(id);
    const client = df.getClient(context);
    // use entityId for management operations
    await client.signalEntity(entityId, 'add', 1);
    const stateResponse = await client.readEntityState(entityId);
    // ...etc.
};
hossam-nasr commented 1 year ago

Looks like we've decided to table this for preview, and we'll revisit this later. This is non-breaking so we can always add it after preview before GA, at GA, or even after GA. We already have two issues tracking this: #445 and #418, so I'm closing this PR