Skip to content

Commit ce5f740

Browse files
committed
Add named export, update README
1 parent 359e8d9 commit ce5f740

File tree

3 files changed

+51
-48
lines changed

3 files changed

+51
-48
lines changed

README.md

+12-15
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,35 @@ Evee is a lightweight event library, written in clean JavaScript.
88
Evee exports ESM, CommonJS and Browser globals, so you can use it in any environment.
99

1010
## Upgrading to Evee 3
11-
Evee now natively supports ESM and CommonJS, so you can import it in any environment.
11+
Evee now natively supports ESM and CommonJS.
12+
13+
We export two things:
14+
- `Evee` (default export): The class you can use to create new instances of Evee.
15+
- `evee` (named export): A pre-made instance of Evee so you can immediately start using it.
1216

1317
If you're planning to use Evee in NodeJS, you can now import it like this:
1418
```js
15-
import Evee from 'evee'; // If you're using ESM (import/export)
16-
const Evee = require('evee').default; // If you're using CommonJS (require)
19+
import Evee, { evee } from 'evee'; // If you're using ESM (import/export)
20+
const { default: Evee, evee } = require('evee'); // If you're using CommonJS (require)
1721
```
1822

1923
If you're planning to use Evee in the browser, you can now import it like this:
2024
```html
2125
<!-- ESM if you're targeting modern browsers -->
2226
<script type="module">
23-
import Evee from 'https://cdn.jsdelivr.net/npm/evee';
24-
const evee = new Evee();
27+
import Evee, { evee } from 'https://cdn.jsdelivr.net/npm/evee';
2528
</script>
2629

2730
<!-- Global export if you're targeting older browsers -->
2831
<script src="https://cdn.jsdelivr.net/npm/evee/dist/browser/index.js"></script>
2932
<script>
30-
const evee = new Evee(); // `Evee` is defined globally
33+
// You can use the `Evee` (class) and `evee` (instance) globals here
3134
</script>
3235
```
3336

3437
## How to use
3538
```js
36-
import Evee from 'evee'
37-
38-
const evee = new Evee()
39+
import { evee } from 'evee'
3940

4041
// Subscribe to the 'update' event
4142
evee.on('update', e => console.log(`Received event #${e.data}`))
@@ -50,9 +51,7 @@ for (let i = 0; i < 100; i++) {
5051
You can also keep track of your event listeners unsubscribe from events you don't need anymore.
5152

5253
```js
53-
import Evee from 'evee'
54-
55-
const evee = new Evee()
54+
import { evee } from 'evee'
5655

5756
// Subscribe to the 'say' event
5857
var receiver = evee.on('say', e => console.log(e.data));
@@ -68,9 +67,7 @@ If you want to fire an event only once, you can do that too!
6867
The event will be automatically removed after the first usage:
6968

7069
```js
71-
import Evee from 'evee'
72-
73-
const evee = new Evee()
70+
import { evee } from 'evee'
7471

7572
// Subscribe to the 'say' event
7673
evee.once('say', e => console.log('hello, world'));

lib/evee.js

+2
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,5 @@ export default class Evee {
178178
this.receivers = [];
179179
}
180180
}
181+
182+
export const evee = new Evee();

test.js

+37-33
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,82 @@
11
// Grab assert and the evee module
22
import assert from 'assert'
3-
import Evee from './dist/esm/index.js'
3+
import Evee, { evee } from './dist/esm/index.js'
44

5-
// Run tests with ES6 evee
6-
run_tests(Evee, "evee (node)");
5+
// Run tests with evee
6+
run_tests(() => new Evee(), "evee (node, new instance)");
7+
run_tests(() => {
8+
evee.clear();
9+
return evee
10+
}, "evee (node, shared instance)");
711

8-
function run_tests(Evee, name) {
12+
function run_tests(makeInstance, name) {
913
describe(name, () => {
1014
describe('#on(name, action)', () => {
1115
it('should dispatch properly', () => {
12-
var evee = new Evee();
16+
const evee = makeInstance();
1317
var res = false;
1418
evee.on('a', _ => res = true);
1519
evee.signal('a');
1620
assert.equal(true, res);
1721
});
1822
it('should increment the id when subscribing', () => {
19-
var evee = new Evee();
23+
const evee = makeInstance();
2024
assert.equal(0, evee.on('a', _ => undefined).id);
2125
assert.equal(1, evee.on('b', _ => undefined).id);
2226
assert.equal(2, evee.on('c', _ => undefined).id);
2327
});
2428
it('should throw when the name is not a string', () => {
25-
var evee = new Evee();
29+
const evee = makeInstance();
2630
assert.throws(() => evee.on(undefined, () => undefined));
2731
});
2832
it('should throw when the action is not a function', () => {
29-
var evee = new Evee();
33+
const evee = makeInstance();
3034
assert.throws(() => evee.on('a', undefined));
3135
});
3236
it('should throw when called without arguments', () => {
33-
var evee = new Evee();
37+
const evee = makeInstance();
3438
assert.throws(() => evee.on());
3539
});
3640
});
3741
describe('#once(name, action)', () => {
3842
it('should only dispatch once', () => {
39-
var evee = new Evee();
43+
const evee = makeInstance();
4044
var res = false;
4145
evee.once('a', _ => res = !res);
4246
evee.signal('a', 'a');
4347
assert.equal(true, res);
4448
});
4549
it('should increment the id when subscribing', () => {
46-
var evee = new Evee();
50+
const evee = makeInstance();
4751
assert.equal(0, evee.once('a', _ => undefined).id);
4852
assert.equal(1, evee.once('b', _ => undefined).id);
4953
assert.equal(2, evee.once('c', _ => undefined).id);
5054
});
5155
it('should throw when the name is not a string', () => {
52-
var evee = new Evee();
56+
const evee = makeInstance();
5357
assert.throws(() => evee.once(undefined, () => undefined));
5458
});
5559
it('should throw when the action is not a function', () => {
56-
var evee = new Evee();
60+
const evee = makeInstance();
5761
assert.throws(() => evee.once('a', undefined));
5862
});
5963
});
6064
describe('#drop(event)', () => {
6165
it('should return true when subscriptions were removed', () => {
62-
var evee = new Evee();
66+
const evee = makeInstance();
6367
var ev = evee.on('a', _ => undefined);
6468
assert.equal(true, evee.drop(ev));
6569
});
6670
it('should throw when the argument is not an event object', () => {
67-
var evee = new Evee();
71+
const evee = makeInstance();
6872
assert.throws(() => evee.drop(0));
6973
});
7074
it('should throw when called without arguments', () => {
71-
var evee = new Evee();
75+
const evee = makeInstance();
7276
assert.throws(() => evee.drop());
7377
});
7478
it('should remove the right event', () => {
75-
var evee = new Evee();
79+
const evee = makeInstance();
7680
var resa = false;
7781
var resb = false;
7882
var eva = evee.on('a', _ => resa = true);
@@ -85,7 +89,7 @@ function run_tests(Evee, name) {
8589
});
8690
describe('#emit(name, data)', () => {
8791
it('should transmit the correct event data when dispatching', () => {
88-
var evee = new Evee();
92+
const evee = makeInstance();
8993
var res = 0;
9094
evee.on('a', e => res += e.data);
9195
evee.on('b', e => res += e.data);
@@ -94,50 +98,50 @@ function run_tests(Evee, name) {
9498
assert.equal(22, res);
9599
});
96100
it('should transmit the correct sender when dispatching', () => {
97-
var evee = new Evee();
101+
const evee = makeInstance();
98102
var res = false;
99103
var sender = evee.on('a', e => res = e.sender === sender);
100104
evee.emit('a');
101105
assert.equal(true, res);
102106
});
103107
it('should not dispatch when the event name matches no receivers', () => {
104-
var evee = new Evee();
108+
const evee = makeInstance();
105109
var res = false;
106110
evee.on('a', _ => res = true);
107111
evee.emit('b');
108112
assert.equal(false, res);
109113
});
110114
it('should dispatch when the event name matches a receiver', () => {
111-
var evee = new Evee();
115+
const evee = makeInstance();
112116
var res = false;
113117
evee.on('a', _ => res = true);
114118
evee.emit('a');
115119
assert.equal(true, res);
116120
});
117121
it('should dispatch when the event name matches multiple receivers', () => {
118-
var evee = new Evee();
122+
const evee = makeInstance();
119123
var res = 0;
120124
evee.on('a', _ => ++res);
121125
evee.on('a', _ => ++res);
122126
evee.emit('a');
123127
assert.equal(2, res);
124128
});
125129
it('should dispatch events with data', () => {
126-
var evee = new Evee();
130+
const evee = makeInstance();
127131
var res = false;
128132
evee.on('a', e => res = e.data);
129133
evee.emit('a', true);
130134
assert.equal(true, res);
131135
});
132136
it('should dispatch events without data', () => {
133-
var evee = new Evee();
137+
const evee = makeInstance();
134138
var res = false;
135139
evee.on('a', e => res = e.data === undefined ? true : false);
136140
evee.emit('a');
137141
assert.equal(true, res);
138142
});
139143
it('should dispatch multiple events with the same data', () => {
140-
var evee = new Evee();
144+
const evee = makeInstance();
141145
var resa = false;
142146
var resb = false;
143147
evee.on('a', e => resa = e.data);
@@ -147,7 +151,7 @@ function run_tests(Evee, name) {
147151
assert.equal(true, resb);
148152
});
149153
it('should dispatch multiple events with no data', () => {
150-
var evee = new Evee();
154+
const evee = makeInstance();
151155
var resa = true;
152156
var resb = false;
153157
evee.on('a', e => resa = e.data === undefined ? resa : !resa);
@@ -157,7 +161,7 @@ function run_tests(Evee, name) {
157161
assert.equal(true, resb);
158162
});
159163
it('should dispatch multiple events with different data', () => {
160-
var evee = new Evee();
164+
const evee = makeInstance();
161165
var resa = false;
162166
var resb = true;
163167
evee.on('a', e => resa = e.data);
@@ -167,13 +171,13 @@ function run_tests(Evee, name) {
167171
assert.equal(false, resb);
168172
});
169173
it('should throw when called without arguments', () => {
170-
var evee = new Evee();
174+
const evee = makeInstance();
171175
assert.throws(() => evee.emit());
172176
});
173177
});
174178
describe('#signal(...names)', () => {
175179
it('should correctly dispatch multiple events a single time', () => {
176-
var evee = new Evee();
180+
const evee = makeInstance();
177181
var resa = false;
178182
var resb = false;
179183
evee.on('a', _ => resa = true);
@@ -183,28 +187,28 @@ function run_tests(Evee, name) {
183187
assert.equal(true, resb);
184188
});
185189
it('should correctly dispatch the same event multiple multiple', () => {
186-
var evee = new Evee();
190+
const evee = makeInstance();
187191
var res = false;
188192
evee.on('a', _ => res = !res);
189193
evee.signal('a', 'a', 'a');
190194
assert.equal(true, res);
191195
});
192196
it('should throw when called without arguments', () => {
193-
var evee = new Evee();
197+
const evee = makeInstance();
194198
assert.throws(() => evee.signal());
195199
});
196200
});
197201
describe('#clear()', () => {
198202
it('should clear the receiver list', () => {
199-
var evee = new Evee();
203+
const evee = makeInstance();
200204
var res = false;
201205
evee.on('a', _ => res = true);
202206
evee.clear();
203207
evee.signal('a');
204208
assert.equal(false, res);
205209
});
206210
it('should not throw when no subscriptions are active', () => {
207-
var evee = new Evee();
211+
const evee = makeInstance();
208212
assert.doesNotThrow(() => evee.clear());
209213
});
210214
});

0 commit comments

Comments
 (0)