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

Trying to use Effect.promise with Firebase's DynamicLinkComponents.shortenURL #94

Closed
npvisual opened this issue Jan 7, 2021 · 5 comments

Comments

@npvisual
Copy link
Collaborator

npvisual commented Jan 7, 2021

I have been trying to use the EffectMiddleware, since it's now available in 0.8.0, as a way to build a quick Middleware to help generating short URLs for Firebase's DynamicLinks, via the Firebase iOS SDK. This seemed like the prototypical use for EffectMiddleware.

The shortenURL(_:options:completion:) (or shorten(completion:)) takes a completion block that gets called when the network call to get the short URL returns.

Screen Shot 2021-01-07 at 9 33 55 AM

So I thought I could call Effect.promise's completion from there.

But apparently not :

Escaping closure captures non-escaping parameter 'completionblock'

Screen Shot 2021-01-07 at 9 45 08 AM

So is there a better way to use EffectMiddleware to accommodate the escaping closure ? Should I not be using .promise ? @luizmb ?

@npvisual
Copy link
Collaborator Author

npvisual commented Jan 7, 2021

Here's my code for reference :

let shortLinkMiddleware = EffectMiddleware<DynamicLinkAction, DynamicLinkAction, AppState, DynamicLinkComponents>.onAction { action, context, getState in
    let logger = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "ShortLinkMiddleware")
    switch action {
        case .generate:
            return .promise(
                token: "Any Hashable. Use this to cancel tasks, or to avoid two tasks of the same type",
                perform: { context, completionblock in
                    os_log(
                        "Creating new short link...",
                        log: logger,
                        type: .debug
                    )
                    let state = getState()
                    let api = context.dependencies
                    if let link = URL(string: state.invitationDeepLink),
                       let bundleid = state.bundleID {
                        api.link = link
                        api.domain = state.universalLinkBaseURL
                        api.options = DynamicLinkComponentsOptions()
                        api.options?.pathLength = .unguessable
                        api.iOSParameters = DynamicLinkIOSParameters(bundleID: bundleid)
                        
                        if let longDynamicLink = api.url {
                            DynamicLinkComponents.shortenURL(
                                longDynamicLink,
                                options: api.options,
                                completion: { url, warnings, error in
                                    if let url = url {
                                        completionblock(.response(url.absoluteString))
                                    } else {
                                        os_log(
                                            "Error shortening the URL from : %s",
                                            log: logger,
                                            type: .debug,
                                            String(describing: api.url)
                                        )
                                        completionblock(.error("Got an error while coverting short url"))
                                    }
                                }
                            )
                        } else {
                            completionblock(.error("Hmmm... No long URL available."))
                        }
                    } else {
                        os_log(
                            "Error converting the deep link or bundleid...",
                            log: logger,
                            type: .debug
                        )
                        completionblock(.error("Got an error while converting short url"))
                    }
                }
            )
        default: return .doNothing
    }
}

@luizmb
Copy link
Member

luizmb commented Jan 7, 2021

You are right. I didn't look yet with more time but it seems that https://github.com/SwiftRex/SwiftRex/blob/develop/Sources/CombineRex/Effect/Effect.swift#L191 is missing a @escaping clause before (OutputAction) -> Void argument of outer closure. Can you test it please and open PR if it solves? Then I can release tonight if PR is open.

CombineRex, RxSwiftRex and ReactiveSwiftRex, I couldn't find a way to write one single Effect to rule them all (yet), so there's a bit of duplication unfortunately.

@luizmb
Copy link
Member

luizmb commented Jan 7, 2021

@npvisual
Copy link
Collaborator Author

npvisual commented Jan 7, 2021

Seems to fix the compilation issue. Testing it out right now.

luizmb pushed a commit that referenced this issue Jan 7, 2021
* FIX : added escaping clause as discussed in issue #94

#94 (comment)

* Added fix for RxSwift and ReactiveSwift implementations.
luizmb pushed a commit that referenced this issue Jan 7, 2021
* FIX : added escaping clause as discussed in issue #94

#94 (comment)

* Added fix for RxSwift and ReactiveSwift implementations.
@luizmb
Copy link
Member

luizmb commented Jan 7, 2021

Ok, v0.8.1 Release created, pushing Pods right now.
PS: I had to merge develop into master because of some release script fixes.

@luizmb luizmb closed this as completed Jan 7, 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

No branches or pull requests

2 participants