npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lepont

v0.11.4

Published

A native <-> browser (webview) bridge library for react-native

Downloads

91

Readme

Sous le pont Mirabeau coule la Seine et nos amours -- Guillaume Apollinaire

Note: LePont (le pont) means "the bridge" in French.

You can bridge the webview and react-native by using lepont. You can consider lepont as PhoneGap (Cordova) on top of react-native.

React Native already have large swathe of library ecosystem. You can leverage its power from browser by using lepont 👍

Usage

First install it:

npm install --save lepont
# or
yarn add lepont

On react-native side

import { useBridge } from 'lepont'
import { WebView } from 'react-native-webview'

type Payload = {
  foo: number
}

// This is your bridge implementation
const myBridgeImpl = (payload: Payload) => new Promise((resolve) => {
  setTimeout(() => resolve(payload.foo * 2), 300)
})

const App = () => {
  // Registers your bridge by the name `my-bridge`
  const [ref, onMessage] = useBridge((registry) => {
    registry.register('my-bridge', myBridgeImpl)
  })

  return (
    <WebView
      // Loads the html which uses your bridge.
      source={{ uri: 'Web.bundle/index.html' }}
      // Needed for sending the message from browser
      ref={ref}
      // Needed for receiving the message from browser
      onMessage={onMessage}
      javaScriptEnabled
    />
  )
}

export default App

Browser side

import { sendMessage } from 'lepont/browser'

const res = await sendMessage({
  type: 'my-bridge',
  payload: { foo: 42 }
})
// => res is now 84 after 300ms. It's doubled on react-native side! :)

Multiple events from react-native side

On react-native side

import { useBridge } from 'lepont'
import { WebView } from 'react-native-webview'

const App = () => {
  const [ref, onMessage] = useBridge((registry) => {
    registry.register('my-streaming-bridge', (_, bridge) => {
      setInterval(() => {
        bridge.sendMessage({
          type: 'my-streaming-event',
          payload: 'stream data!',
        })
      }, 1000)
    })
  })

  return (
    <WebView
      source={{ uri: 'Web.bundle/index.html' }}
      ref={ref}
      onMessage={onMessage}
      javaScriptEnabled
    />
  )
}

export default App

Browser side

import { sendMessage, on } from 'lepont/browser'

// This triggers the event streaming
sendMessage({ type: 'my-streaming-bridge' })

on('my-streaming-event', (payload) => {
  // This fires every second from react-native side! :)
  console.log(`payload=${payload}`)
})

Package html in the App

You can package your html and all other assets (css, js) into your app, and we strongly recommend that strategy for reducing significantly the app load time.

See this article for how to bundle the static web assets in your react-native apps.

API

lepont module

lepont module is for react-native side.

useBridge(...bridgeOptions: BridgeOption[]): [WebViewRef, WebViewOnMessage, { registry: Registry }]

Registers the bridge to the registry by the given BridgeOptions. This returns ref and onMessage of WebView. You need to set these to WebView component to communicate with it.

example:

const [ref, onMessage] = useBridge(registry => {
  registry.register('my-bridge', MyBridgeImpl)
})

return <WebView ref={ref} onMessage={onMessage} />

type BridgeOption = (registry: Registry) => unknown

You can pass BridgeOpion functional option to useBridge hook. In this function you can register your bridge type and implementation through registry.register method.

Registry.register<T>(type: string, impl: BridgeImpl<T>): void

You can register your bridge type and implementation with this method.

type BridgeImpl = <T>(payload: T, brdige: Bridge) => unknown

This is the type of bridge implemetation. The 1st param is the payload of your bridge call. The second param is the bridge object. The returned value is serialized and sent back to browser as the result of bridge call. If you like to send back data multiple times to browser you can use bridge.sendMessage method.

bridge.sendMessage(message: Message): void

Sends the message to browser side. To handle this message, you can register on(type, handler) call on browser side.

lepont/browser module

lepont/browser module is for browser side.

sendMessage<T>(message: Message): Promise<T>

Sends the message to react-native side.

on(type: string, handler: any => void)

Registers the handler to the event of the given type name.

off(type: string, handler: any => void)

Unregisters the handler from the event of the given type name.

Write lepont plugin

You can write reusable lepont plugin by using the above APIs.

TBD

Official Plugins

TODO Plugins (contributions welcome)

License

MIT