Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Nov 16, 2024
1 parent 2f08d44 commit 66222a9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
16 changes: 8 additions & 8 deletions docs/fetching/fetched.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,39 +84,39 @@ Getting the inner data or throw

```tsx
function unwrap<D,F>(fetched: Fetched<D,F>): D {
return fetched.unwrap()
return fetched.getOrThrow()
}
```

Getting the inner data or undefined
Getting the inner data or nullable

```tsx
function data<D,F>(fetched: Fetched<D,F>): D | undefined {
return fetched.ok().get()
return fetched.getOrNull()
}
```

Getting the inner error or undefined
Getting the inner error or nullable

```tsx
function error<D,F>(fetched: Fetched<D,F>): F | undefined {
return fetched.err().get()
return fetched.getErrOrNull()
}
```

Getting the inner data or error

```tsx
function error<D,F>(fetched: Fetched<D,F>): D | F {
return fetched.inner
return fetched.getAny()
}
```

Type-guarding if data

```tsx
function f<D,F>(fetched: Fetched<D,F>): D | F {
if (fetched.isData())
if (fetched.isOk())
return fetched.get()
else
return fetched.getErr()
Expand All @@ -127,7 +127,7 @@ Type-guarding if error

```tsx
function f<D,F>(fetched: Fetched<D,F>): D | F {
if (fetched.isFail())
if (fetched.isErr())
return fetched.getErr()
else
return fetched.get()
Expand Down
10 changes: 5 additions & 5 deletions src/mods/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class Core {

const stored = await Result.runAndWrap(async () => {
return settings.storage?.getOrThrow?.(cacheKey)
}).then(r => r?.ok().inner)
}).then(r => r?.getOrNull())

const unstored = await this.unstoreOrThrow(stored, settings)

Expand Down Expand Up @@ -356,11 +356,11 @@ export class Core {
const normalized = await this.#normalizeOrThrow(next.real?.current, settings)
next = this.#mergeRealStateWithFetched(next, normalized)

if (next.real?.current.isData() && previous.real?.current.isData() && dataEqualser(next.real.current.inner, previous.real.current.inner))
next = new RealState(new DataState(new Data(previous.real.current.inner, next.real.current)))
if (next.real?.current.isData() && previous.real?.current.isData() && dataEqualser(next.real.current.get(), previous.real.current.get()))
next = new RealState(new DataState(new Data(previous.real.current.get(), next.real.current)))

if (next.real?.current.isFail() && previous.real?.current.isFail() && errorEqualser(next.real.current.inner, previous.real.current.inner))
next = new RealState(new FailState(new Fail(previous.real.current.inner, next.real.current), previous.real.data))
if (next.real?.current.isFail() && previous.real?.current.isFail() && errorEqualser(next.real.current.getErr(), previous.real.current.getErr()))
next = new RealState(new FailState(new Fail(previous.real.current.getErr(), next.real.current), previous.real.data))

return await this.#reoptimizeOrThrow(cacheKey, next)
}, settings)
Expand Down
6 changes: 3 additions & 3 deletions src/mods/queries/scroll/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export namespace Scrollable {

const prenormalized = await core.prenormalizeOrThrow(timed, settings)

if (prenormalized?.isData() && previous.real?.data && dataEqualser(prenormalized.inner, previous.real.data.inner))
if (prenormalized?.isData() && previous.real?.data && dataEqualser(prenormalized.get(), previous.real.data.get()))
return new Some(previous.real.data)

return new Some(timed)
Expand All @@ -78,7 +78,7 @@ export namespace Scrollable {
settings: ScrollableFetcherfulQuerySettings<K, D, F>
): Promise<State<D[], F>> {
const previous = await core.getOrThrow(cacheKey, settings)
const previousPages = previous.real?.data?.inner ?? []
const previousPages = previous.real?.data?.get() ?? []
const previousPage = Arrays.last(previousPages)
const key = settings.scroller(previousPage)

Expand All @@ -92,7 +92,7 @@ export namespace Scrollable {
const timed = Fetched.from(fetched).setTimes(times)

return await core.mutateOrThrow(cacheKey, async (previous) => {
const previousPages = previous.real?.data?.inner ?? []
const previousPages = previous.real?.data?.get() ?? []
const paginated = timed.mapSync(data => [...previousPages, ...data])

return new Some(paginated)
Expand Down
4 changes: 2 additions & 2 deletions src/mods/queries/scroll/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class ScrollableFetcherfulQuery<K, D, F> {
async peekOrNull(): Promise<Nullable<K>> {
const { settings } = this
const state = await this.state
const pages = state.real?.data?.inner
const pages = state.real?.data?.get()

if (pages == null)
return undefined
Expand Down Expand Up @@ -223,7 +223,7 @@ export class ScrollableFetcherlessQuery<K, D, F> {
async peekOrNull(): Promise<Nullable<K>> {
const { settings } = this
const state = await this.state
const pages = state.real?.data?.inner
const pages = state.real?.data?.get()

if (pages == null)
return undefined
Expand Down
2 changes: 1 addition & 1 deletion src/mods/react/hooks/blocks/use-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export function useError<K, D, F>(query: ReactQuery<K, D, F>, callback: (query:
useEffect(() => {
if (error == null)
return
callback(query, error.inner)
callback(query, error.getErr())
}, [error])
}
11 changes: 8 additions & 3 deletions src/mods/react/hooks/queries/scroll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { None, Nullable, Option } from "@hazae41/option";
import { None, Nullable } from "@hazae41/option";
import { Err, Fallback, Ok } from "@hazae41/result";
import { ScrollableQuery } from "index.js";
import { Arrays } from "libs/arrays/arrays.js";
Expand Down Expand Up @@ -233,7 +233,12 @@ export function useFetcherlessScrollableQuery<K, D, F>(
const fake = state?.fake

const peekOrNull = useCallback(() => {
return Option.wrap(state?.real?.data?.inner).mapSync(pages => settings.scroller(Arrays.last(pages))).getOrNull()
const pages = state?.real?.data?.get()

if (pages == null)
return undefined

return settings.scroller(Arrays.last(pages))
}, [state?.real?.data, settings.scroller])

return {
Expand Down Expand Up @@ -384,7 +389,7 @@ export function useFetcherfulScrollableQuery<K, D, F>(
const fake = state?.fake

const peekOrNull = useCallback(() => {
const pages = state?.real?.data?.inner
const pages = state?.real?.data?.get()

if (pages == null)
return undefined
Expand Down

0 comments on commit 66222a9

Please sign in to comment.