custom event system for javascript.
Go to file
trans_soup c64f167903 add a bunch of stuff to readme.
mainly info about how to use this.
2023-07-18 15:19:25 +02:00
src change an argument name to make more sense. 2023-07-18 15:19:01 +02:00
test create some tests. 2023-07-18 14:53:59 +02:00
README.md add a bunch of stuff to readme. 2023-07-18 15:19:25 +02:00
export.mjs init. 2023-07-18 14:41:26 +02:00

README.md

custom event system for js.

unlike the regular event system, this doesn't inherently "attach" events to anything; rather, an event listener is simply a function that gets called when an event is triggered.

currently, this is synchronous; triggering an event will take as long as calling all its listeners.

this probably works in nodejs; at least, the tests succeed there.

how it works

here are the functions exported by export.mjs. when the assumptions about an argument aren't given, assume that they're the same as they were for the previously mentioned argument with the same name.

trigger (event_name, data)

event_name should be a string (but can technically be anything in the current implementation). data can be anything (including undefined).

trigger the specified event, with the given additional data. doesn't return anything.

listen (event_name, callback)

callback should be a function.

when the specified event is triggered, call callback.

it's given a single argument: the data provided in the code that triggers the event, in the data argument of the trigger function.

returns a "listener id". you're not meant to look inside this, but if you're curious: it's currently implemented as an array containing:

  • the event name.
  • a numerical id that locates the specific listener within a data structure that's used internally.

ignore (listener_id)

the inverse of listen; this takes a listener id, and makes the corresponding event listener stop listening for events.

returns the callback function that the listener was using.

attach_listener (target, event_name, callback)

target can be anything that can be compared with the === operator. so, probably anything.

basically a wrapper for listen. only calls the callback function, if:

  • the data argument given to the triggering trigger call is an object.
  • that object has a property target.
  • data.target equals the target argument provided to attach_listener.

target equality is determined with the strict equality operator ===.

returns a listener id.

create_alias (event_name, alias)

alias should be of the same type as event_name. that is, a string.

creates a one-directional alias: triggers alias whenever event_name is triggered, with the same data.

aliases are implemented as regular event listeners; thus, this returns an event id, which you can call ignore on to un-alias the alias.

create_group (group_name, members)

group_name should be of the same type as event_name:s, i.e. a string. members should be an array containing elements of that same type.

create an alias from every element in members to group_name. doesn't return anything.

other stuff

all events are internally stored in the same place. you can name them stuff like girl/meow or message/send to keep things organized.