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

line-events

v1.0.0

Published

Line-Events is a lightweight and straightforward library that operates based on the Observable pattern. It is designed to provide a simple and efficient alternative for creating Observables in JavaScript and TypeScript applications.

Downloads

6

Readme

codecov

Line Events

Logo

line-events is a library that allows the creation of observables and the transmission of events between different parts of a program. The name line-events comes from the idea that events are transmitted from one point to another, like a line or an electrical network, where the line is the transmission medium, each event is an electrical pulse, and each observer is a device that receives that pulse.

Installation

npm i line-events

Usage

Here is a basic example of how to use the library:

import * as lvt from 'line-events'

const lineEvts = lvt.create()

lineEvts.createNode<boolean>('loading', (payload) => {
...
})

lvt.stream('loading', true)
lineEvts.destroy()

This code creates a new event called loading and transmits a boolean value true to all observers of that event.

Creating an Event

To create an event, simply call the createNode function and pass the name of the event and a callback function that will be called each time an event is transmitted. The transmission lines are created automatically, so there is no need to worry about that.

Destroying an Event

It is important to call the destroy function to release the resources allocated by the library. This resource should be tied to the lifecycle of the components. If a component is destroyed, such as a menu that no longer exists, it is important to call the destroy function to avoid creating duplicate events, which can lead to undesirable behaviors, such as multiple callback calls.

API

create

Returns a new object that contains the functions to create and transmit events.

createNode

Creates a new event, receives the name of the line and a callback function that will be called when the line receives a transmission.

stream

Transmits an event to all observers of the line.

destroy

Releases the nodes created by the event line.

Examples

Example 1: Transmitting Simple Data

import * as lvt from 'line-events'

const lineEvts = lvt.create()

lineEvts.createNode<string>('message', (payload) => {
  console.log('Received message:', payload)
})

lvt.stream('message', 'Hello, World!')
lineEvts.destroy()

Example 2: Transmitting Complex Data

import * as lvt from 'line-events'

interface Player {
  hp: number
}

const lineEvts = lvt.create()

lineEvts.createNode<Player>('player', (payload) => {
  console.log('Player HP:', payload.hp)
})

lvt.stream('player', { hp: 100 })
lineEvts.destroy()

Example 3: Observing Multiple Events

import * as lvt from 'line-events'

const lineEvts = lvt.create()

lineEvts.createNode<string>('message', (payload) => {
  console.log('Received message:', payload)
})

lineEvts.createNode<number>('number', (payload) => {
  console.log('Received number:', payload)
})

lvt.stream('message', 'Hello, World!')
lvt.stream('number', 42)
lineEvts.destroy()

Example 4: Observing Multiple Events with the Same Callback


import * as lvt from 'line-events'

const lineEvts = lvt.create()

const callback = (payload: any) => {
  console.log('Received:', payload)
}

lineEvts.createNode<string>('message', callback)
lineEvts.createNode<number>('number', callback)

lvt.stream('message', 'Hello, World!')
lvt.stream('number', 42)
lineEvts.destroy()

The line-events library offers various usage examples and can be integrated in many ways. It is compatible with Node.js, Vue, React, Angular, Svelte, among others. You can use it to:

  • Notify the user interface about changes in the application state.
  • Transmit data between components.
  • Create APIs for component manipulation.
  • Implement design patterns like Observer, Pub/Sub, etc.
  • And much more.

One of the most interesting uses is the discovery of dynamically loaded components. In this case, a component can send a message to multiple components that implement a standard interface. Thus, each component, when notified, can identify itself and react accordingly.

Conclusion

line-events is a simple and efficient library for transmitting events between different parts of a program. It is easy to use and can be easily integrated into any project. With it, you can create observables and transmit events quickly and efficiently, without the need for more complex libraries. If you need a simple way to transmit events in your project, line-events is a good choice, as it is lightweight, fast, and easy to use.

I have been using this library in my projects, and it has been very helpful. I hope it can assist you as well.