Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
piecioshka committed Nov 24, 2024
1 parent 8394b34 commit 67baa9e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SuperEventEmitter.mixin(bar);

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

bar.emit('test');
```
Expand Down
21 changes: 16 additions & 5 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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
16 changes: 8 additions & 8 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ describe("SuperEventEmitter", function () {
entity.emit("foo");
});

it("can add listener with expected params", function () {
it("can add listener with expected payload", function () {
entity.on("foo", noop);
entity.emit("foo", { foo: "bar" });

expect(noop).toHaveBeenCalledWith({ foo: "bar" });
});

it("should throw error when try run with incorrect params", function () {
it("should throw error when try run with incorrect payload", function () {
expect(function () {
// @ts-expect-error
entity.on();
Expand Down Expand Up @@ -202,7 +202,7 @@ describe("SuperEventEmitter", function () {
expect(entity._listeners.length).toEqual(0);
});

it("should throw error when try run with incorrect params", function () {
it("should throw error when try run with incorrect payload", function () {
expect(function () {
// @ts-expect-error
entity.once();
Expand Down Expand Up @@ -305,7 +305,7 @@ describe("SuperEventEmitter", function () {
expect(callback).not.toHaveBeenCalled();
});

it("should not throw error when try run with incorrect params", function () {
it("should not throw error when try run with incorrect payload", function () {
expect(function () {
entity.off();
}).not.toThrow();
Expand Down Expand Up @@ -346,7 +346,7 @@ describe("SuperEventEmitter", function () {
expect(noop).toHaveBeenCalled();
});

it("should throw error when try run with incorrect params", function () {
it("should throw error when try run with incorrect payload", function () {
expect(function () {
// @ts-expect-error
entity.emit();
Expand All @@ -369,10 +369,10 @@ describe("SuperEventEmitter", function () {
expect(point).toEqual(6);
});

it("should allow passing params", function () {
it("should allow passing payload", function () {
entity.on("foo", noop);
entity.emit("foo", "params");
expect(noop).toHaveBeenCalledWith("params");
entity.emit("foo", "payload");
expect(noop).toHaveBeenCalledWith("payload");
});

it("should allow chaining emits", function () {
Expand Down

0 comments on commit 67baa9e

Please sign in to comment.