anbaric-state-machine 1.9.0 → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/StateMachine.ts +25 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anbaric-state-machine",
3
- "version": "1.9.0",
3
+ "version": "1.11.0",
4
4
  "description": "This is the state machine library that one can use to add state management to an application. It will, by default, be locally runnable but can deployed to the Anbaric Cloud via the CLI",
5
5
  "license": "MIT",
6
6
  "author": "chris@anbaric.ai",
@@ -15,8 +15,8 @@
15
15
  "typescript": "^7.0.2"
16
16
  },
17
17
  "dependencies": {
18
- "anbaric-impl-cloud": "^1.9.0",
19
- "anbaric-tsapi": "^1.9.0"
18
+ "anbaric-impl-cloud": "^1.11.0",
19
+ "anbaric-tsapi": "^1.11.0"
20
20
  },
21
21
  "files": [
22
22
  "src"
@@ -15,10 +15,11 @@ import {Code} from "./actors/Code";
15
15
 
16
16
  class StateMachine {
17
17
 
18
- private workflowId: string;
19
- private states: Map<string, State>;
20
- private startState: string;
21
- private dataSchema: Map<string, PropertyDefinition>;
18
+ readonly workflowId: string;
19
+ readonly states: Map<string, State>;
20
+ readonly startState: string;
21
+ readonly dataSchema: Map<string, PropertyDefinition>;
22
+
22
23
  private persistence: JobPersistence;
23
24
  private queue: Queue;
24
25
  private consumer: Consumer;
@@ -87,8 +88,7 @@ class StateMachine {
87
88
 
88
89
  private validateProperties(properties : Map<string, any>, isNew : boolean) : boolean {
89
90
  for (const [key, value] of properties) {
90
- const definition = this.dataSchema.get(key);
91
- if (! definition || ! definition.validation(value)) return false;
91
+ if (this.propertyProblem(key, value)) return false;
92
92
  }
93
93
 
94
94
  if (! isNew) return true;
@@ -100,6 +100,13 @@ class StateMachine {
100
100
  return true;
101
101
  }
102
102
 
103
+ private propertyProblem(key : string, value : any) : string | undefined {
104
+ const definition = this.dataSchema.get(key);
105
+ if (! definition) return "is not declared in the data schema";
106
+ if (! definition.validation(value)) return "failed its validation";
107
+ return undefined;
108
+ }
109
+
103
110
  private async progressJob(jobId : string) : Promise<void> {
104
111
  let pristine = true;
105
112
 
@@ -114,14 +121,19 @@ class StateMachine {
114
121
 
115
122
  const newProperties = await action.run(job);
116
123
 
117
- if (! this.validateProperties(newProperties, false)) continue;
118
-
119
- involvedActors.push(action.actor);
120
-
121
- newProperties.forEach((value, key) => {
122
- pristine = false;
124
+ let applied = false;
125
+ for (const [key, value] of newProperties) {
126
+ const problem = this.propertyProblem(key, value);
127
+ if (problem) {
128
+ console.warn(`[${this.workflowId}] action "${action.name}" set "${key}", which ${problem}, on job ${job.id} — skipping that property.`);
129
+ continue;
130
+ }
123
131
  job.properties.set(key, value);
124
- });
132
+ pristine = false;
133
+ applied = true;
134
+ }
135
+
136
+ if (applied) involvedActors.push(action.actor);
125
137
  }
126
138
 
127
139
  let newState : string | undefined = undefined;