Comparing version 3.0.28 to 3.0.29
75
dot.js
@@ -19,16 +19,15 @@ /*global Map Promise Set*/ | ||
dot.add = add.bind({ r: r }) | ||
dot.any = setup.bind({ fn: on, m: "any", r: r }) | ||
dot.on = setup.bind({ fn: on, m: "on", r: r }) | ||
dot.off = setup.bind({ fn: off, r: r }) | ||
dot.reset = reset.bind({ r: r }) | ||
dot.reset() | ||
Object.keys(dot.state).forEach(function(m) { | ||
dot[m] = setup.bind({ fn: on, m: m, r: r }) | ||
}) | ||
return dot | ||
} | ||
// Call "onAny" listener functions | ||
// Emit "any" listener functions | ||
// | ||
function callAny(a, k, m, p, r, s) { | ||
function emitAny(a, k, m, p, pr, r, s) { | ||
// a - arg | ||
@@ -38,2 +37,3 @@ // k - key | ||
// p - prop | ||
// pr - promises | ||
// r - refs | ||
@@ -44,16 +44,13 @@ // s - signal | ||
var promise = k.arr.map(function(prop) { | ||
k.arr.forEach(function(prop) { | ||
key = key ? key + period + prop : prop | ||
return callOn(a, key, m, p, r, s) | ||
emitOn(a, key, m, p, pr, r, s) | ||
}) | ||
return Promise.all([ | ||
callOn(a, undefined, m, p, r, s), | ||
Promise.all(promise), | ||
]) | ||
emitOn(a, undefined, m, p, pr, r, s) | ||
} | ||
// Call "on" listener functions | ||
// Emit "on" listener functions | ||
// | ||
function callOn(a, k, m, p, r, s) { | ||
function emitOn(a, k, m, p, pr, r, s) { | ||
// a - arg | ||
@@ -63,2 +60,3 @@ // k - key | ||
// p - prop | ||
// pr - promises | ||
// r - refs | ||
@@ -72,4 +70,2 @@ // s - signal | ||
if (set) { | ||
var promises = [] | ||
set.forEach(function(fn) { | ||
@@ -79,18 +75,12 @@ if (!s.cancel) { | ||
if (out && out.then) { | ||
promises.push(out) | ||
pr.push(out) | ||
} else if (out !== undefined) { | ||
s.value = s.value || out | ||
s.value = out || s.value | ||
} | ||
} | ||
}) | ||
return Promise.all(promises).then(function() { | ||
return a | ||
}) | ||
} else { | ||
return Promise.resolve(a) | ||
} | ||
} | ||
// Call "on" and "onAny" listener functions | ||
// Emit "on" and "any" listener functions | ||
// | ||
@@ -103,24 +93,13 @@ function emit(a, k, m, p, r) { | ||
// | ||
var s = r.dot.state, | ||
var pr = [], | ||
s = r.dot.state, | ||
sig = {} | ||
var promise = Promise.all([ | ||
callAny(a, k, s.beforeAny, p, r, sig), | ||
callOn(a, k, s.beforeOn, p, r, sig), | ||
]) | ||
.then(function() { | ||
return Promise.all([ | ||
callAny(a, k, s.any, p, r, sig), | ||
callOn(a, k, s.on, p, r, sig), | ||
]) | ||
}) | ||
.then(function() { | ||
return Promise.all([ | ||
callAny(a, k, s.afterAny, p, r, sig), | ||
callOn(a, k, s.afterOn, p, r, sig), | ||
]) | ||
}) | ||
.then(function() { | ||
emitOn(a, k, s.on, p, pr, r, sig) | ||
emitAny(a, k, s.any, p, pr, r, sig) | ||
var promise = Promise.all(pr) | ||
.then(function(results) { | ||
s.events.delete(promise) | ||
return sig.value === undefined ? a : sig.value | ||
return sig.value === undefined ? results : sig.value | ||
}) | ||
@@ -198,3 +177,3 @@ .catch(function(err) { | ||
if (p.event) { | ||
if (m === "any" && p.event && !p.length) { | ||
r.dot[p.event] = | ||
@@ -220,7 +199,3 @@ r.dot[p.event] || | ||
this.r.dot.state = { | ||
afterAny: new Map(), | ||
afterOn: new Map(), | ||
any: new Map(), | ||
beforeAny: new Map(), | ||
beforeOn: new Map(), | ||
events: new Set(), | ||
@@ -231,3 +206,3 @@ on: new Map(), | ||
// Parse arguments for `emit`, `off`, `on`, and `onAny` | ||
// Parse arguments for `emit`, `off`, `on`, and `any` | ||
// | ||
@@ -234,0 +209,0 @@ function setup() { |
@@ -67,32 +67,6 @@ /*global Promise*/ | ||
test("on before/after", function() { | ||
var order = [] | ||
dot.afterOn("a", "b", "c", function() { | ||
order.push(3) | ||
}) | ||
dot.on("a", "b", "c", function() { | ||
order.push(2) | ||
}) | ||
dot.beforeOn(["a", "b"], "c", function() { | ||
order.push(1) | ||
}) | ||
return dot("a", "b", "c").then(function() { | ||
expect(order).toEqual([1, 2, 3]) | ||
}) | ||
}) | ||
test("on cancel", function() { | ||
var called | ||
dot.beforeOn(["a", "b"], "c", function( | ||
p, | ||
a, | ||
d, | ||
e, | ||
sig | ||
) { | ||
dot.on(["a", "b"], "c", function(p, a, d, e, sig) { | ||
sig.cancel = true | ||
@@ -111,9 +85,3 @@ }) | ||
test("on value", function() { | ||
dot.beforeOn(["a", "b"], "c", function( | ||
p, | ||
a, | ||
d, | ||
e, | ||
sig | ||
) { | ||
dot.on(["a", "b"], "c", function(p, a, d, e, sig) { | ||
sig.value = true | ||
@@ -126,3 +94,3 @@ }) | ||
test("on value (from return)", function() { | ||
dot.beforeOn(["a", "b"], "c", function() { | ||
dot.on(["a", "b"], "c", function() { | ||
return true | ||
@@ -143,4 +111,6 @@ }) | ||
return new Promise(function(resolve) { | ||
sig.value = "hi" | ||
resolve() | ||
setTimeout(function() { | ||
sig.value = "hi" | ||
resolve() | ||
}, 1) | ||
}) | ||
@@ -179,22 +149,2 @@ }) | ||
test("onAny before/after", function() { | ||
var order = [] | ||
dot.afterAny(["a", "b"], function() { | ||
order.push(3) | ||
}) | ||
dot.any("a", function() { | ||
order.push(2) | ||
}) | ||
dot.beforeAny("a", function() { | ||
order.push(1) | ||
}) | ||
return dot("a", "b", "c").then(function() { | ||
expect(order).toEqual([1, 2, 3]) | ||
}) | ||
}) | ||
test("off", function() { | ||
@@ -201,0 +151,0 @@ var called |
{ | ||
"name": "dot-event", | ||
"version": "3.0.28", | ||
"version": "3.0.29", | ||
"description": "Powerful event emitter", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
109
README.md
@@ -7,43 +7,98 @@ # dot-event | ||
## Buzzwords | ||
## Purpose | ||
Dot-event produces code that is: | ||
Dot-event provides a succinct interface for calling events and persisting state with a tiny footprint (<1kb). | ||
- Asynchronous | ||
- Debuggable | ||
- Decoupled | ||
- Extensible | ||
- Testable | ||
## Setup | ||
## Listening to events | ||
```js | ||
const dot = require("dot-event")() | ||
``` | ||
## Basics | ||
```js | ||
var dot = require("dot-event")() | ||
dot.on(() => {}) // listener | ||
dot() // emitter | ||
``` | ||
dot.any("someEvent", function(prop, arg, dot, event) { | ||
// ^—— event ^—— listener | ||
}) | ||
## Return value | ||
```js | ||
dot.on(() => "a") | ||
dot() // "a" | ||
``` | ||
- The first listener argument (`prop`) is an `Array.<String>` of prop keys. | ||
- The second listener argument (`arg`) is any value. | ||
- The third listener argument (`dot`) is the dot-event instance. | ||
- The fourth listener argument (`event`) is the event `String`. | ||
## Async return value | ||
## Emitting events | ||
```js | ||
dot.on(async () => "a") | ||
dot().then(result => /* [ "a" ] */) | ||
``` | ||
## Event id | ||
```js | ||
dot.someEvent("a.b.c", ["d", "e", "f"], { opt: true }) | ||
// ^—— event ^—— prop ^—— prop ^—— arg | ||
dot.on("a", () => "b") | ||
dot("a") // "b" | ||
``` | ||
- The last argument (`arg`) may be any value. | ||
- All arguments up to the last form a dot-delimited prop identifier. Only `String` and `Array.<String>` arguments form the prop identifier. | ||
**Event id tip:** The first string or element in an array of strings passed to `dot.on` or `dot.any` is considered the event id. | ||
## Existing dot composers | ||
## Event id & props | ||
| Library | Description | URL | | ||
| ------- | ----------------------------- | ----------------------------------- | | ||
| arg | Run functions from CLI or web | https://github.com/dot-event/arg | | ||
| log | Log functions | https://github.com/dot-event/log2 | | ||
| store | Immutable store | https://github.com/dot-event/store2 | | ||
```js | ||
dot.on("a", "b", "c", prop => prop) | ||
dot("a", "b", "c") // ["b", "c"] | ||
``` | ||
**Prop tip 1:** Any string or array of strings passed to `dot` after the event id are considered prop identifiers. | ||
**Prop tip 2:** The listener function always receives a prop array as its first argument. | ||
## Emit argument | ||
```js | ||
dot.on((prop, arg) => arg) | ||
dot({ a: "b" }) // { a: "b" } | ||
``` | ||
**Arg tip 1:** The last non-prop emit argument is considered the user-provided argument. | ||
**Arg tip 2:** The listener function always receives the user-provided argument as its second argument. | ||
## Any | ||
```js | ||
dot.any(() => "!!!") | ||
dot("a", "b", "c") // "!!!" | ||
``` | ||
## Helper function | ||
```js | ||
dot.any("a", props => props) | ||
dot.a("b", "c") // [ "b", "c" ] | ||
``` | ||
**Helper tip:** Dot-event creates a helper function only if `dot.any` receives an event id with no props. | ||
## Listener arguments | ||
No matter what is passed to the `dot` emitter, listener functions always receive five arguments: | ||
- `prop` — an array of string identifiers | ||
- `arg` — a user-provided argument | ||
- `dot` — the dot-event instance | ||
- `event` — the event id | ||
- `signal` — dot-event signal object (use `signal.cancel = true` for event cancellation) | ||
## Dot composers | ||
| Library | Description | URL | | ||
| ------- | ------------------------------ | ----------------------------------- | | ||
| ad | Google Publisher Tag functions | https://github.com/dot-event/ad | | ||
| arg | CLI and URL argument handling | https://github.com/dot-event/arg | | ||
| fetch | Universal HTTP fetch function | https://github.com/dot-event/fetch | | ||
| log | Log functions | https://github.com/dot-event/log2 | | ||
| store | Immutable store | https://github.com/dot-event/store2 | |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
104
13078
373