vesparny / brcast Goto Github PK
View Code? Open in Web Editor NEWTiny data broadcaster with 0 dependencies
License: MIT License
Tiny data broadcaster with 0 dependencies
License: MIT License
The 2.0.1 patch release is throwing in error on our site whereas 2.0.0 was fine.
The problem is occurring due to an earlier listener causing a later listener on in the listeners object to no longer exist, thus throwing an error.
Perhaps the iteration over the listeners should check to see if it is a function before calling it?
First of all, thanks for writing this! It's very useful to have this pattern wrapped up in a node module!
I caught myself expecting setState()
to merge the new state with the previous state (like React does). Do you think you'd accept a PR to change currentState = state
to Object.assign(currentState, state)
?
The downside is that it would rely on people having Object.assign
polyfilled for older browser support.
From webpack:
ERROR in vendor.8f839b5a1ada59daa303.js from UglifyJs
Unexpected token: name (listeners) [vendor.8f839b5a1ada59daa303.js:2174,6]
Caused because of this library (used in material-ui 1.0.0-beta.4).
After removing "jsnext:main": "index.js",
from the library package.json
build was done correctly
Webpack bundle:
/***/ "./node_modules/brcast/index.js":
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony export (immutable) */ __webpack_exports__["default"] = createBroadcast;
function createBroadcast (initialState) {
let listeners = {}
let id = 0
let _state = initialState
const getState = () => _state
const setState = state => {
_state = state
const keys = Object.keys(listeners)
for (let i = 0; i < keys.length; i += 1) {
// if a listener gets unsubscribed during setState we just skip it
if (typeof listeners[keys[i]] !== 'undefined') {
listeners[keys[i]](state)
}
}
}
const subscribe = listener => {
if (typeof listener !== 'function') { throw new Error('listener must be a function.') }
const currentId = id
let isSubscribed = true
listeners[currentId] = listener
id += 1
return function unsubscribe () {
// in case unsubscribe gets called multiple times we simply return
if (!isSubscribed) return
isSubscribed = false
delete listeners[currentId]
}
}
return { getState, setState, subscribe }
}
/***/ }),
Error thrown in line let listeners = {}
.
Maybe my weback configuration is incorrect but I use a lot of different libraries and don't have any problem.
I had same issue few months ago in another library - here is interesting post
shuhei/material-colors#13 (comment)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.