npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lite-event

v1.0.5

Published

OOP Events based from [TypeScript LiteEvent](https://gist.github.com/JasonKleban/50cee44960c225ac1993c922563aa540)

Downloads

7

Readme

lite-event

OOP Events based from TypeScript LiteEvent

install for npm

Install package with NPM and add it to your dependencies:

npm install lite-event --save

lite-event functions

Initialization

var event = new LiteEvent();
var handler = (sender, args)=>{
	// reaction to an event
}

Subscribe

event.on(handler);

Unsubscribe

by on/off subscribe

event.on(handler);
// some code
event.off(handler);

or one time subscribe

event.one(handler);

Remove all subscribe

event.clean();

Trigger event

var args = {
	//some args
};
event.trigger(this, args);

Usage from typescript

Syntactic sugar

import objects

import { LiteEvent, IEventHandler, IEventHandler } from "lite-event";

use

interface IMyClassFireEventArgs extends IEventHandler {
    count: number;
}

class MyCounter {

    private _count = 0;
    private _fireEvent = new LiteEvent<MyCounter, IMyClassFireEventArgs>();

    public onIncrement(handler: IEventHandler<MyCounter, IMyClassFireEventArgs>) {
        this._fireEvent.on(handler);
        return this;
    }

    public offIncrement(handler: IEventHandler<MyCounter, IMyClassFireEventArgs>) {
        this._fireEvent.off(handler);
        return this;
    }
    
    public oneIncrement(handler: IEventHandler<MyCounter, IMyClassFireEventArgs>) {
        this._fireEvent.one(handler);
        return this;
    }

    public increment() {
        this._count++;
        this._fireEvent.trigger(this, {
            count: this._count
        });
        return this;
    }
}

var myCounter = new MyCounter();
var heandler = (counter, args) => {
        if(args.count === 2) {
            console.log("stop if count equally 2 (count = " + args.count + ")");
        }
    };
myCounter
    .onIncrement((counter, args) => {
        console.log("always (count = " + args.count + ")");
    }).onIncrement().oneIncrement((counter, args) => {
        console.log("one (count = " + args.count + ")");
    });

myCounter.increment();
myCounter.increment();
myCounter.increment();
myCounter.increment();

Default example

import objects

import { LiteEvent, IEventHandler, IEventHandler } from "lite-event";

use

interface IMyClassFireEventArgs {
    count: number;
}

class MyCounter {

    private _count = 0;
    private _incrementEvent = new LiteEvent<MyCounter, IMyClassFireEventArgs>();

    public get incremented(): IEvent<MyCounter, IMyClassFireEventArgs> { return this._incrementEvent; }

    public increment() {
        this._count++;
        this._incrementEvent.trigger(this, {
            count: this._count
        });
        return this;
    }
}

var myCounter = new MyCounter();
var heandler = (counter, args) => {
    if (args.count === 2) {
        console.log("stop if count equally 2 (count = " + args.count + ")");
    } else {
        myCounter.incremented.off(heandler);
    }
};

myCounter.incremented.on((counter, args) => {
    console.log("always (count = " + args.count + ")");
});

myCounter.incremented.one((counter, args) => {
    console.log("one (count = " + args.count + ")");
});

myCounter.incremented.on(heandler);

myCounter.increment();
myCounter.increment();
myCounter.increment();
myCounter.increment();

Usage from javascript

ES6

import objects

import { LiteEvent, IEventHandler, IEventHandler } from "lite-event";

use

class MyCounter {
    constructor() {
        this._count = 0;
        this._incrementEvent = new LiteEvent();
    }
    get incremented() { return this._incrementEvent; }
    increment() {
        this._count++;
        this._incrementEvent.trigger(this, {
            count: this._count
        });
        return this;
    }
}

var myCounter = new MyCounter();
var heandler = (counter, args) => {
    if (args.count === 2) {
        console.log("stop if count equally 2 (count = " + args.count + ")");
    } else {
        myCounter.incremented.off(heandler);
    }
};

myCounter.incremented.on((counter, args) => {
    console.log("always (count = " + args.count + ")");
});

myCounter.incremented.one((counter, args) => {
    console.log("one (count = " + args.count + ")");
});

myCounter.incremented.on(heandler);

myCounter.increment();
myCounter.increment();
myCounter.increment();
myCounter.increment();

ES3

import objects

var LiteEvent = require("lite-event").LiteEvent;

use

var MyCounter = (function () {
    function MyCounter() {
        this._count = 0;
        this._incrementEvent = new LiteEvent();
    }

    LiteEvent.prototype.increment = function () {
        var oneHendler = function (sender, args) {
            this._count++;
            this._incrementEvent.trigger(this, {
                count: this._count
            });
            return this;
        };
    };
    return MyCounter;
}());

var myCounter = new MyCounter();
var heandler = (counter, args) => {
    if (args.count === 2) {
        console.log("stop if count equally 2 (count = " + args.count + ")");
    } else {
        myCounter.incremented.off(heandler);
    }
};

myCounter.incremented.on((counter, args) => {
    console.log("always (count = " + args.count + ")");
});

myCounter.incremented.one((counter, args) => {
    console.log("one (count = " + args.count + ")");
});

myCounter.incremented.on(heandler);

myCounter.increment();
myCounter.increment();
myCounter.increment();
myCounter.increment();