@absolunet/pinki
v3.0.0
Published
Asynchronous libraries wrapper
Downloads
223
Readme
pinki
Pinky Promises
Asynchronous libraries wrapper
Dependencies / Implements
- PubSubJS - Dependency free publish/subscribe for JavaScript
- RSVP.js - A tiny implementation of Promises/A+ with deferred objects
Install
$ npm install @absolunet/pinki
Usage
// Node.js
const pinki = require('@absolunet/pinki');
// Browser - Load vendor libraries
// - /node_modules/pubsub-js/src/pubsub.js
// - /node_modules/rsvp/dist/rsvp.js
const { pinki } = window;
// Messages
pinki.message.publish('alpha', 'Yeah!');
pinki.message.subscribe('alpha', (data) => {
console.log(data);
});
setTimeout(() => {
pinki.message.publish('alpha', 'Yessir!');
}, 100);
// Outputs:
// Yeah!
// Yessir!
pinki.vow.fulfill('gamma', 'Yeah!');
pinki.vow.when('gamma').then((data) => {
console.log(data);
});
pinki.vow.when(['gamma', 'delta']).then(([dataC, dataD]) => {
console.log(dataC, dataD);
});
setTimeout(() => {
pinki.vow.fulfill('delta', 'Yessir!');
}, 100);
// Outputs:
// 'Yeah!'
// { gamma:'Yeah!', delta:'Yessir!' }
API - Messages
message.subscribe(topic, subscriber [, options])
Wraps PubSub.subscribe
, but also executes previously published messages that matches the subscription.
options.executePrevious
Type: Boolean
Default: true
Execute previously published messages.
message.publish(topic, data)
Wraps PubSub.publish
.
message.unsubscribe(topic|token)
Maps PubSub.unsubscribe
.
API - Vows
Vows are name-based Promises that can be referenced anytime.
vow.list
Vows than have been registered.
vow.when(names)
Returns a Promise if one vow given or a RSVP.all
Promise which reacts when all vows are fulfilled or one is broken if multiple vows are given.
names
Type: String
or Array
One or multiple vow names
vow.fulfill(name, data)
Resolve the underlying Promise.
name
Type: String
Vow name
data
Type: <any>
Data to resolve the underlying Promise with
vow.break(name, error)
Reject the underlying Promise.
name
Type: String
Vow name
error
Type: String
Error message to reject the underlying Promise with
License
MIT © Absolunet
“Pinky Finger” icon by Mitchell D. Eva from the Noun Project.