Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a61861

Browse files
committed
docs: improve selection docs
1 parent 71703a3 commit 5a61861

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

apps/docs/docs/state/selections/selections.mdx

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ In most cases it's best to go with solving problems on the early subscriber side
116116

117117
`select` is the stand-alone version of the `RxState#select` top level method. It helps to create default selection's from a changing state source.
118118

119-
```typescritp
119+
```typescript
120120
// emissions:
121121
// 0. - no emission ever happened
122122
// 1. {a: 1} - incomplete state leads to `?` pollution in the template
123123
// 2. {a: 1, b: 'a'} - render relevant emission
124-
// 2. {a: 1, b: 'a'} - same instance emisssion
124+
// 2. {a: 1, b: 'a'} - same instance emission
125125
// 3. {a: 1, b: 'a', c: true} - render irrelevant change
126126
// 4. {a: 1, b: 'b', c: true} - render relevant emission
127-
const model$: Observable<Partial<{a: number, b: string, c: boolean}>>;
127+
const model$: Observable<Partial<{ a: number; b: string; c: boolean }>>;
128128
```
129129

130130
**Problem**
@@ -140,7 +140,7 @@ B: {{(model$ | push)?.b}}
140140

141141
### single property short hand
142142

143-
```typescritp
143+
```typescript
144144
const vm$ = model$.pipe(select('b'));
145145
```
146146

@@ -154,8 +154,8 @@ B: {{(model$ | push).b}}
154154

155155
### single operators
156156

157-
```typescritp
158-
const vm$: Observable<> = model$.pipe(select(map(({b}) => b === 'a')));
157+
```typescript
158+
const vm$: Observable<> = model$.pipe(select(map(({ b }) => b === 'a')));
159159
```
160160

161161
```html
@@ -168,21 +168,21 @@ B: {{(model$ | push).b}}
168168

169169
## selectSlice
170170

171-
## smosh
172-
173171
## distinctUntilSomeChanges
174172

173+
## stateful
174+
175175
# Advanced derivation architecture
176176

177177
**The problem**
178178

179179
We have the following state sources to manage:
180180

181181
- the list of products received form global state - `Product[]`
182-
- the title of the list including it's number of children computen in the component class - `string`
182+
- the title of the list including it's number of children computed in the component class - `string`
183183
- the sort direction triggered over a UI element click - `boolean`
184184

185-
A setup of the compoents class based on `RxState` could look like this:
185+
A setup of the components class based on `RxState` could look like this:
186186

187187
```typescript
188188
@Component({
@@ -226,7 +226,6 @@ interface SelectionScreen1 {
226226
A common implementations looks like this:
227227

228228
```typescript
229-
// template removed for brevity
230229
export class ProblemComponent {
231230
private sortedList$ = this.state.select(
232231
selectSlice(['sortDirection', 'list']),
@@ -240,7 +239,7 @@ export class ProblemComponent {
240239
selectSlice(['title', 'sortedList', 'sortDirection'])
241240
);
242241

243-
// ❌ BAD: modle viewmodel mix up 👇
242+
// ❌ BAD: model and view model mix up 👇
244243
constructor(
245244
private globalState: GlobalState,
246245
private state: RxState<Model & Pick<ViewModel, 'sortedList'>>
@@ -257,41 +256,20 @@ export class ProblemComponent {
257256

258257
![Selections (6)](https://user-images.githubusercontent.com/10064416/152422999-db8260f0-69e1-4d99-b6ac-b2b1d043b4b7.png)
259258

260-
By removing the sorted list form the state and moving it into the selection
261-
we can clean up the state's typing and have a nice separation of which data is owned by the component (model) and which data is owned by the template (view model)
259+
By removing the sorted list form the state and moving it into the selection we can clean up the state's typing and have a nice separation of which data is owned by the component (model) and which data is owned by the template (view model)
262260

263261
```typescript
264-
// template removed for brevity
265262
export class ProblemComponent {
266263
private sortedSlice$ = this.state.select(
267-
selectSlice(['sortDirection', 'list']),
268-
map(({ list, sortDirection }) => {
264+
['sortDirection', 'list'],
265+
({ list, sortDirection }) => {
269266
// sort `list` by `sortDirection` to `sortedList` here
270267
return { sortDirection, sortedList };
271-
})
268+
}
272269
);
273270

274271
// ✔ GOOD: Derive view model from model 👇
275-
viewModel$ = smosh({ title: this.state.select('title') }, this.sortedSlice$);
276-
277-
// target API
278-
viewModel$ = smosh(
279-
{
280-
prop1: 'prop1', // string
281-
prop2: prop1$, // Observable<string>
282-
},
283-
slice1$, // Observable<{prop3: 3}>
284-
slice2$ // Observable<{prop4: 'four'}>,
285-
// durationSelector$ (optional)
286-
);
287-
288-
// ✔ GOOD: Derive view model from model 👇
289-
viewModel$ = smosh(
290-
{
291-
title: this.state.select('title'),
292-
},
293-
[this.sortedSlice$]
294-
);
272+
vm$ = // what to promote here as smosh isn't part of RxA?
295273

296274
constructor(private globalState: GlobalState, private state: RxState<Model>) {
297275
// ...

0 commit comments

Comments
 (0)