Skip to content

Commit

Permalink
Remove TPersisted generic (#4390)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored Oct 26, 2023
1 parent eea74c5 commit bd22b14
Show file tree
Hide file tree
Showing 25 changed files with 87 additions and 278 deletions.
41 changes: 9 additions & 32 deletions packages/core/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import type {
EventObject,
HistoryValue,
MachineContext,
PersistedMachineState,
Prop,
StateConfig,
StateValue,
TODO,
AnyActorRef,
Compute,
EventDescriptor
EventDescriptor,
Snapshot
} from './types.ts';
import { flatten, matchesState } from './utils.ts';

Expand Down Expand Up @@ -300,53 +300,30 @@ export function getPersistedState<
TOutput,
TResolvedTypesMeta
>
): PersistedMachineState<
TContext,
TEvent,
TActor,
TTag,
TOutput,
TResolvedTypesMeta
> {
): Snapshot<unknown> {
const { configuration, tags, machine, children, context, ...jsonValues } =
state;

const childrenJson: Partial<
PersistedMachineState<
TContext,
TEvent,
TActor,
TTag,
TOutput,
TResolvedTypesMeta
>['children']
> = {};
const childrenJson: Record<string, unknown> = {};

for (const id in children) {
const child = children[id] as any;
if (isDevelopment && typeof child.src !== 'string') {
throw new Error('An inline child actor cannot be persisted.');
}
childrenJson[id as keyof typeof childrenJson] = {
state: child.getPersistedState?.(),
state: child.getPersistedState(),
src: child.src
};
}

return {
const persisted = {
...jsonValues,
// TODO: this makes `PersistedMachineState`'s type kind of a lie
// it doesn't truly use `TContext` but rather some kind of a derived form of it
context: persistContext(context) as any,
children: childrenJson
} as PersistedMachineState<
TContext,
TEvent,
TActor,
TTag,
TOutput,
TResolvedTypesMeta
>;
};

return persisted;
}

function persistContext(contextPart: Record<string, unknown>) {
Expand Down
40 changes: 11 additions & 29 deletions packages/core/src/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ import type {
StateMachineDefinition,
StateValue,
TransitionDefinition,
PersistedMachineState,
ParameterizedObject,
AnyActorContext,
AnyEventObject,
ProvidedActor,
AnyActorRef,
Equals,
TODO,
SnapshotFrom
SnapshotFrom,
Snapshot
} from './types.ts';
import { isErrorActorEvent, resolveReferencedActor } from './utils.ts';
import { $$ACTOR_TYPE, createActor } from './interpreter.ts';
Expand Down Expand Up @@ -114,14 +114,6 @@ export class StateMachine<
>,
TEvent,
TInput,
PersistedMachineState<
TContext,
TEvent,
TActor,
TTag,
TOutput,
TResolvedTypesMeta
>,
TODO
>
{
Expand Down Expand Up @@ -541,14 +533,7 @@ export class StateMachine<
TOutput,
TResolvedTypesMeta
>
): PersistedMachineState<
TContext,
TEvent,
TActor,
TTag,
TOutput,
TResolvedTypesMeta
> {
) {
return getPersistedState(state);
}

Expand Down Expand Up @@ -577,14 +562,7 @@ export class StateMachine<
}

public restoreState(
snapshot: PersistedMachineState<
TContext,
TEvent,
TActor,
TTag,
TOutput,
TResolvedTypesMeta
>,
snapshot: Snapshot<unknown>,
_actorCtx: ActorContext<
MachineSnapshot<
TContext,
Expand All @@ -605,10 +583,14 @@ export class StateMachine<
TResolvedTypesMeta
> {
const children: Record<string, AnyActorRef> = {};
const snapshotChildren: Record<
string,
{ src: string; state: Snapshot<unknown> }
> = (snapshot as any).children;

Object.keys(snapshot.children).forEach((actorId) => {
Object.keys(snapshotChildren).forEach((actorId) => {
const actorData =
snapshot.children[actorId as keyof typeof snapshot.children];
snapshotChildren[actorId as keyof typeof snapshotChildren];
const childState = actorData.state;
const src = actorData.src;

Expand All @@ -630,7 +612,7 @@ export class StateMachine<
});

const restoredSnapshot = this.createState(
new State({ ...snapshot, children }, this) as any
new State({ ...(snapshot as any), children }, this) as any
);

let seen = new Set();
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/actors/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
ActorSystem,
ActorRefFrom,
TODO,
Snapshot,
HomomorphicOmit
Snapshot
} from '../types';
import { XSTATE_STOP } from '../constants.ts';

Expand All @@ -24,7 +23,6 @@ export type CallbackActorLogic<
CallbackSnapshot<TInput, TEvent>,
TEvent,
TInput,
HomomorphicOmit<CallbackSnapshot<TInput, TEvent>, '_receivers' | '_dispose'>,
ActorSystem<any>
>;

Expand Down Expand Up @@ -120,7 +118,7 @@ export function fromCallback<TEvent extends EventObject, TInput = unknown>(
};
},
getPersistedState: ({ _dispose, _receivers, ...rest }) => rest,
restoreState: (state) => ({
restoreState: (state: any) => ({
_receivers: new Set(),
_dispose: undefined,
...state
Expand Down
13 changes: 3 additions & 10 deletions packages/core/src/actors/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
Subscription,
AnyActorSystem,
ActorRefFrom,
Snapshot,
HomomorphicOmit
Snapshot
} from '../types';

export type ObservableSnapshot<TContext, TInput> = Snapshot<undefined> & {
Expand All @@ -16,16 +15,10 @@ export type ObservableSnapshot<TContext, TInput> = Snapshot<undefined> & {
_subscription: Subscription | undefined;
};

export type ObservablePersistedState<TContext, TInput> = HomomorphicOmit<
ObservableSnapshot<TContext, TInput>,
'_subscription'
>;

export type ObservableActorLogic<TContext, TInput> = ActorLogic<
ObservableSnapshot<TContext, TInput>,
{ type: string; [k: string]: unknown },
TInput,
ObservablePersistedState<TContext, TInput>,
AnyActorSystem
>;

Expand Down Expand Up @@ -123,7 +116,7 @@ export function fromObservable<TContext, TInput>(
},
getPersistedState: ({ _subscription, ...state }) => state,
restoreState: (state) => ({
...state,
...(state as any),
_subscription: undefined
})
};
Expand Down Expand Up @@ -224,7 +217,7 @@ export function fromEventObservable<T extends EventObject, TInput>(
});
},
getPersistedState: ({ _subscription, ...state }) => state,
restoreState: (state) => ({
restoreState: (state: any) => ({
...state,
_subscription: undefined
})
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/actors/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export type PromiseActorLogic<TOutput, TInput = unknown> = ActorLogic<
PromiseSnapshot<TOutput, TInput>,
{ type: string; [k: string]: unknown },
TInput, // input
PromiseSnapshot<TOutput, TInput>, // persisted state
ActorSystem<any>
>;

Expand Down Expand Up @@ -120,7 +119,7 @@ export function fromPromise<TOutput, TInput = unknown>(
};
},
getPersistedState: (state) => state,
restoreState: (state) => state
restoreState: (state: any) => state
};

return logic;
Expand Down
10 changes: 2 additions & 8 deletions packages/core/src/actors/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ export type TransitionActorLogic<
TContext,
TEvent extends EventObject,
TInput
> = ActorLogic<
TransitionSnapshot<TContext>,
TEvent,
TInput,
TransitionSnapshot<TContext>,
AnyActorSystem
>;
> = ActorLogic<TransitionSnapshot<TContext>, TEvent, TInput, AnyActorSystem>;

export type TransitionActorRef<
TContext,
Expand Down Expand Up @@ -81,6 +75,6 @@ export function fromTransition<
};
},
getPersistedState: (state) => state,
restoreState: (state) => state
restoreState: (state: any) => state
};
}
8 changes: 4 additions & 4 deletions packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import type {
AnyActorLogic,
AnyStateMachine,
EventFromLogic,
PersistedStateFrom,
SnapshotFrom,
AnyActorRef,
DoneActorEvent
DoneActorEvent,
Snapshot
} from './types.ts';
import {
ActorRef,
Expand Down Expand Up @@ -543,8 +543,8 @@ export class Actor<TLogic extends AnyActorLogic>
};
}

public getPersistedState(): PersistedStateFrom<TLogic> | undefined {
return this.logic.getPersistedState?.(this._state);
public getPersistedState(): Snapshot<unknown> {
return this.logic.getPersistedState(this._state);
}

public [symbolObservable](): InteropSubscribable<SnapshotFrom<TLogic>> {
Expand Down
Loading

0 comments on commit bd22b14

Please sign in to comment.