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

wkinterop

v1.0.3

Published

JS counterpart to https://github.com/randymarsh77/wkinterop

Downloads

12

Readme

wkinteropJS

JS counterpart to wkinterop, a Swift package for talking to WKWebViews. Install with npm install wkinterop.

license

Overview

WKWebView provides an api for executing JS code, and recieving messages from JS running in the view's execution environment. However, any nontrivial amount of communication requires boiler plate interop code. Then, there are other considerations like asynchronousity and cancellation.

WKInterop facilitates communication by providing an api to both Swift code, and JS code for publishing events and making asyncrhonous requests. Each execution environment can register to recieve events and process requests.

Usage

Given that we are in the context of a WKWebView created using WKInterop (see Swift Example), we can use:

  • registerEventHandler = (route, handler) => { ... }
  • registerRequestHandler = (route, handler) => { ... }
  • publish = (route, content) => { ... }
  • request = (route, content) => { ... }

Requests are promise-based, IE a handler should return a promise that fulfils the request and making a request will return a promise.

All communications can provide a value as an argument and choose to handle or ignore an argument that has been provided. Requests must return a value.

Example

First, install wkinterop with something like:

import WKInterop from 'wkinterop';
...
const wkinterop = WKInterop.Install();

This is necessary to set up handler functions that Swift will assume exist. Install is a static function that creates an instance of WKInterop and installs it in the window, then returnsthe instance for consumption. Treat this instance as a singleton.

Then, carry on.

wkinterop.registerEventHandler('example.route.swift-published-event', (data) => {
  console.log('This is the event published by Swift provided with some argument/data/context what have you: ', data);
});

wkinterop.registerRequestHandler('example.route.swift-initiated-request', (request) => {
  console.log('This is the request argument: ', request);
  return Promise.resolve({ prop: 'this is my response' });
});

wkinterop.publish('example.route.js-published-event', {
  prop: 'providing some context',
});

wkinterop.request('example.route.js-initiated-request', { prop: 'JS is requesting data from Swift' })
  .then((response) => {
    console.log('recieved this response from Swift: ', response);
  });

Why?

JS is the most cross platform accessible language. However, you might not want to write important business logic in it. You might not be able to. But, you can still ship a consistent UI to any desktop or mobile platform, and even all of the above together with maximal code sharing. If that's what you want to do, then a good framework for communication interop is a must. WKInterop is still in it's early stages, but it aims to be good for this use case.

Ok, so... cross platform, but... WKWebView and Swift? Alright, you got me. Platforms besides macOS and iOS would need a compatible reciever, and this library would need some additional abstraction and environment detection. The potential is there, but I'm starting out with some platform contraints.

Roadmap

These are a few items I imagine I'll need to address in the near future.

  • Needs cancellation support
  • Built-in log forwarding might be nice
  • Custom asset loading... what if your UI wants to specify a platform-agnostic asset url but the application context wants or needs to load that url differently based on platform?