Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(xstate): support getOptions #557

Merged
merged 3 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
}
},
"utils.js": {
"bundled": 13390,
"minified": 6561,
"gzipped": 2449,
"bundled": 13354,
"minified": 6528,
"gzipped": 2437,
"treeshaked": {
"rollup": {
"code": 28,
"import_statements": 28
},
"webpack": {
"code": 1289
"code": 1280
}
}
},
Expand Down Expand Up @@ -84,9 +84,9 @@
}
},
"xstate.js": {
"bundled": 2818,
"minified": 1279,
"gzipped": 633,
"bundled": 2977,
"minified": 1314,
"gzipped": 653,
"treeshaked": {
"rollup": {
"code": 29,
Expand Down
55 changes: 30 additions & 25 deletions src/xstate/atomWithMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,13 @@ export function atomWithMachine<
getMachine:
| StateMachine<TContext, any, TEvent, TTypestate>
| ((get: Getter) => StateMachine<TContext, any, TEvent, TTypestate>),
options: Partial<InterpreterOptions> &
Partial<MachineOptions<TContext, TEvent>> = {}
getOptions?:
| (Partial<InterpreterOptions> & Partial<MachineOptions<TContext, TEvent>>)
| ((
get: Getter
) => Partial<InterpreterOptions> &
Partial<MachineOptions<TContext, TEvent>>)
) {
const {
guards,
actions,
activities,
services,
delays,
...interpreterOptions
} = options
const machineConfig = {
guards,
actions,
activities,
services,
delays,
}
type Machine = StateMachine<TContext, any, TEvent, TTypestate>
type Service = Interpreter<TContext, any, TEvent, TTypestate>
type MachineState = State<TContext, TEvent, any, TTypestate>
Expand All @@ -50,16 +39,32 @@ export function atomWithMachine<
return cachedMachine
}
let initializing = true
const safeGet = (a: Atom<unknown>) => {
if (initializing) {
return get(a)
}
throw new Error('get not allowed after initialization')
}
const machine =
typeof getMachine === 'function'
? getMachine((a: Atom<unknown>) => {
if (initializing) {
return get(a)
}
throw new Error('get not allowed after initialization')
})
: getMachine
typeof getMachine === 'function' ? getMachine(safeGet) : getMachine
const options =
typeof getOptions === 'function' ? getOptions(safeGet) : getOptions
initializing = false
const {
guards,
actions,
activities,
services,
delays,
...interpreterOptions
} = options || {}
const machineConfig = {
guards,
actions,
activities,
services,
delays,
}
const machineWithConfig = machine.withConfig(
machineConfig,
machine.context
Expand Down