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

Introduce Pulse 📡 #176

Merged
merged 9 commits into from
Oct 1, 2021
Merged

Introduce Pulse 📡 #176

merged 9 commits into from
Oct 1, 2021

Conversation

tokijh
Copy link
Member

@tokijh tokijh commented Feb 1, 2021

Introduce Pulse 📡

Renamed to Pulse from Signal because it conflicts with RxCocoa.Signal

Pulse has diff only when mutated
To explain in code, the results are as follows.

var messagePulse: Pulse<String?> = Pulse(wrappedValue: "Hello tokijh")
let oldMessagePulse: Pulse<String?> = message
message = "Hello tokijh"
oldMessagePulse != messagePulse // true
oldMessagePulse.value == messagePulse.value // true

Use when you want to receive an event only if the new value is assigned, even if it is the same value.
like alertMessage (See follows or PulseTests.swift)

// Reactor
private final class MyReactor: Reactor {
  struct State {
    @Pulse var alertMessage: String?
  }
  func mutate(action: Action) -> Observable<Mutation> {
    switch action {
    case let .alert(message):
      return Observable.just(Mutation.setAlertMessage(message))
    }
  }
  func reduce(state: State, mutation: Mutation) -> State {
    var newState = state
    switch mutation {
    case let .setAlertMessage(alertMessage):
      newState.alertMessage = alertMessage
    }
    return newState
  }
}
// View
reactor.pulse(\.$alertMessage)
  .compactMap { $0 } // filter nil
  .subscribe(onNext: { [weak self] (message: String) in
    self?.showAlert(message)
  })
  .disposed(by: disposeBag)
// Cases
reactor.action.onNext(.alert("Hello"))  // showAlert() is called with `Hello`
reactor.action.onNext(.alert("Hello"))  // showAlert() is called with `Hello`
reactor.action.onNext(.doSomeAction)    // showAlert() is not called
reactor.action.onNext(.alert("Hello"))  // showAlert() is called with `Hello`
reactor.action.onNext(.alert("tokijh")) // showAlert() is called with `tokijh`
reactor.action.onNext(.doSomeAction)    // showAlert() is not called

@codecov
Copy link

codecov bot commented Feb 1, 2021

Codecov Report

Merging #176 (cec5136) into master (fc392a1) will decrease coverage by 0.06%.
The diff coverage is 93.75%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #176      +/-   ##
==========================================
- Coverage   94.40%   94.33%   -0.07%     
==========================================
  Files           8       10       +2     
  Lines         143      159      +16     
==========================================
+ Hits          135      150      +15     
- Misses          8        9       +1     
Impacted Files Coverage Δ
Sources/ReactorKit/Pulse.swift 92.85% <92.85%> (ø)
Sources/ReactorKit/Reactor+Pulse.swift 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fc392a1...cec5136. Read the comment docs.

@tokijh tokijh changed the title Introduce Signal 📡 Introduce Pulse 📡 Apr 27, 2021
Copy link
Member

@OhKanghoon OhKanghoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@OhKanghoon OhKanghoon requested a review from devxoul May 17, 2021 17:10
README.md Outdated Show resolved Hide resolved
Co-authored-by: Kanghoon Oh <ggaa96@naver.com>
@OhKanghoon OhKanghoon merged commit 664d50e into ReactorKit:master Oct 1, 2021
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

Successfully merging this pull request may close these issues.

3 participants