GithubHelp home page GithubHelp logo

ribeirolabs / events Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 292 KB

Type safe listener for custom/native events and dispatcher for custom events

License: MIT License

JavaScript 32.41% TypeScript 67.59%
events events-listener browser-events

events's Introduction

@ribeirolabs/events

Type safe listener for custom/native events and dispatcher for custom events

Usage

listenEvent (event, handler) => unlistenEvent

import { listenEvent } from '@ribeirolabs/events';

// adds event listener
const unlisten = listenEvent('hashchange', (e) => console.log(e.name));

// removes event listener
unlisten();

unlistenEvent (event) => void

You can also use it to unlisten a specific event.

import { unlistenEvent } from '@ribeirolabs/events';

// removes event listener
unlistenEvent('hashchange');

dispatchCustomEvent (name, detail) => void

import { listenEvent, dispatchCustomEvent } from '@ribeirolabs/events';

listenEvent('my-event', (event) => {
  console.log(event.detail.message); // it works
});

dispatchCustomEvent('my-event', {
  message: 'it works',
});

React

useEvent (event, handler) => void

This hook automatically adds the listener on mount and removes it on unmount.

import { useEvent } from '@ribeirolabs/events/react';

function Component() {
  const listener = useCallback((event) => {
    console.log(event.name);
  }, []);

  useEvent('hashchange', listener);
}

Browser

index.browser.js

It adds a global ribeirolabs.events with all the methods.

<script src="https://unpkg.com/@ribeirolabs/events/index.browser.js"></script>

<script>
  ribeirolabs.events.listenEvent('my-event', (event) => {
    console.log(event.detail.message) // it works!
  });

  ribeirolabs.events.dispatchCustomEvent('my-event', {
    message: 'it works!',
  });

  ribeirolabs.events.unlistenEvent();
</script>

index.mjs

To work with modules.

<script type="module">
  import { listenEvent, dispatchCustomEvent, unlistenEvent } from "https://unpkg.com/@ribeirolabs/events/index.mjs";

  listenEvent('my-event', (event) => {
    console.log(event.detail.message) // it works!
  });

  dispatchCustomEvent('my-event', {
    message: 'it works!',
  });

  unlistenEvent();
</script>

Typescript

You can type your custom events, just declare a global Events interface. We suggest that you create an events.d.ts file inside the root folder.

interface Events {
  'my-event': {
    message: string;
  };
};

Now dispatchCustomEvent will be typed based on that interface, and the other methods will also consider these events.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.