Skip to content

Commit 6c56ba6

Browse files
committed
refactor(general): 💡 change names
1 parent 72b0a98 commit 6c56ba6

File tree

9 files changed

+70
-93
lines changed

9 files changed

+70
-93
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
/dist
44
/coverage
5+
*.md

README.md

+56-80
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,28 @@ import { initEffects } from '@ngneat/effects';
2424
initEffects();
2525
```
2626

27-
Next, we need to define our actions. For example:
27+
Actions are created by using the `createAction` or `actionsFactory` functions:
2828

2929
```ts
30-
import { createAction } from '@ngneat/effects';
30+
import { actionsFactory, createAction } from '@ngneat/effects';
31+
32+
// todos.actions.ts
33+
export interface Todo {
34+
id: string;
35+
name: string;
36+
}
3137

3238
export const addTodo = createAction('[Todos] Add Todo', props < { title: string });
39+
40+
// Ee recommend using the actions factory to prefix each action for better readability and debug purposes when using redux dev tools
41+
export const todoActions = actionsFactory('todo');
42+
43+
// We can declare an action by passing it a type and an optional payload.
44+
export const loadTodos = todoActions.create('Load Todos')
45+
export const addTodo = todoActions.create('Add Todo', props<Todo>())
3346
```
3447

35-
Next, we need to define the effects, and register them:
48+
Next, we need to define the `effects`, and register them:
3649

3750
```ts
3851
import { createEffect, registerEffects, ofType } from '@ngneat/effects';
@@ -72,13 +85,10 @@ export function TodosPage() {
7285
useEffect(() => dispatch(loadTodos()), []);
7386

7487
return (
75-
<button onClick = {()
76-
=>
77-
dispatch(addTodo({ title: 'foo' }))
78-
}>
79-
Add
80-
< /button>
81-
)
88+
<button onClick = {() => dispatch(addTodo({ title: 'foo' }))}>
89+
Add
90+
</button>
91+
)
8292
}
8393
```
8494

@@ -88,90 +98,61 @@ The effects we pass are tied to the component life cycle hook and will be destro
8898

8999
First, install the package: `npm i @ngneat/effects-ng`.
90100

91-
Then we need to register our root effects in our app module.
101+
Next, create the `effect` provider:
92102

93103
```ts
94-
import { TodoEffects } from "todo/todo.effect.ts"
104+
import { createEffect } from '@ngneat/effects';
95105

96-
// app.module.ts
97-
EffectsNgModule.forRoot([TodoEffects])
106+
@Injectable({ providedIn: 'root' })
107+
export class TodosEffects {
98108

99-
@NgModule({
100-
imports: [
101-
EffectsNgModule.forRoot([TodoEffects]),
102-
]
103-
})
104-
export class AppModule {
109+
constructor(private todosApi: TodosApi) {}
110+
111+
loadTodos$ = createEffect(actions =>
112+
actions.pipe(
113+
ofType(loadTodos),
114+
switchMap((todo) => this.todosApi.loadTodos())
115+
)
116+
);
105117
}
106118
```
107119

108-
In order to register lazily loaded effects use the forFeature method.
120+
Then we need to register our the `effects` in our app module:
109121

110122
```ts
111-
import { UserEffects } from "user/user.effect.ts"
123+
import { EffectsNgModule } from '@ngneat/effects-ng';
124+
import { TodosEffects } from 'todos/todos.effect.ts';
112125

113-
// lazy.module.ts
114126
@NgModule({
115127
imports: [
116-
EffectsNgModule.forFeature([UserEffects])
128+
EffectsNgModule.forRoot([TodosEffects]),
117129
]
118130
})
119-
export class LazyModule {
131+
export class AppModule {
120132
}
121133
```
122134

123-
An injectable effects class holds properties and uses dependency injection as you're familiar from other Angular
124-
injectable services.
135+
In order to register lazily loaded effects use the `forFeature` method:
125136

126137
```ts
127-
// todo.effect.ts
128-
@Injectable()
129-
export class TodoEffects {
130-
131-
constructor(
132-
private todoApiService: TodoApiService
133-
) {
134-
}
138+
import { EffectsNgModule } from '@ngneat/effects-ng';
139+
import { PostsEffects } from "posts/posts.effect.ts"
135140

136-
loadTodos = createEffect(actions => actions.pipe(
137-
ofType(loadTodos),
138-
switchMap((todo) => this.todoApiService.loadTodos()),
139-
map(({ todos }) => actions.dispatch(showSnackbar({ message: `Todos loaded.` })))
140-
));
141-
}
142-
```
143-
144-
Effects listen to actions that can be triggered by using the actions stream and its dispatch method
145-
146-
```ts
147-
// todo.action.ts
148-
export interface Todo {
149-
id: string;
150-
name: string;
151-
status: "not started" | "in progress" | "done"
141+
@NgModule({
142+
imports: [
143+
EffectsNgModule.forFeature([PostsEffects])
144+
]
145+
})
146+
export class LazyModule {
152147
}
153-
154-
// we recommend using the actions factory to prefix each action for better readability and debug purposes when using redux dev tools
155-
const todoActions = actionsFactory("todo")
156-
157-
// we can declare an action by passing it a type and an optional payload.
158-
export const loadTodos = todoActions.create("Load Todos")
159-
export const addTodo = todoActions.create("Add Todo", props<Todo>())
160-
161148
```
162149

163-
If we don't need the reactive behavior of effects and we want to call an effect without using actions we can use the
164-
effect functions as explained [below](#angular-effect-functions).
165-
166-
The actions can be dispatched wherever we have access to the actions stream. Common use cases are dispatching actions in
167-
components or within other effects
150+
The actions can be dispatched by injecting the `Actions` provider:
168151

169152
```ts
170-
// app.component.ts
171153
@Component(...)
172154
export class AppComponent {
173-
constructor(private actions: Actions) {
174-
}
155+
constructor(private actions: Actions) {}
175156

176157
ngOnInit() {
177158
this.actions.dispatch(loadTodos());
@@ -204,16 +185,12 @@ First, install the package: `npm i @ngneat/effects-hook`.
204185
We can register the effect in our component, and call it when we need:
205186

206187
```ts
207-
import { useComponentEffects$ } from '@ngneat/effects-hooks';
188+
import { useEffectFn } from '@ngneat/effects-hooks';
208189

209190
function SearchComponent() {
210-
const searchTodo = useComponentEffects(searchTodoEffect);
191+
const searchTodo = useEffectFn(searchTodoEffect);
211192

212-
return <input onChange = {({ target: { value } })
213-
=>
214-
searchTodo(value)
215-
}
216-
/>
193+
return <input onChange = {({ target: { value } }) => searchTodo(value) }/>
217194
}
218195
```
219196

@@ -223,25 +200,24 @@ We can also register multiple effects:
223200

224201
```ts
225202
function FooComponent() {
226-
const [addTodo, updateTodo, deleteTodo] = useComponentEffects([
203+
const [addTodo, updateTodo, deleteTodo] = useEffectFn([
227204
addTodoEffect, updateTodoEffect, deleteTodoEffect
228205
]);
229206

230-
return
231-
...
207+
return ...
232208
}
233209
```
234210

235211
<h3 id="angular-effect-functions">Use with Angular</h3>
236212

237213
First, install the package: `npm i @ngneat/effects-ng`.
238214

239-
Create an effect class, extends the `ComponentEffects` and use the `createEffectFn` to create your effects:
215+
Create an effect class, extends the `EffectFn` and use the `createEffectFn` to create your effects:
240216

241217
```ts
242-
import { ComponentEffects } from '@ngneat/effects-ng';
218+
import { EffectFn } from '@ngneat/effects-ng';
243219
244-
export class TodosEffects extends ComponentEffects {
220+
export class TodosEffects extends EffectFn {
245221
246222
searchTodo = this.createEffectFn((searchTerm$: Observable<string>) => {
247223
return searchTerm$.pipe(

apps/ng-web/src/app/eager/state/foo-effects.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Injectable } from "@angular/core";
22
import { Observable, tap } from "rxjs";
3-
import { ComponentEffects } from '@ngneat/effects-ng';
3+
import { EffectFn } from '@ngneat/effects-ng';
44

55
@Injectable()
6-
export class Foo extends ComponentEffects {
6+
export class Foo extends EffectFn {
77

88
addTodo = this.createEffectFn((value$: Observable<string>) => {
99
return value$.pipe(

apps/react-web/src/app/todos-page/todos-page.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { useEffect, useState } from 'react';
22
import { addTodo, addTodo$, loadTodos, loadTodos$, searchTodoEffect } from './todos.effects';
33
import { loading$, todos$ } from './todos.repository';
44
import { dispatch } from '@ngneat/effects';
5-
import { useComponentEffects, useEffects } from '@ngneat/effect-hooks';
5+
import { useEffectFn, useEffects } from '@ngneat/effect-hooks';
66
import { useObservable } from '@ngneat/use-observable';
77

88
function SearchComponent() {
9-
const searchTodo = useComponentEffects(searchTodoEffect);
9+
const searchTodo = useEffectFn(searchTodoEffect);
1010

1111
return <input onChange={({ target: { value } }) => searchTodo(value)} />
1212
}

libs/effects-ng/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { EffectsNgModule } from './lib/effects-ng.module';
22
export { Actions } from './lib/actions';
3-
export { ComponentEffects } from './lib/component-effects'
3+
export { EffectFn } from './lib/effect-fn'

libs/effects-ng/src/lib/component-effects.ts renamed to libs/effects-ng/src/lib/effect-fn.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createEffectFn } from "@ngneat/effects";
33
import { Observable, Subject } from "rxjs";
44

55
@Injectable()
6-
export class ComponentEffects implements OnDestroy {
6+
export class EffectFn implements OnDestroy {
77
private destroy = new Subject<boolean>();
88
private destroy$ = this.destroy.asObservable();
99

libs/hooks/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { useEffects } from './lib/use-effects';
2-
export { useComponentEffects } from './lib/use-component-effects';
2+
export { useEffectFn } from './lib/use-effect-fn';

libs/hooks/src/lib/use-component-effects.spec.tsx renamed to libs/hooks/src/lib/use-effect-fn.spec.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { createEffectFn } from '@ngneat/effects';
22
import { Observable } from 'rxjs';
33
import { finalize, tap } from 'rxjs/operators';
4-
import { useComponentEffects } from './use-component-effects';
4+
import { useEffectFn } from './use-effect-fn';
55
import { render, fireEvent } from '@testing-library/react';
66

7-
describe('useComponentEffects', () => {
7+
describe('useEffectFn', () => {
88
const spy = jest.fn();
99
const destroySpy = jest.fn();
1010

@@ -16,7 +16,7 @@ describe('useComponentEffects', () => {
1616
});
1717

1818
function SearchComponent() {
19-
const searchTodo = useComponentEffects(searchTodoEffect);
19+
const searchTodo = useEffectFn(searchTodoEffect);
2020

2121
return <input data-testid="input" onChange={({ target: { value } }) => searchTodo(value)} />
2222
}

libs/hooks/src/lib/use-component-effects.ts renamed to libs/hooks/src/lib/use-effect-fn.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { MonoTypeOperatorFunction, Observable, Subject, takeUntil } from "rxjs";
22
import { useRef, useMemo, useEffect } from 'react';
33

4-
export function useComponentEffects<R extends Effect$[]>(effects: R): ReturnTypes<R>;
5-
export function useComponentEffects<R extends Effect$>(effect: R): ReturnType<R>;
6-
export function useComponentEffects(effects: Effect$[] | Effect$): any {
4+
export function useEffectFn<R extends Effect$[]>(effects: R): ReturnTypes<R>;
5+
export function useEffectFn<R extends Effect$>(effect: R): ReturnType<R>;
6+
export function useEffectFn(effects: Effect$[] | Effect$): any {
77
const { destroyed } = useUntilDestroyed();
88
const result = useRef<any>([]);
99

0 commit comments

Comments
 (0)