Git Product home page Git Product logo

evmt's Introduction

Evmt

Self contained event emitter

Build Status Coverage Status

Communicate between layers of code without the worry to build a complex solution, thinking of depth, or event injection.

Quick start

Install

npm install --save evmt

Usage

Basic concept

import Evmt from 'evmt'

const onAnimalGoes = new Evmt()
const animalGoes = (animal, sound) => {
  console.log(`${animal} goes ${sound}`)
  onAnimalGoes.emit(animal, sound)
}

// Subscribes to the event
const subscription1 = onAnimalGoes.subscribe((animal) => {
  console.log(`The ${animal} should be quiet!`)
})
const subscription2 = onAnimalGoes.subscribe((animal, sound) => {
  console.log(`I like when the ${animal} goes ${sound}`)
})


animalGoes('Sheep', 'beep') // Emits to both subscribed functions
subscription2.remove()
animalGoes('Cow', 'meow') // Emits to the first subscribed function
subscription1.remove()
animalGoes('Cat', 'Bazinga') // Emits but there is nothing to listen

Advanced usage, with components

import Evmt from 'evmt'

const AnimalListComponent = {
  select(animal) {
    AnimalService.select(animal)
  }
}

const AnimalService = {
  onSelect: new Evmt(),
  select(animal) {
    console.log(`Selected ${animal}`)
    this.onSelect.emit(animal)
  }
}

const FarmComponent = {
  init() {
    this.farmAnimals = []
    this.handleSelectedAnimal = this.handleSelectedAnimal.bind(this)
    this.subscriptions = [
      AnimalService.onSelect.subscribe(this.handleSelectedAnimal)
    ]
  },
  destroy() {
    this.subscriptions.forEach(sub => sub.remove())
  },
  handleSelectedAnimal(animal) {
    this.farmAnimals.push(animal)
    console.log(animal, this.farmAnimals)
  }
}

FarmComponent.init()
AnimalListComponent.select('cow')
AnimalListComponent.select('cat')
AnimalListComponent.select('dog')
FarmComponent.destroy()
AnimalListComponent.select('pig')
AnimalListComponent.select('bat')

Documentation

Evmt

It returns it's own instance which is used to subscribe to and emit events.

emit([arg1, ..., argN])

Calls/emits each subscribed callback passing the parameters it received.

Param Type Description
arg1...argN any A list of values to be emitted
Example
const onSelect = new Evmt()

onSelect.emit(1, '2', { exp: 3 }, [4])

subscribe(callback)

Subscribes an callback into an event to wait for the emit. It returns an "subscription" that can removes itself from the subscriptions.

Param Type Description
callback Function A function to handle the emitted event
Example
const onSelect = new Evmt()

const subscription = onSelect.subscribe((param1, param2, param3, param4) => {
  console.log(param1, param2, param3, param4)
})

subscription.remove()

remove(index)

Removes the subscribed function from the subscriptions by it's index

Param Type Description
index Number The subscription index to be removed
Example
const onSelect = new Evmt()

const subscription = onSelect.subscribe((param1, param2, param3, param4) => {
  console.log(param1, param2, param3, param4)
})

// It's the same as subscription.remove()
onSelect.remove(0)

evmt's People

Contributors

klauskpm avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

evmt's Issues

Create NPM scripts

The NPM scripts in package.json must be:

{
  "scripts": {
    "dev": "rollup -c -w",
    "build": "rollup -c",
    "test": "karma start --single-run",
    "test:watch": "karma start",
    "prepublish": "npm run build",
    "release": "np"
  },
}

Create git hooks

Set a ESLint verify before a git commit and a minimum coverage threshold verification before a git push.

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.