# @grabjs/mobile-kit-bridge-sdk

> SDK for mobile kit bridge to offer unified method signatures for Android/iOS.

Latest version **2.2.2** (published 2020-08-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @grabjs/mobile-kit-bridge-sdk
pnpm add @grabjs/mobile-kit-bridge-sdk
yarn add @grabjs/mobile-kit-bridge-sdk
bun add @grabjs/mobile-kit-bridge-sdk
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.2 |
| Published | 2020-08-01 |
| First published | 2019-04-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Viet Hai Pham |
| Maintainers | agarwal.prateek, amar.jain, chang.gan, dannysun, grab.tanay.bera, iamzhanghao, irwan.cannady, jasonporter_grab, jayant.jaiswal, kennylim-grab, laurissa.lam, leela.penumutchu, lssh, lucgu, lzbobr, maggieg, marina_meier, nibir.na, omar.yahya, oprokofiev.gb, prasanth.p, rahul.garg, raja.panidepu, rajasekhar555, rayan.dsouza, rohitsingh11, roscrazy, saichi712, shahzinks, shanthan, sumit.gera, sunnylqm, sushant.tiwari, terryx, tureki, varun.v, viethai.pham_grab, vignesh.prabhu, vinceyuan, vinod8812, wei.zhao, wenjing.mou, xiaojiang.xu, xijun.li |
| Keywords | webview, android, ios |

## Links

- npm: https://www.npmjs.com/package/@grabjs/mobile-kit-bridge-sdk
- Repository: https://github.com/grab/mobile-kit-bridge-sdk
- Homepage: https://github.com/grab/mobile-kit-bridge-sdk#readme
- Issues: https://github.com/grab/mobile-kit-bridge-sdk/issues
- npm.io page: https://npm.io/package/@grabjs/mobile-kit-bridge-sdk

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 2.2.2 (latest) — 2020-08-01
- 2.2.1 — 2020-07-23
- 2.2.0 — 2020-07-23
- 2.1.0 — 2019-12-07
- 2.0.1 — 2019-11-25
- 2.0.0 — 2019-11-23
- 1.2.2 — 2019-10-18
- 1.2.1 — 2019-10-18
- 1.2.0 — 2019-09-12
- 1.1.2 — 2019-09-12
- 1.1.1 — 2019-09-10
- 1.1.0 — 2019-09-10
- 1.0.0 — 2019-04-23

## README

# Disclaimer

This SDK is a generic SDK for native webviews. For Grab `SuperApp` integration, please use the [SuperApp SDK](https://github.com/grab/superapp-sdk).

# mobile-kit-bridge-sdk

SDK for mobile module bridge to offer unified method signatures for Android/iOS.

## Asynchronous returns

For example:

```javascript
const identifier = await window.WrappedLocaleKit.invoke("getLocaleIdentifier");
await window.WrappedAnalyticsModule.invoke("track", { analyticsEvent: event });
await window.WrappedMediaKit.invoke("playDRMContent", { contentURL, license });
```

All module methods will have `callback` as one of the parameters:

```java
class AnalyticsModuleBridge {
  fun track(requestString: String) {
    val request = Gson().fromJson(...)
    val callback = request.callback
    ...
  }
}
```

```swift
final class AnalyticsModuleBridge: WKScriptMessageHandler {
  func userContentController(
    _ userContentController: WKUserContentController,
    didReceive message: WKScriptMessage) {
    let request = message.body as! [String : Any]
    let callback = request["callback"] as! String
    ...
  }
}
```

For the sake of standardization, **all** module methods must invoke the relevant callback after they finish, even if they run synchronously or do not have anything meaningful to return. Use the parameter `callback` to identify the correct callback to invoke:

```java
webView.evaluateJavascript("javascript:window.$callback(...)") { _ -> }
```

```swift
webView.evaluateJavascript("window.\(callback)(...)", nil)
```

The name of the callback always starts with:

```javascript
[moduleName]_[functionName]Callback
```

For e.g.:

```javascript
AnalyticsModule.track -> WrappedAnalyticsModule_trackCallback
MediaKit.playDRMContent -> WrappedMediaKit_playDRMContentCallback
```

This callback style allows us to pass errors to the partner app that they can handle in case something goes wrong.

## Value streaming

All module methods return streams, e.g.:

```javascript
/** Get stream of location updates. */
const subscription = window.WrappedLocationModule.invoke(
  "observeLocationChange"
).subscribe({ next: console.log, complete: console.log });
```

Calling these methods returns `DataStream` objects that can be subscribed to with `StreamHandlers` (i.e. `onValue`, `onComplete` etc.). Once `subscribe` is called, a `Subscription` object is created to control when streaming should be terminated.

Note that `DataStream` always creates new streams whenever `subscribe` is called, so there is no need to worry about invoking it multiple times. The concept of `DataStream` is similar to that of an `Observable`, and it is easy to bridge the two:

```javascript
const playObservable = new Observable(sub => {
  const subscription = window.WrappedMediaKit.invoke('observePlayDRMContent', { isStream: true, ... }).subscribe({
    next: data => sub.next(data),
    complete: () => sub.complete(),
  });

  return () => subscription.unsubscribe();
});

playObservable.pipe(filter(...), map(...)).subscribe(...);
```

`DataStream` also supports `Promise`-style chaining and `async-await`. Instead of getting values over time, this will simply deliver the first value that arrives:

```javascript
const { result, error, status_code } = await window.WrappedMediaKit.invoke('observePlayDRMContent', { isStream: true, ... });
```

Please be aware that **Promises returned by bridged methods are non-eager**, so they will only be triggered on invocation of `then`, or after an `await` in an `async` function.

## Data format

Callback results must be in the format prescribed below:

```javascript
type CallbackResult<T = unknown> = Readonly<{
  /** The result of the operation. */
  result: T,

  /** The error object, if any. */
  error: unknown,

  /** The status code. */
  status_code: number
}>;
```

Make sure native code is passing data in the correct format, or else callbacks will not be invoked.

---
_Source: https://npm.io/package/@grabjs/mobile-kit-bridge-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
