From 4d241798feee39d72f9b541ad7d4e61b59a763f8 Mon Sep 17 00:00:00 2001 From: NickSettler Date: Thu, 4 Aug 2022 02:04:36 +0200 Subject: [PATCH] (docs) updates readme --- README.md | 93 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 74 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 7ca14eb..71260d9 100644 --- a/README.md +++ b/README.md @@ -16,16 +16,15 @@ Event System is a package used for handling different events between parts of co * [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) ## EventsSystem -### init(options: EventsSystemOptions) +### `new EventSystem(options: EventsSystemOptions)` Initializes the Event System class. @@ -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(); -### 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(); -### 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} The arguments to pass to the handler. +* `event {keyof E}` The event name. +* `args {Parameters}` The arguments to pass to the handler. + +Example: +```typescript +type MyEvents = { + "event": (a: number, b: string) => void; +} + +const eventSystem = new EventSystem(); + +// Subscribe to event +eventSystem.subscribe("event", (a, b) => { + // Do something +}); + +// Trigger event +eventSystem.notify("event", 1, "Hello"); +```