@absolunet/pinki

Asynchronous libraries wrapper

Stats

StarsIssuesVersionUpdatedCreatedSize
@absolunet/pinki
103.0.02 years ago3 years agoMinified + gzip package size for @absolunet/pinki in KB

Readme

pinki

pinki

npm npm dependencies npms Travis CI Code style ESLint

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.

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.