* Checks whether a mutation can run now, given the other mutations in its `scope`. * @internal * @param mutation - The mutation to check. * @returns `true` if the mutation has no `scope`, or no other mutation in its scope is pending * ahead of it.
(mutation: Mutation<any, any, any, any>)
| 214 | * ahead of it. |
| 215 | */ |
| 216 | canRun(mutation: Mutation<any, any, any, any>): boolean { |
| 217 | const scope = scopeFor(mutation) |
| 218 | if (typeof scope === 'string') { |
| 219 | const mutationsWithSameScope = this.#scopes.get(scope) |
| 220 | const firstPendingMutation = mutationsWithSameScope?.find( |
| 221 | (m) => m.state.status === 'pending', |
| 222 | ) |
| 223 | // we can run if there is no current pending mutation (start use-case) |
| 224 | // or if WE are the first pending mutation (continue use-case) |
| 225 | return !firstPendingMutation || firstPendingMutation === mutation |
| 226 | } else { |
| 227 | // For unscoped mutations there are never any pending mutations in front of the |
| 228 | // current mutation |
| 229 | return true |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Continues the next paused mutation in the same `scope` after a mutation finishes. |