模极社社区 - 制造业全产业链交流平台

    • 登录
    • 版块
    • 最新
    • 话题
    • 热门
    • 用户
    • 群组

    塑胶模具设计资料分享 - 模极社模具行业技术社区

    社区公告
    1
    1
    2
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • S
      system 2 最后由 编辑

      <h2><img src="media/header.png" width="1000"></h2>

      <blockquote>
      <p>Simple and modern async event emitter</p>
      </blockquote>

      <p><a href="upload://l3MerXwiNXtFVM01rRej25jYS2l.svg"><img src="https://codecov.io/gh/sindresorhus/emittery" alt="Coverage Status</a>"></p>
      <p><a href="upload://jSWoKtcWcqBDoFsFrtu2rCswD4o.svg"><img src="https://bundlephobia.com/result?p=emittery" alt="<img src="media/header.png" width="1000"></a>"></p>

      <p>It works in Node.js and the browser (using a bundler).</p>

      <p>Emitting events asynchronously is important for production code where you want the least amount of synchronous operations. Since JavaScript is single-threaded, no other code can run while doing synchronous operations. For Node.js, that means it will block other requests, defeating the strength of the platform, which is scalability through async. In the browser, a synchronous operation could potentially cause lags and block user interaction.</p>

      <h3>Install</h3>

      <pre><code>
      $ npm install emittery
      </code></pre>

      <h3>Usage</h3>

      <pre><code>
      const Emittery = require('emittery');

      const emitter = new Emittery();

      emitter.on('🦄', data => {
      console.log(data);
      });

      const myUnicorn = Symbol('🦄');

      emitter.on(myUnicorn, data => {
      console.log(Unicorns love ${data});
      });

      emitter.emit('🦄', '🌈'); // Will trigger printing '🌈'
      emitter.emit(myUnicorn, '🦋'); // Will trigger printing 'Unicorns love 🦋'
      </code></pre>

      <h3>API</h3>

      <h4>eventName</h4>

      <p>Emittery accepts strings and symbols as event names.</p>

      <p>Symbol event names can be used to avoid name collisions when your classes are extended, especially for internal events.</p>

      <h4>emitter = new Emittery()</h4>

      <p>#### on(eventName | eventName[], listener)</p>

      <p>Subscribe to one or more events.</p>

      <p>Returns an unsubscribe method.</p>

      <p>Using the same listener multiple times for the same event will result in only one method call per emitted event.</p>

      <pre><code>
      const Emittery = require('emittery');

      const emitter = new Emittery();

      emitter.on('🦄', data => {
      console.log(data);
      });
      emitter.on(['🦄', '🐶'], data => {
      console.log(data);
      });

      emitter.emit('🦄', '🌈'); // log => '🌈' x2
      emitter.emit('🐶', '🍖'); // log => '🍖'
      </code></pre>

      <p>##### Custom subscribable events</p>

      <p>Emittery exports some symbols which represent custom events that can be passed to <code>Emitter.on</code> and similar methods.</p>

      <ul>
      <li><code>Emittery.listenerAdded</code> - Fires when an event listener was added.</li>
      <li><code>Emittery.listenerRemoved</code> - Fires when an event listener was removed.</li>
      </ul>

      <pre><code>
      const Emittery = require('emittery');

      const emitter = new Emittery();

      emitter.on(Emittery.listenerAdded, ({listener, eventName}) => {
      console.log(listener);
      //=> data => {}

      console.log(eventName);
      //=> '🦄'
      

      });

      emitter.on('🦄', data => {
      // Handle data
      });
      </code></pre>

      <p>###### Listener data</p>

      <ul>
      <li><code>listener</code> - The listener that was added.</li>
      <li><code>eventName</code> - The name of the event that was added or removed if <code>.on()</code> or <code>.off()</code> was used, or <code>undefined</code> if <code>.onAny()</code> or <code>.offAny()</code> was used.</li>
      </ul>

      <p>Only events that are not of this type are able to trigger these events.</p>

      <p>##### listener(data)</p>

      <p>#### off(eventName | eventName[], listener)</p>

      <p>Remove one or more event subscriptions.</p>

      <pre><code>
      const Emittery = require('emittery');

      const emitter = new Emittery();

      const listener = data => console.log(data);
      (async () => {
      emitter.on(['🦄', '🐶', '🦊'], listener);
      await emitter.emit('🦄', 'a');
      await emitter.emit('🐶', 'b');
      await emitter.emit('🦊', 'c');
      emitter.off('🦄', listener);
      emitter.off(['🐶', '🦊'], listener);
      await emitter.emit('🦄', 'a'); // Nothing happens
      await emitter.emit('🐶', 'b'); // Nothing happens
      await emitter.emit('🦊', 'c'); // Nothing happens
      })();
      </code></pre>

      <p>##### listener(data)</p>

      <p>#### once(eventName | eventName[])</p>

      <p>Sub</p>

      <hr>
      <p>有没有同行遇到过同样的问题?分享一下经验。</p>

      1 条回复 最后回复 回复 引用 0
      • First post
        Last post
      Powered by NodeBB | Contributors