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

web-sdk-bridge

v1.0.3

Published

Web and native sdk bridge package for front end.

Downloads

12

Readme

web-sdk-bridge

Introduction

A bridge for web and sdk two way messages. For hybrid app send messages.

Inspired by this article Hybrid App技术解析 -- 实战篇

Architecture

architecture

Features

  • Build with ESM
  • No external dependencies
  • Easy to setup and use
  • Based on customEvents and custom schema

Install

> npm i web-sdk-bridge --save

Setup

1. Initialize

import Bridge from 'web-sdk-bridge';

// initianlize
const bridge = new Bridge({
  protocol: 'custom_protocol',
  prefix: 'custom_prefix_',
});

window.bridge = bridge;

2. Send message from web to SDK

Basic usage:

/**
 * This will actually send a get request with
 * this url: custom_protocol://custom_prefix_select_photo/?handler=0.
 */ 
window.bridge.nativeCall(
  'select_photo',
);

Pass params and callback:

/**
 * This will actually send a get request with
 * this url: custom_protocol://custom_prefix_select_photo/?handler=0.
 * SDK need to parse the url to get the params
 */ 
window.bridge.nativeCall(
  'select_photo',
  {
    count: 1,
  },
  (data) => {
    // dosomething with the data
  },
);

3. Send message from SDK to web

Basic usage:

/**
 * Alternatively, javascript can assign the bridge object to window,
 * so that SDK (like ios and android) can access
 * the Bridge instance method.
 */
window.bridge.postMessage(
  {
    handler: 0, // required handler from nativeCall url query
    data: {
      result: 'success',
      images: [],
    },
  }
);

Get web passed params:

/**
 * Use bridge instance method getParam with prev handler
 */
const params = window.bridge.getParam(handler);
// do something with params...
// send to web
window.bridge.postMessage(
  {
    handler: 0, // required handler from nativeCall url query
    data: {
      result: 'success',
      images: [],
    },
  }
);

4. Web event listener

Sometimes web need to listen event from SDK or native.

/**
 * Web add custom event
 */
window.bridge.addEvent('my_custom_event', (ev) => {
  const data = ev.data;
  // dosomething with the data...
});

/**
 * SDK fire event at property time
 */
window.bridge.fireEvent('my_custom_event', {
  a: 1,
  b: 2,
  ...
});

API

Bridge instance

|method|params type|description| |---|---|---| |Bridge|options<BridgeOptions>|constructor| |getScheme|name<string>|get full scheme url| |getParam|handler<string>|get params registered| |nativeCall|scheme<string>params<?any>callback<?(d: any) => void>|send message from web to sdk| |postMessage|e<string>|send message from sdk to web| |addEvent|name<string>callback<(e: CustomEvent) => void>|web add event listener| |removeEvent|handler<string|number>|web remove event listener| |fireEvent|eventName<string>data<any>|fire web event|

BridgeOptions

|option|type|description| |---|---|---| |protocol|?string|custom protocol| |prefix|?string|url prefix|