ryanstone
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 1 - Promisekit ```Swift // ContentAPI.swift func getLandingPage(uid: String) -> Promise<GetLandingPageByUidResponse> { prepare(service: service) var request = GetLandingPageByUidRequest() request.uid = uid let promise: Promise<GetLandingPageByUidResponse> = Promise { seal in _ = try? service!.getLandingPageByUid(request) { response, result in if result.success { seal.resolve(response, nil) } else { seal.reject(self.buildError(callResult: result)!) } } } return attempt { promise } // LandingPageModelController.swift func loadLandingPage(_ completion: @escaping () -> Void) { let uid = landingPageUid contentApi.getLandingPage(uid: landingPageUid) .done { (response) in self.landingPage = response.page self.landingPageIncludes = response.includes completion() }.catch { _ in completion() } ``` Note: In the above example I did not refactor the completion handler, this is because I didn't feel like it. In reality we'd likely want to return the promise up to the view and let it do it's async behavior also (I think it's possible) ## Pros - Clean API - Easy retrying - Better API for cascading or concurrent calls - Large community around `PromiseKit` - We can use promises in non-network places like alert callbacks ## Cons - Dependency - Has a non-matching API to iOS 13 Future protocol making a future upgrade to Combine difficult - Unfamiliarity - Promises would need to be hand written or generated # 2 - Open Combine Open Combine is a cross-platform framework that intends to match the combine API for any platform. We only really need `Future` because that's all we need. ```swift Future<GetLandingPageByUidResponse, NetworkError> { promise in _ = try? self.service.getLandingPageByUid(request, completion: { (response, result) in promise(self.prepareResult(response: response, result: result)) }) } ``` ## Pros - It matches iOS 13 API making future transition hopefully seamless - Meets our needs ## Cons - Very little ergonomic win over something more tested like `PromiseKit` - Bringing in a dependency like this feels very risky # 3 - Hand Rolled Object ```swift class RetryableRequest<Request, Response, Call> { typealias UnvalidatedResponse = (Response?, CallResult) -> Void typealias ValidatedResponse = (Result<Response, NetworkError>) -> Void var response: Response? var result: CallResult? var retryCount = 0 let resultHandler: ValidatedResponse let request: (@escaping UnvalidatedResponse) -> ClientCallUnary? init(grpcRequest: @escaping(Request, @escaping UnvalidatedResponse) throws -> ClientCallUnary, req: Request, resultHandler: @escaping ValidatedResponse) { self.resultHandler = resultHandler self.request = { try? grpcRequest(req, $0) } run() } func run() { _ = request(completionHandler) } func completionHandler(response: Response?, result: CallResult) { shouldRetry(result) ? self.retry(result) : resultHandler(validate(response: response, result: result)) } func retry(_ result: CallResult) { run() retryCount += 1 } func shouldRetry(_ result: CallResult) -> Bool { return retryCount <= 3 && !result.success } func validate(response: Response?, result: CallResult) -> Result<Response, NetworkError> { if let error = buildError(callResult: result) { return .failure(error) } if let response = response { return .success(response) } var error: NetworkError { return NetworkError(result: CallResult(success: false, statusCode: StatusCode.unknown, statusMessage: nil, resultData: nil, initialMetadata: nil, trailingMetadata: nil), statusMessage: nil, host: "", whiteSpaceUsernameErr: false) } return .failure(error) } func parse(callResult: CallResult) -> NetworkError? { guard callResult.statusCode != .unimplemented else { // showUpgradeAlert() return nil } guard callResult.statusCode != .ok else { return nil } return NetworkError(result: callResult, statusMessage: callResult.statusMessage, host: "", whiteSpaceUsernameErr: false) } func buildError(callResult: CallResult) -> NetworkError? { guard let result = parse(callResult: callResult) else { /// This forces an error. // return NetworkError(result: callResult, statusMessage: "Dead meat", host: self.host, whiteSpaceUsernameErr: self.whiteSpaceUsernameErr) return nil } return result } } // Callsite let requ: RetryableRequest = RetryableRequest<GetLandingPageByUidRequest, GetLandingPageByUidResponse, Result<GetLandingPageByUidResponse, NetworkError>>( grpcRequest: self.service!.getLandingPageByUid(_:completion:), req: request, resultHandler: completion) ``` ## Pros - We own the object - Meets our needs - We can do some things like a `Promise` from `Promisekit` like adding `done` method but we can allow multiple giving us the ability to let multiple files along the way listen for completion events (this does get us close in functionaltiy to Rx) ## Cons - Cumbersome API - lots of unfamiliar looking code - Autcomplete hates it # 4 - Recursion This doesn't seem to be a real solution. The previous implementation in legacy appears to have been a different type of gRPC or version. There looks to be some kind of request object that was used, this doesn't appear anywhere in our current codebase. (There _could_ be a solution involving not using our current gRPC files but instead dropping down lower in the gRPC stack. I don't think it's wise) # 5 - Code Gen In theory we can use codegen to take our current gRPC files and generate. I see no reason why this would not be possible but it will require further research that will likely go beyond the time box of this spike. After some investigation codegen could be a solid solution with a few gotchas ## Pros - Little to no work involved in creating a new API - We can quickly create new implementations during future development - Code gen can be used in conjunction with any of the previous methods to generate the API quicky ## Cons - Depending on what we use it can lead to headaches Sourcery for example uses a templating language call `StencilKit` which has a learning curve and will take some core-team learning to get the whole thing or we could use Apple's officially blessed `SwiftSyntax` (https://github.com/apple/swift-syntax) which is all Swift but I'm unsure of how powerful it is as I haven't used it. - Codegen can be brittle if templates aren't robust enough, a single change that's unexpected by the code generator can create errors which leads to core-team then needing to update the templates - This only works for currently generated proto definitions, any custom API's we'd need to build by hand - If any future proto definitions deviate from the current function signature (we got lucky that almost every signature is `func call...(request: Request, completion: (Response?, CallResult) -> Void)`) we'll need to reasses our code generation - Generated code can be difficult to read - Generated code can't be edited ```stencil import SwiftProtobuf import ChewyProtobuf // swiftlint:disable all {% for type in types.inheriting.ServiceClientBase %} class {{ type.name }}API: APIGroup { {# {% for var in type.variables %} #} {% for function in type.methods %} {% if function.returnTypeName | contains: "Call" and function.parameters%} func {{ function.callName }} ( {% for param in function.parameters %} {% if param.name|contains: "request" %} {{ param.name }}:{{ param.typeName }}, {% elif param.name|contains: "completion" %} {# {{ param }} #} completion: @escaping (Result<{{ param.typeName.closure.parameters.first.type.name }}, NetworkError>) -> Void {% endif %} {% endfor %} ) { _ = try? self.service?.{{ function.callName }}(request, validateCompletion(completion)) } {% endif %} {% endfor %} } {% endfor %} ``` Generates ```swift class AddressValidationServiceClientAPI: APIGroup { func verifyAddress ( request:AVRequest, completion: @escaping (Result<AVResponse, NetworkError>) -> Void ) { _ = try? self.service?.verifyAddress(request, validateCompletion(completion)) } func verifyBulkAddress ( request:AVBulkRequest, completion: @escaping (Result<AVBulkResponse, NetworkError>) -> Void ) { _ = try? self.service?.verifyBulkAddress(request, validateCompletion(completion)) } func suggestAddresses ( request:AVAddressSuggestion, completion: @escaping (Result<AVBulkResponse, NetworkError>) -> Void ) { _ = try? self.service?.suggestAddresses(request, validateCompletion(completion)) } } ``` # Other Things to Consider - grpcSwift is deprecating the current iteration in favor of Swift-Nio

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully