Skip to content

Commit

Permalink
⚡ add TypeScript support
Browse files Browse the repository at this point in the history
  • Loading branch information
robinpokorny committed Dec 6, 2020
1 parent 976e49f commit ac1b634
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- **Efficient:** No memory leaks, no duplicate calls, no extra looping.
- **Clean API:** `sub` and `pub` (or `on` and `emit`), need no more, can't go less.
- **Modular:** Use only what you need.
- **Typed** TypeScript definitions are bundled

## Install

Expand All @@ -39,7 +40,7 @@ import eventEmitter from "dead-simple/eventEmitter";
// === PubSub ======
const clicks = pubsub();

const unSub = clicks.sub(target => console.log(`Clicked on ${target}!`));
const unSub = clicks.sub((target) => console.log(`Clicked on ${target}!`));

clicks.pub("button");
// -> Clicked on button!
Expand All @@ -53,9 +54,9 @@ clicks.pub("link");
// eventEmitter = named PubSub
const events = eventEmitter();

events.on("click", target => console.log(`Clicked on ${target}!`));
events.on("click", (target) => console.log(`Clicked on ${target}!`));

const unSubChange = events.on("change", newValue =>
const unSubChange = events.on("change", (newValue) =>
console.log(`Value is now ${newValue}!`)
);

Expand Down Expand Up @@ -99,19 +100,19 @@ We were able to get down to **91B** for PubSub:

```js
export default (s = new Set()) => ({
pub: d => s.forEach(f => f(d)),
sub: f => s.add(f).delete.bind(s, f)
pub: (d) => s.forEach((f) => f(d)),
sub: (f) => s.add(f).delete.bind(s, f),
});
```

And **139B** for EventEmitter (`p` is PubSub):

```js
export default e => (
export default (e) => (
(e = new Map()),
Object.freeze({
on: (n, f) => (e.has(name) || e.set(n, p()), e.get(n).sub(f)),
emit: (n, d) => e.has(n) && e.get(n).pub(d)
emit: (n, d) => e.has(n) && e.get(n).pub(d),
})
);
```
Expand Down
6 changes: 6 additions & 0 deletions eventEmitter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type eventEmitter = () => Readonly<{
on: (name: string, fn: Function) => () => boolean;
emit: (name: string, data: unknown) => false | void;
}>;

export = eventEmitter;
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { default as pubsub } from './pubsub';
import { default as eventEmitter } from './eventEmitter';

export { pubsub, eventEmitter };
6 changes: 6 additions & 0 deletions pubsub.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type pubsub = () => Readonly<{
pub: (data: unknown) => void;
sub: (fn: Function) => () => boolean;
}>;

export = pubsub;

0 comments on commit ac1b634

Please sign in to comment.