3.2.2 • Published 5 years ago

dot-event v3.2.2

Weekly downloads
6
License
MIT
Repository
github
Last release
5 years ago

dot-event

Javascript event emitter, foundation of everything.

neutron star

What is it?

Dot-event creates interfaces for listening to and emitting events.

Dot-event listeners can be synchronous or asynchronous, accept arguments, and return values.

Dot-event has a tiny footprint (<1 kb compressed and gzipped).

Write less code

Event listeners may emit any event through the dot argument, resulting in less require calls and easy access to functionality across your application.

Event id & props

Dot-event optionally uses event id and prop string(s) to add identifying context to an emit. Props pay off with logging, store updates, and even dom element ids.

Dynamic composition

Dot-event uses a composer function pattern to add event listeners. This pattern works very well with dynamic imports to create dot-event instances with dynamic functionality.

State

Dot-event provides basic state via the dot.state object. On this object we built an immutable store that leverages props and is only ~1 kb compressed and gzipped.

SSR-ready

Its simple to wait for all dot-event listeners before rendering the final version of your server side page.

Setup

const dot = require("dot-event")()

Basics

dot.on(() => {}) // listener
dot() // emitter

Return value

dot.on(() => "value")
dot() // "value"

Async return value

dot.on(async () => "value")
dot().then(result => /* "value" */)

Event id

The event id is the first string argument to dot.on or dot.any.

dot.on("myEvent", () => "value")
dot("myEvent") // "value"

ℹ️ The listener function receives the event id as its fourth argument.

Listener arguments

No matter what is passed to the dot emitter, listener functions always receive five arguments:

ArgumentDescription
propArray of string identifiers
argEmit argument
dotDot-event instance
eventEvent id
signalSignal object

Props

String arguments after the event id are prop identifiers.

dot.on("myEvent", "prop", prop => prop)
dot("myEvent", "prop") // [ "prop" ]

ℹ️ The listener function receives the prop array as its first argument.

Emit argument

The last non-prop argument becomes the emit argument (arg).

dot.on((prop, arg) => arg)
dot({ option: true }) // { option: true }

ℹ️ The listener function receives the emit argument as its second argument.

Signal argument

dot.on((prop, arg, dot, eventId, signal) => {
  signal.cancel = true
  return "value"
})
dot.on(() => "never called")
dot() // "value"

ℹ️ There is one other signal, signal.value, which you can set instead of using return in your listener function.

Any

dot.any(() => "!")
dot("myEvent", "prop") // "!"

Any with event id

dot.any("myEvent", prop => prop)
dot("myEvent", "prop") // [ "prop" ]
dot.myEvent("prop") // <-- cool helper function!

ℹ️ Dot-event creates a helper function only if dot.any receives an event id with no props.

Any with props

dot.any("myEvent", "prop", "prop2", props => props)
dot("myEvent") // noop
dot("myEvent", "prop") // noop
dot("myEvent", "prop", "prop2") // [ "prop", "prop2" ]
dot("myEvent", "prop", "prop2", "prop3") // [ "prop", "prop2", "prop3" ]

Composer pattern

A common pattern is for composers to define listeners that respond to any props of a particular event id.

export default function(dot) {
  dot.any("myEvent", myEvent)
}

async function myEvent(prop, arg, dot) {
  prop = prop.concat(["myEvent"])
  await dot.otherEvent(prop)
}

ℹ️ Another common pattern illustrated here is to append a prop id before passing them along to another emit.

Dynamic imports

dot.add(import("./myEvent"))

ℹ️ You might need to run node with --experimental-modules to enable dynamic imports server side.

Wait for pending events

await Promise.all([...dot.state.events])

ℹ️ dot.state.events is a Set of promises.

Dot composers

LibraryDescriptionURL
adGoogle Publisher Taghttps://github.com/dot-event/ad#readme
argsArgument definitionshttps://github.com/dot-event/args#readme
argvParse process.argvhttps://github.com/dot-event/argv#readme
controllerDOM controllerhttps://github.com/dot-event/controller#readme
elDOM elementshttps://github.com/dot-event/el#readme
fetchUniversal HTTP fetchhttps://github.com/dot-event/fetch#readme
logEvent loggerhttps://github.com/dot-event/log#readme
renderServer side renderhttps://github.com/dot-event/render#readme
storeImmutable storehttps://github.com/dot-event/store#readme
viewDOM viewhttps://github.com/dot-event/view#readme
3.2.2

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.3

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.33

5 years ago

3.0.32

5 years ago

3.0.31

5 years ago

3.0.30

5 years ago

3.0.29

5 years ago

3.0.28

5 years ago

3.0.27

5 years ago

3.0.26

5 years ago

3.0.25

5 years ago

3.0.24

5 years ago

3.0.23

5 years ago

3.0.22

5 years ago

3.0.21

5 years ago

3.0.20

5 years ago

3.0.19

5 years ago

3.0.18

5 years ago

3.0.17

5 years ago

3.0.16

5 years ago

3.0.15

5 years ago

3.0.14

5 years ago

3.0.13

5 years ago

3.0.12

5 years ago

3.0.11

5 years ago

3.0.10

5 years ago

3.0.9

5 years ago

3.0.8

5 years ago

3.0.7

5 years ago

3.0.6

5 years ago

3.0.5

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.12.11

5 years ago

2.12.8

5 years ago

2.12.7

5 years ago

2.12.6

5 years ago

2.12.5

5 years ago

2.12.4

5 years ago

2.12.3

5 years ago

2.12.2

5 years ago

2.12.1

5 years ago

2.12.0

5 years ago

2.11.2

5 years ago

2.11.1

5 years ago

2.11.0

5 years ago

2.10.0

5 years ago

2.9.0

5 years ago

2.8.0

6 years ago

2.7.0

6 years ago

2.6.0

6 years ago

2.5.0

6 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago