Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to TypeScript #25

Merged
merged 6 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage/
.nyc_output/

dist/
types/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Piotr Kowalski
Copyright (c) 2024 Piotr Kowalski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
62 changes: 13 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

## Motivation

I was created a blog post (in polish) about this tool:<br/>
<https://piecioshka.pl/blog/2016/01/29/narzedzia-swiata-super-event-emitter-js.html>
I was created a blog post (in polish) about motivation why I created this tool:<br/>
<https://piecioshka.pl/blog/2016/01/29/narzedzia-swiata-super-event-emitter.html>

_(I'm sorry, but I don't have time to translate this article into English)_

## Installation

Expand All @@ -23,49 +25,25 @@ npm install super-event-emitter
## Usage — CommonJS

```javascript
const EventEmitter = require('super-event-emitter');
// or
const { EventEmitter } = require('super-event-emitter');
```
const { SuperEventEmitter } = require('super-event-emitter');

## Usage — ECMAScript Modules (ex. in TypeScript world)

```ts
import { EventEmitter } from "super-event-emitter";
// or
// import EventEmitter from "super-event-emitter";

class Cart extends EventEmitter {
addProduct(product: Product) {
this.emit('cart:addProduct', { product });
}
}
```

### Demo #1 — Typical object literal

<details>

```javascript
const bar = {};

EventEmitter.mixin(bar);
SuperEventEmitter.mixin(bar);

bar.on('test', function () {
console.log('triggered!');
}, this);
});

bar.emit('test');
```

</details>
## Usage — ECMAScript Modules `*.mjs` or TypeScript `*.ts`

### Demo #2 — Class API from ECMAScript 2015

<details>
```ts
import { SuperEventEmitter } from "super-event-emitter";

```javascript
class Person extends EventEmitter {
class Person extends SuperEventEmitter {
say(message) {
this.emit('say', message);
}
Expand All @@ -80,24 +58,10 @@ p1.on('say', function (message) {
p1.say('I love cookie');
```

</details>

## Documentation

* [API](./docs/api.md)

## Unit tests

```bash
npm test
```

## Code coverage

```bash
npm run coverage
```
API Documentation is available in separated [document](./docs/api.md).

## License

[The MIT License](https://piecioshka.mit-license.org) @ 2016
[The MIT License](https://piecioshka.mit-license.org) @ 2016-2024
23 changes: 17 additions & 6 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# API
# SuperEventEmitter API

#### `on( name: string, fn: Function, ctx: Object )`
## `on( name: string, fn: Function, ctx?: Object )`

* `name` - a string value representing the name of event
* `fn` - action which will be called when event will be triggered
Expand All @@ -9,6 +9,10 @@
Example:

```javascript
instance.on('foo', function (payload) {
console.log(payload, this);
});

instance.on('foo', function (payload) {
console.log(payload, this);
}, instance);
Expand All @@ -28,37 +32,44 @@ instance.on('all', function (name, payload) {
instance.emit('something', { foo: 1 });
```

#### `once( name: string, fn: Function, ctx: Object )`
## `once( name: string, fn: Function, ctx?: Object )`

The same as `on` but, after triggered event, destroy all listeners

Example:

```javascript
instance.once('foo', function (payload) {
console.log(payload, this);
});

instance.once('foo', function (payload) {
console.log(payload, this);
}, instance);
```

#### `off( name: string, fn: Function )`
## `off( name?: string, fn?: Function )`

* `name` - a string value representing the name of event
* `fn` - action which will be removed from listeners

Example:

```javascript
instance.off();
instance.off('foo');
instance.off('foo', fooHandler);
```

#### `emit( name: string, params: Object )`
## `emit( name: string, payload?: Object )`

* `name` - a string value representing the name of event
* `params` - will be passed as first argument in called actions
* `payload` - will be passed as first argument in called actions

Example:

```javascript
instance.emit('foo');
instance.emit('foo', { name: 'bar' });
```

Expand Down
35 changes: 0 additions & 35 deletions index.d.ts

This file was deleted.

4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
'use strict';

module.exports = require('./src/index');
module.exports = require("./dist/main");
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
testEnvironment: "node",
transform: {
"^.+.tsx?$": ["ts-jest", {}],
},
};
Loading
Loading