Giter VIP home page Giter VIP logo

brcast's Introduction

Brcast

Tiny data broadcaster with 0 dependencies

Travis Code Coverage David npm npm JavaScript Style Guide MIT License

The current size of brcast/dist/brcast.umd.min.js is:

gzip size

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

This project uses node and npm. Go check them out if you don't have them locally installed.

$ npm install --save brcast

Then with a module bundler like rollup or webpack, use as you would anything else:

// using ES6 modules
import brcast from 'brcast'

// using CommonJS modules
var brcast = require('brcast')

The UMD build is also available on unpkg:

<script src="https://unpkg.com/brcast/dist/brcast.umd.js"></script>

You can find the library on window.brcast.

Usage

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

$ npm run test

MIT License © Alessandro Arnodo

brcast's People

Contributors

esprehn avatar vesparny avatar zholmes1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

esprehn

brcast's Issues

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)

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.

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.