@yugu/subscribe
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Subscribe发布订阅类

使用import Subscribe from "@yugu/subscribe"导入类 通过const pool = new Subscribe创建发布订阅实例 该类实例可以通过add方式向订阅池中添加事件,pool.add(fn) 该类实例可以通过remove方式从订阅池中移除事件,pool.remove(fn) 该类实例可以通过fire方式触发订阅池中所有的函数,pool.fire()

<script type="module">
  import Subscribe from "@yugu/subscribe";
  const btn = document.querySelector(".submit");
  const pool = new Subscribe();
  const fn1 = () => {
    console.log(1);
  };
  const fn2 = () => {
    console.log(2);
    pool.remove(fn1);
  };
  const fn3 = () => {
    console.log(3);
  };
  const fn4 = () => {
    console.log(4);
  };
  pool.add(fn1);
  pool.add(fn2);
  pool.add(fn3);
  pool.add(fn4);
  btn.onclick = function (event) {
    pool.fire();
    console.log(pool);
  };
</script>

Package Sidebar

Install

npm i @yugu/subscribe

Weekly Downloads

2

Version

1.0.1

License

ISC

Unpacked Size

5.36 kB

Total Files

5

Last publish

Collaborators

  • yugu