Skip to content

Commit

Permalink
V3 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Giammarchi committed Oct 5, 2018
1 parent 7cd10db commit e351460
Show file tree
Hide file tree
Showing 35 changed files with 718 additions and 1,248 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
.DS_Store
node_modules/
node_modules/
package-lock.json
3 changes: 2 additions & 1 deletion .npmignore
Expand Up @@ -8,4 +8,5 @@ test
utils
node_modules
Makefile
API.md
API.md
package-lock.json
5 changes: 1 addition & 4 deletions .travis.yml
@@ -1,9 +1,6 @@
language: node_js
node_js:
- 0.12
- 4
- 6
- 7
- stable
git:
depth: 1
branches:
Expand Down
File renamed without changes.
149 changes: 0 additions & 149 deletions Makefile

This file was deleted.

38 changes: 27 additions & 11 deletions README.md
Expand Up @@ -6,6 +6,27 @@ Previously known as [notify-js](https://www.webreflection.co.uk/blog/2015/08/14/

Useful for loaders, components bootstrap, geo position updates, and all other asynchronous or on demand user granted privileges operations, `broadcast` works on every browser and every platform, it's 100% tests covered, and it weights less than 1Kb.

### V3 Release

* **Breaking**
* all callbacks now are invoked with a single parameter/argument to normalize Promise vs callback behavior
* removed `about` alias for `that` as just redundant / confusing
* **New**
* Fully Promises micro-tasks based, including callbacks.
* `drop(type)` to delete from the internal Map the `type`. Watch out, if the type was unresolved and there were promises related to such type, these promises will be inevitably forever pending. If you drop a type without ever resolving it, please be sure you either never returned promises or resolve it via `that(type, void 0)` then `drop` it.
* ESM module as `broadcast/esm/index.js`
* CJS module as `broadcast/cjs`

### API

* `.all(type:any, callback:Function):void` to be notified every time a specific _type_ changes (i.e. each `.that(type, value)` call in the future)
* `.drop(type:any[, callback:Function]):void` remove a specific _callback_ from all future changes. If omitted, it removes _type_ from the internal _Map_
* `.new():broadcast` create a new private broadcaster.
* `.that(type:any[, value:any]):Function|void` broadcast to all callbacks and resolves all promises with `value`. If omitted, it returns a callback that will broadcast, once invoked, the received `value` (i.e. `thing.addListener(any, broadcast.that(type))`).
* `.when(type:any[, callback:Function]):Promise|void` invokes the callback next tick if _type_ is already known, it will invoke it as soon as _type_ is known otherwise. If omitted, it returns a _Promise_ that will resolve once _type_ is known.

### Examples

```js
// as Promise,
// inspired by customRegistry.whenDefined(...).then(...)
Expand All @@ -19,8 +40,8 @@ broadcast.when('geo:position').then(info => {
// receiving one or more arguments
// have you read that file before or
// will you read it at some point ?
broadcast.when('fs:README.md', (err, result) => {
if (!err) echomd(result.toString());
broadcast.when('fs:README.md', data => {
echomd(data.toString());
});

// as one-off Event (Promise or Callback)
Expand Down Expand Up @@ -50,7 +71,7 @@ navigator.geolocation.watchPosition(
fs.readFile(
'README.md',
// will broadcast once executed
broadcast.that('fs:README.md')
(err, data) => broadcast.that('fs:README.md', err || data)
);

// top of the page
Expand All @@ -63,6 +84,7 @@ document.addEventListener(
#### one broadcast VS all broadcasts

A `broadcast` happens only once asked for it, and it will receive the latest resolution.

If you'd like to listen to all broadcasted changes, you can use `broadcast.all(type, callback)`,
and eventually stop listening to it via `broadcast.drop(type, callback)`.

Expand Down Expand Up @@ -93,19 +115,13 @@ button.addEventListener('click', e => {
#### private broadcasts
There are two different ways to have a private broadcasts:

* using a secret unique string or a private `Symbol` as channel, like in `broadcast.when(privateSymbol).then(log)`
* using a secret `type` as channel, like in `broadcast.when(privateSymbol).then(log)`
* create a local version of the notifier that will share nothing with the main one:
`const pvt = broadcast.new();`

The first way enables shared, yet private, resolutions while the second one would be unreachable outside its scope.


### broadcast files
The [node module](https://github.com/WebReflection/broadcast/blob/master/build/broadcast.node.js) is available via `npm install broadcast`.

The [minified version](https://github.com/WebReflection/broadcast/blob/master/build/min.js) is available through https://unpkg.com/broadcast@latest/min.js


## Compatibility
This library is compatible with every JS engine since ES3, both browser and server,
but a `Promise` polyfill might be needed to use Promise based patterns.
but a `Promise` and a `Map` polyfill might be needed in very old engines.

0 comments on commit e351460

Please sign in to comment.