### Installing Subscribe Library via npm Source: https://github.com/kucukkanat/subscribe/blob/master/README.md This command installs the 'subscribe' library from the npm registry and adds it as a dependency to your project's package.json file. ```Shell $ npm install --save subscribe ``` -------------------------------- ### Basic Usage of Emitter Class in JavaScript Source: https://github.com/kucukkanat/subscribe/blob/master/README.md This example illustrates the fundamental operations of the Emitter class, including creating an instance, attaching listeners for specific events ('foo') and all events ('*'), emitting events with data, removing listeners by reference or type, and using the 'once' method for handlers that should only fire a single time. ```js import {Emitter} from 'subscribe' // OR const {Emitter} = require('subscribe') const eventbus = new Emitter() // listen to an event eventbus.on('foo', e => console.log('foo', e) ) // listen to all events // This will fire first the 'type' event if it exists eventbus.on('*', (type, e) => console.log(type, e) ) // fire an event eventbus.emit('foo', { a: 'b' }) // working with handler references: function onFoo() {} eventbus.on('foo', onFoo) // listen eventbus.off('foo', onFoo) // unlisten // remove all listeners for specific event eventbus.off('foo') // Fire event listener only once eventbus.once('knock knock', (data) => console.log("I will be fired only once")) eventbus.emit('knock knock', null) // This will not fire event knock', null) ``` -------------------------------- ### Including Subscribe Library via UMD Script Tag Source: https://github.com/kucukkanat/subscribe/blob/master/README.md This HTML snippet shows how to include the UMD build of the 'subscribe' library directly in a web page using a script tag, typically for browser environments without a module bundler. The library will be available globally under `window.__Emitter__`. ```html ``` -------------------------------- ### Importing Emitter Class in JavaScript Source: https://github.com/kucukkanat/subscribe/blob/master/README.md These snippets demonstrate how to import the Emitter class into your JavaScript project using either ES6 module syntax (import) or CommonJS module syntax (require). Choose the method appropriate for your project's module system. ```javascript // using ES6 modules import { Emitter } from 'subscribe' ``` ```javascript // using CommonJS modules var { Emitter } = require('subscribe') ``` -------------------------------- ### Using Emitter with Typescript for Event Typing Source: https://github.com/kucukkanat/subscribe/blob/master/README.md This snippet demonstrates how to leverage Typescript generics when instantiating the Emitter class to define the expected event names ('foo' | 'bar'). This enables static type checking, allowing the Typescript compiler to catch errors if you attempt to register a listener for an undefined event type ('bar2') before runtime. ```js import {Emitter} from 'subscribe' const emitter = new Emitter<'foo' | 'bar'>() emitter.on('foo', e => console.log('foo', e) ) emitter.on('bar', e => console.log('foo', e) ) // Will throw an error: (on typescript typecheck NOT ON RUNTIME!) emitter.on('bar2', e => console.log('foo', e) ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.