Skip to content

Commit

Permalink
(docs) updates readme
Browse files Browse the repository at this point in the history
  • Loading branch information
NickSettler committed Aug 4, 2022
1 parent e0f4d55 commit 4d24179
Showing 1 changed file with 74 additions and 19 deletions.
93 changes: 74 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ Event System is a package used for handling different events between parts of co

<!-- TOC -->
* [EventsSystem](#eventssystem)
* [init(options: EventsSystemOptions)](#init--options--eventssystemoptions-)
* [getInstance()](#getinstance--)
* [subscribe(event, handler)](#subscribe--event-handler-)
* [unsubscribe(event, handler)](#unsubscribe--event-handler-)
* [notify(event, ...args)](#notify--event-args-)
* [`new EventSystem(options: EventsSystemOptions)`](#new-eventsystemoptions-eventssystemoptions)
* [`subscribe(event, handler)`](#subscribeevent-handler)
* [`unsubscribe(event, handler)`](#unsubscribeevent-handler)
* [`notify(event, ...args)`](#notifyevent-args)
<!-- TOC -->

## EventsSystem

### init(options: EventsSystemOptions)
### `new EventSystem(options: EventsSystemOptions)`

Initializes the Event System class.

Expand All @@ -34,33 +33,89 @@ Parameters:
* `bufferDirection` {"FIFO" | "LIFO"} - The direction of the events buffer. Defaults to "FIFO".
* `bufferSize` {number} - The size of the buffer.

Example:
```typescript
// Initialization without options
const eventSystem = new EventSystem(); // OK ✅

### getInstance()
// Initialization with options
const eventSystem = new EventSystem({
bufferDirection: "LIFO",
bufferSize: 10
}); // OK ✅
```

Returns the Event System instance.

Returns: `EventSystem`

### subscribe(event, handler)
### `subscribe(event, handler)`

Subscribes handler to be called when the event is triggered.

Parameters:
* event {EVENT_SYSTEM_EVENT_NAMES} The event name.
* handler {Function} The handler function.
* `event {keyof E}` The event name.
* `handler {Function}` The handler function.

Example:
```typescript
type MyEvents = {
"event": () => void;
}

const eventSystem = new EventSystem<MyEvents>();

### unsubscribe(event, handler)
// Subscribe to event
eventSystem.subscribe("event", () => {
// Do something
});
```

### `unsubscribe(event, handler)`

Unsubscribes the handler from the event.

Parameters:
* event {EVENT_SYSTEM_EVENT_NAMES} The event name.
* handler {Function} The handler function.
* `event {keyof E}` The event name.
* `handler {Function}` The handler function.

Example:
```typescript
type MyEvents = {
"event": () => void;
}

const eventSystem = new EventSystem<MyEvents>();

### notify(event, ...args)
// Subscribe to event
eventSystem.subscribe("event", () => {
// Do something
});

// Unsubscribe from event
eventSystem.unsubscribe("event", () => {
// Do something
});
```

### `notify(event, ...args)`

Triggers the event with the given arguments.

Parameters:
* event {EVENT_SYSTEM_EVENT_NAMES} The event name.
* args {Parameters<EventSystemEvents[EVENT_SYSTEM_EVENT_NAMES]>} The arguments to pass to the handler.
* `event {keyof E}` The event name.
* `args {Parameters<E[keyof E]>}` The arguments to pass to the handler.

Example:
```typescript
type MyEvents = {
"event": (a: number, b: string) => void;
}

const eventSystem = new EventSystem<MyEvents>();

// Subscribe to event
eventSystem.subscribe("event", (a, b) => {
// Do something
});

// Trigger event
eventSystem.notify("event", 1, "Hello");
```

0 comments on commit 4d24179

Please sign in to comment.