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

easy-event

v1.2.0

Published

easy-event 是一个简单易用的事件模块。提供了3个类:Event、EventDispatcher 和 EventCenter。

Downloads

3

Readme

easy-event

easy-event 是一个简单易用的事件模块。提供了3个类:EventEventDispatcherEventCenter

Event 类作为创建事件实例的基类,当发生事件时,Event 对象将作为参数传递给事件侦听器。Event 类的属性包含有关事件的基本信息。

EventDispatcher 是事件派发器类,负责进行事件的发送和侦听。通常,用户定义的类能够调度事件的最简单方法是扩展 EventDispatcher

EventCenter 是一个全局的事件监听器,调用它的静态方法 EventCenter.register() 注册事件,然后可以在代码的任何地方调用 EventCenter.send() 派发事件。这种方式派发事件非常灵活,但是不便于日后的维护,一般用于监听系统的全局事件。

Install

npm i easy-event

Demo

const {Event, EventDispatcher, EventCenter} = require('./easy-event');


class Main {
    constructor() {

        let loader = new Loader();

        loader.once(Event.OPEN, (event) => {
            console.log("------- open -------");
            console.log("loader has Event.OPEN:" + loader.hasEventListener(Event.OPEN));
        }, this);

        loader.addEventListener(Event.COMPLETE, this.onComplete1, this);
        loader.addEventListener(Event.COMPLETE, this.onComplete2, this, 100);

        // 用 EventCenter 可以全局监听
        EventCenter.register(Event.CHANGE, this.onChange, this);

        loader.open();
        loader.load("a.jpg");
        loader.change();
    }

    onComplete1(event) {
        console.log("------- onComplete1 -------");

        let loader = event.target;

        console.log("event.target:" + loader.toString());
        console.log("event.type:" + event.type.toString());
        console.log("event.data:" + `size:${event.data.size} path:${event.data.path}`);

        console.log("loader has Event.COMPLETE:" + loader.hasEventListener(Event.COMPLETE));
        //移除监听器
        loader.removeEventListener(Event.COMPLETE, this.onComplete1);
        console.log("loader has Event.COMPLETE:" + loader.hasEventListener(Event.COMPLETE));
    }

    onComplete2(event) {
        console.log("------- onComplete2 --------");
        console.log("优先级被提高");
        //移除监听器
        event.target.removeEventListener(Event.COMPLETE, this.onComplete2);
    }

    onChange(event) {
        console.log("------- onChange -------");
        console.log("这是来自 EventCenter 派发的事件")
    }
}

/**
 * 创建一个EventDispatcher的子类,模拟加载器。
 * 当加调用load()方法时,延迟一段时间会派发Event.COMPLETE事件
 */
class Loader extends EventDispatcher {

    open() {
        this.dispatchEvent(new Event(Event.OPEN));
    }

    // 模拟异步加载
    load(path) {

        let self = this;

        setTimeout(() => {
            //加载完成,将加载数据封装为Event对象,派发出去。
            let evt = new Event(Event.COMPLETE, {path: path, size: '10KB'});
            self.dispatchEvent(evt);

        }, 2000);
    }

    change() {
        EventCenter.send(new Event(Event.CHANGE), this);
    }

    toString() {
        return "My name is Loader ~~";
    }
}

new Main();

Output

------- open -------
loader has Event.OPEN:false
------- onChange -------
这是来自 EventCenter 派发的事件
------- onComplete2 --------
优先级被提高
------- onComplete1 -------
event.target:My name is Loader ~~
event.type:Symbol(COMPLETE)
event.data:size:10KB path:a.jpg
loader has Event.COMPLETE:true
loader has Event.COMPLETE:false

API

Event

Event 类作为创建事件实例的基类,当发生事件时,Event 实例将作为参数传递给事件侦听器。Event 类的属性包含有关事件的基本信息。您可以通过扩展 Event 类,将此类其他信息传递给事件侦听器。

公共属性

| 属性 | 类型 | 说明 | | --- | --- | --- | | type | string | [只读] 事件的类型 | | target | object | [只读] 事件目标 | | data | object | 与此事件对象关联的可选数据 |

公共方法

| 方法 | | --- | | Event ( type :string, data ? :object ) : Event 创建一个作为参数传递给事件侦听器的 Event 对象 | | clone() : Event 复制该对象 | | toString() : string 转换为字符串 | | create(EventClass : 'class', type : 'string', data ? :object) : Event [静态] 从对象池中取出或创建一个新的事件实例 | | release(event : 'Event') : void [静态] 释放一个事件对象,并缓存到对象池 |

EventDispatcher

EventDispatcher 是事件派发器类,负责进行事件的发送和侦听。通常,用户定义的类能够调度事件的最简单方法是扩展 EventDispatcher。如果无法扩展(即,如果该类已经扩展了另一个类),则可以创建 EventDispatcher 成员,并编写一些简单的映射,将调用连接到聚合的 EventDispatcher 中。

公共方法

| 方法 | | -------- | | EventDispatcher ( target ? :object ) : EventDispatcher 创建一个 EventDispatcher 类的实例 | | addEventListener ( type :string, listener :function, thisObj ? :object, priority ? :number ) : void 使用 EventDispatcher 对象注册事件侦听器对象,以使侦听器能够接收事件通知 | | dispatchEvent ( event : Event ) : void 派发事件 | | dispatchEventWith ( type :string , data ? : object ) : void 派发一个指定参数的事件 | | hasEventListener ( type :string ) : boolean 检查 EventDispatcher 对象是否为特定事件类型注册了任何侦听器 | | once ( type :string, listener :function, thisObj ? :object, priority ? :number ) : void 添加仅回调一次的事件侦听器,此方法与addEventListener()方法不同,addEventListener()方法会持续产生回调,而此方法在第一次回调时就会自动移除监听 | | removeEventListener ( type :string, listener :function) : void EventDispatcher 对象中删除侦听器 | | removeAll () : void EventDispatcher 对象中删除全部侦听器 |

EventCenter

EventCenter 是一个全局的事件监听器,调用它的静态方法 EventCenter.register() 注册事件,然后可以在代码的任何地方调用 EventCenter.send() 派发事件。这种方式派发事件非常灵活,但是不便于日后的维护,一般用于监听系统的全局事件。

公共方法

| 方法 | | -------- | | [静态] register ( type :string, listener :function, thisObj ? :object, priority ? :number ) : void 注册事件侦听器对象,以使侦听器能够接收事件通知 | | [静态] send ( event : Event, target ? :object ) : void 派发事件 | | [静态] unregister ( type :string, listener :function) : void 删除侦听器 |

更新日志

| 版本 | 更新内容 | | --- | ------- | | 1.2.0 | 使用 webpack 打包。| | 1.1.0 | 新增 Event.create()Event.release()EventDispatcher.dispatchEventWith() 方法。|