Skip to content

Commit 6de1e17

Browse files
v_0.1.0
add options in emittor setState functions
1 parent c099110 commit 6de1e17

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

README.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,12 @@ function Page() {
7878

7979
This adjusted code snippet provides a simple and easy-to-understand usage example of the `emittor` package. It demonstrates using the `useEmittor` hook to manage state, similar to how `useState` is used in React.
8080

81+
</br>
82+
8183
> **Warning** </br>
8284
> Always create an emittor in a separate file</br>
8385
> Because in development mode, the whole file will be rendered again. That can cause unexpected bugs.
8486
85-
.
86-
87-
> **Note** </br>
88-
> create emittor anywhere in your project like [`createContext`](https://react.dev/reference/react/createContext) in React. </br>
89-
> and import `Emittor` where use or link (in components and other places)
90-
91-
</br>
92-
9387
## Emittor In Multiple Components
9488

9589
The emittor package enables automatic state synchronization across React components. By simply integrating emittor, any component subscribing to state changes will update whenever the state changes anywhere in the application. This eliminates the need for manual state passing or provider wrapping, streamlining development and enhancing component responsiveness.
@@ -242,6 +236,30 @@ export default function Page() {
242236
}
243237
```
244238

239+
</br>
240+
241+
---
242+
243+
---
244+
245+
</br>
246+
247+
# API Reference
248+
249+
| Method | Description |
250+
|-----------------|-------------------------------------------------------------------------------------------------|
251+
| `constructor(initialState: T, options: { match?: boolean })` | Initializes an instance of `Emittor` with an initial state and optional matching behavior. If `options.match` is true, `setMatchState` and `emit` are bound to handle state updates; otherwise, `setOnlyState` and `emit` are used. |
252+
| `setState()`, `emit()` | Sets the state and Executes all subscribed callbacks (also use `state`) |
253+
| `getState()` | Returns the current state (also use `state`). |
254+
| `state` | Is current state (with `get` & `set`) you can modify directly. |
255+
| `exec()`, `refresh()` | Executes all subscribed callbacks with the current state. |
256+
| `run(state: T)` | Executes all callbacks with the provided `state` (this will not changed current state). |
257+
| `connect(callback: Callback<T>)` | Adds the `callback` to the list of callbacks to be executed on state changes. |
258+
| `disconnect(callback: Callback<T>)` | Removes the specified `callback` from the list of callbacks. |
259+
| *`setOnlyState(state: T)` | Updates the state without checking and executes all subscribed callbacks. |
260+
| *`setMatchState(state: T)` | Updates the state only if the new `state` differs from the current state and then executes `setOnlyState(state)`. |
261+
262+
245263
</br>
246264

247265
## 📄 License

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"name": "emittor",
3-
"version": "0.0.6",
3+
"version": "0.1.0",
44
"description": "State Management using custom hook without Wrapping components using Providers",
55
"private": false,
66
"repository": {
77
"type": "git",
8-
"url": "https://github.com/immiProgrammer/emittor.git"
8+
"url": "https://github.com/programming-with-ia/emittor.git"
99
},
1010
"bugs": {
11-
"url": "https://github.com/immiProgrammer/emittor/issues"
11+
"url": "https://github.com/programming-with-ia/emittor/issues"
1212
},
13-
"homepage": "https://github.com/immiProgrammer/emittor#readme",
13+
"homepage": "https://github.com/programming-with-ia/emittor#readme",
1414
"typings": "dist/index.d.ts",
1515
"files": [
1616
"dist"
@@ -34,7 +34,7 @@
3434
"nextjs state manager",
3535
"state management"
3636
],
37-
"author": "immiProgrammer",
37+
"author": "immi",
3838
"license": "MIT",
3939
"devDependencies": {
4040
"@types/react": "^18.2.73",

src/createEmittor.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
type Callback<T> = (state: T) => void;
22

3+
type EmittorOptions = {
4+
match?: boolean
5+
}
6+
37
export class Emittor<T> {
48
private callbacks: Callback<T>[] = [];
59
private _state: T;
10+
public setState: Callback<T>;
11+
public emit: Callback<T>
612

7-
constructor(initialState: T) {
13+
constructor(initialState: T, options:EmittorOptions = { match: true }) {
814
this._state = initialState;
15+
if (options.match) {
16+
this.emit = this.setState = this.setMatchState.bind(this);
17+
} else {
18+
this.emit = this.setState = this.setOnlyState.bind(this);
19+
}
20+
}
21+
22+
setOnlyState(state: T){
23+
this._state = state
24+
this.exec()
25+
}
26+
27+
setMatchState(state: T){
28+
if (state == this._state)return
29+
this.setOnlyState(state)
930
}
1031

1132
exec(): void {
@@ -21,19 +42,12 @@ export class Emittor<T> {
2142
});
2243
}
2344

24-
emit(state: T): void {
25-
// Set the new state if provided and execute callbacks
26-
this._state = state;
27-
this.exec();
28-
29-
} setState = this.emit
30-
3145
get state(): T {
3246
return this._state;
3347
}
3448

3549
set state(nState: T) {
36-
this.emit(nState);
50+
this.setState(nState);
3751
}
3852

3953
getState() {
@@ -54,8 +68,8 @@ export class Emittor<T> {
5468
}
5569
}
5670

57-
export function createEmittor<T>(state: T) {
58-
return new Emittor(state)
71+
export function createEmittor<T>(state: T, options?:EmittorOptions) {
72+
return new Emittor(state, options)
5973
}
6074

6175
//* ---
@@ -73,15 +87,15 @@ type ReducerEmittorCallback<R extends string> = {
7387

7488
export class ReducerEmittor<T, R extends string> extends Emittor<T> {
7589
reducers: ReducerEmittorCallback<R>
76-
constructor(initState: T, reducers: Reducers<T, R>) {
77-
super(initState)
90+
constructor(initState: T, reducers: Reducers<T, R>, options?:EmittorOptions) {
91+
super(initState, options)
7892
this.reducers = {} as ReducerEmittorCallback<R>
7993
Object.entries(reducers).map(([name, callback]) => {
8094
this.reducers[name as R] = () => (callback as ReducerCallback<T>)(this)
8195
})
8296
}
8397
}
8498

85-
export function createReducerEmittor<T, K extends string>(initState: T, reducers: Reducers<T, K>) {
86-
return new ReducerEmittor(initState, reducers)
99+
export function createReducerEmittor<T, K extends string>(initState: T, reducers: Reducers<T, K>, options?:EmittorOptions) {
100+
return new ReducerEmittor(initState, reducers, options)
87101
}

0 commit comments

Comments
 (0)