Skip to content

Commit d7798ff

Browse files
committed
docs(www): use code blocks for code snippets
1 parent da6225a commit d7798ff

6 files changed

Lines changed: 104 additions & 1 deletion

File tree

projects/www/src/app/pages/guide/signals/faq.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export class CounterStore extends signalStore(
5151

5252
To get the type of a SignalStore, use the `InstanceType` utility type.
5353

54+
<ngrx-code-example>
55+
5456
```ts
5557
const CounterStore = signalStore(withState({ count: 0 }));
5658

@@ -61,13 +63,17 @@ function logCount(store: CounterStore): void {
6163
}
6264
```
6365

66+
</ngrx-code-example>
67+
6468
</details>
6569

6670
<details>
6771
<summary><b>#5</b> Can I inject a SignalStore via the constructor?</summary>
6872

6973
Yes. To inject a SignalStore via the constructor, define and export its type with the same name.
7074

75+
<ngrx-code-example>
76+
7177
```ts
7278
// counter-store.ts
7379
export const CounterStore = signalStore(withState({ count: 0 }));
@@ -85,6 +91,8 @@ export class Counter {
8591
}
8692
```
8793

94+
</ngrx-code-example>
95+
8896
</details>
8997

9098
<details>

projects/www/src/app/pages/guide/signals/rxjs-integration.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export class Numbers {
3737
Each invocation of the reactive method pushes the input value through the reactive chain.
3838
When called with a static value, the reactive chain executes once.
3939

40+
<ngrx-code-example>
41+
4042
```ts
4143
import { Component } from '@angular/core';
4244
import { map, pipe, tap } from 'rxjs';
@@ -63,8 +65,12 @@ export class Numbers {
6365
}
6466
```
6567

68+
</ngrx-code-example>
69+
6670
When a reactive method is called with a signal the reactive chain is executed every time the signal value changes.
6771

72+
<ngrx-code-example>
73+
6874
```ts
6975
import { Component, signal } from '@angular/core';
7076
import { map, pipe, tap } from 'rxjs';
@@ -92,8 +98,12 @@ export class Numbers {
9298
}
9399
```
94100

101+
</ngrx-code-example>
102+
95103
In addition to providing a signal, it is also possible to provide a computation function and combine multiple signals within it.
96104

105+
<ngrx-code-example>
106+
97107
```ts
98108
import { Component, signal } from '@angular/core';
99109
import { map, pipe, tap } from 'rxjs';
@@ -123,8 +133,12 @@ export class Numbers {
123133
}
124134
```
125135

136+
</ngrx-code-example>
137+
126138
When a reactive method is called with an observable, the reactive chain is executed every time the observable emits a new value.
127139

140+
<ngrx-code-example>
141+
128142
```ts
129143
import { Component } from '@angular/core';
130144
import { interval, map, of, pipe, tap } from 'rxjs';
@@ -153,6 +167,8 @@ export class Numbers {
153167
}
154168
```
155169

170+
</ngrx-code-example>
171+
156172
By default, the `rxMethod` needs to be executed within an injection context.
157173
It's tied to its lifecycle and is automatically cleaned up when the injector is destroyed.
158174

@@ -161,6 +177,8 @@ It's tied to its lifecycle and is automatically cleaned up when the injector is
161177
The `rxMethod` is a great choice for handling API calls in a reactive manner.
162178
The subsequent example demonstrates how to use `rxMethod` to fetch the book by id whenever the `selectedBookId` signal value changes.
163179

180+
<ngrx-code-example>
181+
164182
```ts
165183
import { Component, inject, signal } from '@angular/core';
166184
import { concatMap, filter, pipe } from 'rxjs';
@@ -206,6 +224,8 @@ export class BookList {
206224
}
207225
```
208226

227+
</ngrx-code-example>
228+
209229
<ngrx-docs-alert type="inform">
210230

211231
For safe handling of API responses, it is recommended to use the `tapResponse` operator from the `@ngrx/operators` package.
@@ -220,6 +240,8 @@ Further details can be found in the [Reactive Store Methods](guide/signals/signa
220240

221241
To create a reactive method without arguments, the `void` type should be specified as a generic argument to the `rxMethod` function.
222242

243+
<ngrx-code-example>
244+
223245
```ts
224246
import { Component, inject, signal } from '@angular/core';
225247
import { exhaustMap } from 'rxjs';
@@ -253,13 +275,17 @@ export class BookList {
253275
}
254276
```
255277

278+
</ngrx-code-example>
279+
256280
### Reactive Methods and Injector Hierarchies
257281

258282
The cleanup behavior of reactive methods differs when they're created and called across different injector hierarchies.
259283

260284
If the reactive method is called within the descendant injection context, the call will be automatically cleaned up when the descendant injector is destroyed.
261285
However, when the call is made outside of the descendant injection context, it's necessary to explicitly provide the descendant injector reference to ensure proper cleanup. Otherwise, the cleanup occurs when the ascendant injector where the reactive method is initialized gets destroyed.
262286

287+
<ngrx-code-example>
288+
263289
```ts
264290
import {
265291
Component,
@@ -297,6 +323,8 @@ export class Numbers implements OnInit {
297323
}
298324
```
299325

326+
</ngrx-code-example>
327+
300328
<ngrx-docs-alert type="inform">
301329

302330
If the injector is not provided when calling the reactive method with a signal, a computation function, or an observable outside the injection context, a warning message about a potential memory leak is displayed in development mode.
@@ -307,6 +335,8 @@ If the injector is not provided when calling the reactive method with a signal,
307335

308336
If a reactive method needs to be cleaned up before the injector is destroyed, manual cleanup can be performed by calling the `destroy` method.
309337

338+
<ngrx-code-example>
339+
310340
```ts
311341
import { Component } from '@angular/core';
312342
import { interval, tap } from 'rxjs';
@@ -333,9 +363,13 @@ export class Numbers {
333363
}
334364
```
335365

366+
</ngrx-code-example>
367+
336368
When invoked, the reactive method returns the object with the `destroy` method.
337369
This allows manual cleanup of a specific call, preserving the activity of other reactive method calls until the corresponding injector is destroyed.
338370

371+
<ngrx-code-example>
372+
339373
```ts
340374
import { Component } from '@angular/core';
341375
import { interval, tap } from 'rxjs';
@@ -362,6 +396,8 @@ export class Numbers {
362396
}
363397
```
364398

399+
</ngrx-code-example>
400+
365401
### Initialization Outside of Injection Context
366402

367403
Initialization of the reactive method outside an injection context is possible by providing an injector as the second argument to the `rxMethod` function.

projects/www/src/app/pages/guide/signals/signal-method.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export class Numbers {
2525

2626
`logDoubledNumber` can be called with a static value of type `number` or a `Signal<number>`.
2727

28+
<ngrx-code-example>
29+
2830
```ts
2931
@Component({
3032
/* ... */
@@ -49,8 +51,12 @@ export class Numbers {
4951
}
5052
```
5153

54+
</ngrx-code-example>
55+
5256
In addition to providing a Signal, it is also possible to provide a computation function and combine multiple Signals within it.
5357

58+
<ngrx-code-example>
59+
5460
```ts
5561
@Component({
5662
/* ... */
@@ -72,13 +78,17 @@ export class Numbers {
7278
}
7379
```
7480

81+
</ngrx-code-example>
82+
7583
## Automatic Cleanup
7684

7785
`signalMethod` uses an `effect` internally to track the Signal changes.
7886
By default, the `effect` runs in the injection context of the caller. In the example above, that is the `Numbers` component. That means, that the `effect` is automatically cleaned up when the component is destroyed.
7987

8088
If the call happens outside an injection context, then the injector of the `signalMethod` is used. This would be the case, if `logDoubledNumber` runs in `ngOnInit`:
8189

90+
<ngrx-code-example>
91+
8292
```ts
8393
@Component({
8494
/* ... */
@@ -97,10 +107,14 @@ export class Numbers implements OnInit {
97107
}
98108
```
99109

110+
</ngrx-code-example>
111+
100112
Even though `logDoubledNumber` is called outside an injection context, automatic cleanup occurs when the `Numbers` component is destroyed, since `logDoubledNumber` was created within the component's injection context.
101113

102114
However, when creating a `signalMethod` in an ancestor injection context, the cleanup behavior is different:
103115

116+
<ngrx-code-example>
117+
104118
```ts
105119
@Injectable({ providedIn: 'root' })
106120
export class NumbersService {
@@ -124,6 +138,8 @@ export class Numbers implements OnInit {
124138
}
125139
```
126140

141+
</ngrx-code-example>
142+
127143
Here, the `effect` used internally by `signalMethod` outlives the component, which would produce a memory leak.
128144

129145
<ngrx-docs-alert type="inform">
@@ -136,6 +152,8 @@ If an injector is not provided when a method generated by `signalMethod` is call
136152

137153
When a `signalMethod` is created in an ancestor injection context, it's necessary to explicitly provide the caller injector to ensure proper cleanup:
138154

155+
<ngrx-code-example>
156+
139157
```ts
140158
@Component({
141159
/* ... */
@@ -158,10 +176,14 @@ export class Numbers implements OnInit {
158176
}
159177
```
160178

179+
</ngrx-code-example>
180+
161181
## Initialization Outside of Injection Context
162182

163183
The `signalMethod` must be initialized within an injection context. To initialize it outside an injection context, it's necessary to provide an injector as the second argument:
164184

185+
<ngrx-code-example>
186+
165187
```ts
166188
@Component({
167189
/* ... */
@@ -178,6 +200,8 @@ export class Numbers implements OnInit {
178200
}
179201
```
180202

203+
</ngrx-code-example>
204+
181205
## Advantages over Effect
182206

183207
At first sight, `signalMethod`, might be the same as `effect`:

projects/www/src/app/pages/guide/signals/signal-state.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ The state's type must be a record/object literal. Add arrays or primitive values
2727

2828
`signalState` returns an extended version of a signal that possesses all the capabilities of a read-only signal.
2929

30+
<ngrx-code-example>
31+
3032
```ts
3133
import { computed, effect } from '@angular/core';
3234

@@ -37,8 +39,12 @@ const userStateStr = computed(() => JSON.stringify(userState()));
3739
effect(() => console.log('userState', userState()));
3840
```
3941

42+
</ngrx-code-example>
43+
4044
Additionally, the `signalState` function generates signals for each state property.
4145

46+
<ngrx-code-example>
47+
4248
```ts
4349
const user = userState.user; // type: DeepSignal<User>
4450
const isAdmin = userState.isAdmin; // type: Signal<boolean>
@@ -47,9 +53,13 @@ console.log(user()); // logs: { firstName: 'Eric', lastName: 'Clapton' }
4753
console.log(isAdmin()); // logs: false
4854
```
4955

56+
</ngrx-code-example>
57+
5058
When a state property holds an object as its value, the `signalState` function generates a `DeepSignal`.
5159
It can be used as a regular read-only signal, but it also contains signals for each property of the object it refers to.
5260

61+
<ngrx-code-example>
62+
5363
```ts
5464
const firstName = user.firstName; // type: Signal<string>
5565
const lastName = user.lastName; // type: Signal<string>
@@ -58,6 +68,8 @@ console.log(firstName()); // logs: 'Eric'
5868
console.log(lastName()); // logs: 'Clapton'
5969
```
6070

71+
</ngrx-code-example>
72+
6173
<ngrx-docs-alert type="help">
6274

6375
For enhanced performance, deeply nested signals are generated lazily and initialized only upon first access.
@@ -69,6 +81,8 @@ For enhanced performance, deeply nested signals are generated lazily and initial
6981
The `patchState` function provides a type-safe way to perform updates on pieces of state.
7082
It takes a SignalState or SignalStore instance as the first argument, followed by a sequence of partial states or partial state updaters as additional arguments.
7183

84+
<ngrx-code-example>
85+
7286
```ts
7387
import { patchState } from '@ngrx/signals';
7488

@@ -86,6 +100,8 @@ patchState(userState, { isAdmin: false }, (state) => ({
86100
}));
87101
```
88102

103+
</ngrx-code-example>
104+
89105
<ngrx-docs-alert type="error">
90106

91107
Updaters passed to the `patchState` function must perform state updates in an immutable manner.
@@ -96,6 +112,8 @@ Updaters passed to the `patchState` function must perform state updates in an im
96112

97113
Instead of providing partial states or updaters directly to the `patchState` function, it's possible to create custom state updaters.
98114

115+
<ngrx-code-example>
116+
99117
```ts
100118
import { PartialStateUpdater } from '@ngrx/signals';
101119

@@ -108,8 +126,12 @@ function setFirstName(
108126
const setAdmin = () => ({ isAdmin: true });
109127
```
110128

129+
</ngrx-code-example>
130+
111131
Custom state updaters are easy to test and can be reused across different parts of the application.
112132

133+
<ngrx-code-example>
134+
113135
```ts
114136
// Before:
115137
patchState(userState, (state) => ({
@@ -121,6 +143,8 @@ patchState(userState, (state) => ({
121143
patchState(userState, setFirstName('Stevie'), setAdmin());
122144
```
123145

146+
</ngrx-code-example>
147+
124148
## Usage
125149

126150
### Example 1: SignalState in a Component

0 commit comments

Comments
 (0)