Skip to content

Commit

Permalink
feat: updateWorkflowContext function now accepts updater function or …
Browse files Browse the repository at this point in the history
…new context value

fix #15
  • Loading branch information
retro committed Jan 26, 2024
1 parent f250035 commit d6a1c1b
Show file tree
Hide file tree
Showing 5 changed files with 475 additions and 16 deletions.
17 changes: 15 additions & 2 deletions src/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,21 @@ export class Service<
.getWorkflow(input.workflowId)
.pipe(Effect.map((w) => w.context));
},
updateWorkflowContext(context: unknown) {
return state.updateWorkflowContext(input.workflowId, context);
updateWorkflowContext(contextOrUpdater: unknown) {
return Effect.gen(function* ($) {
if (typeof contextOrUpdater === 'function') {
const workflow = yield* $(state.getWorkflow(input.workflowId));
return yield* $(
state.updateWorkflowContext(
input.workflowId,
contextOrUpdater(workflow.context)
)
);
}
return yield* $(
state.updateWorkflowContext(input.workflowId, contextOrUpdater)
);
});
},
},
queue: {
Expand Down
223 changes: 223 additions & 0 deletions src/__tests__/__snapshots__/update-workflow-context.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`handles context update when updater function is passed 1`] = `
{
"conditions": [
{
"marking": 0,
"name": "start",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "end",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"tasks": [
{
"generation": 1,
"name": "t1",
"state": "started",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"workItems": [],
"workflows": [
{
"context": {
"count": 2,
},
"id": "workflow-1",
"name": "activities",
"parent": null,
"state": "started",
},
],
}
`;

exports[`handles context update when value is passed 1`] = `
{
"conditions": [
{
"marking": 0,
"name": "start",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "end",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"tasks": [
{
"generation": 1,
"name": "t1",
"state": "started",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"workItems": [],
"workflows": [
{
"context": {
"count": 2,
},
"id": "workflow-1",
"name": "activities",
"parent": null,
"state": "started",
},
],
}
`;

exports[`handles parent context update when updater function is passed 1`] = `
{
"conditions": [
{
"marking": 0,
"name": "start",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "end",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 1,
"name": "start",
"workflowId": "workflow-2",
"workflowName": "sub",
},
{
"marking": 0,
"name": "end",
"workflowId": "workflow-2",
"workflowName": "sub",
},
],
"tasks": [
{
"generation": 1,
"name": "t1",
"state": "started",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 0,
"name": "subT1",
"state": "enabled",
"workflowId": "workflow-2",
"workflowName": "sub",
},
],
"workItems": [],
"workflows": [
{
"context": {
"count": 1,
},
"id": "workflow-1",
"name": "activities",
"parent": null,
"state": "started",
},
{
"context": undefined,
"id": "workflow-2",
"name": "sub",
"parent": {
"taskGeneration": 1,
"taskName": "t1",
"workflowId": "workflow-1",
"workflowName": "activities",
},
"state": "started",
},
],
}
`;

exports[`handles parent context update when value is passed 1`] = `
{
"conditions": [
{
"marking": 0,
"name": "start",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "end",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 1,
"name": "start",
"workflowId": "workflow-2",
"workflowName": "sub",
},
{
"marking": 0,
"name": "end",
"workflowId": "workflow-2",
"workflowName": "sub",
},
],
"tasks": [
{
"generation": 1,
"name": "t1",
"state": "started",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 0,
"name": "subT1",
"state": "enabled",
"workflowId": "workflow-2",
"workflowName": "sub",
},
],
"workItems": [],
"workflows": [
{
"context": {
"count": 1,
},
"id": "workflow-1",
"name": "activities",
"parent": null,
"state": "started",
},
{
"context": undefined,
"id": "workflow-2",
"name": "sub",
"parent": {
"taskGeneration": 1,
"taskName": "t1",
"workflowId": "workflow-1",
"workflowName": "activities",
},
"state": "started",
},
],
}
`;
Loading

0 comments on commit d6a1c1b

Please sign in to comment.