Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mrg-microevent

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mrg-microevent

mrg-microevent is a library which provides the observer pattern to JavaScript objects

  • 0.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

MicroEvent.js

MicroEvent.js is a event emitter library which provides the observer pattern to javascript objects.

How to Use It

You need a single file microevent.js. Include it in a webpage via the usual script tag.

<script src="microevent.js"></script>

To include it in a nodejs code isnt much harder

var MicroEvent = require('./microevent.js')

Now suppose you got a class Foobar, and you wish it to support the observer partern. do

MicroEvent.mixin(Foobar)

That's it. The repository contains an example in browser and an example in nodejs. Both use the same code in different contexts. Let me walk you thru it.

Example

First we define the class which gonna use MicroEvent.js. This is a ticker, it is triggering 'tick' event every second, and add the current date as parameter

var Ticker = function(){
    var self = this;
    setInterval(function(){
        self.trigger('tick', new Date());
    }, 1000);
};

We mixin MicroEvent into Ticker and we are all set.

MicroEvent.mixin(Ticker);

Now lets actually use the Ticker Class. First, create the object.

var ticker = new Ticker();

and bind our tick event with its data parameter

ticker.bind('tick', function(date) {
    console.log('notified date', date);
});

And you will see this output:

notified date Tue, 22 Mar 2011 14:43:41 GMT
notified date Tue, 22 Mar 2011 14:43:42 GMT
...

Conclusion

MicroEvent.js is available on github here under MIT license. If you hit bugs, fill issues on github. Feel free to fork, modify and have fun with it :)

Keywords

FAQs

Package last updated on 07 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc