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

fox-listener

v1.0.3

Published

Pub/Sub model realize by es6

Downloads

6

Readme

build-pass codecov

Fox-Listener

Pub/Sub model realize by es6

这是一个小小的工具项目

发布订阅模式(观察者模式)的 es6实现

install

npm i fox-listener --save

Use

基本的发布订阅

import fl from "fox-listener";

const foxListener = new fl();

foxListener.subscribe(`Event`,(who) => {
    console.log("我被 %s 订阅了", who)
});

// do something ...

foxListener.publish(`Event`, "Owen");

可以使用命名空间

import fl from "fox-listener";

const foxListener = new fl();

foxListener.subscribe(`Event.say`,(who) => {
    console.log("%s 说话了", who)
});

foxListener.subscribe(`Event.sleep`,(who) => {
    console.log("%s 睡觉了", who)
});

// do something ...

foxListener.publish(`Event.sleep`, "Owen");

// Owen 睡觉了

如果执行类目,则将全部匿名空间执行

import fl from "fox-listener";

const foxListener = new fl();

foxListener.subscribe(`Event.say`,(who) => {
    console.log("%s 说话了", who)
});

foxListener.subscribe(`Event.sleep`,(who) => {
    console.log("%s 睡觉了", who)
});

// do something ...

foxListener.publish(`Event`, "Owen");

// Owen 说话了
// Owen 睡觉了

可以使用unsubscribe 取消订阅的项目

import fl from "fox-listener";

const foxListener = new fl();

foxListener.subscribe(`Event.say`,(who) => {
    console.log("%s 说话了", who)
});


foxListener.unsubscribe(`Event.say`);

// do something ...

foxListener.publish(`Event.say`, "Owen");

// throw Error The Event didnt bind !

如果 取消的是总类目,那么命名空间下订阅的内容也全部会被销毁

import fl from "fox-listener";

const foxListener = new fl();

foxListener.subscribe(`Event`,(who) => {
    console.log("我是 %s", who)
});

foxListener.subscribe(`Event.say`,(who) => {
    console.log("%s 说话了", who)
});



foxListener.unsubscribe(`Event`);

// do something ...

// 即使删除的是Event 旗下的say类也会被删除清空

foxListener.publish(`Event.say`, "Owen");
// throw Error The Event didnt bind !

使用once函数,订阅的项目可以只执行一次就销毁。

import fl from "fox-listener";

const foxListener = new fl();

foxListener.subscribe(`Event.say`,(who) => {
    console.log("%s 说话了", who)
});

// do something ...

foxListener.once(`Event.say`, "Owen");

// Owen 说话了

foxListener.publish(`Event.say`, "Owen");

// throw Error The Event didnt bind !

浅绑定

import fl from "fox-listener";

const foxListener = new fl();

let data = {
    name: {
        firstName: 'Brown',
        lastName: 'Owen'
    },
    hobby: ['sanguosha', 'movie'],
    address: {
        compony: 'Baidu.Inc',
        info: {
            compony_address: '上地十街',
            home_address: 'blablabla'
        }
    }
}

 listener.watch('name.firstName', {deep: 0}, (val) => {
    // 我改变了我的名字
    return val + 'Blue';
})

data.name.firstName = 'Tim';
console.log(data.name.firstName); // TimBlue;

深绑定

import fl from "fox-listener";

const foxListener = new fl();

let data = {
    name: {
        firstName: 'Brown',
        lastName: 'Owen'
    },
    hobby: ['sanguosha', 'movie'],
    address: {
        compony: 'Baidu.Inc',
        info: {
            compony_address: '上地十街',
            home_address: 'blablabla'
        }
    }
}

 listener.watch('address', {deep: 1}, (val) => {
    // 我改变了我的地址
    return val + '_wait';
})

data.address.compony = 'Tencent';
data.address.info.compony_address = '南山科技园';

console.log(data.address.compony); //'Tencent_wait'
console.log(data.address.info.compony_address); //'南山科技园_wait';

licence

MIT