@newworldcode/talkie
v1.0.9
Published
EventEmitter, Request/Reply messaging bus for Node.
Downloads
4
Maintainers
Readme
Talkie
EventEmitter, Request/Reply messaging bus for Node. Allows you to extend any object, function or class with Talkie and expose a messaging bus and request/reply interface.
Talkie does not modify your original object or function, it extends it's prototype with helpful functions to make your things become tasty messaging busses.
Examples
Below are some common use-case examples.
.request()
/.reply()
Create a sort-of listener that replies to requests with some data.
const Talkie = require("talkie")
const test_obj = () => "buzz"
// Extend the test object.
Talkie().extend(test_obj)
// Set up `test_obj` to reply with a string.
test_obj.reply("Fizz", "Buzz")
console.log(test_obj()) // => "buzz"
console.log(test_obj.request("Fizz")) // => "Buzz"
.new()
Create an instance of a stored object to .reply()
with.
const Talkie = require("talkie")
const test_obj = {}
// Extend the test object.
Talkie().extend(test_obj)
// Set up `test_obj` to reply with a string.
test_obj.reply("fizz", function Fizz(){})
console.log(test_obj.new("Fizz")) // => "Fizz"
.on(event_name, callback)
/.off(event_name, callback)
on
and off
act in the same way as the respective functions do in other common libraries, they add and remove listeners respectively.
const Talkie = require("talkie")
const test_obj = () => "buzz"
// Extend the test object.
Talkie().extend(test_obj)
// Set up `test_obj` to reply with a string.
test_obj
.on("fizz", event => {
// Do something here.
})
.off("fizz", event => {
// Do something here with the last emission of the event name.
})
.trigger(event_name, ...event_data)
Trigger an event on our object.
const Talkie = require("talkie")
const test_obj = () => "buzz"
// Extend the test object.
Talkie().extend(test_obj)
// Set up `test_obj` to reply with a string.
test_obj.on("fizz", event => {
// Do something here.
})
test_obj.trigger("fizz")
.reset()
Remove all stored .reply()
calls and event listeners.
const Talkie = require("talkie")
const wrap_me = {}
// Extend the test object.
Talkie().extend(wrap_me)
wrap_me.reply("fizz", 1)
console.log(wrap_me.request("fizz")) // 1
wrap_me.reset()
console.log(wrap_me.request("fizz")) // undefined