* 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>)
| 225 | * ahead of it. |
| 226 | */ |
| 227 | canRun(mutation: Mutation<any, any, any, any>): boolean { |
| 228 | const scope = scopeFor(mutation) |
| 229 | if (typeof scope === 'string') { |
| 230 | const mutationsWithSameScope = this.#scopes.get(scope) |
| 231 | const firstPendingMutation = mutationsWithSameScope?.find( |
| 232 | (m) => m.state.status === 'pending', |
| 233 | ) |
| 234 | // we can run if there is no current pending mutation (start use-case) |
| 235 | // or if WE are the first pending mutation (continue use-case) |
| 236 | return !firstPendingMutation || firstPendingMutation === mutation |
| 237 | } else { |
| 238 | // For unscoped mutations there are never any pending mutations in front of the |
| 239 | // current mutation |
| 240 | return true |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Continues the next paused mutation in the same `scope` after a mutation finishes. |