in src/common/quick_chat/utils.ts [119:165]
export function subscribeToStateChanges({
actorTitle,
actor,
callback,
}: {
actorTitle: string;
actor: Actor<UnknownActorLogic>;
callback?: (snapshot: { value: unknown }) => void;
}) {
let previousState = actor.getSnapshot().value;
const subscription = actor.subscribe(snapshot => {
if (previousState !== snapshot.value) {
if (typeof previousState === 'string') {
log.debug(`[${actorTitle}]: state changed from "${previousState}" to "${snapshot.value}"`);
} else {
// if actor is a parallel state machine, state is returned as an object
const stateChangeLines: string[] = [];
const allKeys = [
...new Set([...Object.keys(previousState), ...Object.keys(snapshot.value)]),
];
allKeys.forEach(key => {
const prevValue = previousState[key];
const currentValue = snapshot.value[key];
// Only show properties that changed
if (prevValue !== currentValue) {
stateChangeLines.push(`[${key}]: "${prevValue}" to "${currentValue}"`);
}
});
if (stateChangeLines.length > 0) {
const formattedChanges = stateChangeLines.join(',\n');
log.debug(`[${actorTitle}]: State changes: ${formattedChanges}`);
}
}
previousState = snapshot.value;
callback?.(snapshot);
}
});
// return a function to unsubscribe
return () => {
subscription.unsubscribe();
};
}