New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

thor-event

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thor-event

Redux but with events

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

Thor Event Emitter

Event emitter to manage your state.

An event emitter that allows you to manage your global state. Instead of using global giant object to store your shared sate, you can simply emit the events you need with the necessary payload and keep your state managed locally.

npm install thor-event

Somewhere in your code:

import ThorEvent from "thor-event";

const appEvent = new ThorEvent();

appEvent.emit({
  id: "evt-1",
  type: "auth",
  issuer: "api.auth.com",
  type: "auth", // notify all listeners of this type
  payload: {
    name: "name",
    address: "address-1",
  },
});

And inside you React JSX component

const handleUserAuth = ({payload, ...rest}: UserInfo) => {
  // update my state locally
  setUserInfo({...payload)};

  // do something else with the rest
};

React.useEffect(() => {
  appEvent.on("auth", handleUserAuth);

  return () => {
    appEvent.off("auth", handleUserAuth);
  };
}, []);

return (
  <div>
    <p>userInfo</p>
  </div>
);

API

Initialize the event emitter (optional):

init(evt: EventInterface<EventTypes, PayLoadInterface, IssuerTypes>)

The evt object contains:

  • id?: string,
  • type?: EventTypes | string,
  • issuer?: IssuerTypes | string,
  • payload?: PayLoadInterface | any,

Update the payload of an event:

setPayload(evt: EventInterface<EventTypes, PayLoadInterface, IssuerTypes>)

Update the id of an event:

updateID({ id, newID });
  • id: string,
  • newID: string,

To get all events from the same type:

getEventsByType(type?: EventTypes | string) : EventInterface[]

Bind an event listener to an event type:

on(type: EventTypes | string, listeners: (evt: EventInterface<EventTypes, PayLoadInterface, IssuerTypes>) => void)

Emit an event:

emit(evt: EventInterface<EventTypes, PayLoadInterface, IssuerTypes>)

To clear event and its binding listeners:

clear(id:string)

Unbind an event listener:

off(type: EventTypes | string, listeners: (evt: EventInterface<EventTypes, PayLoadInterface, IssuerTypes>) => void)

Test

npm test

License

This project is licensed under the MIT

FAQs

Package last updated on 26 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts