change-emitter

WebJar for change-emitter

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

change-emitter
Last Version

Last Version

0.1.6
Release Date

Release Date

Type

Type

jar
Description

Description

change-emitter
WebJar for change-emitter
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/acdlite/change-emitter

Download change-emitter

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/change-emitter/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>change-emitter</artifactId>
    <version>0.1.6</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/change-emitter/
implementation 'org.webjars.npm:change-emitter:0.1.6'
// https://jarcasting.com/artifacts/org.webjars.npm/change-emitter/
implementation ("org.webjars.npm:change-emitter:0.1.6")
'org.webjars.npm:change-emitter:jar:0.1.6'
<dependency org="org.webjars.npm" name="change-emitter" rev="0.1.6">
  <artifact name="change-emitter" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='change-emitter', version='0.1.6')
)
libraryDependencies += "org.webjars.npm" % "change-emitter" % "0.1.6"
[org.webjars.npm/change-emitter "0.1.6"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

change-emitter

build status npm version

Listen for changes. Like an event emitter that only emits a single event type. Really tiny.

I extracted this from Redux's createStore() because I found it to be useful in other contexts. Use it where you want the most minimal event subscription implementation possible.

Usage

import { createChangeEmitter } from 'change-emitter'

const emitter = createChangeEmitter()

// Called `listen` instead of `subscribe` to avoid confusion with observable spec
const unlisten = emitter.listen((...args) => {
  console.log(args)
})

emitter.emit(1, 2, 3) // logs `[1, 2, 3]`
unlisten()
emitter.emit(4, 5, 6) // doesn't log

Larger example

Here's a (partial) implementation of Redux's createStore:

const createStore = (reducer, initialState) => {
  let state = initialState
  const emitter = createChangeEmitter()

  function dispatch(action) {
    state = reducer(state, action)
    emitter.emit()
    return action
  }

  function getState() {
    return state
  }

  return {
    dispatch,
    getState,
    subscribe: emitter.listen
  }
}

Versions

Version
0.1.6