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

Potential race condition in Store.run (?) #21

Closed
stuartro opened this issue Feb 17, 2020 · 1 comment
Closed

Potential race condition in Store.run (?) #21

stuartro opened this issue Feb 17, 2020 · 1 comment

Comments

@stuartro
Copy link
Contributor

In the code snippet below, Store's run method invokes transactionObj.run and then shortly (probably fast enough for it to work) invokes transactionObj.throttle. Would it not be better to reverse the order of run and throttle?

@discardableResult @inlinable @inline(__always)
public func run<A: ActionProtocol, M>(
    action: A,
    mode: Dispatcher.Strategy = .async(nil),
    throttle: TimeInterval = 0,
    handler: Dispatcher.TransactionCompletionHandler = nil
) -> Transaction<A> where A.AssociatedStoreType: Store<M> {
  let transactionObj = transaction(action: action, mode: mode)
  transactionObj.run(handler: handler) // RUN STARTED HERE
  if throttle > TimeInterval.ulpOfOne {
    transactionObj.throttle(throttle) // THROTTLING APPLIED HERE
  }
  return transactionObj
}

That is, instead of:

  let transactionObj = transaction(action: action, mode: mode)
  transactionObj.run(handler: handler) // RUN STARTED HERE
  if throttle > TimeInterval.ulpOfOne {
    transactionObj.throttle(throttle) // THROTTLING APPLIED HERE
  }

use:

  let transactionObj = transaction(action: action, mode: mode)

  if throttle > TimeInterval.ulpOfOne {
    transactionObj.throttle(throttle) // THROTTLING APPLIED HERE
  }
  transactionObj.run(handler: handler) // RUN STARTED HERE

?

@alexdrone
Copy link
Owner

Good point :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants