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

@uupaa/messagepassing

v0.0.10

Published

Implementation of lightweight message passing logic for TypeScript.

Downloads

18

Readme

Implementation of lightweight message passing logic for TypeScript.

PREPARE

$ npm i -S @uupaa/messagepassing

import

// Pattern 1 (recommended): import complete relative path without `--moduleResolution` and `--baseUrl` compiler options.
// For import url, specify the complete relative path.
// This works with node.js and browser. Please consider bundled into one file.
import { MessagePassing, MessageSubscriber, MessageOptions, MessageResult } from "../node_modules/@uupaa/messagepassing/lib/MessagePassing.esm.js";

/*
{
  "compilerOptions": {
    "module": "ESNext",
  //"moduleResolution": "node",
  //"baseUrl": "./",
  }
}
 */
// Pattern 2: import package name with `--moduleResolution` and `--baseUrl` compiler options.
// For import url, specify the short package name.
// This works in node.js, but does not work if you import directly from the browser.
import { MessagePassing, MessageSubscriber, MessageOptions, MessageResult } from "@uupaa/messagepassing";

/*
{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "node",
    "baseUrl": "./",
  }
}
 */

Build and Bundle modules

The npm run build command, build to lib/MessagePassing.js file.

The npm run bundle:all command, bundle to lib/MessagePassing.esm.js and lib/MessagePassing.cjs.js files.

Other commands.

| command | input | output | |----------------------|---------------|-------------| | npm run build | ts/ | lib/MessagePassing.js lib/MessagePassing.d.ts | | npm run bundle | lib/MessagePassing.js | lib/MessagePassing.esm.js | | npm run bundle:esm | lib/MessagePassing.js | lib/MessagePassing.esm.js | | npm run bundle:cjs | lib/MessagePassing.js | lib/MessagePassing.cjs.js | | npm run bundle:all | lib/MessagePassing.js | lib/MessagePassing.esm.js lib/MessagePassing.cjs.js | | npm run watch | | |

Browser and runtime support

| Browser | <script type>import | <script> | require() | |---------------------------|----------|----------------|---------------| | Chrome | 61+ | :o: | | | Chrome (Android) | 61+ | :o: | | | Safari | 10.1+ | :o: | | | Safari (iOS) | 10.3+ | :o: | | | Firefox | 60+ | :o: | | | Edge | 16+ | :o: | | | new Edge (Chromium based) | 76+ | :o: | | | IE | :x: | :x: | | | Electron(render) | :o: | :o: | | | Electron(main) | | | :o: | | Node.js | | | :o: |

USAGE

Browser with ESModule

Use import and <script type="module"> style.

// test/esm.html
<!DOCTYPE html><html><head> 
<title>MessagePassing browser test</title> 
</head> 
<body>
<script type="module">
import { MessagePassing } from "./node_modules/@uupaa/messagepassing/lib/MessagePassing.esm.js";

class Subscriber1 {
  constructor(msg) {
    msg.register(this, ["Hello"]);
  }
  onmessage(selector, options) {
    switch (selector) {
      case "Hello": return "World";
    }

  }
}
class Subscriber2 {
  constructor(msg) {
    msg.register(this, ["Happy"]);
  }
  onmessage(selector, options) {
    switch (selector) {
      case "Happy": return "Halloween";
    }
  }
}

const msg = new MessagePassing();
const sub1 = new Subscriber1(msg);
const sub2 = new Subscriber2(msg);

// post() is no result
msg.to(sub1, sub2).post("Hello"); // multicast
msg.to().remove(sub1).post("Happy", [1, 2, 3]); // broadcast(exclude sub1)

// send() with result
const result1 = msg.to(sub1, sub2).send("Hello"); // multicas
const result2 = msg.to().remove(sub1).send("Happy", [1, 2, 3]); // broadcast(exclude sub1)

if (result1.get(sub1) === "World" &&
    result1.get(sub2) === undefined &&
    result2.get(sub1) === undefined &&
    result2.get(sub2) === "Halloween" &&
    result2.list(sub2).join("") === ["Halloween"].join("") ) {
  document.body.style.backgroundColor = "lime";
} else {
  document.body.style.backgroundColor = "red";
}
</script> 
</body> 
</html> 

Node.js with require()

Use require() style.

// test/cjs.js
const MessagePassing = require("../lib/MessagePassing.cjs.js").MessagePassing;

d.ts

export declare type Selector = string|number;
export interface MessageSubscriber {
    onmessage(selector: Selector, options: any): any | void;
}
export declare type MessageOptions = number | string | undefined | object | Array<number> | Array<string>;
export declare class MessageResult {
    private _result;
    set(subscriber: MessageSubscriber, value: any): void;
    get(subscriber: MessageSubscriber): any;
    has(subscriber: MessageSubscriber): any;
    list(): Array<any>;
}
export declare class MessagePassing {
    private _subscribers;
    private _selectors;
    to(...subscribers: Array<MessageSubscriber>): Message;
    register(subscriber: MessageSubscriber, selectors?: Array<Selector>): MessagePassing;
    unregister(subscriber: MessageSubscriber): MessagePassing;
    unregisterAll(): MessagePassing;
}
export declare class Message {
    private _selectors;
    private _to;
    constructor(selectors: Map<MessageSubscriber, Array<Selector>>, to: Set<MessageSubscriber>);
    remove(subscriber: MessageSubscriber): Message;
    send(selector: Selector, options?: MessageOptions): MessageResult;
    post(selector: Selector, options?: MessageOptions): void;
}

LICENSE

MIT License

Copyright (c) 2019 uupa

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.