xstate 6.0.0-alpha.39 → 6.0.0-alpha.40
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/dist/{StateMachine-94084ac7.cjs.js → StateMachine-1464d5e4.cjs.js} +132 -0
- package/dist/{StateMachine-2f47d890.esm.js → StateMachine-6082c3cf.esm.js} +132 -0
- package/dist/{StateMachine-144215d2.development.cjs.js → StateMachine-76ee1d6e.development.cjs.js} +132 -0
- package/dist/{StateMachine-da29dd2e.development.esm.js → StateMachine-f8c7feca.development.esm.js} +132 -0
- package/dist/declarations/src/StateMachine.d.ts +9 -1
- package/dist/declarations/src/createActor.d.ts +2 -2
- package/dist/declarations/src/index.d.ts +1 -1
- package/dist/declarations/src/machineVersion.types.d.ts +37 -0
- package/dist/declarations/src/machineVersions.d.ts +58 -28
- package/dist/declarations/src/types.d.ts +19 -3
- package/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/xstate-durable.umd.min.js.map +1 -1
- package/dist/xstate-graph.cjs.js +1 -1
- package/dist/xstate-graph.development.cjs.js +1 -1
- package/dist/xstate-graph.development.esm.js +1 -1
- package/dist/xstate-graph.esm.js +1 -1
- package/dist/xstate-graph.umd.min.js +1 -1
- package/dist/xstate-graph.umd.min.js.map +1 -1
- package/dist/xstate.cjs.js +71 -55
- package/dist/xstate.development.cjs.js +71 -55
- package/dist/xstate.development.esm.js +72 -56
- package/dist/xstate.esm.js +72 -56
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { T as TimeoutError, c as createAsyncLogic, a as createCallbackLogic, b as createEmptyActor, d as createEventObservableLogic, e as createLogic, f as createObservableLogic } from './index-17f889df.development.esm.js';
|
|
2
2
|
import { m as matchesEventDescriptor, t as toArray$1, b as builtInActions, X as XSTATE_INIT, f as finalizeTransitionResult, d as deriveDeferredStarts, c as createTransitionEnqueue, w as withActorScope, a as XSTATE_STOP, r as resolveActionsWithContext, e as XSTATE_TIMER, g as createSendToEffect, i as isLazyActorScope, p as parseDelayToMilliseconds, h as isAtomicStateNode, j as createActor } from './createActor-2a03d4b2.development.esm.js';
|
|
3
3
|
export { A as Actor, x as __unsafe_getAllOwnEventDescriptors, z as checkStateIn, j as createActor, k as createListenerLogic, l as createSubscriptionLogic, D as executeEffects, o as getInitialSnapshot, q as getNextSnapshot, v as getStateNodes, E as isBuiltInExecutableAction, u as isMachineSnapshot, n as listenerLogic, y as matchesState, B as pathToStateValue, s as subscriptionLogic, C as toObserver } from './createActor-2a03d4b2.development.esm.js';
|
|
4
|
-
import { S as StateMachine } from './StateMachine-
|
|
5
|
-
export { S as StateMachine, a as StateNode } from './StateMachine-
|
|
4
|
+
import { S as StateMachine } from './StateMachine-f8c7feca.development.esm.js';
|
|
5
|
+
export { S as StateMachine, a as StateNode } from './StateMachine-f8c7feca.development.esm.js';
|
|
6
6
|
export { a as getInitialMicrosteps, g as getMicrosteps, b as getNextTransitions, i as initialTransition, t as transition } from './transition-f581ffa6.development.esm.js';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -1967,6 +1967,15 @@ function mapState(snapshot, mapper) {
|
|
|
1967
1967
|
function isObject(value) {
|
|
1968
1968
|
return value !== null && typeof value === 'object';
|
|
1969
1969
|
}
|
|
1970
|
+
function isVersionedStateMachine(entry) {
|
|
1971
|
+
return 'transition' in entry && typeof entry.transition === 'function';
|
|
1972
|
+
}
|
|
1973
|
+
function hasSnapshotSchema(entry) {
|
|
1974
|
+
return 'snapshotSchema' in entry && entry.snapshotSchema !== undefined;
|
|
1975
|
+
}
|
|
1976
|
+
function hasEventSchema(entry) {
|
|
1977
|
+
return 'eventSchema' in entry && entry.eventSchema !== undefined;
|
|
1978
|
+
}
|
|
1970
1979
|
function getSnapshotSource(raw, defaultId) {
|
|
1971
1980
|
if (!isObject(raw)) {
|
|
1972
1981
|
return {};
|
|
@@ -1993,65 +2002,57 @@ async function validate(schema, value, description) {
|
|
|
1993
2002
|
return result.value;
|
|
1994
2003
|
}
|
|
1995
2004
|
async function finalizeSnapshot(snapshot, target) {
|
|
1996
|
-
|
|
1997
|
-
return {
|
|
2005
|
+
return await validate(target.snapshotSchema, {
|
|
1998
2006
|
...snapshot,
|
|
1999
|
-
context,
|
|
2000
2007
|
machine: {
|
|
2001
2008
|
id: target.id,
|
|
2002
2009
|
version: target.version
|
|
2003
2010
|
},
|
|
2004
2011
|
version: target.version
|
|
2005
|
-
};
|
|
2012
|
+
}, `snapshot for machine '${target.id}' version '${target.version}'`);
|
|
2006
2013
|
}
|
|
2007
|
-
async function validateEvents(events,
|
|
2014
|
+
async function validateEvents(events, source) {
|
|
2008
2015
|
return Promise.all(events.map(async (event, index) => {
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
throw new Error(`
|
|
2017
|
-
}
|
|
2018
|
-
if (!schema) {
|
|
2019
|
-
return event;
|
|
2016
|
+
const result = await source.eventSchema['~standard'].validate(event);
|
|
2017
|
+
if (result.issues) {
|
|
2018
|
+
const message = result.issues[0]?.message;
|
|
2019
|
+
if (message?.startsWith('Unknown event ')) {
|
|
2020
|
+
throw new Error(message);
|
|
2021
|
+
}
|
|
2022
|
+
const type = isObject(event) ? event.type : undefined;
|
|
2023
|
+
throw new Error(typeof type === 'string' ? `Invalid event '${type}' at index ${index}${message ? `: ${message}` : '.'}` : `Invalid event at index ${index}${message ? `: ${message}` : '.'}`);
|
|
2020
2024
|
}
|
|
2021
|
-
const
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
} = event;
|
|
2025
|
-
const validatedPayload = await validate(schema, payload, `event '${type}' at index ${index}`);
|
|
2026
|
-
if (!isObject(validatedPayload)) {
|
|
2027
|
-
throw new Error(`Invalid event '${type}' at index ${index}.`);
|
|
2025
|
+
const validated = result.value;
|
|
2026
|
+
if (!isObject(validated) || typeof validated.type !== 'string') {
|
|
2027
|
+
throw new Error(`Invalid event at index ${index}.`);
|
|
2028
2028
|
}
|
|
2029
|
-
return
|
|
2030
|
-
...validatedPayload,
|
|
2031
|
-
type
|
|
2032
|
-
};
|
|
2029
|
+
return validated;
|
|
2033
2030
|
}));
|
|
2034
2031
|
}
|
|
2035
2032
|
|
|
2036
|
-
/** Creates migration and adaptation utilities
|
|
2037
|
-
function machineVersions(
|
|
2033
|
+
/** Creates migration and adaptation utilities for versions of one machine. */
|
|
2034
|
+
function machineVersions(entries, options) {
|
|
2038
2035
|
const byIdentity = new Map();
|
|
2039
|
-
const
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2036
|
+
const machinesByIdentity = new Map();
|
|
2037
|
+
const machineId = entries[0].id;
|
|
2038
|
+
for (const entry of entries) {
|
|
2039
|
+
if (entry.version === undefined) {
|
|
2040
|
+
throw new Error(`Machine '${entry.id}' must define a version.`);
|
|
2043
2041
|
}
|
|
2044
|
-
if (
|
|
2042
|
+
if (entry.version === '*') {
|
|
2045
2043
|
throw new Error("Machine version '*' is reserved for wildcard migrations.");
|
|
2046
2044
|
}
|
|
2047
|
-
if (
|
|
2048
|
-
throw new Error(`Machine '${
|
|
2045
|
+
if (entry.id !== machineId) {
|
|
2046
|
+
throw new Error(`Machine '${entry.id}' does not match machine ID '${machineId}'.`);
|
|
2049
2047
|
}
|
|
2050
|
-
const key = `${
|
|
2048
|
+
const key = `${entry.id}\0${entry.version}`;
|
|
2051
2049
|
if (byIdentity.has(key)) {
|
|
2052
|
-
throw new Error(`Duplicate machine identity '${
|
|
2050
|
+
throw new Error(`Duplicate machine identity '${entry.id}' version '${entry.version}'.`);
|
|
2051
|
+
}
|
|
2052
|
+
byIdentity.set(key, entry);
|
|
2053
|
+
if (isVersionedStateMachine(entry)) {
|
|
2054
|
+
machinesByIdentity.set(key, entry);
|
|
2053
2055
|
}
|
|
2054
|
-
byIdentity.set(key, machine);
|
|
2055
2056
|
}
|
|
2056
2057
|
if (options?.unversioned !== undefined && !byIdentity.has(`${machineId}\0${options.unversioned}`)) {
|
|
2057
2058
|
throw new Error(`Unversioned snapshot version '${options.unversioned}' is not retained for machine '${machineId}'.`);
|
|
@@ -2085,16 +2086,21 @@ function machineVersions(machines, options) {
|
|
|
2085
2086
|
if (raw.version !== undefined && raw.version !== version) {
|
|
2086
2087
|
throw new Error(`Persisted snapshot version '${raw.version}' conflicts with machine version '${version}'.`);
|
|
2087
2088
|
}
|
|
2088
|
-
const
|
|
2089
|
-
if (!
|
|
2089
|
+
const source = byIdentity.get(`${id}\0${version}`);
|
|
2090
|
+
if (!source) {
|
|
2090
2091
|
throw new Error(`Unknown machine identity '${id}' version '${version}'.`);
|
|
2091
2092
|
}
|
|
2092
|
-
|
|
2093
|
+
if (!hasSnapshotSchema(source)) {
|
|
2094
|
+
throw new Error(`Machine version '${version}' does not define a snapshot schema.`);
|
|
2095
|
+
}
|
|
2096
|
+
const snapshot = await validate(source.snapshotSchema, raw, `snapshot for machine '${id}' version '${version}'`);
|
|
2093
2097
|
return {
|
|
2094
|
-
|
|
2098
|
+
source,
|
|
2099
|
+
...(isVersionedStateMachine(source) && {
|
|
2100
|
+
machine: source
|
|
2101
|
+
}),
|
|
2095
2102
|
snapshot: {
|
|
2096
|
-
...
|
|
2097
|
-
context,
|
|
2103
|
+
...snapshot,
|
|
2098
2104
|
machine: {
|
|
2099
2105
|
id,
|
|
2100
2106
|
version
|
|
@@ -2105,15 +2111,16 @@ function machineVersions(machines, options) {
|
|
|
2105
2111
|
return {
|
|
2106
2112
|
parseSnapshot,
|
|
2107
2113
|
async adaptEvents(events, adaptationOptions) {
|
|
2108
|
-
const target =
|
|
2114
|
+
const target = machinesByIdentity.get(`${machineId}\0${adaptationOptions.to}`);
|
|
2109
2115
|
if (!target) {
|
|
2110
|
-
throw new Error(`Target version '${adaptationOptions.to}' is not
|
|
2116
|
+
throw new Error(`Target version '${adaptationOptions.to}' is not backed by a machine for '${machineId}'.`);
|
|
2111
2117
|
}
|
|
2112
2118
|
const sourceId = adaptationOptions.from.id ?? machineId;
|
|
2113
2119
|
const source = byIdentity.get(`${sourceId}\0${adaptationOptions.from.version}`);
|
|
2114
2120
|
let sourceError;
|
|
2115
2121
|
let sourceValidationFailed = false;
|
|
2116
|
-
|
|
2122
|
+
const hasTypedSource = source && hasEventSchema(source);
|
|
2123
|
+
if (hasTypedSource) {
|
|
2117
2124
|
let sourceEvents;
|
|
2118
2125
|
try {
|
|
2119
2126
|
sourceEvents = await validateEvents(events, source);
|
|
@@ -2135,7 +2142,7 @@ function machineVersions(machines, options) {
|
|
|
2135
2142
|
if (wildcardAdapter) {
|
|
2136
2143
|
return validateEvents(await wildcardAdapter(events, adaptationOptions.from), target);
|
|
2137
2144
|
}
|
|
2138
|
-
if (
|
|
2145
|
+
if (hasTypedSource) {
|
|
2139
2146
|
if (sourceValidationFailed) {
|
|
2140
2147
|
throw sourceError instanceof Error ? sourceError : new Error(`Unable to validate event history for machine '${source.id}' version '${source.version}'.`, {
|
|
2141
2148
|
cause: sourceError
|
|
@@ -2143,12 +2150,15 @@ function machineVersions(machines, options) {
|
|
|
2143
2150
|
}
|
|
2144
2151
|
throw new Error(`No event adapter from version '${source.version}' to '${target.version}' for machine '${target.id}'.`);
|
|
2145
2152
|
}
|
|
2153
|
+
if (source) {
|
|
2154
|
+
throw new Error(`Machine version '${source.version}' does not define an event schema; only the '*' adapter can handle its event history.`);
|
|
2155
|
+
}
|
|
2146
2156
|
throw new Error(`Unknown event history source '${sourceId}' version '${adaptationOptions.from.version}'.`);
|
|
2147
2157
|
},
|
|
2148
2158
|
async migrateSnapshot(raw, migrationOptions) {
|
|
2149
|
-
const target =
|
|
2159
|
+
const target = machinesByIdentity.get(`${machineId}\0${migrationOptions.to}`);
|
|
2150
2160
|
if (!target) {
|
|
2151
|
-
throw new Error(`Target version '${migrationOptions.to}' is not
|
|
2161
|
+
throw new Error(`Target version '${migrationOptions.to}' is not backed by a machine for '${machineId}'.`);
|
|
2152
2162
|
}
|
|
2153
2163
|
let source;
|
|
2154
2164
|
let parseError;
|
|
@@ -2158,10 +2168,10 @@ function machineVersions(machines, options) {
|
|
|
2158
2168
|
parseError = error;
|
|
2159
2169
|
}
|
|
2160
2170
|
if (source) {
|
|
2161
|
-
if (source.
|
|
2171
|
+
if (source.source.version === target.version) {
|
|
2162
2172
|
return finalizeSnapshot(source.snapshot, target);
|
|
2163
2173
|
}
|
|
2164
|
-
const exactMigration = migrationOptions.migrations[source.
|
|
2174
|
+
const exactMigration = migrationOptions.migrations[source.source.version];
|
|
2165
2175
|
if (exactMigration) {
|
|
2166
2176
|
return finalizeSnapshot(await exactMigration(source.snapshot), target);
|
|
2167
2177
|
}
|
|
@@ -2169,7 +2179,7 @@ function machineVersions(machines, options) {
|
|
|
2169
2179
|
const wildcardMigration = migrationOptions.migrations['*'];
|
|
2170
2180
|
if (!wildcardMigration) {
|
|
2171
2181
|
if (source) {
|
|
2172
|
-
throw new Error(`No snapshot migration from version '${source.
|
|
2182
|
+
throw new Error(`No snapshot migration from version '${source.source.version}' to '${target.version}' for machine '${target.id}'.`);
|
|
2173
2183
|
}
|
|
2174
2184
|
throw parseError;
|
|
2175
2185
|
}
|
|
@@ -2631,6 +2641,12 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
|
|
|
2631
2641
|
|
|
2632
2642
|
/** A persisted snapshot tied to a versioned actor logic identity. */
|
|
2633
2643
|
|
|
2644
|
+
/**
|
|
2645
|
+
* A persisted snapshot restorable into the given actor logic: any snapshot
|
|
2646
|
+
* created by logic with the same machine `id` is accepted, regardless of
|
|
2647
|
+
* version (version mismatches are handled at runtime, e.g. by `migrate`).
|
|
2648
|
+
*/
|
|
2649
|
+
|
|
2634
2650
|
/** @deprecated Use `AnyActor` instead. */
|
|
2635
2651
|
|
|
2636
2652
|
// Based on RxJS types
|
package/dist/xstate.esm.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { T as TimeoutError, c as createAsyncLogic, a as createCallbackLogic, b as createEmptyActor, d as createEventObservableLogic, e as createLogic, f as createObservableLogic } from './index-7020402e.esm.js';
|
|
2
2
|
import { m as matchesEventDescriptor, t as toArray$1, b as builtInActions, X as XSTATE_INIT, f as finalizeTransitionResult, d as deriveDeferredStarts, c as createTransitionEnqueue, w as withActorScope, a as XSTATE_STOP, r as resolveActionsWithContext, e as XSTATE_TIMER, g as createSendToEffect, i as isLazyActorScope, p as parseDelayToMilliseconds, h as isAtomicStateNode, j as createActor } from './createActor-a211c276.esm.js';
|
|
3
3
|
export { A as Actor, x as __unsafe_getAllOwnEventDescriptors, z as checkStateIn, j as createActor, k as createListenerLogic, l as createSubscriptionLogic, D as executeEffects, o as getInitialSnapshot, q as getNextSnapshot, v as getStateNodes, E as isBuiltInExecutableAction, u as isMachineSnapshot, n as listenerLogic, y as matchesState, B as pathToStateValue, s as subscriptionLogic, C as toObserver } from './createActor-a211c276.esm.js';
|
|
4
|
-
import { S as StateMachine } from './StateMachine-
|
|
5
|
-
export { S as StateMachine, a as StateNode } from './StateMachine-
|
|
4
|
+
import { S as StateMachine } from './StateMachine-6082c3cf.esm.js';
|
|
5
|
+
export { S as StateMachine, a as StateNode } from './StateMachine-6082c3cf.esm.js';
|
|
6
6
|
export { a as getInitialMicrosteps, g as getMicrosteps, b as getNextTransitions, i as initialTransition, t as transition } from './transition-e8a13c00.esm.js';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -1967,6 +1967,15 @@ function mapState(snapshot, mapper) {
|
|
|
1967
1967
|
function isObject(value) {
|
|
1968
1968
|
return value !== null && typeof value === 'object';
|
|
1969
1969
|
}
|
|
1970
|
+
function isVersionedStateMachine(entry) {
|
|
1971
|
+
return 'transition' in entry && typeof entry.transition === 'function';
|
|
1972
|
+
}
|
|
1973
|
+
function hasSnapshotSchema(entry) {
|
|
1974
|
+
return 'snapshotSchema' in entry && entry.snapshotSchema !== undefined;
|
|
1975
|
+
}
|
|
1976
|
+
function hasEventSchema(entry) {
|
|
1977
|
+
return 'eventSchema' in entry && entry.eventSchema !== undefined;
|
|
1978
|
+
}
|
|
1970
1979
|
function getSnapshotSource(raw, defaultId) {
|
|
1971
1980
|
if (!isObject(raw)) {
|
|
1972
1981
|
return {};
|
|
@@ -1993,65 +2002,57 @@ async function validate(schema, value, description) {
|
|
|
1993
2002
|
return result.value;
|
|
1994
2003
|
}
|
|
1995
2004
|
async function finalizeSnapshot(snapshot, target) {
|
|
1996
|
-
|
|
1997
|
-
return {
|
|
2005
|
+
return await validate(target.snapshotSchema, {
|
|
1998
2006
|
...snapshot,
|
|
1999
|
-
context,
|
|
2000
2007
|
machine: {
|
|
2001
2008
|
id: target.id,
|
|
2002
2009
|
version: target.version
|
|
2003
2010
|
},
|
|
2004
2011
|
version: target.version
|
|
2005
|
-
};
|
|
2012
|
+
}, `snapshot for machine '${target.id}' version '${target.version}'`);
|
|
2006
2013
|
}
|
|
2007
|
-
async function validateEvents(events,
|
|
2014
|
+
async function validateEvents(events, source) {
|
|
2008
2015
|
return Promise.all(events.map(async (event, index) => {
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
throw new Error(`
|
|
2017
|
-
}
|
|
2018
|
-
if (!schema) {
|
|
2019
|
-
return event;
|
|
2016
|
+
const result = await source.eventSchema['~standard'].validate(event);
|
|
2017
|
+
if (result.issues) {
|
|
2018
|
+
const message = result.issues[0]?.message;
|
|
2019
|
+
if (message?.startsWith('Unknown event ')) {
|
|
2020
|
+
throw new Error(message);
|
|
2021
|
+
}
|
|
2022
|
+
const type = isObject(event) ? event.type : undefined;
|
|
2023
|
+
throw new Error(typeof type === 'string' ? `Invalid event '${type}' at index ${index}${message ? `: ${message}` : '.'}` : `Invalid event at index ${index}${message ? `: ${message}` : '.'}`);
|
|
2020
2024
|
}
|
|
2021
|
-
const
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
} = event;
|
|
2025
|
-
const validatedPayload = await validate(schema, payload, `event '${type}' at index ${index}`);
|
|
2026
|
-
if (!isObject(validatedPayload)) {
|
|
2027
|
-
throw new Error(`Invalid event '${type}' at index ${index}.`);
|
|
2025
|
+
const validated = result.value;
|
|
2026
|
+
if (!isObject(validated) || typeof validated.type !== 'string') {
|
|
2027
|
+
throw new Error(`Invalid event at index ${index}.`);
|
|
2028
2028
|
}
|
|
2029
|
-
return
|
|
2030
|
-
...validatedPayload,
|
|
2031
|
-
type
|
|
2032
|
-
};
|
|
2029
|
+
return validated;
|
|
2033
2030
|
}));
|
|
2034
2031
|
}
|
|
2035
2032
|
|
|
2036
|
-
/** Creates migration and adaptation utilities
|
|
2037
|
-
function machineVersions(
|
|
2033
|
+
/** Creates migration and adaptation utilities for versions of one machine. */
|
|
2034
|
+
function machineVersions(entries, options) {
|
|
2038
2035
|
const byIdentity = new Map();
|
|
2039
|
-
const
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2036
|
+
const machinesByIdentity = new Map();
|
|
2037
|
+
const machineId = entries[0].id;
|
|
2038
|
+
for (const entry of entries) {
|
|
2039
|
+
if (entry.version === undefined) {
|
|
2040
|
+
throw new Error(`Machine '${entry.id}' must define a version.`);
|
|
2043
2041
|
}
|
|
2044
|
-
if (
|
|
2042
|
+
if (entry.version === '*') {
|
|
2045
2043
|
throw new Error("Machine version '*' is reserved for wildcard migrations.");
|
|
2046
2044
|
}
|
|
2047
|
-
if (
|
|
2048
|
-
throw new Error(`Machine '${
|
|
2045
|
+
if (entry.id !== machineId) {
|
|
2046
|
+
throw new Error(`Machine '${entry.id}' does not match machine ID '${machineId}'.`);
|
|
2049
2047
|
}
|
|
2050
|
-
const key = `${
|
|
2048
|
+
const key = `${entry.id}\0${entry.version}`;
|
|
2051
2049
|
if (byIdentity.has(key)) {
|
|
2052
|
-
throw new Error(`Duplicate machine identity '${
|
|
2050
|
+
throw new Error(`Duplicate machine identity '${entry.id}' version '${entry.version}'.`);
|
|
2051
|
+
}
|
|
2052
|
+
byIdentity.set(key, entry);
|
|
2053
|
+
if (isVersionedStateMachine(entry)) {
|
|
2054
|
+
machinesByIdentity.set(key, entry);
|
|
2053
2055
|
}
|
|
2054
|
-
byIdentity.set(key, machine);
|
|
2055
2056
|
}
|
|
2056
2057
|
if (options?.unversioned !== undefined && !byIdentity.has(`${machineId}\0${options.unversioned}`)) {
|
|
2057
2058
|
throw new Error(`Unversioned snapshot version '${options.unversioned}' is not retained for machine '${machineId}'.`);
|
|
@@ -2085,16 +2086,21 @@ function machineVersions(machines, options) {
|
|
|
2085
2086
|
if (raw.version !== undefined && raw.version !== version) {
|
|
2086
2087
|
throw new Error(`Persisted snapshot version '${raw.version}' conflicts with machine version '${version}'.`);
|
|
2087
2088
|
}
|
|
2088
|
-
const
|
|
2089
|
-
if (!
|
|
2089
|
+
const source = byIdentity.get(`${id}\0${version}`);
|
|
2090
|
+
if (!source) {
|
|
2090
2091
|
throw new Error(`Unknown machine identity '${id}' version '${version}'.`);
|
|
2091
2092
|
}
|
|
2092
|
-
|
|
2093
|
+
if (!hasSnapshotSchema(source)) {
|
|
2094
|
+
throw new Error(`Machine version '${version}' does not define a snapshot schema.`);
|
|
2095
|
+
}
|
|
2096
|
+
const snapshot = await validate(source.snapshotSchema, raw, `snapshot for machine '${id}' version '${version}'`);
|
|
2093
2097
|
return {
|
|
2094
|
-
|
|
2098
|
+
source,
|
|
2099
|
+
...(isVersionedStateMachine(source) && {
|
|
2100
|
+
machine: source
|
|
2101
|
+
}),
|
|
2095
2102
|
snapshot: {
|
|
2096
|
-
...
|
|
2097
|
-
context,
|
|
2103
|
+
...snapshot,
|
|
2098
2104
|
machine: {
|
|
2099
2105
|
id,
|
|
2100
2106
|
version
|
|
@@ -2105,15 +2111,16 @@ function machineVersions(machines, options) {
|
|
|
2105
2111
|
return {
|
|
2106
2112
|
parseSnapshot,
|
|
2107
2113
|
async adaptEvents(events, adaptationOptions) {
|
|
2108
|
-
const target =
|
|
2114
|
+
const target = machinesByIdentity.get(`${machineId}\0${adaptationOptions.to}`);
|
|
2109
2115
|
if (!target) {
|
|
2110
|
-
throw new Error(`Target version '${adaptationOptions.to}' is not
|
|
2116
|
+
throw new Error(`Target version '${adaptationOptions.to}' is not backed by a machine for '${machineId}'.`);
|
|
2111
2117
|
}
|
|
2112
2118
|
const sourceId = adaptationOptions.from.id ?? machineId;
|
|
2113
2119
|
const source = byIdentity.get(`${sourceId}\0${adaptationOptions.from.version}`);
|
|
2114
2120
|
let sourceError;
|
|
2115
2121
|
let sourceValidationFailed = false;
|
|
2116
|
-
|
|
2122
|
+
const hasTypedSource = source && hasEventSchema(source);
|
|
2123
|
+
if (hasTypedSource) {
|
|
2117
2124
|
let sourceEvents;
|
|
2118
2125
|
try {
|
|
2119
2126
|
sourceEvents = await validateEvents(events, source);
|
|
@@ -2135,7 +2142,7 @@ function machineVersions(machines, options) {
|
|
|
2135
2142
|
if (wildcardAdapter) {
|
|
2136
2143
|
return validateEvents(await wildcardAdapter(events, adaptationOptions.from), target);
|
|
2137
2144
|
}
|
|
2138
|
-
if (
|
|
2145
|
+
if (hasTypedSource) {
|
|
2139
2146
|
if (sourceValidationFailed) {
|
|
2140
2147
|
throw sourceError instanceof Error ? sourceError : new Error(`Unable to validate event history for machine '${source.id}' version '${source.version}'.`, {
|
|
2141
2148
|
cause: sourceError
|
|
@@ -2143,12 +2150,15 @@ function machineVersions(machines, options) {
|
|
|
2143
2150
|
}
|
|
2144
2151
|
throw new Error(`No event adapter from version '${source.version}' to '${target.version}' for machine '${target.id}'.`);
|
|
2145
2152
|
}
|
|
2153
|
+
if (source) {
|
|
2154
|
+
throw new Error(`Machine version '${source.version}' does not define an event schema; only the '*' adapter can handle its event history.`);
|
|
2155
|
+
}
|
|
2146
2156
|
throw new Error(`Unknown event history source '${sourceId}' version '${adaptationOptions.from.version}'.`);
|
|
2147
2157
|
},
|
|
2148
2158
|
async migrateSnapshot(raw, migrationOptions) {
|
|
2149
|
-
const target =
|
|
2159
|
+
const target = machinesByIdentity.get(`${machineId}\0${migrationOptions.to}`);
|
|
2150
2160
|
if (!target) {
|
|
2151
|
-
throw new Error(`Target version '${migrationOptions.to}' is not
|
|
2161
|
+
throw new Error(`Target version '${migrationOptions.to}' is not backed by a machine for '${machineId}'.`);
|
|
2152
2162
|
}
|
|
2153
2163
|
let source;
|
|
2154
2164
|
let parseError;
|
|
@@ -2158,10 +2168,10 @@ function machineVersions(machines, options) {
|
|
|
2158
2168
|
parseError = error;
|
|
2159
2169
|
}
|
|
2160
2170
|
if (source) {
|
|
2161
|
-
if (source.
|
|
2171
|
+
if (source.source.version === target.version) {
|
|
2162
2172
|
return finalizeSnapshot(source.snapshot, target);
|
|
2163
2173
|
}
|
|
2164
|
-
const exactMigration = migrationOptions.migrations[source.
|
|
2174
|
+
const exactMigration = migrationOptions.migrations[source.source.version];
|
|
2165
2175
|
if (exactMigration) {
|
|
2166
2176
|
return finalizeSnapshot(await exactMigration(source.snapshot), target);
|
|
2167
2177
|
}
|
|
@@ -2169,7 +2179,7 @@ function machineVersions(machines, options) {
|
|
|
2169
2179
|
const wildcardMigration = migrationOptions.migrations['*'];
|
|
2170
2180
|
if (!wildcardMigration) {
|
|
2171
2181
|
if (source) {
|
|
2172
|
-
throw new Error(`No snapshot migration from version '${source.
|
|
2182
|
+
throw new Error(`No snapshot migration from version '${source.source.version}' to '${target.version}' for machine '${target.id}'.`);
|
|
2173
2183
|
}
|
|
2174
2184
|
throw parseError;
|
|
2175
2185
|
}
|
|
@@ -2631,6 +2641,12 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
|
|
|
2631
2641
|
|
|
2632
2642
|
/** A persisted snapshot tied to a versioned actor logic identity. */
|
|
2633
2643
|
|
|
2644
|
+
/**
|
|
2645
|
+
* A persisted snapshot restorable into the given actor logic: any snapshot
|
|
2646
|
+
* created by logic with the same machine `id` is accepted, regardless of
|
|
2647
|
+
* version (version mismatches are handled at runtime, e.g. by `migrate`).
|
|
2648
|
+
*/
|
|
2649
|
+
|
|
2634
2650
|
/** @deprecated Use `AnyActor` instead. */
|
|
2635
2651
|
|
|
2636
2652
|
// Based on RxJS types
|