(config: {
context: ((input: unknown) => StoreContext) | StoreContext;
schemas?: StoreSchemas<any, any, any>;
on: TransitionsFromEventPayloadMap<any, any, any>;
})
| 253 | FromStoreEmittedEvents<TEmittedPayloadMap, TEmittedSchemaMap> |
| 254 | >; |
| 255 | export function fromStore(config: { |
| 256 | context: ((input: unknown) => StoreContext) | StoreContext; |
| 257 | schemas?: StoreSchemas<any, any, any>; |
| 258 | on: TransitionsFromEventPayloadMap<any, any, any>; |
| 259 | }): StoreLogic<any, any, any, any> { |
| 260 | const initialContext = config.context; |
| 261 | const transition = createStoreTransition(config.on); |
| 262 | |
| 263 | return { |
| 264 | transition: (snapshot, event, actorScope) => { |
| 265 | const [nextSnapshot, effects] = transition(snapshot, event); |
| 266 | |
| 267 | // The actor only commits `nextSnapshot` after this function returns, so |
| 268 | // synchronous effects must read it directly; effects that run later |
| 269 | // (after an `await`) read the latest committed snapshot from the actor. |
| 270 | // This matches `createStore`, where `currentSnapshot` is updated before |
| 271 | // effects run. |
| 272 | let committed = false; |
| 273 | const effectEnqueue = { |
| 274 | send: (event: EventObject) => actorScope.self.send(event), |
| 275 | trigger: new Proxy({} as any, { |
| 276 | get: (_, eventType: string) => { |
| 277 | return (payload: any) => |
| 278 | actorScope.self.send({ type: eventType, ...payload }); |
| 279 | } |
| 280 | }), |
| 281 | getSnapshot: () => |
| 282 | committed ? actorScope.self.getSnapshot() : nextSnapshot |
| 283 | }; |
| 284 | |
| 285 | for (const effect of effects) { |
| 286 | if (typeof effect === 'function') { |
| 287 | effect(effectEnqueue); |
| 288 | } else { |
| 289 | actorScope.emit(effect); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | committed = true; |
| 294 | |
| 295 | return nextSnapshot; |
| 296 | }, |
| 297 | getInitialSnapshot: (_, input: unknown) => { |
| 298 | return { |
| 299 | status: 'active', |
| 300 | context: |
| 301 | typeof initialContext === 'function' |
| 302 | ? initialContext(input) |
| 303 | : initialContext, |
| 304 | output: undefined, |
| 305 | error: undefined |
| 306 | }; |
| 307 | }, |
| 308 | getPersistedSnapshot: (s: Snapshot<unknown>) => s, |
| 309 | restoreSnapshot: (s: Snapshot<unknown>) => s as StoreSnapshot<any> |
| 310 | }; |
| 311 | } |
no test coverage detected
searching dependent graphs…