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

syh-observer

v1.0.1

Published

组件间消息传递组件

Downloads

4

Readme

Observer 公共事件观察器

安装

npm install syh-observer --save

说明

用于订阅和触发公共事件, 使用全局字符串名称表示事件ID,使事件订阅者与发布者无直接依赖,达到模块间弱耦合的目的。

依赖模块

示例

该模块支持 ES6/CMD/AMD规范,也可以在html文件中通过script标签引入进行使用;下面以cmd规范为例

公共事件触发与定阅
    // Map.js;
    var observer = require('observer');
    // 触发了一个名称为'map:init'的事件;并传递 {time: new Date()}作为监听事件处理函数的参数;
    observer.trigger('map:init', {time: new Date()});
// A模块
  var observer = require('observer');
  // 使用 observer.on方法进行消息订阅
  observer.on('map:init', function(time){
    alert('地图已初始化')
  });

  ///..............

  //撤消指定事件的订阅
  observer.off('map:init')
// B模块
  var observer = require('observer');
  //past 即使B模块在'map:init'公共事件触发之后加载,下面的事件处理函数也能被调用
  //调用时time事件参数为最后一次触发时的参数
  observer.past('map:init', function(time){
    alert('地图已初始化')
  });
// C模块
  var observer = require('observer');
  // 使用observer.once,只监听一次消息,之后的消息不再监听
  observer.once('map:init', function(time){
    alert('地图已初始化')
  });
define(function(require, exports, modules) {
  var observer = require('observer');
  //清空observer中每个事件的最后状态
  observer.clearAll();
});
define(function(require, exports, modules) {
  var observer = require('observer');
  //清空observer中指定事件的最后状态
  observer.clearOther(['event-a','event-b']);
});

调试功能

  1. 设置 seajs.debug 为 true
  2. 在JS调试器控制台中运行 printObserver() 可显示当前已注册的事件,及其所在代码行
  3. 在JS调试器控制台中运行 window.ObserverDebugOut = '*' , 可显示此后所有事件触发的调试信息
  4. 使用 window.ObserverDebugOut = 'event-a'window.ObserverDebugOut = ['event-a','event-b','event-a'] 可显示此后指定事件触发的调试信息