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

🐛 :: Append closure to observers before executing #5

Merged
merged 1 commit into from
Jan 15, 2021

Conversation

dudek-j
Copy link
Contributor

@dudek-j dudek-j commented Jan 10, 2021

When subscribing to a Variable<T> the initial value triggers the provided closure. If the closure makes changes to the underlying value of the same Variable<T> the closure does not re-trigger on its first run.

Sample Code Showcasing Issue

enum ExampleEnum {
    case initialValue
    case secondaryValue
}

let exampleVariable = Variable<ExampleEnum>(.initialValue)

let disposable = exampleVariable.subscribe({ new, old in
    if new == .initialValue {
        print("InitialValue")
        exampleVariable.value = .secondaryValue
    }
    if new == .secondaryValue {
        print("SecondaryValue")
    }
})

Expected Output
InitialValue
SecondaryValue

Actual Output
InitialValue

Solution
The proposed changes make it so the closure is added to the observer list before it's executed for the first time

@dudek-j dudek-j changed the title 🐛 :: Append closure to observer before executing 🐛 :: Append closure to observers before executing Jan 10, 2021
@dudek-j dudek-j force-pushed the master branch 2 times, most recently from aa815da to 538ad03 Compare January 10, 2021 21:21
@fxm90
Copy link
Owner

fxm90 commented Jan 15, 2021

Great fix, thank you 👍

@fxm90 fxm90 merged commit d32aca7 into fxm90:master Jan 15, 2021
@fxm90 fxm90 self-assigned this Jan 15, 2021
@fxm90 fxm90 added the bug Something isn't working label Jan 15, 2021
@fxm90 fxm90 self-requested a review January 15, 2021 12:32
fxm90 added a commit that referenced this pull request Jan 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants