Skip to content

Commit

Permalink
[COLIB] - DOCS - update README
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesMangwa committed Aug 25, 2022
1 parent 3a4a701 commit a6384ab
Showing 1 changed file with 3 additions and 69 deletions.
72 changes: 3 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
# Emittify - a tiny event emitter

This is a tiny event emitter written with first class typescript support.

It supports caching and has hooks for React and Solid.
<br/><br/>
This is a tiny event emitter written with first class Typescript support.
It supports caching and has hooks for both React and Solid.

## Installation

<br/>

```bash
yarn add @colorfy-software/emittify
```

<br/><br/>

## Getting started

<br/><br/>

### Creating an Emitter with types

<br/>

```typescript
// events-core.ts

Expand All @@ -46,17 +36,12 @@ const emitter = new Emittify<EventsType>({
export default emitter
```

<br/>

### Sending and listening to events

<br/>

```typescript
// File where you want to use it
import emitter from './events-core'


// register a listener for the 'toast-notification' event
emitter.listen('toast-notification', data => {
const { message, type } = data // All is typed and auto-completed
Expand All @@ -71,16 +56,13 @@ emitter.send('toast-notification', {
type: 'success'
}


// emit the 'direct-message-count' event
emitter.send('direct-message-count', 10)

// get the cached event
const cachedEvent = emitter.getCache('direct-message-count', 0) // Can provide second argument as default value if none is sent yet
```
<br/>
### Testing with Jest
If you don't already have a Jest setup file configured, please add the following to your [Jest configuration file](https://jestjs.io/docs/configuration) and create the new `jest.setup.js` file in project root:
Expand All @@ -95,32 +77,22 @@ You can then add the following line to that setup file to mock the `NativeModule
jest.mock('@colorfy-software/emittify', () => require('@colorfy-software/emittify/mock'));
```
## Hooks
<br/>
<br/>
### For React:
```typescript
import Emittify from '@colorfy-software/emittify/react'
```
<br/>
### For Solid:
```typescript
import Emittify from '@colorfy-software/emittify/solid'
```
<br/>
## Usage
<br/>
```tsx
// import previously created emitter
import emitter from '../core/events-core.ts'
Expand All @@ -133,35 +105,17 @@ const Component = () => {
}
```
<br/>
## All Methods on Emittify class
<br/>
<!-- send -> emittify.send('event-name', value) -> Sends an event with specified name and value -->
<!-- listen -> emittify.listen('event-name', callback) -> Listens to events with specified name and triggers a callback on each event-->
<!-- useEventListener -> emittify.useEventListener('event-name', initialValue) -> Emits an event with specified name and value. Returns cached value if one exists, otherwise returns initial value if that is provided -->
<!-- getCache -> emittify.getCache('event-name', initialValue) -> Gets the cached value for event name -->
<!-- clearCache -> emittify.clearCache('event-name') -> Clears cache for given event name -->
<!-- clearAllCache -> emittify.clearAllCache() -> Clears all of the cache -->
<!-- clear -> emittify.clear('event-name') -> Clears listeners for given event name -->
### send
<br/>
```typescript
// send an event with specified name and value
emittify.send('event-name', value)
```
<br/>
### listen
<br/>
```typescript
// listen to events with specified name and triggers a callback on each event
const listener = emittify.listen('event-name', callback)
Expand All @@ -172,56 +126,36 @@ listener.event // Name of the event
listener.clearListener() // Clears the listener
```
<br/>
### useEventListener
<br/>
```typescript
// emits an event with specified name and value. Returns cached value if one exists, otherwise returns initial value if that is provided
emittify.useEventListener('event-name', initialValue)
```
<br/>
``

### getCache

<br/>
```typescript
// gets the cached value for event name
emittify.getCache('event-name', initialValue)
```

<br/>
### clearCache

<br/>
```typescript
// clears cache for given event name
emittify.clearCache('event-name')
```

<br/>
### clearAllCache

<br/>
```typescript
// clears all of the cache
emittify.clearAllCache()
```

<br/>
### clear

<br/>
```typescript
// clears listeners for given listener id
emittify.clear('listener-id')
Expand Down

0 comments on commit a6384ab

Please sign in to comment.