hurtle
v1.0.0
Published
Hurtle 🐢
Downloads
8
Readme
Hurtle 🐢
Zero dependency, channel-like API for JavaScript and TypeScript
(basically just a slightly more ergonomic wrapper around EventTarget
).
Usage
A simple, almost useless example.
import { Channel } from "hurtle";
const { rx, tx } = new Channel();
let state = 0;
const rx_cleanup = rx.recv((msg) => {
state += msg;
});
for (let i = 0; i < 10; i += 1) {
tx.send(1);
}
console.log(state); // expect 10
rx_cleanup();
Motivation
Can be used to more cleanly organize DOM manipulation actions spawned in response to user and network events.
// main.js
import { Channel } from "hurtle";
const { tx, rx } = new Channel();
export { tx, rx };
// req.js handle network response
import { tx } from "./main.js";
tx.send({ text: res.someJunk });
// a.js DOM manipulation
import { rx } from "./main.js";
rx.recv((event) => {
elementA.textContent = event.text;
});
// b.js DOM manipulation
import { rx } from "./main.js";
rx.recv((event) => {
elementB.textContent = event.text;
});
Deno Usage
import { Channel } from "npm:[email protected]/dist/cjs/index.js";