waddup
Table of contents
Breaking change notice
If you were using v1.x.x, there are a few changes when moving to v2.x.x:
Map
polyfill may be required depending on your targeted browser support (check [support])- Previously used standard objects,
Map
change was to allow non-string objects
- Previously used standard objects,
- Subscribe handlers now receive a single parameter, an object with
data
andtopic
as properties- Previously received
topic
and thendata
as individual parameters, change was to allow avoidance of topic reference if not needed
- Previously received
Usage
// ES2015; // or you can import individual functions; // CommonJSconst waddup = default; // scriptconst waddup = windowwaddup;
Size
- Uncompressed: 14.8kB
- Minified: 3.39kB
- Minified and gzipped: 1.5kB
API
publish(topic)
Publish a topic, usually upon some other event. This topic can be subscribed to by an unlimited number of functions, and can be any object type.
- topic
{*}
required- Any object you want to use as a unique identifier
- data
{*}
optional- Data that is passed to the second argument of the subscription
; const div = document; div;
subscribe(topic[, options][, fn]])
returns {number}
id of subscription, or {Array<number>}
of ids based on topic(s) passed
Subscribe to a topic, so that a specific function you pass in will be executed upon each publishing of that topic.
- topic(s)
{*|Array<*>}
required- Topic(s) to which you are subscribing
- options
{object}
optional, defaults to fn- Options passed to subscription action
- Available options:
- maxPublishCount
{number}
defaults to Infinity
- maxPublishCount
- fn
{function}
required- Function that will be executed upon each publishing of given topic
; // do a standard subscriptionconst div = document; const persistentSubscription = ; // or provide optionsconst options = maxPublishCount: 1;const oneTimeSubscription = ; // or do multiple subscriptions that use the same handler at onceconst subscriptions = ;
unsubscribe(id)
Remove subscription(s) to a topic based on subscription id passed in.
- id(s)
{number|Array<number>}
required- Single id, or array of ids, to remove subscriptions for
; // unsubscribe a single idconst id = ; ; // or multiplesconst otherId = ; ;
getSubscriptions([...topics])
;
returns {Array<Object>|Object|Map}
either the complete list of subscriptions, a subset Array of subscriptions, or a single subscription
Convenience function to see subscriptions that are currently active. If no topics are passed you get the complete list, or if you pass a topic you get that subscription, or if you pass multiple topics you get an Array of matching subscriptions.
; const array = "bar" "baz"; ;; const subscriptions = ; // Map {"foo" =>
Support
Support for the following browsers natively:
- Edge (all)
- Chrome
- Chrome for Android
- Firefox
- Firefox Mobile
- Opera (25+)
- Safari (7.1+)
- Safari Mobile (8+)
- Internet Explorer (11+)
Support for the following browsers when including polyfill for Map:
- Opera (15-)
- Safari (7-)
- Safari Mobile (7.1-)
- Internet Explorer (9-10)
- Android Browser (all)
- Opera Mobile (all)
Development
Standard practice, clone the repo and npm i
to get the dependencies. The following npm scripts are available:
- build => build unminified dist version with source map and NODE_ENV=development via webpack
- build-minified => build minified dist version with NODE_ENV=production via webpack
- dev => start webpack playground App for eyeball testing
- lint => run ESLint on all files in
src
folder (also runs ondev
script) - prepublish => run
lint
,test
,transpile
,build
, andbuild-minified
scripts - test => run AVA with NODE_ENV=test on all files in
test
folder - test:watch => run same script as
test
but keep persistent watcher - transpile => run Babel on all files in
src
folder (transpiled tolib
folder)