quidproquo-xstate 0.1.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.
- package/README.md +1 -0
- package/lib/commonjs/actionProcessor/getStateMachineCreateActionProcessor.d.ts +2 -0
- package/lib/commonjs/actionProcessor/getStateMachineCreateActionProcessor.js +66 -0
- package/lib/commonjs/actionProcessor/getStateMachineGetActionProcessor.d.ts +2 -0
- package/lib/commonjs/actionProcessor/getStateMachineGetActionProcessor.js +39 -0
- package/lib/commonjs/actionProcessor/getStateMachineGetStateActionProcessor.d.ts +2 -0
- package/lib/commonjs/actionProcessor/getStateMachineGetStateActionProcessor.js +54 -0
- package/lib/commonjs/actionProcessor/getStateMachineSendEventActionProcessor.d.ts +2 -0
- package/lib/commonjs/actionProcessor/getStateMachineSendEventActionProcessor.js +100 -0
- package/lib/commonjs/actionProcessor/index.d.ts +2 -0
- package/lib/commonjs/actionProcessor/index.js +20 -0
- package/lib/commonjs/actions/StateMachineActionType.d.ts +6 -0
- package/lib/commonjs/actions/StateMachineActionType.js +10 -0
- package/lib/commonjs/actions/StateMachineCreateActionRequester.d.ts +2 -0
- package/lib/commonjs/actions/StateMachineCreateActionRequester.js +14 -0
- package/lib/commonjs/actions/StateMachineCreateActionTypes.d.ts +13 -0
- package/lib/commonjs/actions/StateMachineCreateActionTypes.js +2 -0
- package/lib/commonjs/actions/StateMachineGetActionRequester.d.ts +2 -0
- package/lib/commonjs/actions/StateMachineGetActionRequester.js +13 -0
- package/lib/commonjs/actions/StateMachineGetActionTypes.d.ts +12 -0
- package/lib/commonjs/actions/StateMachineGetActionTypes.js +2 -0
- package/lib/commonjs/actions/StateMachineGetStateActionRequester.d.ts +2 -0
- package/lib/commonjs/actions/StateMachineGetStateActionRequester.js +13 -0
- package/lib/commonjs/actions/StateMachineGetStateActionTypes.d.ts +16 -0
- package/lib/commonjs/actions/StateMachineGetStateActionTypes.js +2 -0
- package/lib/commonjs/actions/StateMachineSendEventActionRequester.d.ts +2 -0
- package/lib/commonjs/actions/StateMachineSendEventActionRequester.js +14 -0
- package/lib/commonjs/actions/StateMachineSendEventActionTypes.d.ts +17 -0
- package/lib/commonjs/actions/StateMachineSendEventActionTypes.js +2 -0
- package/lib/commonjs/actions/index.d.ts +9 -0
- package/lib/commonjs/actions/index.js +25 -0
- package/lib/commonjs/config/QPQConfig.d.ts +3 -0
- package/lib/commonjs/config/QPQConfig.js +7 -0
- package/lib/commonjs/config/index.d.ts +3 -0
- package/lib/commonjs/config/index.js +19 -0
- package/lib/commonjs/config/qpqXStateConfigUtils.d.ts +4 -0
- package/lib/commonjs/config/qpqXStateConfigUtils.js +13 -0
- package/lib/commonjs/config/settings/index.d.ts +1 -0
- package/lib/commonjs/config/settings/index.js +17 -0
- package/lib/commonjs/config/settings/stateMachine.d.ts +22 -0
- package/lib/commonjs/config/settings/stateMachine.js +27 -0
- package/lib/commonjs/index.d.ts +3 -0
- package/lib/commonjs/index.js +19 -0
- package/lib/esm/actionProcessor/getStateMachineCreateActionProcessor.d.ts +2 -0
- package/lib/esm/actionProcessor/getStateMachineCreateActionProcessor.js +51 -0
- package/lib/esm/actionProcessor/getStateMachineGetActionProcessor.d.ts +2 -0
- package/lib/esm/actionProcessor/getStateMachineGetActionProcessor.js +24 -0
- package/lib/esm/actionProcessor/getStateMachineGetStateActionProcessor.d.ts +2 -0
- package/lib/esm/actionProcessor/getStateMachineGetStateActionProcessor.js +39 -0
- package/lib/esm/actionProcessor/getStateMachineSendEventActionProcessor.d.ts +2 -0
- package/lib/esm/actionProcessor/getStateMachineSendEventActionProcessor.js +85 -0
- package/lib/esm/actionProcessor/index.d.ts +2 -0
- package/lib/esm/actionProcessor/index.js +10 -0
- package/lib/esm/actions/StateMachineActionType.d.ts +6 -0
- package/lib/esm/actions/StateMachineActionType.js +7 -0
- package/lib/esm/actions/StateMachineCreateActionRequester.d.ts +2 -0
- package/lib/esm/actions/StateMachineCreateActionRequester.js +11 -0
- package/lib/esm/actions/StateMachineCreateActionTypes.d.ts +13 -0
- package/lib/esm/actions/StateMachineCreateActionTypes.js +1 -0
- package/lib/esm/actions/StateMachineGetActionRequester.d.ts +2 -0
- package/lib/esm/actions/StateMachineGetActionRequester.js +10 -0
- package/lib/esm/actions/StateMachineGetActionTypes.d.ts +12 -0
- package/lib/esm/actions/StateMachineGetActionTypes.js +1 -0
- package/lib/esm/actions/StateMachineGetStateActionRequester.d.ts +2 -0
- package/lib/esm/actions/StateMachineGetStateActionRequester.js +10 -0
- package/lib/esm/actions/StateMachineGetStateActionTypes.d.ts +16 -0
- package/lib/esm/actions/StateMachineGetStateActionTypes.js +1 -0
- package/lib/esm/actions/StateMachineSendEventActionRequester.d.ts +2 -0
- package/lib/esm/actions/StateMachineSendEventActionRequester.js +11 -0
- package/lib/esm/actions/StateMachineSendEventActionTypes.d.ts +17 -0
- package/lib/esm/actions/StateMachineSendEventActionTypes.js +1 -0
- package/lib/esm/actions/index.d.ts +9 -0
- package/lib/esm/actions/index.js +9 -0
- package/lib/esm/config/QPQConfig.d.ts +3 -0
- package/lib/esm/config/QPQConfig.js +4 -0
- package/lib/esm/config/index.d.ts +3 -0
- package/lib/esm/config/index.js +3 -0
- package/lib/esm/config/qpqXStateConfigUtils.d.ts +4 -0
- package/lib/esm/config/qpqXStateConfigUtils.js +8 -0
- package/lib/esm/config/settings/index.d.ts +1 -0
- package/lib/esm/config/settings/index.js +1 -0
- package/lib/esm/config/settings/stateMachine.d.ts +22 -0
- package/lib/esm/config/settings/stateMachine.js +21 -0
- package/lib/esm/index.d.ts +3 -0
- package/lib/esm/index.js +3 -0
- package/package.json +42 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# quidproquo-xstate
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getStateMachineCreateActionProcessor = void 0;
|
|
13
|
+
const quidproquo_core_1 = require("quidproquo-core");
|
|
14
|
+
const xstate_1 = require("xstate");
|
|
15
|
+
const actions_1 = require("../actions");
|
|
16
|
+
const config_1 = require("../config");
|
|
17
|
+
const getProcessStateMachineCreate = (qpqConfig) => {
|
|
18
|
+
return (payload, session, actionProcessors, logger, updateSession, dynamicModuleLoader, streamRegistry) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
const smConfig = (0, config_1.getStateMachineByName)(qpqConfig, payload.stateMachineName);
|
|
20
|
+
if (!smConfig) {
|
|
21
|
+
return (0, quidproquo_core_1.actionResultError)(quidproquo_core_1.ErrorTypeEnum.NotFound, `State machine not found: ${payload.stateMachineName}`);
|
|
22
|
+
}
|
|
23
|
+
const resolveStory = (0, quidproquo_core_1.createImplementationRuntime)(qpqConfig, ['StateMachine Create'], () => new Date().toISOString(), () => `${Date.now()}-${Math.random().toString(36).slice(2)}`, session, actionProcessors, logger, dynamicModuleLoader, streamRegistry);
|
|
24
|
+
// XState: determine initial state and which entry actions fire
|
|
25
|
+
const firedActions = [];
|
|
26
|
+
const actionImpls = {};
|
|
27
|
+
for (const actionName of Object.keys(smConfig.actions)) {
|
|
28
|
+
actionImpls[actionName] = () => {
|
|
29
|
+
firedActions.push(actionName);
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const machine = (0, xstate_1.createMachine)(smConfig.config, {
|
|
33
|
+
actions: actionImpls,
|
|
34
|
+
});
|
|
35
|
+
const actor = (0, xstate_1.createActor)(machine);
|
|
36
|
+
actor.start();
|
|
37
|
+
const initialSnapshot = actor.getPersistedSnapshot();
|
|
38
|
+
actor.stop();
|
|
39
|
+
// Build entity with id and machine state
|
|
40
|
+
const entity = Object.assign(Object.assign({}, payload.item), { id: payload.id });
|
|
41
|
+
entity[smConfig.stateField] = initialSnapshot;
|
|
42
|
+
// Persist to KVS
|
|
43
|
+
const upsertResult = yield resolveStory(function* () { yield* (0, quidproquo_core_1.askKeyValueStoreUpsert)(smConfig.keyValueStoreName, entity); }, []);
|
|
44
|
+
if (upsertResult.error) {
|
|
45
|
+
return (0, quidproquo_core_1.actionResultError)(upsertResult.error.errorType, upsertResult.error.errorText);
|
|
46
|
+
}
|
|
47
|
+
// Execute side-effect stories for any initial entry actions
|
|
48
|
+
for (const actionName of firedActions) {
|
|
49
|
+
const runtime = smConfig.actions[actionName];
|
|
50
|
+
if (runtime) {
|
|
51
|
+
const storyModule = yield dynamicModuleLoader(runtime);
|
|
52
|
+
const storyResult = yield resolveStory(storyModule, [entity]);
|
|
53
|
+
if (storyResult.error) {
|
|
54
|
+
return (0, quidproquo_core_1.actionResultError)(storyResult.error.errorType, storyResult.error.errorText);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return (0, quidproquo_core_1.actionResult)(entity);
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
const getStateMachineCreateActionProcessor = (qpqConfig) => __awaiter(void 0, void 0, void 0, function* () {
|
|
62
|
+
return ({
|
|
63
|
+
[actions_1.StateMachineActionType.Create]: getProcessStateMachineCreate(qpqConfig),
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
exports.getStateMachineCreateActionProcessor = getStateMachineCreateActionProcessor;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getStateMachineGetActionProcessor = void 0;
|
|
13
|
+
const quidproquo_core_1 = require("quidproquo-core");
|
|
14
|
+
const actions_1 = require("../actions");
|
|
15
|
+
const config_1 = require("../config");
|
|
16
|
+
const getProcessStateMachineGet = (qpqConfig) => {
|
|
17
|
+
return (payload, session, actionProcessors, logger, updateSession, dynamicModuleLoader, streamRegistry) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
const smConfig = (0, config_1.getStateMachineByName)(qpqConfig, payload.stateMachineName);
|
|
19
|
+
if (!smConfig) {
|
|
20
|
+
return (0, quidproquo_core_1.actionResultError)(quidproquo_core_1.ErrorTypeEnum.NotFound, `State machine not found: ${payload.stateMachineName}`);
|
|
21
|
+
}
|
|
22
|
+
const resolveStory = (0, quidproquo_core_1.createImplementationRuntime)(qpqConfig, ['StateMachine Get'], () => new Date().toISOString(), () => `${Date.now()}-${Math.random().toString(36).slice(2)}`, session, actionProcessors, logger, dynamicModuleLoader, streamRegistry);
|
|
23
|
+
const getResult = yield resolveStory(function* () { return yield* (0, quidproquo_core_1.askKeyValueStoreGet)(smConfig.keyValueStoreName, payload.id); }, []);
|
|
24
|
+
if (getResult.error) {
|
|
25
|
+
return (0, quidproquo_core_1.actionResultError)(getResult.error.errorType, getResult.error.errorText);
|
|
26
|
+
}
|
|
27
|
+
const entity = getResult.result;
|
|
28
|
+
if (!entity) {
|
|
29
|
+
return (0, quidproquo_core_1.actionResultError)(quidproquo_core_1.ErrorTypeEnum.NotFound, `Entity not found: ${payload.id}`);
|
|
30
|
+
}
|
|
31
|
+
return (0, quidproquo_core_1.actionResult)(entity);
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
const getStateMachineGetActionProcessor = (qpqConfig) => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
+
return ({
|
|
36
|
+
[actions_1.StateMachineActionType.Get]: getProcessStateMachineGet(qpqConfig),
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
exports.getStateMachineGetActionProcessor = getStateMachineGetActionProcessor;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getStateMachineGetStateActionProcessor = void 0;
|
|
13
|
+
const quidproquo_core_1 = require("quidproquo-core");
|
|
14
|
+
const xstate_1 = require("xstate");
|
|
15
|
+
const actions_1 = require("../actions");
|
|
16
|
+
const config_1 = require("../config");
|
|
17
|
+
const getProcessStateMachineGetState = (qpqConfig) => {
|
|
18
|
+
return (payload, session, actionProcessors, logger, updateSession, dynamicModuleLoader, streamRegistry) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
const smConfig = (0, config_1.getStateMachineByName)(qpqConfig, payload.stateMachineName);
|
|
20
|
+
if (!smConfig) {
|
|
21
|
+
return (0, quidproquo_core_1.actionResultError)(quidproquo_core_1.ErrorTypeEnum.NotFound, `State machine not found: ${payload.stateMachineName}`);
|
|
22
|
+
}
|
|
23
|
+
const resolveStory = (0, quidproquo_core_1.createImplementationRuntime)(qpqConfig, ['StateMachine GetState'], () => new Date().toISOString(), () => `${Date.now()}-${Math.random().toString(36).slice(2)}`, session, actionProcessors, logger, dynamicModuleLoader, streamRegistry);
|
|
24
|
+
// Load entity from KVS
|
|
25
|
+
const getResult = yield resolveStory(function* () { return yield* (0, quidproquo_core_1.askKeyValueStoreGet)(smConfig.keyValueStoreName, payload.id); }, []);
|
|
26
|
+
if (getResult.error) {
|
|
27
|
+
return (0, quidproquo_core_1.actionResultError)(getResult.error.errorType, getResult.error.errorText);
|
|
28
|
+
}
|
|
29
|
+
const entity = getResult.result;
|
|
30
|
+
if (!entity) {
|
|
31
|
+
return (0, quidproquo_core_1.actionResultError)(quidproquo_core_1.ErrorTypeEnum.NotFound, `Entity not found: ${payload.id}`);
|
|
32
|
+
}
|
|
33
|
+
const persistedSnapshot = entity[smConfig.stateField];
|
|
34
|
+
if (!persistedSnapshot) {
|
|
35
|
+
return (0, quidproquo_core_1.actionResultError)(quidproquo_core_1.ErrorTypeEnum.NotFound, `No state machine state found on entity: ${payload.id}`);
|
|
36
|
+
}
|
|
37
|
+
// XState: rehydrate to read current state
|
|
38
|
+
const machine = (0, xstate_1.createMachine)(smConfig.config);
|
|
39
|
+
const actor = (0, xstate_1.createActor)(machine, { snapshot: persistedSnapshot });
|
|
40
|
+
actor.start();
|
|
41
|
+
const snapshot = actor.getSnapshot();
|
|
42
|
+
actor.stop();
|
|
43
|
+
return (0, quidproquo_core_1.actionResult)({
|
|
44
|
+
value: typeof snapshot.value === 'string' ? snapshot.value : JSON.stringify(snapshot.value),
|
|
45
|
+
done: snapshot.status === 'done',
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
const getStateMachineGetStateActionProcessor = (qpqConfig) => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
return ({
|
|
51
|
+
[actions_1.StateMachineActionType.GetState]: getProcessStateMachineGetState(qpqConfig),
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
exports.getStateMachineGetStateActionProcessor = getStateMachineGetStateActionProcessor;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getStateMachineSendEventActionProcessor = void 0;
|
|
13
|
+
const quidproquo_core_1 = require("quidproquo-core");
|
|
14
|
+
const xstate_1 = require("xstate");
|
|
15
|
+
const actions_1 = require("../actions");
|
|
16
|
+
const config_1 = require("../config");
|
|
17
|
+
const getProcessStateMachineSendEvent = (qpqConfig) => {
|
|
18
|
+
return (payload, session, actionProcessors, logger, updateSession, dynamicModuleLoader, streamRegistry) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
const smConfig = (0, config_1.getStateMachineByName)(qpqConfig, payload.stateMachineName);
|
|
20
|
+
if (!smConfig) {
|
|
21
|
+
return (0, quidproquo_core_1.actionResultError)(quidproquo_core_1.ErrorTypeEnum.NotFound, `State machine not found: ${payload.stateMachineName}`);
|
|
22
|
+
}
|
|
23
|
+
const resolveStory = (0, quidproquo_core_1.createImplementationRuntime)(qpqConfig, ['StateMachine SendEvent'], () => new Date().toISOString(), () => `${Date.now()}-${Math.random().toString(36).slice(2)}`, session, actionProcessors, logger, dynamicModuleLoader, streamRegistry);
|
|
24
|
+
// Load entity from KVS
|
|
25
|
+
let entity;
|
|
26
|
+
const getResult = yield resolveStory(function* () { return yield* (0, quidproquo_core_1.askKeyValueStoreGet)(smConfig.keyValueStoreName, payload.id); }, []);
|
|
27
|
+
if (getResult.error) {
|
|
28
|
+
return (0, quidproquo_core_1.actionResultError)(getResult.error.errorType, getResult.error.errorText);
|
|
29
|
+
}
|
|
30
|
+
entity = getResult.result;
|
|
31
|
+
if (!entity) {
|
|
32
|
+
return (0, quidproquo_core_1.actionResultError)(quidproquo_core_1.ErrorTypeEnum.NotFound, `Entity not found: ${payload.id}`);
|
|
33
|
+
}
|
|
34
|
+
// Pre-evaluate guards via QPQ stories
|
|
35
|
+
const guardResults = {};
|
|
36
|
+
for (const [guardName, guardRuntime] of Object.entries(smConfig.guards)) {
|
|
37
|
+
const guardModule = yield dynamicModuleLoader(guardRuntime);
|
|
38
|
+
const guardResult = yield resolveStory(guardModule, [entity, payload.event]);
|
|
39
|
+
if (guardResult.error) {
|
|
40
|
+
return (0, quidproquo_core_1.actionResultError)(guardResult.error.errorType, guardResult.error.errorText);
|
|
41
|
+
}
|
|
42
|
+
guardResults[guardName] = guardResult.result;
|
|
43
|
+
}
|
|
44
|
+
const guardImpls = {};
|
|
45
|
+
for (const [guardName, result] of Object.entries(guardResults)) {
|
|
46
|
+
guardImpls[guardName] = () => result;
|
|
47
|
+
}
|
|
48
|
+
// Track which actions fire during the transition
|
|
49
|
+
const firedActions = [];
|
|
50
|
+
const actionImpls = {};
|
|
51
|
+
for (const actionName of Object.keys(smConfig.actions)) {
|
|
52
|
+
actionImpls[actionName] = () => {
|
|
53
|
+
firedActions.push(actionName);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
// XState: build machine, rehydrate, send event
|
|
57
|
+
const machine = (0, xstate_1.createMachine)(smConfig.config, {
|
|
58
|
+
actions: actionImpls,
|
|
59
|
+
guards: guardImpls,
|
|
60
|
+
});
|
|
61
|
+
const persistedSnapshot = entity[smConfig.stateField];
|
|
62
|
+
const actor = (0, xstate_1.createActor)(machine, {
|
|
63
|
+
snapshot: persistedSnapshot || undefined,
|
|
64
|
+
});
|
|
65
|
+
const previousState = actor.getSnapshot().value;
|
|
66
|
+
actor.start();
|
|
67
|
+
actor.send(payload.event);
|
|
68
|
+
const newSnapshot = actor.getPersistedSnapshot();
|
|
69
|
+
const currentState = actor.getSnapshot().value;
|
|
70
|
+
const isDone = actor.getSnapshot().status === 'done';
|
|
71
|
+
actor.stop();
|
|
72
|
+
if (previousState === currentState && !isDone) {
|
|
73
|
+
return (0, quidproquo_core_1.actionResultError)(quidproquo_core_1.ErrorTypeEnum.BadRequest, `Event '${payload.event.type}' is not valid for current state '${JSON.stringify(previousState)}'`);
|
|
74
|
+
}
|
|
75
|
+
// Persist new snapshot
|
|
76
|
+
entity[smConfig.stateField] = newSnapshot;
|
|
77
|
+
const upsertResult = yield resolveStory(function* () { yield* (0, quidproquo_core_1.askKeyValueStoreUpsert)(smConfig.keyValueStoreName, entity); }, []);
|
|
78
|
+
if (upsertResult.error) {
|
|
79
|
+
return (0, quidproquo_core_1.actionResultError)(upsertResult.error.errorType, upsertResult.error.errorText);
|
|
80
|
+
}
|
|
81
|
+
// Execute side-effect stories for fired actions
|
|
82
|
+
for (const actionName of firedActions) {
|
|
83
|
+
const runtime = smConfig.actions[actionName];
|
|
84
|
+
if (runtime) {
|
|
85
|
+
const storyModule = yield dynamicModuleLoader(runtime);
|
|
86
|
+
const storyResult = yield resolveStory(storyModule, [entity, payload.event]);
|
|
87
|
+
if (storyResult.error) {
|
|
88
|
+
return (0, quidproquo_core_1.actionResultError)(storyResult.error.errorType, storyResult.error.errorText);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return (0, quidproquo_core_1.actionResult)(entity);
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
const getStateMachineSendEventActionProcessor = (qpqConfig) => __awaiter(void 0, void 0, void 0, function* () {
|
|
96
|
+
return ({
|
|
97
|
+
[actions_1.StateMachineActionType.SendEvent]: getProcessStateMachineSendEvent(qpqConfig),
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
exports.getStateMachineSendEventActionProcessor = getStateMachineSendEventActionProcessor;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getStateMachineActionProcessor = void 0;
|
|
13
|
+
const getStateMachineCreateActionProcessor_1 = require("./getStateMachineCreateActionProcessor");
|
|
14
|
+
const getStateMachineGetActionProcessor_1 = require("./getStateMachineGetActionProcessor");
|
|
15
|
+
const getStateMachineGetStateActionProcessor_1 = require("./getStateMachineGetStateActionProcessor");
|
|
16
|
+
const getStateMachineSendEventActionProcessor_1 = require("./getStateMachineSendEventActionProcessor");
|
|
17
|
+
const getStateMachineActionProcessor = (qpqConfig, dynamicModuleLoader) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
return (Object.assign(Object.assign(Object.assign(Object.assign({}, (yield (0, getStateMachineCreateActionProcessor_1.getStateMachineCreateActionProcessor)(qpqConfig, dynamicModuleLoader))), (yield (0, getStateMachineGetActionProcessor_1.getStateMachineGetActionProcessor)(qpqConfig, dynamicModuleLoader))), (yield (0, getStateMachineGetStateActionProcessor_1.getStateMachineGetStateActionProcessor)(qpqConfig, dynamicModuleLoader))), (yield (0, getStateMachineSendEventActionProcessor_1.getStateMachineSendEventActionProcessor)(qpqConfig, dynamicModuleLoader))));
|
|
19
|
+
});
|
|
20
|
+
exports.getStateMachineActionProcessor = getStateMachineActionProcessor;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StateMachineActionType = void 0;
|
|
4
|
+
var StateMachineActionType;
|
|
5
|
+
(function (StateMachineActionType) {
|
|
6
|
+
StateMachineActionType["Create"] = "@quidproquo-core/StateMachine/Create";
|
|
7
|
+
StateMachineActionType["Get"] = "@quidproquo-core/StateMachine/Get";
|
|
8
|
+
StateMachineActionType["GetState"] = "@quidproquo-core/StateMachine/GetState";
|
|
9
|
+
StateMachineActionType["SendEvent"] = "@quidproquo-core/StateMachine/SendEvent";
|
|
10
|
+
})(StateMachineActionType || (exports.StateMachineActionType = StateMachineActionType = {}));
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.askStateMachineCreate = askStateMachineCreate;
|
|
4
|
+
const StateMachineActionType_1 = require("./StateMachineActionType");
|
|
5
|
+
function* askStateMachineCreate(stateMachineName, id, item) {
|
|
6
|
+
return yield {
|
|
7
|
+
type: StateMachineActionType_1.StateMachineActionType.Create,
|
|
8
|
+
payload: {
|
|
9
|
+
stateMachineName,
|
|
10
|
+
id,
|
|
11
|
+
item,
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Action, ActionProcessor, ActionRequester } from 'quidproquo-core';
|
|
2
|
+
import { StateMachineActionType } from './StateMachineActionType';
|
|
3
|
+
export interface StateMachineCreateActionPayload<T> {
|
|
4
|
+
stateMachineName: string;
|
|
5
|
+
id: string;
|
|
6
|
+
item: T;
|
|
7
|
+
}
|
|
8
|
+
export interface StateMachineCreateAction<T> extends Action<StateMachineCreateActionPayload<T>> {
|
|
9
|
+
type: StateMachineActionType.Create;
|
|
10
|
+
payload: StateMachineCreateActionPayload<T>;
|
|
11
|
+
}
|
|
12
|
+
export type StateMachineCreateActionProcessor<T> = ActionProcessor<StateMachineCreateAction<T>, T>;
|
|
13
|
+
export type StateMachineCreateActionRequester<T> = ActionRequester<StateMachineCreateAction<T>, T>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.askStateMachineGet = askStateMachineGet;
|
|
4
|
+
const StateMachineActionType_1 = require("./StateMachineActionType");
|
|
5
|
+
function* askStateMachineGet(stateMachineName, id) {
|
|
6
|
+
return yield {
|
|
7
|
+
type: StateMachineActionType_1.StateMachineActionType.Get,
|
|
8
|
+
payload: {
|
|
9
|
+
stateMachineName,
|
|
10
|
+
id,
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Action, ActionProcessor, ActionRequester } from 'quidproquo-core';
|
|
2
|
+
import { StateMachineActionType } from './StateMachineActionType';
|
|
3
|
+
export interface StateMachineGetActionPayload {
|
|
4
|
+
stateMachineName: string;
|
|
5
|
+
id: string;
|
|
6
|
+
}
|
|
7
|
+
export interface StateMachineGetAction<T> extends Action<StateMachineGetActionPayload> {
|
|
8
|
+
type: StateMachineActionType.Get;
|
|
9
|
+
payload: StateMachineGetActionPayload;
|
|
10
|
+
}
|
|
11
|
+
export type StateMachineGetActionProcessor<T> = ActionProcessor<StateMachineGetAction<T>, T>;
|
|
12
|
+
export type StateMachineGetActionRequester<T> = ActionRequester<StateMachineGetAction<T>, T>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.askStateMachineGetState = askStateMachineGetState;
|
|
4
|
+
const StateMachineActionType_1 = require("./StateMachineActionType");
|
|
5
|
+
function* askStateMachineGetState(stateMachineName, id) {
|
|
6
|
+
return yield {
|
|
7
|
+
type: StateMachineActionType_1.StateMachineActionType.GetState,
|
|
8
|
+
payload: {
|
|
9
|
+
stateMachineName,
|
|
10
|
+
id,
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Action, ActionProcessor, ActionRequester } from 'quidproquo-core';
|
|
2
|
+
import { StateMachineActionType } from './StateMachineActionType';
|
|
3
|
+
export interface StateMachineGetStateActionPayload {
|
|
4
|
+
stateMachineName: string;
|
|
5
|
+
id: string;
|
|
6
|
+
}
|
|
7
|
+
export interface StateMachineStateInfo {
|
|
8
|
+
value: string;
|
|
9
|
+
done: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface StateMachineGetStateAction extends Action<StateMachineGetStateActionPayload> {
|
|
12
|
+
type: StateMachineActionType.GetState;
|
|
13
|
+
payload: StateMachineGetStateActionPayload;
|
|
14
|
+
}
|
|
15
|
+
export type StateMachineGetStateActionProcessor = ActionProcessor<StateMachineGetStateAction, StateMachineStateInfo>;
|
|
16
|
+
export type StateMachineGetStateActionRequester = ActionRequester<StateMachineGetStateAction, StateMachineStateInfo>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.askStateMachineSendEvent = askStateMachineSendEvent;
|
|
4
|
+
const StateMachineActionType_1 = require("./StateMachineActionType");
|
|
5
|
+
function* askStateMachineSendEvent(stateMachineName, id, event) {
|
|
6
|
+
return yield {
|
|
7
|
+
type: StateMachineActionType_1.StateMachineActionType.SendEvent,
|
|
8
|
+
payload: {
|
|
9
|
+
stateMachineName,
|
|
10
|
+
id,
|
|
11
|
+
event,
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Action, ActionProcessor, ActionRequester } from 'quidproquo-core';
|
|
2
|
+
import { StateMachineActionType } from './StateMachineActionType';
|
|
3
|
+
export interface StateMachineEvent {
|
|
4
|
+
type: string;
|
|
5
|
+
[key: string]: any;
|
|
6
|
+
}
|
|
7
|
+
export interface StateMachineSendEventActionPayload {
|
|
8
|
+
stateMachineName: string;
|
|
9
|
+
id: string;
|
|
10
|
+
event: StateMachineEvent;
|
|
11
|
+
}
|
|
12
|
+
export interface StateMachineSendEventAction<T> extends Action<StateMachineSendEventActionPayload> {
|
|
13
|
+
type: StateMachineActionType.SendEvent;
|
|
14
|
+
payload: StateMachineSendEventActionPayload;
|
|
15
|
+
}
|
|
16
|
+
export type StateMachineSendEventActionProcessor<T> = ActionProcessor<StateMachineSendEventAction<T>, T>;
|
|
17
|
+
export type StateMachineSendEventActionRequester<T> = ActionRequester<StateMachineSendEventAction<T>, T>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './StateMachineActionType';
|
|
2
|
+
export * from './StateMachineCreateActionRequester';
|
|
3
|
+
export * from './StateMachineCreateActionTypes';
|
|
4
|
+
export * from './StateMachineGetActionRequester';
|
|
5
|
+
export * from './StateMachineGetActionTypes';
|
|
6
|
+
export * from './StateMachineGetStateActionRequester';
|
|
7
|
+
export * from './StateMachineGetStateActionTypes';
|
|
8
|
+
export * from './StateMachineSendEventActionRequester';
|
|
9
|
+
export * from './StateMachineSendEventActionTypes';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./StateMachineActionType"), exports);
|
|
18
|
+
__exportStar(require("./StateMachineCreateActionRequester"), exports);
|
|
19
|
+
__exportStar(require("./StateMachineCreateActionTypes"), exports);
|
|
20
|
+
__exportStar(require("./StateMachineGetActionRequester"), exports);
|
|
21
|
+
__exportStar(require("./StateMachineGetActionTypes"), exports);
|
|
22
|
+
__exportStar(require("./StateMachineGetStateActionRequester"), exports);
|
|
23
|
+
__exportStar(require("./StateMachineGetStateActionTypes"), exports);
|
|
24
|
+
__exportStar(require("./StateMachineSendEventActionRequester"), exports);
|
|
25
|
+
__exportStar(require("./StateMachineSendEventActionTypes"), exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QPQXStateConfigSettingType = void 0;
|
|
4
|
+
var QPQXStateConfigSettingType;
|
|
5
|
+
(function (QPQXStateConfigSettingType) {
|
|
6
|
+
QPQXStateConfigSettingType["StateMachine"] = "@quidproquo-xstate/config/StateMachine";
|
|
7
|
+
})(QPQXStateConfigSettingType || (exports.QPQXStateConfigSettingType = QPQXStateConfigSettingType = {}));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./QPQConfig"), exports);
|
|
18
|
+
__exportStar(require("./qpqXStateConfigUtils"), exports);
|
|
19
|
+
__exportStar(require("./settings"), exports);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { QPQConfig } from 'quidproquo-core';
|
|
2
|
+
import { StateMachineQPQConfigSetting } from './settings/stateMachine';
|
|
3
|
+
export declare const getAllStateMachines: (qpqConfig: QPQConfig) => StateMachineQPQConfigSetting[];
|
|
4
|
+
export declare const getStateMachineByName: (qpqConfig: QPQConfig, stateMachineName: string) => StateMachineQPQConfigSetting | undefined;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStateMachineByName = exports.getAllStateMachines = void 0;
|
|
4
|
+
const quidproquo_core_1 = require("quidproquo-core");
|
|
5
|
+
const QPQConfig_1 = require("./QPQConfig");
|
|
6
|
+
const getAllStateMachines = (qpqConfig) => {
|
|
7
|
+
return quidproquo_core_1.qpqCoreUtils.getConfigSettings(qpqConfig, QPQConfig_1.QPQXStateConfigSettingType.StateMachine);
|
|
8
|
+
};
|
|
9
|
+
exports.getAllStateMachines = getAllStateMachines;
|
|
10
|
+
const getStateMachineByName = (qpqConfig, stateMachineName) => {
|
|
11
|
+
return (0, exports.getAllStateMachines)(qpqConfig).find((sm) => sm.stateMachineName === stateMachineName);
|
|
12
|
+
};
|
|
13
|
+
exports.getStateMachineByName = getStateMachineByName;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './stateMachine';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./stateMachine"), exports);
|