Giter VIP home page Giter VIP logo

brcast's Issues

Should setState merge with previous via Object.assign?

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.

UglifyJs: Unexpected token: name (listeners)

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.