Git Product home page Git Product logo

jsbridge's Introduction

JSBridge

Bridge your JavaScript library for usage in Swift ๐Ÿš€

Installation

SwiftPM

dependencies: [
    .package(url: "https://github.com/LinusU/JSBridge", from: "1.0.0"),
]

Carthage

github "LinusU/JSBridge" ~> 1.0.0

Usage

foobar.js:

window.Foobar = {
  add (a, b) {
    return a + b
  },
  greet (name) {
    return `Hello, ${name}!`
  },
  async fetch (url) {
    const response = await fetch(url)
    const body = await response.text()

    return { status: response.status, body }
  }
}

Foobar.swift:

struct FetchResponse: Decodable {
    let status: Int
    let body: String
}

class Foobar {
    static internal let bridge: JSBridge = {
        let libraryPath = Bundle.main.path(forResource: "foobar", ofType: "js")!
        let libraryCode = try! String(contentsOfFile: libraryPath)

        return JSBridge(libraryCode: libraryCode)
    }()

    static func add(_ lhs: Int, _ rhs: Int) -> Promise<Int> {
        return Foobar.bridge.call(function: "Foobar.add", withArgs: (lhs, rhs)) as Promise<Int>
    }

    static func greet(name: String) -> Promise<String> {
        return Foobar.bridge.call(function: "Foobar.greet", withArg: name) as Promise<String>
    }

    static func fetch(url: URL) -> Promise<FetchResponse> {
        return Foobar.bridge.call(function: "Foobar.fetch", withArg: url) as Promise<FetchResponse>
    }
}

API

JSBridge(libraryCode: String)

Create a new JSBridge instance, with the supplied library source code.

The libraryCode should be a string of JavaScript that attaches one or more functions to the window object. These functions can then be called using the call method.

call(function: String) -> Promise<Void>

Call a function without any arguments, ignoring the return value. The returned promise will settle once the function have completed running.

call<Result: Decodable>(function: String) -> Promise<Result>

Call a function without any arguments. The returned promise will settle with the return value of the function.

call<A: Encodable>(function: String, withArg: A) -> Promise<Void>

Call a function with a single argument, ignoring the return value. The returned promise will settle once the function have completed running.

call<Result: Decodable, A: Encodable>(function: String, withArg: A) -> Promise<Result>

Call a function with a single argument. The returned promise will settle with the return value of the function.

call<A: Encodable, B: Encodable, ...>(function: String, withArgs: (A, B, ...)) -> Promise<Void>

Call a function with multiple arguments, ignoring the return value. The returned promise will settle once the function have completed running.

call<Result: Decodable, A: Encodable, B: Encodable, ...>(function: String, withArgs: (A, B, ...)) -> Promise<Result>

Call a function with multiple arguments. The returned promise will settle with the return value of the function.

iOS

To be able to use JSBridge on iOS, you need to give JSBridge a hook to your view hierarchy. Otherwise the WKWebView will get suspended by the OS, and your Promises will never settle.

This is accomplished by using the setGlobalUIHook function before instantiating any JSBridge instances.

App:

// Can be called from anywhere, e.g. your AppDelegate
JSBridge.setGlobalUIHook(window: UIApplication.shared.windows.first)

App Extension:

// From within your root view controller
JSBridge.setGlobalUIHook(viewController: self)

Hacking

The Xcode project is generated automatically from project.yml using XcodeGen. It's only checked in because Carthage needs it, do not edit it manually.

$ mint run yonaskolb/xcodegen
๐Ÿ’พ  Saved project to JSBridge.xcodeproj

jsbridge's People

Contributors

linusu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jsbridge's Issues

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.