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

shimo-broadcast-channel

v1.2.0

Published

基于 [BroadcastChannel]() 封装了一个更易用的通信库。

Downloads

41

Readme

ShimoBroadcastChannel

基于 BroadcastChannel 封装了一个更易用的通信库。

兼容 Chrome、IE 11、Firefox 和 Safari,但需要 Object.assign(),在 IE 11 下使用需要引入 polyfill。

Usage

import { ShimoBroadcastChannel } from 'shimo-broadcast-channel'

const channel = new ShimoBroadcastChannel({
  channelId: 'test'
})

await channel.postMessage('message')

channel.on('message', (msg: ShimoMessageEvent) => {
  handleMessageData(msg.data)
})

// 直接从其他频道取回值
const myValue = await channel.invoke('my_method', [arg1, arg2, ...])

Context

Context 是用于传递和消息有关的上下文,在消息传递时会保留。

Context.audiecen: string

用于限定消息听众,比如 channel.postMessage(msg, { audience: 'a' }),以便过滤消息处理器:

  • on('message', fn, { audience: 'a' }) 会收到消息
  • on('message', fn, { audience: 'b' }) 不会收到消息
  • on('message', fn, { audience: '' }) 不会收到消息
  • on('message', fn) 不会收到消息

channel.invoke(method, [], { audience: 'a' })

  • addInvokeHandler(method, fn, { audience: 'a' }) 会收到消息
  • addInvokeHandler(method, fn, { audience: 'b' }) 不会收到消息
  • addInvokeHandler(method, fn, { audience: '' }) 不会收到消息
  • addInvokeHandler(method, fn) 不会收到消息

传入 '*' 则会忽略收到消息的 audience,不进行过滤。

BroadcastChannel 无法使用时

在 BroadcastChannel 无法使用的场合,比如 cross origin,可以用 channel.addMessagePoster()channel.distributeMessage() 转发消息。

以 iframe 为例。

Parent window:

import {
  SOURCE_NAMESPACE, // 'ShimoBroadcastChannel'
  ShimoBroadcastChannel
} from 'shimo-broadcast-channel'

const iframe = document.querySelector('iframe')

const channel = new ShimoBroadcastChannel({
  channelId: 'test'
})

// 监听 postMessage 事件,把消息通过其他方式发出去
channel.on('postMessage', (evt: ShimoMessageEvent) => {
  iframe.contentWindow.postMessage(evt, '*')
})

window.addEventListener('message', (evt: MessageEvent) => {
  // 如果消息符合规则,则让 channel 来分发消息
  if (evt.data && evt.data.source === SOURCE_NAMESPACE) {
    channel.distributeMessage(evt.data)
    // 将消息转发到 same origin channel
    channel.postMessage(evt.data).catch(errorHandler)
  }
})

channel.addInvokeHandler('greeting', (name: string) => {
  return `Hello, ${name}`
})

channel.on('message', (evt: ShimoMessageEvent) => {
  console.log(evt.data) // 'Hello, John'
})

iframe window:

import {
  SOURCE_NAMESPACE,
  ShimoBroadcastChannel
} from 'shimo-broadcast-channel'

const channel = new ShimoBroadcastChannel({
  channelId: 'test'
})

// 监听 postMessage 事件,把消息通过其他方式发出去
channel.on('postMessage', (evt: ShimoMessageEvent) => {
  window.parent.postMessage(evt, '*')
})

window.addEventListener('message', (evt: MessageEvent) => {
  // 如果消息符合规则,则让 channel 来分发消息
  if (evt.data && evt.data.source === SOURCE_NAMESPACE) {
    channel.distributeMessage(evt.data)
    // 将消息转发到 same origin channel
    channel.postMessage(evt.data).catch(errorHandler)
  }
})

channel.invoke('greeting', ['John']).then((msg: string) => {
  console.log(msg) // 'Hello, John'

  channel.postMessage(msg)
})

多重 iframe 嵌套或一个 window 嵌套多个 iframe,用上述方式会导致 BroadcastChannel 中有多条重复消息,建议只在单 window 到单 iframe 的情况下使用,或使用 onMessageArrive 进行去重。

const channel = new ShimoBroadcastChannel()

const cache = new SomeCache({ ttl: CACHE_TTL })

channel.onMessageArrive = async (evt: ShimoMessageEvent) => {
  // 抛弃超过缓存时间后收到的旧消息
  if (Date.now() - evt.time > CACHE_TTL) {
    return
  }

  // 消息在 cache 中说明被处理过
  if (await cache.has(evt.id)) {
    return
  }
  cache.add(evt.id)
  return evt
}