Added logging, changed some directory structure

This commit is contained in:
2018-01-13 21:33:40 -05:00
parent f079a5f067
commit 8e72ffb917
73656 changed files with 35284 additions and 53718 deletions

View File

@@ -0,0 +1,97 @@
# Change Log
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
<a name="3.0.1"></a>
## [3.0.1](https://github.com/vesparny/brcast/compare/v3.0.0...v3.0.1) (2017-08-16)
### Bug Fixes
* point jsnext:main to the transpiled es module closes #7 ([9b182e2](https://github.com/vesparny/brcast/commit/9b182e2)), closes [#7](https://github.com/vesparny/brcast/issues/7)
<a name="3.0.0"></a>
# [3.0.0](https://github.com/vesparny/brcast/compare/v2.0.2...v3.0.0) (2017-08-02)
### Performance Improvements
* improve performance by not allocating an unsubscribe function for ([e70f01e](https://github.com/vesparny/brcast/commit/e70f01e))
### BREAKING CHANGES
* subscribe does not return a function anymore, but a
subscriptionId instead
<a name="2.0.2"></a>
## [2.0.2](https://github.com/vesparny/brcast/compare/v2.0.1...v2.0.2) (2017-07-27)
### Bug Fixes
* prevent handlers unsubscribed in the middle of setState from being called (#5) ([3ad6f58](https://github.com/vesparny/brcast/commit/3ad6f58))
<a name="2.0.1"></a>
## [2.0.1](https://github.com/vesparny/brcast/compare/v2.0.0...v2.0.1) (2017-06-28)
### Bug Fixes
* increase performance by using an object literal for storing listeners ([4d36f91](https://github.com/vesparny/brcast/commit/4d36f91))
<a name="2.0.0"></a>
# [2.0.0](https://github.com/vesparny/brcast/compare/v1.1.6...v2.0.0) (2017-04-19)
### Chores
* remove mitt dependency ([52929e6](https://github.com/vesparny/brcast/commit/52929e6))
### BREAKING CHANGES
* does not support channel anymore
<a name="1.1.6"></a>
## [1.1.6](https://github.com/vesparny/brcast/compare/v1.1.4...v1.1.6) (2017-04-08)
<a name="1.1.4"></a>
## [1.1.4](https://github.com/vesparny/brcast/compare/v1.1.3...v1.1.4) (2017-04-08)
<a name="1.1.3"></a>
## [1.1.3](https://github.com/vesparny/brcast/compare/v1.1.2...v1.1.3) (2017-04-07)
<a name="1.1.2"></a>
## [1.1.2](https://github.com/vesparny/brcast/compare/v1.1.1...v1.1.2) (2017-04-07)
<a name="1.1.1"></a>
## [1.1.1](https://github.com/vesparny/brcast/compare/v1.1.0...v1.1.1) (2017-04-07)
<a name="1.1.0"></a>
# 1.1.0 (2017-04-07)
### Features
* **lib:** init ([cefcbb2](https://github.com/vesparny/brcast/commit/cefcbb2))

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017-present Alessandro Arnodo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,139 @@
# Brcast
> Tiny data broadcaster with 0 dependencies
[![Travis](https://img.shields.io/travis/vesparny/brcast.svg)](https://travis-ci.org/vesparny/brcast)
[![Code Coverage](https://img.shields.io/codecov/c/github/vesparny/brcast.svg?style=flat-square)](https://codecov.io/github/vesparny/brcast)
[![David](https://img.shields.io/david/vesparny/brcast.svg)](https://david-dm.org/vesparny/brcast)
[![npm](https://img.shields.io/npm/v/brcast.svg)](https://www.npmjs.com/package/brcast)
[![npm](https://img.shields.io/npm/dm/brcast.svg)](https://npm-stat.com/charts.html?package=brcast&from=2017-04-01)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
[![MIT License](https://img.shields.io/npm/l/brcast.svg?style=flat-square)](https://github.com/vesparny/brcast/blob/master/LICENSE)
The current size of `brcast/dist/brcast.umd.min.js` is:
[![gzip size](http://img.badgesize.io/https://unpkg.com/brcast/dist/brcast.umd.min.js?compression=gzip&label=gzip%20size&style=flat-square)](https://unpkg.com/brcast/dist/)
It's like a data store you can subscribe to, with a setter to pump data in.
For browsers and node.
## Table of Contents
- [Install](#install)
- [Usage](#usage)
- [API](#API)
- [Testing](#tests)
- [License](#license)
## Install
This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.
```sh
$ npm install --save brcast
```
Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else:
```javascript
// using ES6 modules
import brcast from 'brcast'
// using CommonJS modules
var brcast = require('brcast')
```
The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):
```html
<script src="https://unpkg.com/brcast/dist/brcast.umd.js"></script>
```
You can find the library on `window.brcast`.
## Usage
```js
import brcast from 'brcast'
let broadcast = brcast()
// subscribe
const subscriptionId = broadcast.subscribe(state => console.log(state))
// setState sets the state and invoke all subscription callbacks passing in the state
broadcast.setState(1)
// setState returns the current state
broadcast.getState()
// unsubscribe to unbind the handler
broadcast.unsubscribe(subscriptionId)
```
## API
### `brcast([initialState])`
Creates a `broadcast` object.
#### Arguments
1 - [`initialState`] *(any)*: The initial state.
#### Returns
(`broadcast`): An object that holds the state.
### `broadcast.setState(state)`
Store the new state.
#### Arguments
1 - `state` *(any)*: The new state.
#### Returns
Nothing.
### `broadcast.getState()`
Get the stored state.
#### Returns
(`Any`): The stored state.
### `broadcast.subscribe(handler)`
Subscribe to state changes.
#### Arguments
1 - `handler` *(Function)*: The callback to be invoked any time the state changes.
#### Returns
(`Number`): The subscription id to be used to unsubscribe.
### `broadcast.unsubscribe(subscriptionId)`
Unsubscribe the change listener.
#### Arguments
1 - `subscriptionId` *(Number)*: The subscription id returned by subscribing.
#### Returns
Nothing.
## Tests
```sh
$ npm run test
```
[MIT License](LICENSE.md) © [Alessandro Arnodo](https://alessandro.arnodo.net/)

View File

@@ -0,0 +1,40 @@
function createBroadcast (initialState) {
var listeners = {};
var id = 1;
var _state = initialState;
function getState () {
return _state
}
function setState (state) {
_state = state;
var keys = Object.keys(listeners);
var i = 0;
var len = keys.length;
for (; i < len; i++) {
// if a listener gets unsubscribed during setState we just skip it
if (listeners[keys[i]]) { listeners[keys[i]](state); }
}
}
// subscribe to changes and return the subscriptionId
function subscribe (listener) {
if (typeof listener !== 'function') {
throw new Error('listener must be a function.')
}
var currentId = id;
listeners[currentId] = listener;
id += 1;
return currentId
}
// remove subscription by removing the listener function
function unsubscribe (id) {
listeners[id] = undefined;
}
return { getState: getState, setState: setState, subscribe: subscribe, unsubscribe: unsubscribe }
}
module.exports = createBroadcast;

View File

@@ -0,0 +1,2 @@
function createBroadcast(t){var e={},r=1,n=t;return{getState:function(){return n},setState:function(t){n=t;for(var r=Object.keys(e),o=0,u=r.length;o<u;o++)e[r[o]]&&e[r[o]](t)},subscribe:function(t){if("function"!=typeof t)throw new Error("listener must be a function.");var n=r;return e[n]=t,r+=1,n},unsubscribe:function(t){e[t]=void 0}}}module.exports=createBroadcast;
//# sourceMappingURL=brcast.cjs.min.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"brcast.cjs.min.js","sources":["../index.js"],"sourcesContent":["export default function createBroadcast (initialState) {\n let listeners = {}\n let id = 1\n let _state = initialState\n\n function getState () {\n return _state\n }\n\n function setState (state) {\n _state = state\n const keys = Object.keys(listeners)\n let i = 0\n const len = keys.length\n for (; i < len; i++) {\n // if a listener gets unsubscribed during setState we just skip it\n if (listeners[keys[i]]) listeners[keys[i]](state)\n }\n }\n\n // subscribe to changes and return the subscriptionId\n function subscribe (listener) {\n if (typeof listener !== 'function') {\n throw new Error('listener must be a function.')\n }\n const currentId = id\n listeners[currentId] = listener\n id += 1\n return currentId\n }\n\n // remove subscription by removing the listener function\n function unsubscribe (id) {\n listeners[id] = undefined\n }\n\n return { getState, setState, subscribe, unsubscribe }\n}\n"],"names":["createBroadcast","initialState","let","listeners","id","_state","getState","setState","state","const","keys","Object","i","len","length","subscribe","listener","Error","currentId","unsubscribe","undefined"],"mappings":"AAAe,SAASA,gBAAiBC,GACvCC,IAAIC,KACAC,EAAK,EACLC,EAASJ,EAiCb,OAASK,SA/BT,WACE,OAAOD,GA8BUE,SA3BnB,SAAmBC,GACjBH,EAASG,EAIT,IAHAC,IAAMC,EAAOC,OAAOD,KAAKP,GACrBS,EAAI,EACFC,EAAMH,EAAKI,OACVF,EAAIC,EAAKD,IAEVT,EAAUO,EAAKE,KAAKT,EAAUO,EAAKE,IAAIJ,IAoBlBO,UAf7B,SAAoBC,GAClB,GAAwB,mBAAbA,EACT,MAAM,IAAIC,MAAM,gCAElBR,IAAMS,EAAYd,EAGlB,OAFAD,EAAUe,GAAaF,EACvBZ,GAAM,EACCc,GAQ+BC,YAJxC,SAAsBf,GACpBD,EAAUC,QAAMgB"}

View File

@@ -0,0 +1,40 @@
function createBroadcast (initialState) {
var listeners = {};
var id = 1;
var _state = initialState;
function getState () {
return _state
}
function setState (state) {
_state = state;
var keys = Object.keys(listeners);
var i = 0;
var len = keys.length;
for (; i < len; i++) {
// if a listener gets unsubscribed during setState we just skip it
if (listeners[keys[i]]) { listeners[keys[i]](state); }
}
}
// subscribe to changes and return the subscriptionId
function subscribe (listener) {
if (typeof listener !== 'function') {
throw new Error('listener must be a function.')
}
var currentId = id;
listeners[currentId] = listener;
id += 1;
return currentId
}
// remove subscription by removing the listener function
function unsubscribe (id) {
listeners[id] = undefined;
}
return { getState: getState, setState: setState, subscribe: subscribe, unsubscribe: unsubscribe }
}
export default createBroadcast;

View File

@@ -0,0 +1,48 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.brcast = factory());
}(this, (function () {
function createBroadcast (initialState) {
var listeners = {};
var id = 1;
var _state = initialState;
function getState () {
return _state
}
function setState (state) {
_state = state;
var keys = Object.keys(listeners);
var i = 0;
var len = keys.length;
for (; i < len; i++) {
// if a listener gets unsubscribed during setState we just skip it
if (listeners[keys[i]]) { listeners[keys[i]](state); }
}
}
// subscribe to changes and return the subscriptionId
function subscribe (listener) {
if (typeof listener !== 'function') {
throw new Error('listener must be a function.')
}
var currentId = id;
listeners[currentId] = listener;
id += 1;
return currentId
}
// remove subscription by removing the listener function
function unsubscribe (id) {
listeners[id] = undefined;
}
return { getState: getState, setState: setState, subscribe: subscribe, unsubscribe: unsubscribe }
}
return createBroadcast;
})));

View File

@@ -0,0 +1,2 @@
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.brcast=t()}(this,function(){return function(e){var t={},n=1,o=e;return{getState:function(){return o},setState:function(e){o=e;for(var n=Object.keys(t),r=0,u=n.length;r<u;r++)t[n[r]]&&t[n[r]](e)},subscribe:function(e){if("function"!=typeof e)throw new Error("listener must be a function.");var o=n;return t[o]=e,n+=1,o},unsubscribe:function(e){t[e]=void 0}}}});
//# sourceMappingURL=brcast.umd.min.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"brcast.umd.min.js","sources":["../index.js"],"sourcesContent":["export default function createBroadcast (initialState) {\n let listeners = {}\n let id = 1\n let _state = initialState\n\n function getState () {\n return _state\n }\n\n function setState (state) {\n _state = state\n const keys = Object.keys(listeners)\n let i = 0\n const len = keys.length\n for (; i < len; i++) {\n // if a listener gets unsubscribed during setState we just skip it\n if (listeners[keys[i]]) listeners[keys[i]](state)\n }\n }\n\n // subscribe to changes and return the subscriptionId\n function subscribe (listener) {\n if (typeof listener !== 'function') {\n throw new Error('listener must be a function.')\n }\n const currentId = id\n listeners[currentId] = listener\n id += 1\n return currentId\n }\n\n // remove subscription by removing the listener function\n function unsubscribe (id) {\n listeners[id] = undefined\n }\n\n return { getState, setState, subscribe, unsubscribe }\n}\n"],"names":["initialState","let","listeners","id","_state","getState","setState","state","const","keys","Object","i","len","length","subscribe","listener","Error","currentId","unsubscribe","undefined"],"mappings":"4KAAe,SAA0BA,GACvCC,IAAIC,KACAC,EAAK,EACLC,EAASJ,EAiCb,OAASK,SA/BT,WACE,OAAOD,GA8BUE,SA3BnB,SAAmBC,GACjBH,EAASG,EAIT,IAHAC,IAAMC,EAAOC,OAAOD,KAAKP,GACrBS,EAAI,EACFC,EAAMH,EAAKI,OACVF,EAAIC,EAAKD,IAEVT,EAAUO,EAAKE,KAAKT,EAAUO,EAAKE,IAAIJ,IAoBlBO,UAf7B,SAAoBC,GAClB,GAAwB,mBAAbA,EACT,MAAM,IAAIC,MAAM,gCAElBR,IAAMS,EAAYd,EAGlB,OAFAD,EAAUe,GAAaF,EACvBZ,GAAM,EACCc,GAQ+BC,YAJxC,SAAsBf,GACpBD,EAAUC,QAAMgB"}

View File

@@ -0,0 +1,38 @@
export default function createBroadcast (initialState) {
let listeners = {}
let id = 1
let _state = initialState
function getState () {
return _state
}
function setState (state) {
_state = state
const keys = Object.keys(listeners)
let i = 0
const len = keys.length
for (; i < len; i++) {
// if a listener gets unsubscribed during setState we just skip it
if (listeners[keys[i]]) listeners[keys[i]](state)
}
}
// subscribe to changes and return the subscriptionId
function subscribe (listener) {
if (typeof listener !== 'function') {
throw new Error('listener must be a function.')
}
const currentId = id
listeners[currentId] = listener
id += 1
return currentId
}
// remove subscription by removing the listener function
function unsubscribe (id) {
listeners[id] = undefined
}
return { getState, setState, subscribe, unsubscribe }
}

View File

@@ -0,0 +1,115 @@
import brcast from './index'
test('default export is a function', () => {
expect(typeof brcast).toBe('function')
})
test('exposes the public API', () => {
const broadcast = brcast()
const methods = Object.keys(broadcast)
expect(methods.length).toBe(4)
expect(methods).toContain('subscribe')
expect(methods).toContain('unsubscribe')
expect(methods).toContain('getState')
expect(methods).toContain('setState')
})
test('throws if listener is not a function', () => {
const broadcast = brcast()
expect(() => broadcast.subscribe()).toThrow()
expect(() => broadcast.subscribe('throw')).toThrow()
expect(() => broadcast.subscribe({})).toThrow()
expect(() => broadcast.subscribe(() => {})).not.toThrow()
})
test('is able to start with an undefined state and update it accordingly', () => {
const broadcast = brcast()
expect(broadcast.getState()).toBeUndefined()
broadcast.setState(2)
expect(broadcast.getState()).toBe(2)
})
test('it updates the state', () => {
const handler = jest.fn()
const broadcast = brcast()
broadcast.subscribe(handler)
broadcast.setState(2)
expect(handler.mock.calls.length).toBe(1)
expect(handler.mock.calls[0][0]).toBe(2)
})
test('it unsubscribes only relevant listeners', () => {
const handler = jest.fn()
const handler1 = jest.fn()
const broadcast = brcast(1)
const subscriptionId = broadcast.subscribe(handler)
broadcast.subscribe(handler1)
broadcast.unsubscribe(subscriptionId)
broadcast.setState(2)
broadcast.setState(3)
expect(handler.mock.calls.length).toBe(0)
expect(handler1.mock.calls.length).toBe(2)
})
test('removes listeners only once when unsubscribing more than once', () => {
const handler = jest.fn()
const broadcast = brcast(1)
const subscriptionId = broadcast.subscribe(handler)
broadcast.unsubscribe(subscriptionId)
broadcast.unsubscribe(subscriptionId)
broadcast.setState(2)
expect(handler.mock.calls.length).toBe(0)
})
test('supports removing a subscription within a subscription', () => {
const broadcast = brcast(1)
const handler = jest.fn()
const handler1 = jest.fn()
const handler2 = jest.fn()
broadcast.subscribe(handler)
const sub1Id = broadcast.subscribe(() => {
handler1()
broadcast.unsubscribe(sub1Id)
})
broadcast.subscribe(handler2)
broadcast.setState(2)
broadcast.setState(3)
expect(handler.mock.calls.length).toBe(2)
expect(handler1.mock.calls.length).toBe(1)
expect(handler2.mock.calls.length).toBe(2)
})
test('do not notify subscribers getting unsubscribed in the middle of a setState', () => {
const broadcast = brcast()
const unsubscribeIds = []
const doUnsubscribeAll = () =>
unsubscribeIds.forEach(id => broadcast.unsubscribe(id))
const handler = jest.fn()
const handler1 = jest.fn()
const handler2 = jest.fn()
unsubscribeIds.push(broadcast.subscribe(handler))
unsubscribeIds.push(
broadcast.subscribe(() => {
handler1()
doUnsubscribeAll()
})
)
unsubscribeIds.push(broadcast.subscribe(handler2))
broadcast.setState(2)
expect(handler.mock.calls.length).toBe(1)
expect(handler1.mock.calls.length).toBe(1)
expect(handler2.mock.calls.length).toBe(0)
broadcast.setState(3)
expect(handler.mock.calls.length).toBe(1)
expect(handler1.mock.calls.length).toBe(1)
expect(handler2.mock.calls.length).toBe(0)
})

View File

@@ -0,0 +1,113 @@
{
"_args": [
[
"brcast@3.0.1",
"C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project"
]
],
"_from": "brcast@3.0.1",
"_id": "brcast@3.0.1",
"_inBundle": false,
"_integrity": "sha512-eI3yqf9YEqyGl9PCNTR46MGvDylGtaHjalcz6Q3fAPnP/PhpKkkve52vFdfGpwp4VUvK6LUr4TQN+2stCrEwTg==",
"_location": "/material-ui/brcast",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "brcast@3.0.1",
"name": "brcast",
"escapedName": "brcast",
"rawSpec": "3.0.1",
"saveSpec": null,
"fetchSpec": "3.0.1"
},
"_requiredBy": [
"/material-ui",
"/material-ui/theming"
],
"_resolved": "https://registry.npmjs.org/brcast/-/brcast-3.0.1.tgz",
"_spec": "3.0.1",
"_where": "C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project",
"amdName": "brcast",
"authors": [
"Alessandro Arnodo <alessandro@arnodo.net>"
],
"bugs": {
"url": "https://github.com/vesparny/brcast/issues"
},
"dependencies": {},
"description": "Tiny data broadcaster with 0 dependencies",
"devDependencies": {
"babel-core": "^6.24.1",
"babel-eslint": "^7.2.2",
"babel-preset-es2015": "^6.24.1",
"babel-register": "^6.24.1",
"cross-env": "^5.0.1",
"gzip-size-cli": "^2.0.0",
"husky": "^0.14.3",
"jest": "^20.0.4",
"lint-staged": "^4.0.2",
"npm-run-all": "^4.0.2",
"prettier": "^1.5.3",
"rimraf": "^2.5.2",
"rollup": "^0.45.2",
"rollup-plugin-buble": "^0.15.0",
"rollup-plugin-uglify": "^2.0.1",
"standard": "^10.0.2",
"standard-version": "^4.0.0"
},
"files": [
"dist",
"index.js",
"index.spec.js"
],
"homepage": "https://github.com/vesparny/brcast",
"jsnext:main": "dist/brcast.es.js",
"keywords": [
"events",
"eventemitter",
"pubsub",
"broadcast"
],
"license": "MIT",
"lint-staged": {
"*.js": [
"prettier --write --semi false --single-quote",
"standard --fix",
"git add"
]
},
"main": "dist/brcast.cjs.js",
"module": "dist/brcast.es.js",
"name": "brcast",
"repository": {
"type": "git",
"url": "git+https://github.com/vesparny/brcast.git"
},
"scripts": {
"build": "npm-run-all test clean rollup rollup:min size",
"bump": "standard-version",
"clean": "rimraf dist",
"format": "prettier --write --semi false '*.js' && standard --fix",
"lint": "standard",
"precommit": "lint-staged",
"release": "npm run build && npm run bump && git push --follow-tags origin master && npm publish",
"rollup": "rollup -c",
"rollup:min": "cross-env MINIFY=minify rollup -c",
"size": "echo \"Gzipped Size: $(cat dist/brcast.umd.min.js | gzip-size)\"",
"test": "npm run lint && npm run testonly",
"testonly": "jest --coverage"
},
"standard": {
"parser": "babel-eslint",
"globals": [
"jest",
"expect",
"it",
"test",
"describe"
]
},
"umd:main": "dist/brcast.umd.js",
"version": "3.0.1"
}