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

easy-post-message

v0.1.0

Published

postMessage + mitt 封装, 支持返回值, 支持自定义 adapter

Downloads

11

Readme

EasyPostMessage

npm version bundle JSDocs

postMessage + mitt 支持返回值, 支持自定义 adapter

  • 一组和 mitt 类似的方法: emitonoff
  • 一组 Promise 风格传递数据的方法: sendanswer
  • 多端支持: 浏览器、electron、自定义

Usage

完整例子: docs\index.html

npm i easy-post-message
import EasyPostMessage from 'easy-post-message';

const pm = new EasyPostMessage();

// 监听 event1 事件
pm.on('event1', ({ data, source }) => consle.log({ data, source }));

// 监听 event2 事件
pm.on('event2', ({ data, source }) => consle.log({ data, source }));

// 向 window.top 发送事件
pm.emit('event1', data, window.top);

// 监听 event10 事件 并返回数据
pm.answer('event10', (data) => {
  return 'answer val';
});

pm.answer('event11', async (data) => {
  return Promise.resolve('answer xxx');
});

// 向 window.top 发送事件, 并接收返回值
consle.log(await pm.send('event10', data, window.top));

Methods

/**
 * 监听事件
 * @param {string | symbol} event 事件类型
 * @param {Function} callback 回调函数
 */
on(event: Event, callback: (arg: EventData) => void): void;

/**
 * 移除自定义事件监听器
 * @param {string | symbol} event 事件类型
 * @param {Function} callback 回调函数
 */
off(event: Event, callback: (arg: EventData) => void): void;

/**
 * 发送事件
 * @param {string | symbol} event 事件类型
 * @param {Object} data
 * @param {Window} target 发送对象
 */
emit(event: Event, data: any, target?: Window | unknown): void;

/**
 * 发送事件, 可以接收参数
 * @param {string | symbol} event 事件类型
 * @param {Object} data
 * @param {Window} target 发送对象
 * @returns {Promise}
 */
send(event: Event, data: any, target?: Window | unknown): Promise<any>;

/**
 * 监听事件, 可以返回参数
 * @param {string | symbol} event 事件类型
 * @param {Function} callback 回调函数
 */
answer(event: Event, callback: (data: any) => void): void;

Adapter

默认 adapter 是基于浏览器的 window.postMessage, 你也可以自定义 adapter 用以支持其他平台

new EasyPostMessage(Adapter);

Electron

基于 ipcMainipcRenderer, main 进程发送消息时需要指定窗口

import EasyPostMessage from 'easy-post-message';
import Adapter from 'easy-post-message/electron-adapter';

const pm = new EasyPostMessage(Adapter);

// main
const win = new BrowserWindow(/** */);
pm.emit('a', data, win);
pm.on('b', () => {});

pm.send('c', data, win);
pm.answer('d', () => {
  return 'xx';
});

// renderer
pm.emit('b', data);
pm.on('a', () => {});

pm.send('d', data);
pm.answer('c', () => {
  return 'xx';
});