| 1 |
# array-to-events [](https://travis-ci.org/jamestalmage/array-to-events) |
| 2 |
|
| 3 |
> Push a series of events to an emitter. Useful for testing. |
| 4 |
|
| 5 |
|
| 6 |
## Install |
| 7 |
|
| 8 |
``` |
| 9 |
$ npm install --save array-to-events |
| 10 |
``` |
| 11 |
|
| 12 |
|
| 13 |
## Usage |
| 14 |
|
| 15 |
```js |
| 16 |
import arrayToEvents from 'array-to-events'; |
| 17 |
|
| 18 |
arrayToEvents(targetEmitter, [ |
| 19 |
['foo', 'fooArg1', 'fooArg2'], |
| 20 |
['bar', 'barArg1', 'barArg2'] |
| 21 |
]); |
| 22 |
|
| 23 |
// pushes both a 'foo' and 'bar' event (with specified arguments) to the emitter. |
| 24 |
``` |
| 25 |
|
| 26 |
|
| 27 |
## API |
| 28 |
|
| 29 |
### arrayToEvents(targetEmitter, eventArray, [options]) |
| 30 |
|
| 31 |
Returns: `stop` callback |
| 32 |
|
| 33 |
Emit a series of events on `targetEmitter`. Depending on the specified options, events will be emitted synchronously or at specified intervals. In async mode, a `stop` callback is returned that, when called, will prevent further events from being emitted. |
| 34 |
|
| 35 |
#### targetEmitter |
| 36 |
|
| 37 |
*Required*<br> |
| 38 |
Type: `EventEmitter` |
| 39 |
|
| 40 |
The emitter where events will be pushed |
| 41 |
|
| 42 |
#### eventArray |
| 43 |
|
| 44 |
*Required*<br> |
| 45 |
Type: `two dimensional array` |
| 46 |
|
| 47 |
An array of event argument arrays. Must be a two dimensional array. The first value of each child array must be a `string` value with the event name. Additional array members will be passed as arguments when the array is triggered. |
| 48 |
|
| 49 |
``` |
| 50 |
[ |
| 51 |
['foo', 1], |
| 52 |
['bar', 2] |
| 53 |
] |
| 54 |
``` |
| 55 |
|
| 56 |
The above defines two events. First a `foo` event will be emitted with argument `1`. Then a `bar` event will be emitted with argument `2`. |
| 57 |
|
| 58 |
|
| 59 |
#### options |
| 60 |
|
| 61 |
##### sync |
| 62 |
|
| 63 |
Type: `boolean`<br> |
| 64 |
Default: *it depends* |
| 65 |
|
| 66 |
If `true`, all events will be emitted immediately and synchronously, one right after the other. It defaults to `true` *unless* the `delay` option or `done` callback are specified (in which case it defaults to `false`). Explicitly setting it to `true` will cause the `delay` option to be ignored. |
| 67 |
|
| 68 |
|
| 69 |
##### delay |
| 70 |
|
| 71 |
Type: `a number or the string "immediate"`<br> |
| 72 |
Default: `"immediate"` |
| 73 |
|
| 74 |
If set to `"immediate"`, events fire asynchronously with a minimal delay in between (`setImmediate` is used to schedule the next event). |
| 75 |
|
| 76 |
If set to a `number`, events are scheduled every `delay` milliseconds (via `setTimeout(nextEvent, opts.delay)`). |
| 77 |
|
| 78 |
##### done |
| 79 |
|
| 80 |
Type: `callback(error, finished)` |
| 81 |
|
| 82 |
An optional callback to be executed when all events have finished. The first argument will contain any error thrown during execution (not currently implemented). The second argument (`finished`) will be `true` if every event was emitted, `false` if the event stream was stopped prematurely using the `stop` function. |
| 83 |
|
| 84 |
## See Also |
| 85 |
|
| 86 |
- [`events-to-array`](https://www.npmjs.com/package/events-to-array) |
| 87 |
|
| 88 |
## License |
| 89 |
|
| 90 |
MIT © [James Talmage](http://github.com/jamestalmage) |