Skip to content

Commit

Permalink
Update the documenting storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
weltenwort committed Jan 26, 2021
1 parent 3929eba commit 631cc4a
Showing 1 changed file with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,35 @@ be issued by calling the returned `search()` function. For each new request the
hook emits an object describing the request and its state in the `requests$`
`Observable`.

Since the specific response shape depends on the data strategy used, the hook
takes a projection function, that is responsible for decoding the response in
an appropriate way. Because most response projections follow a similar pattern
there's a helper `normalizeDataSearchResponses(initialResponse,
parseRawResponse)`, which generates an RxJS operator, that...

- emits an initial response containing the given `initialResponse` value
- applies `parseRawResponse` to the `rawResponse` property of each emitted response
- transforms transport layer errors as well as parsing errors into
`SearchStrategyError`s

```typescript
const parseMyCustomSearchResponse = normalizeDataSearchResponses(
'initial value',
decodeOrThrow(myCustomSearchResponsePayloadRT)
);

const { search, requests$ } = useDataSearch({
getRequest: useCallback((searchTerm: string) => ({
request: {
params: {
searchTerm
}
}
}), []);
},
options: {
strategy: 'my-custom-search-strategy',
},
},
}), []),
parseResponses: parseMyCustomSearchResponse,
});
```

Expand All @@ -68,10 +88,6 @@ observables are unsubscribed from for proper cancellation if a new request has
been created. This uses RxJS's `switchMap()` operator under the hood. The hook
also makes sure that all observables are unsubscribed from on unmount.

Since the specific response shape depends on the data strategy used, the hook
takes a projection function, that is responsible for decoding the response in
an appropriate way.

A request can fail due to various reasons that include servers-side errors,
Elasticsearch shard failures and network failures. The intention is to map all
of them to a common `SearchStrategyError` interface. While the
Expand All @@ -94,11 +110,7 @@ const {
latestResponseErrors,
loaded,
total,
} = useLatestPartialDataSearchResponse(
requests$,
'initialValue',
useMemo(() => decodeOrThrow(mySearchStrategyResponsePayloadRT), []),
);
} = useLatestPartialDataSearchResponse(requests$);
```

## Representing the request state to the user
Expand Down

0 comments on commit 631cc4a

Please sign in to comment.