From e3514604ca6ed7013e218fd0305d5fadec4a8536 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Fri, 5 Oct 2018 08:43:21 +0200 Subject: [PATCH] V3 Release --- .gitignore | 3 +- .npmignore | 3 +- .travis.yml | 5 +- LICENSE.txt => LICENSE | 0 Makefile | 149 ------------- README.md | 38 +++- build/broadcast.js | 255 ---------------------- build/broadcast.node.js | 252 --------------------- cjs/index.js | 62 ++++++ coverage/coverage.json | 2 +- coverage/lcov-report/cjs/index.html | 93 ++++++++ coverage/lcov-report/cjs/index.js.html | 251 +++++++++++++++++++++ coverage/lcov-report/index.html | 20 +- coverage/lcov.info | 220 +++++++------------ esm/index.js | 62 ++++++ index.js | 61 ++++++ min.js | 4 +- package.json | 17 +- src/broadcast.js | 23 +- template/amd.after | 1 - template/amd.before | 1 - template/copyright | 1 - template/license.after | 2 - template/license.before | 1 - template/md.after | 2 - template/md.before | 32 --- template/node.after | 2 - template/node.before | 0 template/var.after | 4 - template/var.before | 1 - test/test.js | 290 ++++--------------------- utils/browserify.sh | 23 -- utils/jshint.sh | 43 ---- utils/uglifyjs.sh | 20 -- utils/watchify.sh | 23 -- 35 files changed, 718 insertions(+), 1248 deletions(-) rename LICENSE.txt => LICENSE (100%) delete mode 100644 Makefile delete mode 100644 build/broadcast.js delete mode 100644 build/broadcast.node.js create mode 100644 cjs/index.js create mode 100644 coverage/lcov-report/cjs/index.html create mode 100644 coverage/lcov-report/cjs/index.js.html create mode 100644 esm/index.js create mode 100644 index.js delete mode 100644 template/amd.after delete mode 100644 template/amd.before delete mode 100644 template/copyright delete mode 100644 template/license.after delete mode 100644 template/license.before delete mode 100644 template/md.after delete mode 100644 template/md.before delete mode 100644 template/node.after delete mode 100644 template/node.before delete mode 100644 template/var.after delete mode 100644 template/var.before delete mode 100644 utils/browserify.sh delete mode 100755 utils/jshint.sh delete mode 100644 utils/uglifyjs.sh delete mode 100644 utils/watchify.sh diff --git a/.gitignore b/.gitignore index 58b805f..6c79168 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store -node_modules/ \ No newline at end of file +node_modules/ +package-lock.json \ No newline at end of file diff --git a/.npmignore b/.npmignore index 27953b2..08f528b 100644 --- a/.npmignore +++ b/.npmignore @@ -8,4 +8,5 @@ test utils node_modules Makefile -API.md \ No newline at end of file +API.md +package-lock.json \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 9fce9e3..7383617 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,6 @@ language: node_js node_js: - - 0.12 - - 4 - - 6 - - 7 + - stable git: depth: 1 branches: diff --git a/LICENSE.txt b/LICENSE similarity index 100% rename from LICENSE.txt rename to LICENSE diff --git a/Makefile b/Makefile deleted file mode 100644 index 9949bf3..0000000 --- a/Makefile +++ /dev/null @@ -1,149 +0,0 @@ -.PHONY: build duk var node amd size hint clean test web preview pages dependencies - -# repository name -REPO = broadcast - -# make var files -VAR = src/$(REPO).js - -# make node files -NODE = $(VAR) - -# make amd files -AMD = $(VAR) - -# README constant - - -# default build task -build: - make clean - make var - make node - #make amd - make test - make hint - make size - -# build generic version -var: - mkdir -p build - cat template/var.before $(VAR) template/var.after >build/no-copy.$(REPO).js - node node_modules/uglify-js/bin/uglifyjs --verbose build/no-copy.$(REPO).js >build/no-copy.min.js - cat template/license.before LICENSE.txt template/license.after build/no-copy.$(REPO).js >build/$(REPO).js - cat template/copyright build/no-copy.min.js >build/min.js - rm build/no-copy.$(REPO).js - rm build/no-copy.min.js - mv build/min.js ./ - -# build node.js version -node: - mkdir -p build - cat template/license.before LICENSE.txt template/license.after template/node.before $(NODE) template/node.after >build/$(REPO).node.js - -# build AMD version -amd: - mkdir -p build - cat template/amd.before $(AMD) template/amd.after >build/no-copy.$(REPO).max.amd.js - node node_modules/uglify-js/bin/uglifyjs --verbose build/no-copy.$(REPO).max.amd.js >build/no-copy.$(REPO).amd.js - cat template/license.before LICENSE.txt template/license.after build/no-copy.$(REPO).max.amd.js >build/$(REPO).max.amd.js - cat template/copyright build/no-copy.$(REPO).amd.js >build/$(REPO).amd.js - rm build/no-copy.$(REPO).max.amd.js - rm build/no-copy.$(REPO).amd.js - -# build self executable for duktape -duk: - node -e 'var fs=require("fs");\ - fs.writeFileSync(\ - "test/duk.js",\ - fs.readFileSync("node_modules/wru/build/wru.console.js") +\ - "\n" +\ - fs.readFileSync("build/$(REPO).js") +\ - "\n" +\ - fs.readFileSync("test/$(REPO).js").toString().replace(/^[^\x00]+?\/\/:remove\s*/,"")\ - );' - - -size: - wc -c build/$(REPO).js - gzip -c min.js | wc -c - -# hint built file -hint: - node node_modules/jshint/bin/jshint build/$(REPO).js - -# clean/remove build folder -clean: - rm -rf build - -# tests, as usual and of course -test: - npm test - -# launch tiny-cdn (ctrl+click to open the page) -web: - ./node_modules/.bin/tiny-cdn run ./ - -# markdown the readme and view it -preview: - node_modules/markdown/bin/md2html.js README.md >README.md.htm - cat template/md.before README.md.htm template/md.after >README.md.html - open README.md.html - sleep 3 - rm README.md.htm README.md.html - -pages: - git pull --rebase - git checkout gh-pages - git pull --rebase - git checkout master - make var - mkdir -p ~/tmp - mkdir -p ~/tmp/$(REPO) - cp .gitignore ~/tmp/ - cp -rf src ~/tmp/$(REPO) - cp -rf build ~/tmp/$(REPO) - cp -rf test ~/tmp/$(REPO) - cp index.html ~/tmp/$(REPO) - git checkout gh-pages - cp ~/tmp/.gitignore ./ - mkdir -p test - rm -rf test - cp -rf ~/tmp/$(REPO) test - git add .gitignore - git add test - git add test/. - git commit -m 'automatic test generator' - git push - git checkout master - rm -r ~/tmp/$(REPO) - -# modules used in this repo -dependencies: - rm -rf node_modules - mkdir node_modules - npm install wru - npm install tiny-cdn - npm install uglify-js@1 - npm install jshint - npm install markdown - npm install browserify - npm install watchify - -# bundle: creates the browserified version of the project as js/bundle.max.js -bundle: - sh utils/browserify.sh - -# watch: update the browserified version of the project as soon as file changes -watch: - sh utils/watchify.sh - -# minified: create the minifeid version of the project as js/bundle.js -minified: - make -s bundle - sh utils/uglifyjs.sh $(LICENSE) - make -s size - -# jshint: recursively checks for javascript files inside the src folder and lint them -jshint: - sh utils/jshint.sh diff --git a/README.md b/README.md index f2a7297..1b73732 100644 --- a/README.md +++ b/README.md @@ -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(...) @@ -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) @@ -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 @@ -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)`. @@ -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. diff --git a/build/broadcast.js b/build/broadcast.js deleted file mode 100644 index fb0f682..0000000 --- a/build/broadcast.js +++ /dev/null @@ -1,255 +0,0 @@ -/*! -ISC License - -Copyright (c) 2015-2018, Andrea Giammarchi, @WebReflection - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY -AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE -OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -PERFORMANCE OF THIS SOFTWARE. - -*/ -var broadcast = (function () { -/** - * // assuming "data" event hasn't happened yet - * broadcast.when("data", function (data) { - * console.log(data); - * }); - * - * // whenever it will happen - * broadcast.that("data", {any:'value'}); - * // all listeners waiting for it, will be triggered - * - * - * // what if you add a listener after the `.that` call? - * broadcast.when("data", function (data) { - * console.log('yep, instantly called!', data); - * }); - * - * - * // what if we redefine data ? - * broadcast.that("data", {another:'value'}); - * // from now on, whoever will ask `.when` data - * // the value will be the updated one - * // but every listener already fired and satisfied - * // will be simply ignored - * - * - * // what if I want to be sure the channel is private? - * // feel free to use a Symbol as channel - * var myPrivateSymbol = Symbol(); - * broadcast.when(myPrivateSymbol, ...); - * - * // otherwise create a new broadcast like variable - * var privateBroadcast = broadcast.new(); - */ -function create(O) {'use strict'; - - var - // flag for internal operations (used by all) - invoke = true, - // create a dictionary, fallback as regular object - _ = (O.create || O)(null), - // dictionaries don't have this method, borrow it - hOP = O.prototype.hasOwnProperty, - // will invoke the callback within the Promise - broadcast = function (args) { - this.apply(null, args); - }, - // IE < 9 doesn't have this method, sham it - bind = O.bind || function (self) { - var cb = this; - return function () { - return cb.apply(self, arguments); - }; - }, - // IE < 9 doesn't have this method, sham it - indexOf = Array.prototype.indexOf || function indexOf(v) { - var i = this.length; - while (i-- && this[i] !== v) {} - return i; - }, - resolve = typeof Promise == 'undefined' ? - function (value) { - return {then: function (cb) { - setTimeout(cb, 1, value); - }}; - } : - function (value) { - return Promise.resolve(value); - }, - // little partial WeakMap poly - wm = typeof WeakMap == 'undefined' ? - (function (k, v) { - return { - // delete used to be a reserved property name - 'delete': function (x) { - var i = indexOf.call(k, x); - k.splice(i, 1); - v.splice(i, 1); - }, - get: function (x) { - return v[indexOf.call(k, x)]; - }, - set: function (x, y) { - v[k.push(x) - 1] = y; - } - }; - }([], [])) : - new WeakMap() - ; - - // check if a private _[type] is known - // if not, create the info object - // returns such object either cases - function get(type) { - return hOP.call(_, type) ? - _[type] : - (_[type] = { - args: null, - cb: [] - }); - } - - function that(type) { - var - len = arguments.length, - info = get(type), - i = 1, - cb - ; - // in case it's invoked - // without any error or value - if (i === len) { - // creates a callback - // that once invoked will resolve - return function () { - var args = [type]; - args.push.apply(args, arguments); - return that.apply(null, args); - }; - } - // in every other case - // resolve the type with any amount - // of arguments received - else { - info.args = []; - while (i < len) info.args.push(arguments[i++]); - i = 0; - len = info.cb.length; - // be sure the list of waiting listeners - // will be cleaned up so these won't - // every be notified more than once - // ( unless these are passed again via `.when` ) - // NOTE: .splice(0) would be enough - // but IE8 wants the length too - cb = info.cb.splice(i, len); - while (i < len) resolve(info.args).then(bind.call(broadcast, cb[i++])); - } - return info.args[0]; - } - - function when(type, callback) { - var info = get(type), out; - if (arguments.length === 1) { - out = new Promise(function (resolve) { - callback = resolve; - }); - } - if (invoke && info.args) { - resolve(info.args).then(bind.call(broadcast, callback)); - } else if(indexOf.call(info.cb, callback) < 0) { - info.cb.push(callback); - } - return out; - } - - // freeze, if possible, the broadcast object - // to be sure no other scripts can change its methods - return (O.freeze || O)({ - - // There are two ways to use this method - // - // .when(type, callback) - // add a listener to a generic type - // whenever such type will happen - // or if it happened already - // invoke the callback with the resolved value - // - // .when(type) - // return a new Promise that will be resolved - // once the notification will happen - // - // broadcast.when('event').then(function (data) { ... }); - // - when: when, - - // .about is an alias for .that - about: that, - - // There are two ways to use this method - // - // .that(type) - // will return a callback - // that will try to resolve once executed - // fs.readFile('setup.json', broadcast.that('setup.json')) - // - // overload - // .that(type, any1[, any2[, ...]]) - // resolve type passing anyValue around - // - // // through one argument - // broadcast.that('some-event', {all: 'good'}); - // // through more arguments - // broadcast.that('some-event', null, 'OK'); - // - that: that, - - // if we set a listener through `.when` - // and this hasn't been notified yet - // but we changed our mind about such notification - // we can still remove such listener via `.drop` - drop: function drop(type, callback) { - var fn = wm.get(callback), cb, i; - if (fn) { - wm['delete'](callback); - drop(type, fn); - } else { - cb = get(type).cb; - i = indexOf.call(cb, callback); - if (~i) cb.splice(i, 1); - } - }, - - // create a new broadcast-like object - 'new': function () { - return create(O); - }, - - // in case we'd like to react each time - // to a specific event. - // In this case a callback is mandatory, - // and it's also needed to eventually drop. - all: function all(type, callback) { - if (!wm.get(callback)) { - wm.set(callback, function fn() { - invoke = false; - when(type, fn); - invoke = true; - resolve(arguments).then(bind.call(broadcast, callback)); - }); - when(type, wm.get(callback)); - } - } - }); -} -return create(Object); - -}()); \ No newline at end of file diff --git a/build/broadcast.node.js b/build/broadcast.node.js deleted file mode 100644 index 409bb7c..0000000 --- a/build/broadcast.node.js +++ /dev/null @@ -1,252 +0,0 @@ -/*! -ISC License - -Copyright (c) 2015-2018, Andrea Giammarchi, @WebReflection - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY -AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE -OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -PERFORMANCE OF THIS SOFTWARE. - -*/ -/** - * // assuming "data" event hasn't happened yet - * broadcast.when("data", function (data) { - * console.log(data); - * }); - * - * // whenever it will happen - * broadcast.that("data", {any:'value'}); - * // all listeners waiting for it, will be triggered - * - * - * // what if you add a listener after the `.that` call? - * broadcast.when("data", function (data) { - * console.log('yep, instantly called!', data); - * }); - * - * - * // what if we redefine data ? - * broadcast.that("data", {another:'value'}); - * // from now on, whoever will ask `.when` data - * // the value will be the updated one - * // but every listener already fired and satisfied - * // will be simply ignored - * - * - * // what if I want to be sure the channel is private? - * // feel free to use a Symbol as channel - * var myPrivateSymbol = Symbol(); - * broadcast.when(myPrivateSymbol, ...); - * - * // otherwise create a new broadcast like variable - * var privateBroadcast = broadcast.new(); - */ -function create(O) {'use strict'; - - var - // flag for internal operations (used by all) - invoke = true, - // create a dictionary, fallback as regular object - _ = (O.create || O)(null), - // dictionaries don't have this method, borrow it - hOP = O.prototype.hasOwnProperty, - // will invoke the callback within the Promise - broadcast = function (args) { - this.apply(null, args); - }, - // IE < 9 doesn't have this method, sham it - bind = O.bind || function (self) { - var cb = this; - return function () { - return cb.apply(self, arguments); - }; - }, - // IE < 9 doesn't have this method, sham it - indexOf = Array.prototype.indexOf || function indexOf(v) { - var i = this.length; - while (i-- && this[i] !== v) {} - return i; - }, - resolve = typeof Promise == 'undefined' ? - function (value) { - return {then: function (cb) { - setTimeout(cb, 1, value); - }}; - } : - function (value) { - return Promise.resolve(value); - }, - // little partial WeakMap poly - wm = typeof WeakMap == 'undefined' ? - (function (k, v) { - return { - // delete used to be a reserved property name - 'delete': function (x) { - var i = indexOf.call(k, x); - k.splice(i, 1); - v.splice(i, 1); - }, - get: function (x) { - return v[indexOf.call(k, x)]; - }, - set: function (x, y) { - v[k.push(x) - 1] = y; - } - }; - }([], [])) : - new WeakMap() - ; - - // check if a private _[type] is known - // if not, create the info object - // returns such object either cases - function get(type) { - return hOP.call(_, type) ? - _[type] : - (_[type] = { - args: null, - cb: [] - }); - } - - function that(type) { - var - len = arguments.length, - info = get(type), - i = 1, - cb - ; - // in case it's invoked - // without any error or value - if (i === len) { - // creates a callback - // that once invoked will resolve - return function () { - var args = [type]; - args.push.apply(args, arguments); - return that.apply(null, args); - }; - } - // in every other case - // resolve the type with any amount - // of arguments received - else { - info.args = []; - while (i < len) info.args.push(arguments[i++]); - i = 0; - len = info.cb.length; - // be sure the list of waiting listeners - // will be cleaned up so these won't - // every be notified more than once - // ( unless these are passed again via `.when` ) - // NOTE: .splice(0) would be enough - // but IE8 wants the length too - cb = info.cb.splice(i, len); - while (i < len) resolve(info.args).then(bind.call(broadcast, cb[i++])); - } - return info.args[0]; - } - - function when(type, callback) { - var info = get(type), out; - if (arguments.length === 1) { - out = new Promise(function (resolve) { - callback = resolve; - }); - } - if (invoke && info.args) { - resolve(info.args).then(bind.call(broadcast, callback)); - } else if(indexOf.call(info.cb, callback) < 0) { - info.cb.push(callback); - } - return out; - } - - // freeze, if possible, the broadcast object - // to be sure no other scripts can change its methods - return (O.freeze || O)({ - - // There are two ways to use this method - // - // .when(type, callback) - // add a listener to a generic type - // whenever such type will happen - // or if it happened already - // invoke the callback with the resolved value - // - // .when(type) - // return a new Promise that will be resolved - // once the notification will happen - // - // broadcast.when('event').then(function (data) { ... }); - // - when: when, - - // .about is an alias for .that - about: that, - - // There are two ways to use this method - // - // .that(type) - // will return a callback - // that will try to resolve once executed - // fs.readFile('setup.json', broadcast.that('setup.json')) - // - // overload - // .that(type, any1[, any2[, ...]]) - // resolve type passing anyValue around - // - // // through one argument - // broadcast.that('some-event', {all: 'good'}); - // // through more arguments - // broadcast.that('some-event', null, 'OK'); - // - that: that, - - // if we set a listener through `.when` - // and this hasn't been notified yet - // but we changed our mind about such notification - // we can still remove such listener via `.drop` - drop: function drop(type, callback) { - var fn = wm.get(callback), cb, i; - if (fn) { - wm['delete'](callback); - drop(type, fn); - } else { - cb = get(type).cb; - i = indexOf.call(cb, callback); - if (~i) cb.splice(i, 1); - } - }, - - // create a new broadcast-like object - 'new': function () { - return create(O); - }, - - // in case we'd like to react each time - // to a specific event. - // In this case a callback is mandatory, - // and it's also needed to eventually drop. - all: function all(type, callback) { - if (!wm.get(callback)) { - wm.set(callback, function fn() { - invoke = false; - when(type, fn); - invoke = true; - resolve(arguments).then(bind.call(broadcast, callback)); - }); - when(type, wm.get(callback)); - } - } - }); -} -module.exports = create(Object); \ No newline at end of file diff --git a/cjs/index.js b/cjs/index.js new file mode 100644 index 0000000..e79249e --- /dev/null +++ b/cjs/index.js @@ -0,0 +1,62 @@ +/*! (c) Andrea Giammarchi - ISC */ +function broadcast() { + var map = new Map; + return { + all: all, + drop: drop, + new: broadcast, + that: that, + when: when + }; + function all(type, c) { + var _ = _get(type); + if (_.c.indexOf(c) < 0) + _.c.push(c); + when(type, c); + } + function drop(type, c) { + var _ = _get(type); + if (1 < arguments.length) { + _cancel(_.c, c); + _cancel(_.f, c); + } + else + map.delete(type); + } + function that(type, value) { + if (1 < arguments.length) { + var _ = _get(type); + _.$ = Promise.resolve(value); + while (_.f.length) _.$.then(_.f.shift()); + while (_.r.length) _.$.then(_.r.shift()); + _.f.push.apply(_.f, _.c); + } + else + return that.bind(null, type); + } + function when(type, f) { + var _ = _get(type); + if (_.$ !== null) + _.$.then(that.bind(null, type)); + if (1 < arguments.length) { + if (_.f.indexOf(f) < 0) + _.f.push(f); + } + else + return new Promise(function (r) { _.r.push(r); }); + } + function _cancel(a, f) { + var i = a.indexOf(f); + if (-1 < i) + a.splice(i, 1); + } + function _get(type) { + return map.get(type) || _set(type); + } + function _set(type) { + var _ = {c: [], f: [], r: [], $: null}; + map.set(type, _); + return _; + } +} +module.exports = broadcast(); diff --git a/coverage/coverage.json b/coverage/coverage.json index 585c2f0..0591934 100644 --- a/coverage/coverage.json +++ b/coverage/coverage.json @@ -1 +1 @@ -{"/home/webreflection/code/broadcast/build/broadcast.node.js":{"path":"/home/webreflection/code/broadcast/build/broadcast.node.js","s":{"1":1,"2":2,"3":16,"4":15,"5":15,"6":15,"7":22,"8":22,"9":22,"10":15,"11":15,"12":1,"13":1,"14":1,"15":1,"16":1,"17":7,"18":1,"19":1,"20":37,"21":1,"22":17,"23":17,"24":2,"25":2,"26":2,"27":2,"28":15,"29":15,"30":18,"31":15,"32":15,"33":15,"34":15,"35":9,"36":15,"37":1,"38":17,"39":17,"40":2,"41":2,"42":17,"43":5,"44":12,"45":11,"46":17,"47":2,"48":4,"49":4,"50":1,"51":1,"52":3,"53":3,"54":3,"55":2,"56":1,"57":2,"58":1,"59":2,"60":2,"61":2,"62":2,"63":1,"64":1},"b":{"1":[2,1],"2":[2,1],"3":[2,1],"4":[22,7],"5":[1,1],"6":[1,1],"7":[26,11],"8":[2,15],"9":[2,15],"10":[5,12],"11":[17,15],"12":[11,1],"13":[2,1],"14":[1,3],"15":[2,1],"16":[1,1]},"f":{"1":2,"2":16,"3":15,"4":15,"5":22,"6":15,"7":15,"8":1,"9":1,"10":1,"11":7,"12":1,"13":37,"14":17,"15":2,"16":17,"17":2,"18":4,"19":1,"20":2,"21":2},"fnMap":{"1":{"name":"create","line":56,"loc":{"start":{"line":56,"column":0},"end":{"line":56,"column":19}}},"2":{"name":"(anonymous_2)","line":66,"loc":{"start":{"line":66,"column":16},"end":{"line":66,"column":32}}},"3":{"name":"(anonymous_3)","line":70,"loc":{"start":{"line":70,"column":21},"end":{"line":70,"column":37}}},"4":{"name":"(anonymous_4)","line":72,"loc":{"start":{"line":72,"column":13},"end":{"line":72,"column":25}}},"5":{"name":"indexOf","line":77,"loc":{"start":{"line":77,"column":41},"end":{"line":77,"column":61}}},"6":{"name":"(anonymous_6)","line":83,"loc":{"start":{"line":83,"column":6},"end":{"line":83,"column":23}}},"7":{"name":"(anonymous_7)","line":84,"loc":{"start":{"line":84,"column":22},"end":{"line":84,"column":36}}},"8":{"name":"(anonymous_8)","line":88,"loc":{"start":{"line":88,"column":6},"end":{"line":88,"column":23}}},"9":{"name":"(anonymous_9)","line":93,"loc":{"start":{"line":93,"column":7},"end":{"line":93,"column":23}}},"10":{"name":"(anonymous_10)","line":96,"loc":{"start":{"line":96,"column":20},"end":{"line":96,"column":33}}},"11":{"name":"(anonymous_11)","line":101,"loc":{"start":{"line":101,"column":15},"end":{"line":101,"column":28}}},"12":{"name":"(anonymous_12)","line":104,"loc":{"start":{"line":104,"column":15},"end":{"line":104,"column":31}}},"13":{"name":"get","line":115,"loc":{"start":{"line":115,"column":2},"end":{"line":115,"column":21}}},"14":{"name":"that","line":124,"loc":{"start":{"line":124,"column":2},"end":{"line":124,"column":22}}},"15":{"name":"(anonymous_15)","line":136,"loc":{"start":{"line":136,"column":13},"end":{"line":136,"column":25}}},"16":{"name":"when","line":162,"loc":{"start":{"line":162,"column":2},"end":{"line":162,"column":32}}},"17":{"name":"(anonymous_17)","line":165,"loc":{"start":{"line":165,"column":24},"end":{"line":165,"column":43}}},"18":{"name":"drop","line":222,"loc":{"start":{"line":222,"column":10},"end":{"line":222,"column":40}}},"19":{"name":"(anonymous_19)","line":235,"loc":{"start":{"line":235,"column":11},"end":{"line":235,"column":23}}},"20":{"name":"all","line":243,"loc":{"start":{"line":243,"column":9},"end":{"line":243,"column":38}}},"21":{"name":"fn","line":245,"loc":{"start":{"line":245,"column":25},"end":{"line":245,"column":39}}}},"statementMap":{"1":{"start":{"line":56,"column":0},"end":{"line":255,"column":1}},"2":{"start":{"line":58,"column":2},"end":{"line":110,"column":3}},"3":{"start":{"line":67,"column":6},"end":{"line":67,"column":29}},"4":{"start":{"line":71,"column":6},"end":{"line":71,"column":20}},"5":{"start":{"line":72,"column":6},"end":{"line":74,"column":8}},"6":{"start":{"line":73,"column":8},"end":{"line":73,"column":41}},"7":{"start":{"line":78,"column":6},"end":{"line":78,"column":26}},"8":{"start":{"line":79,"column":6},"end":{"line":79,"column":37}},"9":{"start":{"line":80,"column":6},"end":{"line":80,"column":15}},"10":{"start":{"line":84,"column":8},"end":{"line":86,"column":11}},"11":{"start":{"line":85,"column":10},"end":{"line":85,"column":35}},"12":{"start":{"line":89,"column":8},"end":{"line":89,"column":38}},"13":{"start":{"line":94,"column":8},"end":{"line":107,"column":10}},"14":{"start":{"line":97,"column":12},"end":{"line":97,"column":39}},"15":{"start":{"line":98,"column":12},"end":{"line":98,"column":27}},"16":{"start":{"line":99,"column":12},"end":{"line":99,"column":27}},"17":{"start":{"line":102,"column":12},"end":{"line":102,"column":41}},"18":{"start":{"line":105,"column":12},"end":{"line":105,"column":33}},"19":{"start":{"line":115,"column":2},"end":{"line":122,"column":3}},"20":{"start":{"line":116,"column":4},"end":{"line":121,"column":9}},"21":{"start":{"line":124,"column":2},"end":{"line":160,"column":3}},"22":{"start":{"line":125,"column":4},"end":{"line":130,"column":5}},"23":{"start":{"line":133,"column":4},"end":{"line":158,"column":5}},"24":{"start":{"line":136,"column":6},"end":{"line":140,"column":8}},"25":{"start":{"line":137,"column":8},"end":{"line":137,"column":26}},"26":{"start":{"line":138,"column":8},"end":{"line":138,"column":41}},"27":{"start":{"line":139,"column":8},"end":{"line":139,"column":38}},"28":{"start":{"line":146,"column":6},"end":{"line":146,"column":21}},"29":{"start":{"line":147,"column":6},"end":{"line":147,"column":53}},"30":{"start":{"line":147,"column":22},"end":{"line":147,"column":53}},"31":{"start":{"line":148,"column":6},"end":{"line":148,"column":12}},"32":{"start":{"line":149,"column":6},"end":{"line":149,"column":27}},"33":{"start":{"line":156,"column":6},"end":{"line":156,"column":34}},"34":{"start":{"line":157,"column":6},"end":{"line":157,"column":77}},"35":{"start":{"line":157,"column":22},"end":{"line":157,"column":77}},"36":{"start":{"line":159,"column":4},"end":{"line":159,"column":24}},"37":{"start":{"line":162,"column":2},"end":{"line":175,"column":3}},"38":{"start":{"line":163,"column":4},"end":{"line":163,"column":30}},"39":{"start":{"line":164,"column":4},"end":{"line":168,"column":5}},"40":{"start":{"line":165,"column":6},"end":{"line":167,"column":9}},"41":{"start":{"line":166,"column":8},"end":{"line":166,"column":27}},"42":{"start":{"line":169,"column":4},"end":{"line":173,"column":5}},"43":{"start":{"line":170,"column":6},"end":{"line":170,"column":62}},"44":{"start":{"line":171,"column":11},"end":{"line":173,"column":5}},"45":{"start":{"line":172,"column":6},"end":{"line":172,"column":29}},"46":{"start":{"line":174,"column":4},"end":{"line":174,"column":15}},"47":{"start":{"line":179,"column":2},"end":{"line":254,"column":5}},"48":{"start":{"line":223,"column":6},"end":{"line":223,"column":39}},"49":{"start":{"line":224,"column":6},"end":{"line":231,"column":7}},"50":{"start":{"line":225,"column":8},"end":{"line":225,"column":31}},"51":{"start":{"line":226,"column":8},"end":{"line":226,"column":23}},"52":{"start":{"line":228,"column":8},"end":{"line":228,"column":26}},"53":{"start":{"line":229,"column":8},"end":{"line":229,"column":39}},"54":{"start":{"line":230,"column":8},"end":{"line":230,"column":32}},"55":{"start":{"line":230,"column":16},"end":{"line":230,"column":32}},"56":{"start":{"line":236,"column":6},"end":{"line":236,"column":23}},"57":{"start":{"line":244,"column":6},"end":{"line":252,"column":7}},"58":{"start":{"line":245,"column":8},"end":{"line":250,"column":11}},"59":{"start":{"line":246,"column":10},"end":{"line":246,"column":25}},"60":{"start":{"line":247,"column":10},"end":{"line":247,"column":25}},"61":{"start":{"line":248,"column":10},"end":{"line":248,"column":24}},"62":{"start":{"line":249,"column":10},"end":{"line":249,"column":66}},"63":{"start":{"line":251,"column":8},"end":{"line":251,"column":37}},"64":{"start":{"line":256,"column":0},"end":{"line":256,"column":32}}},"branchMap":{"1":{"line":62,"type":"binary-expr","locations":[{"start":{"line":62,"column":9},"end":{"line":62,"column":17}},{"start":{"line":62,"column":21},"end":{"line":62,"column":22}}]},"2":{"line":70,"type":"binary-expr","locations":[{"start":{"line":70,"column":11},"end":{"line":70,"column":17}},{"start":{"line":70,"column":21},"end":{"line":75,"column":5}}]},"3":{"line":77,"type":"binary-expr","locations":[{"start":{"line":77,"column":14},"end":{"line":77,"column":37}},{"start":{"line":77,"column":41},"end":{"line":81,"column":5}}]},"4":{"line":79,"type":"binary-expr","locations":[{"start":{"line":79,"column":13},"end":{"line":79,"column":16}},{"start":{"line":79,"column":20},"end":{"line":79,"column":33}}]},"5":{"line":82,"type":"cond-expr","locations":[{"start":{"line":83,"column":6},"end":{"line":87,"column":7}},{"start":{"line":88,"column":6},"end":{"line":90,"column":7}}]},"6":{"line":92,"type":"cond-expr","locations":[{"start":{"line":93,"column":7},"end":{"line":108,"column":15}},{"start":{"line":109,"column":6},"end":{"line":109,"column":19}}]},"7":{"line":116,"type":"cond-expr","locations":[{"start":{"line":117,"column":6},"end":{"line":117,"column":13}},{"start":{"line":118,"column":7},"end":{"line":121,"column":7}}]},"8":{"line":133,"type":"if","locations":[{"start":{"line":133,"column":4},"end":{"line":133,"column":4}},{"start":{"line":133,"column":4},"end":{"line":133,"column":4}}]},"9":{"line":164,"type":"if","locations":[{"start":{"line":164,"column":4},"end":{"line":164,"column":4}},{"start":{"line":164,"column":4},"end":{"line":164,"column":4}}]},"10":{"line":169,"type":"if","locations":[{"start":{"line":169,"column":4},"end":{"line":169,"column":4}},{"start":{"line":169,"column":4},"end":{"line":169,"column":4}}]},"11":{"line":169,"type":"binary-expr","locations":[{"start":{"line":169,"column":8},"end":{"line":169,"column":14}},{"start":{"line":169,"column":18},"end":{"line":169,"column":27}}]},"12":{"line":171,"type":"if","locations":[{"start":{"line":171,"column":11},"end":{"line":171,"column":11}},{"start":{"line":171,"column":11},"end":{"line":171,"column":11}}]},"13":{"line":179,"type":"binary-expr","locations":[{"start":{"line":179,"column":10},"end":{"line":179,"column":18}},{"start":{"line":179,"column":22},"end":{"line":179,"column":23}}]},"14":{"line":224,"type":"if","locations":[{"start":{"line":224,"column":6},"end":{"line":224,"column":6}},{"start":{"line":224,"column":6},"end":{"line":224,"column":6}}]},"15":{"line":230,"type":"if","locations":[{"start":{"line":230,"column":8},"end":{"line":230,"column":8}},{"start":{"line":230,"column":8},"end":{"line":230,"column":8}}]},"16":{"line":244,"type":"if","locations":[{"start":{"line":244,"column":6},"end":{"line":244,"column":6}},{"start":{"line":244,"column":6},"end":{"line":244,"column":6}}]}}}} \ No newline at end of file +{"/Users/agiammarchi/git/broadcast/cjs/index.js":{"path":"/Users/agiammarchi/git/broadcast/cjs/index.js","s":{"1":1,"2":2,"3":2,"4":1,"5":2,"6":2,"7":1,"8":2,"9":1,"10":3,"11":3,"12":2,"13":2,"14":1,"15":1,"16":13,"17":8,"18":8,"19":8,"20":5,"21":8,"22":2,"23":8,"24":5,"25":1,"26":7,"27":7,"28":2,"29":7,"30":5,"31":3,"32":2,"33":2,"34":1,"35":4,"36":4,"37":2,"38":1,"39":20,"40":1,"41":3,"42":3,"43":3,"44":1},"b":{"1":[1,1],"2":[2,1],"3":[8,5],"4":[2,5],"5":[5,2],"6":[3,2],"7":[2,2],"8":[20,3]},"f":{"1":2,"2":2,"3":3,"4":13,"5":7,"6":2,"7":4,"8":20,"9":3},"fnMap":{"1":{"name":"broadcast","line":2,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":21}}},"2":{"name":"all","line":11,"loc":{"start":{"line":11,"column":2},"end":{"line":11,"column":24}}},"3":{"name":"drop","line":17,"loc":{"start":{"line":17,"column":2},"end":{"line":17,"column":25}}},"4":{"name":"that","line":26,"loc":{"start":{"line":26,"column":2},"end":{"line":26,"column":29}}},"5":{"name":"when","line":37,"loc":{"start":{"line":37,"column":2},"end":{"line":37,"column":25}}},"6":{"name":"(anonymous_6)","line":46,"loc":{"start":{"line":46,"column":25},"end":{"line":46,"column":38}}},"7":{"name":"_cancel","line":48,"loc":{"start":{"line":48,"column":2},"end":{"line":48,"column":25}}},"8":{"name":"_get","line":53,"loc":{"start":{"line":53,"column":2},"end":{"line":53,"column":22}}},"9":{"name":"_set","line":56,"loc":{"start":{"line":56,"column":2},"end":{"line":56,"column":22}}}},"statementMap":{"1":{"start":{"line":2,"column":0},"end":{"line":61,"column":1}},"2":{"start":{"line":3,"column":2},"end":{"line":3,"column":20}},"3":{"start":{"line":4,"column":2},"end":{"line":10,"column":4}},"4":{"start":{"line":11,"column":2},"end":{"line":16,"column":3}},"5":{"start":{"line":12,"column":4},"end":{"line":12,"column":23}},"6":{"start":{"line":13,"column":4},"end":{"line":14,"column":18}},"7":{"start":{"line":14,"column":6},"end":{"line":14,"column":18}},"8":{"start":{"line":15,"column":4},"end":{"line":15,"column":18}},"9":{"start":{"line":17,"column":2},"end":{"line":25,"column":3}},"10":{"start":{"line":18,"column":4},"end":{"line":18,"column":23}},"11":{"start":{"line":19,"column":4},"end":{"line":24,"column":23}},"12":{"start":{"line":20,"column":6},"end":{"line":20,"column":22}},"13":{"start":{"line":21,"column":6},"end":{"line":21,"column":22}},"14":{"start":{"line":24,"column":6},"end":{"line":24,"column":23}},"15":{"start":{"line":26,"column":2},"end":{"line":36,"column":3}},"16":{"start":{"line":27,"column":4},"end":{"line":35,"column":35}},"17":{"start":{"line":28,"column":6},"end":{"line":28,"column":25}},"18":{"start":{"line":29,"column":6},"end":{"line":29,"column":35}},"19":{"start":{"line":30,"column":6},"end":{"line":30,"column":47}},"20":{"start":{"line":30,"column":25},"end":{"line":30,"column":47}},"21":{"start":{"line":31,"column":6},"end":{"line":31,"column":47}},"22":{"start":{"line":31,"column":25},"end":{"line":31,"column":47}},"23":{"start":{"line":32,"column":6},"end":{"line":32,"column":31}},"24":{"start":{"line":35,"column":6},"end":{"line":35,"column":35}},"25":{"start":{"line":37,"column":2},"end":{"line":47,"column":3}},"26":{"start":{"line":38,"column":4},"end":{"line":38,"column":23}},"27":{"start":{"line":39,"column":4},"end":{"line":40,"column":38}},"28":{"start":{"line":40,"column":6},"end":{"line":40,"column":38}},"29":{"start":{"line":41,"column":4},"end":{"line":46,"column":56}},"30":{"start":{"line":42,"column":6},"end":{"line":43,"column":20}},"31":{"start":{"line":43,"column":8},"end":{"line":43,"column":20}},"32":{"start":{"line":46,"column":6},"end":{"line":46,"column":56}},"33":{"start":{"line":46,"column":40},"end":{"line":46,"column":52}},"34":{"start":{"line":48,"column":2},"end":{"line":52,"column":3}},"35":{"start":{"line":49,"column":4},"end":{"line":49,"column":25}},"36":{"start":{"line":50,"column":4},"end":{"line":51,"column":21}},"37":{"start":{"line":51,"column":6},"end":{"line":51,"column":21}},"38":{"start":{"line":53,"column":2},"end":{"line":55,"column":3}},"39":{"start":{"line":54,"column":4},"end":{"line":54,"column":39}},"40":{"start":{"line":56,"column":2},"end":{"line":60,"column":3}},"41":{"start":{"line":57,"column":4},"end":{"line":57,"column":43}},"42":{"start":{"line":58,"column":4},"end":{"line":58,"column":21}},"43":{"start":{"line":59,"column":4},"end":{"line":59,"column":13}},"44":{"start":{"line":62,"column":0},"end":{"line":62,"column":29}}},"branchMap":{"1":{"line":13,"type":"if","locations":[{"start":{"line":13,"column":4},"end":{"line":13,"column":4}},{"start":{"line":13,"column":4},"end":{"line":13,"column":4}}]},"2":{"line":19,"type":"if","locations":[{"start":{"line":19,"column":4},"end":{"line":19,"column":4}},{"start":{"line":19,"column":4},"end":{"line":19,"column":4}}]},"3":{"line":27,"type":"if","locations":[{"start":{"line":27,"column":4},"end":{"line":27,"column":4}},{"start":{"line":27,"column":4},"end":{"line":27,"column":4}}]},"4":{"line":39,"type":"if","locations":[{"start":{"line":39,"column":4},"end":{"line":39,"column":4}},{"start":{"line":39,"column":4},"end":{"line":39,"column":4}}]},"5":{"line":41,"type":"if","locations":[{"start":{"line":41,"column":4},"end":{"line":41,"column":4}},{"start":{"line":41,"column":4},"end":{"line":41,"column":4}}]},"6":{"line":42,"type":"if","locations":[{"start":{"line":42,"column":6},"end":{"line":42,"column":6}},{"start":{"line":42,"column":6},"end":{"line":42,"column":6}}]},"7":{"line":50,"type":"if","locations":[{"start":{"line":50,"column":4},"end":{"line":50,"column":4}},{"start":{"line":50,"column":4},"end":{"line":50,"column":4}}]},"8":{"line":54,"type":"binary-expr","locations":[{"start":{"line":54,"column":11},"end":{"line":54,"column":24}},{"start":{"line":54,"column":28},"end":{"line":54,"column":38}}]}}}} \ No newline at end of file diff --git a/coverage/lcov-report/cjs/index.html b/coverage/lcov-report/cjs/index.html new file mode 100644 index 0000000..f9db601 --- /dev/null +++ b/coverage/lcov-report/cjs/index.html @@ -0,0 +1,93 @@ + + + + Code coverage report for cjs/ + + + + + + + +
+
+

+ all files cjs/ +

+
+
+ 100% + Statements + 44/44 +
+
+ 100% + Branches + 16/16 +
+
+ 100% + Functions + 9/9 +
+
+ 100% + Lines + 41/41 +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FileStatementsBranchesFunctionsLines
index.js
100%44/44100%16/16100%9/9100%41/41
+
+
+ + + + + + + diff --git a/coverage/lcov-report/cjs/index.js.html b/coverage/lcov-report/cjs/index.js.html new file mode 100644 index 0000000..3653f9f --- /dev/null +++ b/coverage/lcov-report/cjs/index.js.html @@ -0,0 +1,251 @@ + + + + Code coverage report for cjs/index.js + + + + + + + +
+
+

+ all files / cjs/ index.js +

+
+
+ 100% + Statements + 44/44 +
+
+ 100% + Branches + 16/16 +
+
+ 100% + Functions + 9/9 +
+
+ 100% + Lines + 41/41 +
+
+
+
+

+
+
1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59 +60 +61 +62 +63  + + + +  +  +  +  +  +  + + + + + +  + + + + + +  +  + +  + +13× + + + + + +  +  + +  + + + + + + + +  +  + +  + + + + +  + +20× +  + + + + +  +  + + 
/*! (c) Andrea Giammarchi - ISC */
+function broadcast() {
+  var map = new Map;
+  return {
+    all: all,
+    drop: drop,
+    new: broadcast,
+    that: that,
+    when: when
+  };
+  function all(type, c) {
+    var _ = _get(type);
+    if (_.c.indexOf(c) < 0)
+      _.c.push(c);
+    when(type, c);
+  }
+  function drop(type, c) {
+    var _ = _get(type);
+    if (1 < arguments.length) {
+      _cancel(_.c, c);
+      _cancel(_.f, c);
+    }
+    else
+      map.delete(type);
+  }
+  function that(type, value) {
+    if (1 < arguments.length) {
+      var _ = _get(type);
+      _.$ = Promise.resolve(value);
+      while (_.f.length) _.$.then(_.f.shift());
+      while (_.r.length) _.$.then(_.r.shift());
+      _.f.push.apply(_.f, _.c);
+    }
+    else
+      return that.bind(null, type);
+  }
+  function when(type, f) {
+    var _ = _get(type);
+    if (_.$ !== null)
+      _.$.then(that.bind(null, type));
+    if (1 < arguments.length) {
+      if (_.f.indexOf(f) < 0)
+        _.f.push(f);
+    }
+    else
+      return new Promise(function (r) { _.r.push(r); });
+  }
+  function _cancel(a, f) {
+    var i = a.indexOf(f);
+    if (-1 < i)
+      a.splice(i, 1);
+  }
+  function _get(type) {
+    return map.get(type) || _set(type);
+  }
+  function _set(type) {
+    var _ = {c: [], f: [], r: [], $: null};
+    map.set(type, _);
+    return _;
+  }
+}
+module.exports = broadcast();
+ 
+
+
+ + + + + + + diff --git a/coverage/lcov-report/index.html b/coverage/lcov-report/index.html index cbb590e..b4f465f 100644 --- a/coverage/lcov-report/index.html +++ b/coverage/lcov-report/index.html @@ -22,22 +22,22 @@

100% Statements - 64/64 + 44/44
100% Branches - 32/32 + 16/16
100% Functions - 21/21 + 9/9
100% Lines - 61/61 + 41/41
@@ -59,16 +59,16 @@

- build/ + cjs/
100% - 64/64 + 44/44 100% - 32/32 + 16/16 100% - 21/21 + 9/9 100% - 61/61 + 41/41 @@ -77,7 +77,7 @@

diff --git a/coverage/lcov.info b/coverage/lcov.info index 6719437..65dcaa3 100644 --- a/coverage/lcov.info +++ b/coverage/lcov.info @@ -1,144 +1,84 @@ TN: -SF:/home/webreflection/code/broadcast/build/broadcast.node.js -FN:56,create -FN:66,(anonymous_2) -FN:70,(anonymous_3) -FN:72,(anonymous_4) -FN:77,indexOf -FN:83,(anonymous_6) -FN:84,(anonymous_7) -FN:88,(anonymous_8) -FN:93,(anonymous_9) -FN:96,(anonymous_10) -FN:101,(anonymous_11) -FN:104,(anonymous_12) -FN:115,get -FN:124,that -FN:136,(anonymous_15) -FN:162,when -FN:165,(anonymous_17) -FN:222,drop -FN:235,(anonymous_19) -FN:243,all -FN:245,fn -FNF:21 -FNH:21 -FNDA:2,create -FNDA:16,(anonymous_2) -FNDA:15,(anonymous_3) -FNDA:15,(anonymous_4) -FNDA:22,indexOf -FNDA:15,(anonymous_6) -FNDA:15,(anonymous_7) -FNDA:1,(anonymous_8) -FNDA:1,(anonymous_9) -FNDA:1,(anonymous_10) -FNDA:7,(anonymous_11) -FNDA:1,(anonymous_12) -FNDA:37,get -FNDA:17,that -FNDA:2,(anonymous_15) -FNDA:17,when -FNDA:2,(anonymous_17) -FNDA:4,drop -FNDA:1,(anonymous_19) +SF:/Users/agiammarchi/git/broadcast/cjs/index.js +FN:2,broadcast +FN:11,all +FN:17,drop +FN:26,that +FN:37,when +FN:46,(anonymous_6) +FN:48,_cancel +FN:53,_get +FN:56,_set +FNF:9 +FNH:9 +FNDA:2,broadcast FNDA:2,all -FNDA:2,fn +FNDA:3,drop +FNDA:13,that +FNDA:7,when +FNDA:2,(anonymous_6) +FNDA:4,_cancel +FNDA:20,_get +FNDA:3,_set +DA:2,1 +DA:3,2 +DA:4,2 +DA:11,1 +DA:12,2 +DA:13,2 +DA:14,1 +DA:15,2 +DA:17,1 +DA:18,3 +DA:19,3 +DA:20,2 +DA:21,2 +DA:24,1 +DA:26,1 +DA:27,13 +DA:28,8 +DA:29,8 +DA:30,8 +DA:31,8 +DA:32,8 +DA:35,5 +DA:37,1 +DA:38,7 +DA:39,7 +DA:40,2 +DA:41,7 +DA:42,5 +DA:43,3 +DA:46,2 +DA:48,1 +DA:49,4 +DA:50,4 +DA:51,2 +DA:53,1 +DA:54,20 DA:56,1 -DA:58,2 -DA:67,16 -DA:71,15 -DA:72,15 -DA:73,15 -DA:78,22 -DA:79,22 -DA:80,22 -DA:84,15 -DA:85,15 -DA:89,1 -DA:94,1 -DA:97,1 -DA:98,1 -DA:99,1 -DA:102,7 -DA:105,1 -DA:115,1 -DA:116,37 -DA:124,1 -DA:125,17 -DA:133,17 -DA:136,2 -DA:137,2 -DA:138,2 -DA:139,2 -DA:146,15 -DA:147,18 -DA:148,15 -DA:149,15 -DA:156,15 -DA:157,15 -DA:159,15 -DA:162,1 -DA:163,17 -DA:164,17 -DA:165,2 -DA:166,2 -DA:169,17 -DA:170,5 -DA:171,12 -DA:172,11 -DA:174,17 -DA:179,2 -DA:223,4 -DA:224,4 -DA:225,1 -DA:226,1 -DA:228,3 -DA:229,3 -DA:230,3 -DA:236,1 -DA:244,2 -DA:245,1 -DA:246,2 -DA:247,2 -DA:248,2 -DA:249,2 -DA:251,1 -DA:256,1 -LF:61 -LH:61 -BRDA:62,1,0,2 -BRDA:62,1,1,1 -BRDA:70,2,0,2 -BRDA:70,2,1,1 -BRDA:77,3,0,2 -BRDA:77,3,1,1 -BRDA:79,4,0,22 -BRDA:79,4,1,7 -BRDA:82,5,0,1 -BRDA:82,5,1,1 -BRDA:92,6,0,1 -BRDA:92,6,1,1 -BRDA:116,7,0,26 -BRDA:116,7,1,11 -BRDA:133,8,0,2 -BRDA:133,8,1,15 -BRDA:164,9,0,2 -BRDA:164,9,1,15 -BRDA:169,10,0,5 -BRDA:169,10,1,12 -BRDA:169,11,0,17 -BRDA:169,11,1,15 -BRDA:171,12,0,11 -BRDA:171,12,1,1 -BRDA:179,13,0,2 -BRDA:179,13,1,1 -BRDA:224,14,0,1 -BRDA:224,14,1,3 -BRDA:230,15,0,2 -BRDA:230,15,1,1 -BRDA:244,16,0,1 -BRDA:244,16,1,1 -BRF:32 -BRH:32 +DA:57,3 +DA:58,3 +DA:59,3 +DA:62,1 +LF:41 +LH:41 +BRDA:13,1,0,1 +BRDA:13,1,1,1 +BRDA:19,2,0,2 +BRDA:19,2,1,1 +BRDA:27,3,0,8 +BRDA:27,3,1,5 +BRDA:39,4,0,2 +BRDA:39,4,1,5 +BRDA:41,5,0,5 +BRDA:41,5,1,2 +BRDA:42,6,0,3 +BRDA:42,6,1,2 +BRDA:50,7,0,2 +BRDA:50,7,1,2 +BRDA:54,8,0,20 +BRDA:54,8,1,3 +BRF:16 +BRH:16 end_of_record diff --git a/esm/index.js b/esm/index.js new file mode 100644 index 0000000..90a8fdf --- /dev/null +++ b/esm/index.js @@ -0,0 +1,62 @@ +/*! (c) Andrea Giammarchi - ISC */ +function broadcast() { + var map = new Map; + return { + all: all, + drop: drop, + new: broadcast, + that: that, + when: when + }; + function all(type, c) { + var _ = _get(type); + if (_.c.indexOf(c) < 0) + _.c.push(c); + when(type, c); + } + function drop(type, c) { + var _ = _get(type); + if (1 < arguments.length) { + _cancel(_.c, c); + _cancel(_.f, c); + } + else + map.delete(type); + } + function that(type, value) { + if (1 < arguments.length) { + var _ = _get(type); + _.$ = Promise.resolve(value); + while (_.f.length) _.$.then(_.f.shift()); + while (_.r.length) _.$.then(_.r.shift()); + _.f.push.apply(_.f, _.c); + } + else + return that.bind(null, type); + } + function when(type, f) { + var _ = _get(type); + if (_.$ !== null) + _.$.then(that.bind(null, type)); + if (1 < arguments.length) { + if (_.f.indexOf(f) < 0) + _.f.push(f); + } + else + return new Promise(function (r) { _.r.push(r); }); + } + function _cancel(a, f) { + var i = a.indexOf(f); + if (-1 < i) + a.splice(i, 1); + } + function _get(type) { + return map.get(type) || _set(type); + } + function _set(type) { + var _ = {c: [], f: [], r: [], $: null}; + map.set(type, _); + return _; + } +} +export default broadcast(); diff --git a/index.js b/index.js new file mode 100644 index 0000000..afbfd81 --- /dev/null +++ b/index.js @@ -0,0 +1,61 @@ +/*! (c) Andrea Giammarchi - ISC */ +function broadcast() { + var map = new Map; + return { + all: all, + drop: drop, + new: broadcast, + that: that, + when: when + }; + function all(type, c) { + var _ = _get(type); + if (_.c.indexOf(c) < 0) + _.c.push(c); + when(type, c); + } + function drop(type, c) { + var _ = _get(type); + if (1 < arguments.length) { + _cancel(_.c, c); + _cancel(_.f, c); + } + else + map.delete(type); + } + function that(type, value) { + if (1 < arguments.length) { + var _ = _get(type); + _.$ = Promise.resolve(value); + while (_.f.length) _.$.then(_.f.shift()); + while (_.r.length) _.$.then(_.r.shift()); + _.f.push.apply(_.f, _.c); + } + else + return that.bind(null, type); + } + function when(type, f) { + var _ = _get(type); + if (_.$ !== null) + _.$.then(that.bind(null, type)); + if (1 < arguments.length) { + if (_.f.indexOf(f) < 0) + _.f.push(f); + } + else + return new Promise(function (r) { _.r.push(r); }); + } + function _cancel(a, f) { + var i = a.indexOf(f); + if (-1 < i) + a.splice(i, 1); + } + function _get(type) { + return map.get(type) || _set(type); + } + function _set(type) { + var _ = {c: [], f: [], r: [], $: null}; + map.set(type, _); + return _; + } +} diff --git a/min.js b/min.js index ec6d44a..0ec1d7d 100644 --- a/min.js +++ b/min.js @@ -1,2 +1,2 @@ -/*! (C) Andrea Giammarchi ISC License */ -var broadcast=function(){function e(t){"use strict";function l(e){return i.call(r,e)?r[e]:r[e]={args:null,cb:[]}}function c(e){var t=arguments.length,n=l(e),r=1,i;if(r===t)return function(){var t=[e];return t.push.apply(t,arguments),c.apply(null,t)};n.args=[];while(r> cjs/index.js", + "esm": "cp index.js esm/index.js && echo 'export default broadcast();' >> esm/index.js", + "min": "uglifyjs index.js --support-ie8 --comments=/^!/ -c -m -o min.js", + "test": "istanbul cover test/test.js", + "coveralls": "cat ./coverage/lcov.info | coveralls" }, "devDependencies": { - "coveralls": "^2.11.15", + "coveralls": "^3.0.2", "istanbul": "^0.4.5", - "jshint": "~2.x", - "tiny-cdn": "^0.7.0", + "jshint": "^2.9.6", "tressa": "^0.3.1", "uglify-js": "^2.8.29" } diff --git a/src/broadcast.js b/src/broadcast.js index 0e59bef..3847447 100644 --- a/src/broadcast.js +++ b/src/broadcast.js @@ -197,15 +197,20 @@ function create(O) {'use strict'; // and this hasn't been notified yet // but we changed our mind about such notification // we can still remove such listener via `.drop` + // otherwise, if we know the key, we can drop it. drop: function drop(type, callback) { - var fn = wm.get(callback), cb, i; - if (fn) { - wm['delete'](callback); - drop(type, fn); - } else { - cb = get(type).cb; - i = indexOf.call(cb, callback); - if (~i) cb.splice(i, 1); + if (arguments.length === 1) + delete _[type]; + else { + var fn = wm.get(callback), cb, i; + if (fn) { + wm['delete'](callback); + drop(type, fn); + } else { + cb = get(type).cb; + i = indexOf.call(cb, callback); + if (~i) cb.splice(i, 1); + } } }, @@ -230,4 +235,4 @@ function create(O) {'use strict'; } } }); -} \ No newline at end of file +} diff --git a/template/amd.after b/template/amd.after deleted file mode 100644 index 8916587..0000000 --- a/template/amd.after +++ /dev/null @@ -1 +0,0 @@ -); \ No newline at end of file diff --git a/template/amd.before b/template/amd.before deleted file mode 100644 index 4c15333..0000000 --- a/template/amd.before +++ /dev/null @@ -1 +0,0 @@ -define( \ No newline at end of file diff --git a/template/copyright b/template/copyright deleted file mode 100644 index 9502fa4..0000000 --- a/template/copyright +++ /dev/null @@ -1 +0,0 @@ -/*! (C) Andrea Giammarchi ISC License */ diff --git a/template/license.after b/template/license.after deleted file mode 100644 index d7365df..0000000 --- a/template/license.after +++ /dev/null @@ -1,2 +0,0 @@ - -*/ diff --git a/template/license.before b/template/license.before deleted file mode 100644 index bf11147..0000000 --- a/template/license.before +++ /dev/null @@ -1 +0,0 @@ -/*! diff --git a/template/md.after b/template/md.after deleted file mode 100644 index 691287b..0000000 --- a/template/md.after +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/template/md.before b/template/md.before deleted file mode 100644 index 2e48892..0000000 --- a/template/md.before +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/template/node.after b/template/node.after deleted file mode 100644 index 81bacaa..0000000 --- a/template/node.after +++ /dev/null @@ -1,2 +0,0 @@ - -module.exports = create(Object); \ No newline at end of file diff --git a/template/node.before b/template/node.before deleted file mode 100644 index e69de29..0000000 diff --git a/template/var.after b/template/var.after deleted file mode 100644 index 3f4618a..0000000 --- a/template/var.after +++ /dev/null @@ -1,4 +0,0 @@ - -return create(Object); - -}()); \ No newline at end of file diff --git a/template/var.before b/template/var.before deleted file mode 100644 index b03f825..0000000 --- a/template/var.before +++ /dev/null @@ -1 +0,0 @@ -var broadcast = (function () { diff --git a/test/test.js b/test/test.js index d9bd962..210b2a3 100644 --- a/test/test.js +++ b/test/test.js @@ -1,258 +1,48 @@ -// run the test -var delay = 30; -var later = 1; -var test = require('tressa'); +var tressa = require('tressa'); -if (!process.env.NATIVE) { - // drop native to force use polyfills - // and be sure everything is covered - var WeakMap = global.WeakMap; - global.WeakMap = undefined; - var Promise = global.Promise; - global.Promise = undefined; - var indexOf = global.Array.prototype.indexOf; - global.Array.prototype.indexOf = undefined; - var bind = global.Function.prototype.bind; - global.Function.prototype.bind = undefined; - var create = global.Object.create; - global.Object.create = undefined; - var freeze = global.Object.freeze; - global.Object.freeze = undefined; -} -// grab the module -var broadcast = require('../build/broadcast.node.js'); +var broadcast = require('../cjs').new(); +tressa.title('broadcast'); +tressa.assert(typeof broadcast === 'object', 'module'); -if (!process.env.NATIVE) { - // put everything back where it was - global.WeakMap = WeakMap; - global.Promise = Promise; - global.Array.prototype.indexOf = indexOf; - global.Function.prototype.bind = bind; - global.Object.create = create; - global.Object.freeze = freeze; -} - - -test.title('broadcast'); -test(typeof broadcast === 'object', 'module'); - -test.async(function (done) { - var i = 0, r = Math.random(); - function increase() { i++; } - broadcast.when('test-when', increase); - // if set twice but same callback - // don't add it twice - broadcast.when('test-when', increase); - setTimeout(function () { - test.log('## when'); - test(i === 0, 'did not happen'); - broadcast.that('test-when', r); - test(i === 0, 'happens asynchronously'); - setTimeout(function () { - test(i === 1, 'it was invoked'); - broadcast.that('test-when', r); - setTimeout(function () { - test(i === 1, 'it was NOT invoked'); - broadcast.when('test-when', increase); - setTimeout(function () { - test(i === 2, 'it was invoked again'); - done(); - }, delay); - }, delay); - }, delay); - }, delay * later); -}); - -later += 5; - -test.async(function (done) { - var - r1 = Math.random(), - r2 = r1 + Math.random(), - v - ; - function about(value) { v = value; } - broadcast.when('test-about', about); - setTimeout(function () { - test.log('## that'); - test(v === undefined, 'never invoked'); - broadcast.that('test-about', r1); - setTimeout(function () { - test(v === r1, 'value assigned'); - v = null; - broadcast.when('test-about', about); - broadcast.that('test-about', r2); - broadcast.when('test-about', about); - setTimeout(function () { - test(v === r2, 'new value'); - done(); - }, delay); - }, delay); - }, delay * later); -}); - -later += 5; - -test.async(function (done) { - var i = 0, r = Math.random(); - function increase() { i++; } - broadcast.when('test-drop', increase); - broadcast.drop('test-drop', increase); - broadcast.that('test-drop', r); - setTimeout(function () { - test.log('## drop'); - test(i === 0, 'it was NOT invoked'); - done(); - }, delay * later); -}); - - -later += 5; - -test.async(function (done) { - var other = broadcast['new'](); - var which = '', r = Math.random(); - other.when('test-new', function () { - which += 'other'; +tressa.async(function (done) { + var random = Math.random(); + var sameCalls = 0; + var thingsCalls = []; + broadcast.that('random', random); + broadcast.when('random').then(function (value) { + tressa.assert(value === random, 'promise resolved with right value'); }); - broadcast.when('test-new', function () { - which += 'broadcast'; + broadcast.when('random', function (value) { + tressa.assert(value === random, 'callback executed with right value'); }); - other.that('test-new', r); - setTimeout(function () { - test.log('## new'); - test(which === 'other', 'invoked as other'); - broadcast.that('test-new', r); - setTimeout(function () { - test(which === 'otherbroadcast', 'invoked as broadcast'); - done(); - }, delay); - }, delay * later); -}); - -later += 5; - -test.async(function (done) { - var args; - function increase() { args = arguments; } - broadcast.when('test-multiple-arguments', increase); - broadcast.that('test-multiple-arguments', 1, 2); - setTimeout(function () { - test.log('## multiple arguments'); - test(args[0] === 1 && args[1] === 2 && args.length === 2, 'expected arguments'); - done(); - }, delay * later); -}); - -later += 5; - -test.async(function (done) { - var args; - function increase() { args = arguments; } - broadcast.when('test-create-callback', increase); - setTimeout(function () { - test.log('## create callback'); - var fn = broadcast.that('test-create-callback'); - test(typeof fn === 'function', 'a function is returned'); - setTimeout(function () { - test(!args, 'nothing was resolved'); - fn('a', 'b', 'c'); - setTimeout(function () { - test( - args[0] === 'a' && - args[1] === 'b' && - args[2] === 'c', - 'listeners was invoked' - ); - done(); - }, delay); - }, delay); - }, delay * later); -}); - -later += 5; - -test.async(function (done) { - var i = 0, r = Math.random(); - function increase() { i++; } - broadcast.all('test-all', increase); - setTimeout(function () { - test.log('## all'); - test(i === 0, 'did not happen'); - broadcast.that('test-all', r); - test(i === 0, 'happens asynchronously'); + broadcast.when('another').then(function (value) { + tressa.assert(value !== random, 'another promise resolved with right value'); + }); + broadcast.when('another', same); + broadcast.when('another', same); + broadcast.that('another')(Math.random()); + broadcast.all('things', things); + broadcast.all('things', things); + broadcast.that('things')(1); + broadcast.that('things')(2); + broadcast.that('things')(3); + setTimeout(function () { + tressa.assert(JSON.stringify(thingsCalls) === '[1,2,3]', 'things invoked 3 times with 3 values'); + broadcast.drop('things', things); + broadcast.that('things')(1); setTimeout(function () { - test(i === 1, 'it was invoked'); - broadcast.that('test-all', r); - test(i === 1, 're-happens asynchronously'); - setTimeout(function () { - test(i === 2, 'it was invoked'); - // if already set don't set it again - broadcast.all('test-all', increase); - // drop it anyway - broadcast.drop('test-all', increase); - broadcast.that('test-all', r); - setTimeout(function () { - test(i === 2, 'it was NOT invoked again'); - // if already dropped don't fail or throw - broadcast.drop('test-all', increase); - done(); - }, delay); - }, delay); - }, delay); - }, delay * later); -}); - -later += 5; - -test.async(function (done) { - setTimeout(function () { - test.log('## .that(type, value):value'); - var rand = Math.random(); - var through = broadcast.that('test-through', rand); - broadcast.when('test-through', function (value) { - test(value === rand, 'same value resolved'); + tressa.assert(thingsCalls.length === 3, 'things was removed from invokes'); + broadcast.drop('things', things); + broadcast.drop('things'); done(); - }); - }, delay * later); + }, 100); + }, 100); + function things(value) { + thingsCalls.push(value); + } + function same(value) { + if (sameCalls++) throw new Error('too many invokes'); + tressa.assert(value !== random, 'another callback executed with right value'); + } }); - -later += 5; - -if (typeof Promise !== 'undefined') { - later += 5; - - test.async(function (done) { - setTimeout(function () { - test.log('## .when(type):Promise'); - var v = Math.random(), args; - var p = broadcast.when('test-promise'); - test(typeof p === 'object', 'object returned'); - broadcast.that('test-promise', v); - p.then(function (value) { - test(value === v, 'the value is the right one'); - done(); - }); - }, delay * later); - }); - - later += 5; - - test.async(function (done) { - setTimeout(function () { - var rand = Math.random(); - test.log('## .when(type):futureValue'); - Promise.resolve(rand) - .then(broadcast.that('test-future-value')) - .then(function (value) { - broadcast.when('test-future-value').then(function (later) { - test(value === later, 'later value is the same'); - test(value === rand, 'later value is correct'); - done(); - }); - }); - }, delay * later); - }); - -} diff --git a/utils/browserify.sh b/utils/browserify.sh deleted file mode 100644 index 595f684..0000000 --- a/utils/browserify.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env bash - -program () { - local bin="$1" - if [ -d "node_modules/$bin" ]; then - bin="$2" - else - if [ "$(which $bin)" = "" ]; then - mkdir -p node_modules - echo "installing $1" - npm install $bin >/dev/null 2>&1 - bin="$2" - fi - fi - echo $bin -} - -run () { - local bin="$(program 'browserify' 'node_modules/browserify/bin/cmd.js')" - $bin src/main.js -o build/bundle.max.js -d -} - -run \ No newline at end of file diff --git a/utils/jshint.sh b/utils/jshint.sh deleted file mode 100755 index 7813df4..0000000 --- a/utils/jshint.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env bash - -program () { - local bin="$1" - if [ -d "node_modules/$bin" ]; then - bin="$2" - else - if [ "$(which $bin)" = "" ]; then - mkdir -p node_modules - echo "installing $1" - npm install $bin >/dev/null 2>&1 - bin="$2" - fi - fi - echo $bin -} - -run () { - local bin="$(program 'jshint' 'node_modules/jshint/bin/jshint')" - local folder=$1 - local js="" - # drop some info in the stdout - echo "linting $f ... " - for f in $folder/*; do - # if it's a fodler, go for recursion - if [ -d "$f" ]; then - run "$f" - else - # grab .js files only - js=$(echo "$f" | sed 's/.js//') - if [ "$js" != "$f" ]; then - # finally use jshint to verify the file - $bin "$f" - # in case there was an error - if [[ $? -ne 0 ]] ; then - exit 1 - fi - fi - fi - done -} - -run src \ No newline at end of file diff --git a/utils/uglifyjs.sh b/utils/uglifyjs.sh deleted file mode 100644 index 17ded0e..0000000 --- a/utils/uglifyjs.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash - -run () { - local bin="uglifyjs" - if [ -d "node_modules/uglify-js" ]; then - bin="node_modules/uglify-js/bin/uglifyjs" - else - if [ "$(which $bin)" = "" ]; then - mkdir -p node_modules - echo "installing uglify-js" - npm install "uglify-js@1" >/dev/null 2>&1 - bin="node_modules/uglify-js/bin/uglifyjs" - fi - fi - echo "$@" >build/bundle.js - $bin --verbose build/bundle.max.js >>build/bundle.js - -} - -run "$@" \ No newline at end of file diff --git a/utils/watchify.sh b/utils/watchify.sh deleted file mode 100644 index 41232b3..0000000 --- a/utils/watchify.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env bash - -program () { - local bin="$1" - if [ -d "node_modules/$bin" ]; then - bin="$2" - else - if [ "$(which $bin)" = "" ]; then - mkdir -p node_modules - echo "installing $1" - npm install $bin >/dev/null 2>&1 - bin="$2" - fi - fi - echo $bin -} - -run () { - local bin="$(program 'watchify' 'node_modules/watchify/bin/cmd.js')" - $bin src/main.js -o build/bundle.max.js -v -} - -run