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

uieventdispatcher

v1.0.3

Published

Process events, post events to server in batch using simple configuration

Downloads

13

Readme

EventDispatcher

Build Status Language grade: JavaScript Coverage Status code style: prettier

Process events, post events to server in batch, enrich events by appendig additonal data using simple configuration. Persists events in case of browser reload, closing the browser window (local storage used).

Usage

EventDispatcher is an abstract class. So you need to impliment a class that extends EventDispatcher. Create a service EventDispatcherService that extends EventDispatcher.

// EventDispatcherService.js
import EventDispatcher from "uieventdispatcher";

export class EventDispatcherService extends EventDispatcher {
  constructor() {
    super({
      eventsToPostInSingleCall: 1
    });
  }

  eventEnricher(event) {
    // Modify event if needed
    event.userId = '1KL';
    return event;
  }

  methodToPostEvents(data) {
    return fetch("http://backend-url.com/api/analytics", {
      body: JSON.stringify(data),
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
      },
      method: "POST"
    });
  }
}

// Expose the same instance to your whole application 
const eventDispatcher = new EventDispatcherService();
export default eventDispatcher;

In your application code

import eventDispatcher from 'EventDispatcherService';

eventDispatcher.sendEvent({ page: 1});

eventDispatcher.getEventList(); 
/** output
  
  [ { page: 1,
     eventDispatcherUuid: '1548943992704__DEL__126690',   // a unique id generated for eventDispatcher instance
     timeStamp: 1548943992705,                            // timestamp when sendEvent method was called
     userId: '1KL'                                        // modification we did in eventEnricher
    } 
 ]
 x
**/

eventDispatcher.sendEvent({ page: 2}); 
// This will trigger methodToPostEvents as we defined eventsToPostInSingleCall as 1 
// and this is 2nd event we are submitting

Config

EventDispatcher supports following config keys:

| Name | Type | Description | | ------------------------ | --------------------- | -------- | --- | | eventsToPostInSingleCall | number | Wait for these many events before triggering methodToPostEvents|

Abstract methods

The application need to extend EventDispatcher class and impliment these functions.

| Name | Type | Description | | ------------------------ | --------------------- | -------- | --- | | eventEnricher | (event: IEvent) => any | Triggered every time sendEvent is called, can be used to append additonal data to each event.| | methodToPostEvents | (data: any) => Promise | Triggered when after sendEvent method has been called eventsToPostInSingleCall times atleast.| | getLocalStorageKeyName | () => string | Provide a unique string which will be used to store event in localStorage. Should not change on page reload. Bad example: timeStamp, good example: appName_userId |

Supported methods

| Name | arguments in order | Description | | ------------ | ----------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | sendEvent | eventToSend: IEvent; forceful = false; consoleEvent = false | eventToSend: event data; forceful: ignore the check for eventsToPostInSingleCall and call methodToPostEvents or apiPathToPostEvents immidiately; consoleEvent: console.log the event being sent to eventToSend method. Called after eventEnricher if provided | | getEventList | no arguments | Returns the events currently present with eventDispatcher instance | | clearEvents | no arguments | Clear the events currently present with eventDispatcher instance | | getUUID | no arguments | Each EventDispatcher instance gets a unique UUID, use this method to get the |