GithubHelp home page GithubHelp logo

Comments (4)

mbrandonw avatar mbrandonw commented on August 22, 2024

Hey @mathieutozer, good question! And it's definitely a bit of an oversight right now.

The way to handle this is to construct an Effect<Result<[Video], Error>>, and then you would have an action that takes that payload:

enum AppAction {
  ...
  case refreshVideosResponse(Result<[Video], Error>)
}

However, the current eraseToEffect only works when the Failure of the Publisher is Never. We should have another overload that works on unconstrained Publisher and it puts the failure in a Result.

extension Publisher {
  public func eraseToEffect() -> Effect<Result<Output, Failure>> {
    self.map(Result.success)
      .catch { Just(Result.failure($0)) }
      .eraseToEffect()
  }
}

If you use that method in your example you should get a proper Effect<Result<_, _>> that will probably be useful to you.

Does that make sense?

from episode-code-samples.

mathieutozer avatar mathieutozer commented on August 22, 2024

Thanks @mbrandonw! I was able to conceive of the first step, but didn't have the chops to figure out the second.

I am now having trouble holding onto a long running cancellable effect (I think that is my issue). I tried implementing

@available(iOS 13.0, *)
public extension Publisher {
  func cancellable<Id: Hashable>(id: Id) -> Effect<Result<Output, Failure>> {
    return Deferred { () -> PassthroughSubject<Output, Failure> in
      cancellables[id]?.cancel()
      let subject = PassthroughSubject<Output, Failure>()
      cancellables[id] = self.subscribe(subject)
      return subject
    }
    .eraseToEffect()
  }

  static func cancel<Id: Hashable>(id: Id) -> Effect<Result<Output, Failure>> {
    .fireAndForget {
      cancellables[id]?.cancel()
    }
  }
}

As per other advice found in the issues here, but the other implementation where Failure == Never get used instead

@available(iOS 13.0, *)
extension Publisher where Failure == Never {
  public func cancellable<Id: Hashable>(id: Id) -> Effect<Output> {
    return Deferred { () -> PassthroughSubject<Output, Failure> in
      cancellables[id]?.cancel()
      let subject = PassthroughSubject<Output, Failure>()
      cancellables[id] = self.subscribe(subject)
      return subject
    }
    .eraseToEffect()
  }

  public static func cancel<Id: Hashable>(id: Id) -> Effect<Output> {
    .fireAndForget {
      cancellables[id]?.cancel()
    }
  }
}

from episode-code-samples.

mbrandonw avatar mbrandonw commented on August 22, 2024

I believe there shouldn't be a need to define overloads on for cancellation. You should be able to get away with the one definition, as seen here: https://gist.github.com/mbrandonw/cb4848c9896680af6d250d9ae5f15cdd

Did you need the overload for something in particular?

from episode-code-samples.

mathieutozer avatar mathieutozer commented on August 22, 2024

You are correct - my issue was that I hadn't set up a publisher correctly so the subscription closed once the first effect was received. Still a new to these patterns!

from episode-code-samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.