Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koheing/observevent
observe event
https://github.com/koheing/observevent
event observer observer-pattern typescript
Last synced: 1 day ago
JSON representation
observe event
- Host: GitHub
- URL: https://github.com/koheing/observevent
- Owner: koheing
- License: mit
- Created: 2022-07-16T01:43:57.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-09-08T13:44:33.000Z (over 2 years ago)
- Last Synced: 2024-12-10T09:54:13.226Z (26 days ago)
- Topics: event, observer, observer-pattern, typescript
- Language: TypeScript
- Homepage:
- Size: 1010 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Observevent
Event Observer inspired by [svelte/store](https://github.com/sveltejs/svelte) and [rxjs](https://github.com/ReactiveX/rxjs)## Installation
```
npm i observevent
```## Usage
### `subjectify`
```ts
import { subjectify } from 'observevent'const food = subjectify('')
const unsubscribe = food.subscribe((newValue, oldValue) => console.log(newValue, oldValue))
// or
// food.subscribe((newValue) => console.log(newValue))food.notify('apple')
food.notify((it) => it + ' pie')unsubscribe()
// Output
// ----------
// '', undefined
// 'apple, '
// 'apple pie, apple'
```### `observify`
```ts
import { observify, select } from 'observevent'
import { EventEmitter } from 'events'const emitter = new EventEmitter()
const logger = {
info: (...args: unknown[]) => console.log('[food]', ...args)
}function isPie(food: string): boolean {
return food.indexOf('pie') > -1
}const food = observify('', (trigger) => {
emitter.on('food', trigger)
return () => emitter.removeAllListener('food')
}, /** Option **/ { logging: true, logger })const unsubscribe = select(food, isPie).subscribe((it) => it)
emitter.emit('food', 'apple')
unsubscribe()
// Output
// ----------
// '[food] "", undefined'
// '[food] "", apple'
```## Options
`observify` and `subjectify` and `combine` and `combineMap` have the following options:### `logging`
Setting `logging` to `true` will output changelog.
Default value is `false`.
```ts
import { subjectify } from 'observevent'const food = subjectify('', { logging: true })
const unsubscribe = food.subscribe((it) => it)food.notify('apple')
food.notify((it) => it + ' pie')unsubscribe()
// Output
// ----------
// { before: undefined, after: '' }
// { before: '', after: 'apple' }
// { before: 'apple', after: 'apple pie' }
```### `logger`
You can set a custom logger.
```ts
import { subjectify } from 'observevent'const logger = {
info: (value: unknown) => console.log('[food]', value)
}const food = subjectify('', { logging: true, logger })
const unsubscribe = food.subscribe((it) => it)food.notify('apple')
food.notify((it) => it + ' pie')unsubscribe()
// Output
// ----------
// [food] { before: undefined, after: '' }
// [food] { before: '', after: 'apple' }
// [food] { before: 'apple', after: 'apple pie' }
```### `immediate`
If `immediate` is set to `false`, the current value will not be notified and will be notified from changes after subscription.
Default value is `true`.
```ts
import { subjectify } from 'observevent'const food = subjectify('', { immediate: false })
const unsubscribe = food.subscribe((it) => console.log(it))food.notify('apple')
food.notify((it) => it + ' pie')unsubscribe()
// Output
// ----------
// 'apple'
// 'apple pie'
```## Derives
- [map](https://github.com/koheing/observevent/blob/main/src/derives/map.ts)
- [select](https://github.com/koheing/observevent/blob/main/src/derives/select.ts)
- [exclude](https://github.com/koheing/observevent/blob/main/src/derives/exclude.ts)
- [combine](https://github.com/koheing/observevent/blob/main/src/derives/combine.ts)
- [combineMap](https://github.com/koheing/observevent/blob/main/src/derives/combinemap.ts)