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

@dking/event-proxy

v1.0.1

Published

event proxy plug-in implemented with TS

Downloads

15

Readme

EventProxy

event proxy plug-in implemented with TS 用ts实现的事件代理插件

安装方法

    npm install @dking/event-proxy 

示例

创建事件代理实例

    import EventProxy from '@dking/event-proxy';
    const ep1 = EventProxy.create();
    const ep2 = new EventProxy();

    class SomeComponent extends EventProxy {
        componentDidMount() {
            this.on('Test', (data) => {
                
            })
        }
    }

单事件绑定

    import EventProxy from '@dking/event-proxy';
    const ep = EventProxy.create();
    ep.on('Test', (payload) => {
        console.log(payload);
    })
    // 别名
    ep.register('Test1', (payload) => {
        console.log(payload);
    })
    ep.subscribe('Test1', (payload) => {
        console.log(payload);
    })
    ep.bind('Test1', (payload) => {
        console.log(payload);
    })

    setTimeout(() => {
        ep.emit('Test1', {
            time: new Date()
        })
    }, 1000)

联合事件绑定

当事件都触发后才会触发的事件

    import EventProxy from '@dking/event-proxy';
    const ep = EventProxy.create();
    ep.on(['Test1', 'Test2'], (...payload) => {
        console.log(...payload);
    })
    setTimeout(() => {
        ep.emit('Test1', {
            time: new Date()
        })
    }, 1000)
    setTimeout(() => {
        ep.emit('Test2', {
            time: new Date()
        })
    }, 1000)

once只监视一次的绑定方式

该绑定方法 会在事件触发后 立即解绑

    import EventProxy from '@dking/event-proxy';
    const ep = EventProxy.create();
    fetch(url, options).then(function(response) {
        {`... 复杂业务 ...`}
        ep.done('Test1', data);
    })
    fetch(url, options).then(function(response) {
        {`... 复杂业务 ...`}
        ep.done('Test2', data);
    })
    ep.once(['Test1', 'Test2'], (v1, v2) => {
        {`... 只执行一次的业务 ... `}
    })

bindNTime 绑定 N 次 事件

该绑定方法 会在事件触发 指定 次数后 解绑, 当 n = 1 时 和 once 效果相同

    import EventProxy from '@dking/event-proxy';
    const ep = EventProxy.create();
    ep.bindNTime('Test', 2 /* 指定绑定次数 */, (v) => {
        console.log(v);
    })

    ep.done('Test', {
        time: new Date()
    })

    ep.done('Test', {
        time: new Date()
    })

    ep.done('Test', {
        time: new Date()
    }) // 该事件不会触发

wait绑定的事件

该绑定方法 会等待事件触发 指定次数后 ,才触发回调函数

    import EventProxy from '@dking/event-proxy';
    const ep = EventProxy.create();
    const fetch1 = () => {
        fetch(url, options).then(function(response) {
            {`... 复杂业务 ...`}
            ep.emit('Test1', data1);
        })
    }
     const fetch2 = () => {
        fetch(url, options).then(function(response) {
            {`... 复杂业务 ...`}
            ep.emit('Test2', data2);
        })
    }
    fetch1();
    fetch2(); //第一次满足条件不会触发
    fetch1();
    fetch2(); //第二次满足条件才会触发
    
    ep.wait(['Test1', 'Test2'], 2/* 等待深度 */, (v1, v2) => {
        {`... 执行的业务 ... `}
        {v1 == [data1, data2]} //等于每一层深度的所有data数组
    })

取消监视某事件

    import EventProxy from '@dking/event-proxy';
    const ep = EventProxy.create();
    const unregister = ep.register(['Test1', 'Test2'], (data1, data2) => {
        console.log(data1, data2)
    });  //register的返回了卸载函数
    unregister(); // 可以取消解绑事件
    ep.emit('Test1', {
        time: new Date()
    })

    ep.emit('Test2', {
        time: new Date()
    })

继承的方式使用 EventProxy

    import EventProxy from '@dking/event-proxy';
    const ep = EventProxy.create();
    const EVENT_NAME_1 = 'Test1';
    const EVENT_NAME_2 = 'Test1';
    class SomeComponent extends EventProxy {
        componentDidMount() {
            const ug1 = this.on(EVENT_NAME_1, (data) => {
                console.log(data);
            })
            const ug2 = this.on(EVENT_NAME_2, (data) => {
                console.log(data);
            })

            this.ugs = [ug1, ug2];
        }
        componentWillUnMount() {
            this.ugs && this.ugs.forEach(ug => ug());
        }

        run(): void {
            setTimeout(() => {
                this.emit(EVENT_NAME_1, {
                    type: EVENT_NAME_1
                })
            }, 100)

            setTimeout(() => {
                this.emit(EVENT_NAME_2, {
                    type: EVENT_NAME_2
                })
            }, 100)
        }
    }

    const component = new SomeComponent();
    component.componentDidMount();
    component.run();
    component.componentWillUnMount();
    component.run(); // 没有事件会触发