Skip to content

Commit

Permalink
updaters now yield results instead of states, added realData
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Oct 5, 2022
1 parent 27d70ac commit add66c9
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 29 deletions.
2 changes: 1 addition & 1 deletion dist/index.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dist/mods/react/hooks/handles/handle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface Handle<D = any, E = any, K = any> {
* Data, if any
*/
data?: D;
/**
* Data, if any, but not optimistic
*/
realData?: D;
/**
* Error, if any
*/
Expand Down
1 change: 1 addition & 0 deletions dist/mods/types/state.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface State<D = any, E = any, K = any> {
optimistic?: boolean;
cooldown?: number;
expiration?: number;
realData?: D;
}
3 changes: 2 additions & 1 deletion dist/mods/types/updater.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Result } from "./result";
import { State } from "./state";
export declare type Updater<D = any, E = any, K = any> = (previous: State<D, E, K> | undefined, more: UpdaterMore<D, E, K>) => AsyncGenerator<State<D, E, K>, State<D, E, K> | void>;
export declare type Updater<D = any, E = any, K = any> = (previous: State<D, E, K> | undefined, more: UpdaterMore<D, E, K>) => AsyncGenerator<Result<D, E, K>, Result<D, E, K> | void>;
export interface UpdaterMore<D = any, E = any, K = any> {
signal?: AbortSignal;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hazae41/xswr",
"version": "1.0.85",
"version": "1.0.86",
"author": "hazae41",
"license": "MIT",
"description": "The simplest React data (re)fetching library ever made",
Expand Down
20 changes: 11 additions & 9 deletions src/mods/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,26 @@ export class Core extends Ortho<string, State | undefined> {
if (state.optimistic === undefined && current?.optimistic)
return current

const {
equals = DEFAULT_EQUALS
} = params

const next: State<D, E, K> = {
...current,
...state
}

if (next.time === undefined)
next.time = Date.now()
next.data = await this.normalize(false, next, params)

const {
equals = DEFAULT_EQUALS
} = params

if (equals(next.data, current?.data)) // Prevent some renders if the data is the same
if (next.time === undefined)
next.time = Date.now()
if (equals(next.data, current?.data))
next.data = current?.data
if (shallowEquals(next, current)) // Shallow comparison because aborter is not serializable
return current
if (!next.optimistic)
next.realData = next.data

if (shallowEquals(next, current))
return current
await this.set(skey, next, params)
return next as State<D, E, K>
}
Expand Down
5 changes: 5 additions & 0 deletions src/mods/react/hooks/handles/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export interface Handle<D = any, E = any, K = any> {
*/
data?: D

/**
* Data, if any, but not optimistic
*/
realData?: D

/**
* Error, if any
*/
Expand Down
4 changes: 2 additions & 2 deletions src/mods/react/hooks/handles/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ export function useScroll<D = any, E = any, K = any>(

const state = stateRef.current

const { data, error, time, cooldown, expiration, aborter, optimistic } = state ?? {}
const { data, error, time, cooldown, expiration, aborter, optimistic, realData } = state ?? {}

const ready = state !== null
const loading = Boolean(aborter)

return { key, skey, data, error, time, cooldown, expiration, aborter, optimistic, loading, ready, mutate, fetch, refetch, scroll, clear, suspend }
return { key, skey, data, error, time, cooldown, expiration, aborter, optimistic, realData, loading, ready, mutate, fetch, refetch, scroll, clear, suspend }
}
4 changes: 2 additions & 2 deletions src/mods/react/hooks/handles/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ export function useSingle<D = any, E = any, K = any>(

const state = stateRef.current

const { data, error, time, cooldown, expiration, aborter, optimistic } = state ?? {}
const { data, error, time, cooldown, expiration, aborter, optimistic, realData } = state ?? {}

const ready = state !== null
const loading = Boolean(aborter)

return { key, skey, data, error, time, cooldown, expiration, aborter, optimistic, loading, ready, mutate, fetch, refetch, update, clear, suspend }
return { key, skey, data, error, time, cooldown, expiration, aborter, optimistic, realData, loading, ready, mutate, fetch, refetch, update, clear, suspend }
}
3 changes: 2 additions & 1 deletion src/mods/types/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export interface State<D = any, E = any, K = any> {
aborter?: AbortController,
optimistic?: boolean,
cooldown?: number
expiration?: number
expiration?: number,
realData?: D
}
3 changes: 2 additions & 1 deletion src/mods/types/updater.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Result } from "./result"
import { State } from "./state"

export type Updater<D = any, E = any, K = any> =
(previous: State<D, E, K> | undefined, more: UpdaterMore<D, E, K>) => AsyncGenerator<State<D, E, K>, State<D, E, K> | void>
(previous: State<D, E, K> | undefined, more: UpdaterMore<D, E, K>) => AsyncGenerator<Result<D, E, K>, Result<D, E, K> | void>

export interface UpdaterMore<D = any, E = any, K = any> {
signal?: AbortSignal
Expand Down
14 changes: 7 additions & 7 deletions test/next/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@hazae41/xswr": "^1.0.85",
"@hazae41/xswr": "^1.0.86",
"next": "12.2.5",
"react": "18.2.0",
"react-dom": "18.2.0"
Expand Down
7 changes: 5 additions & 2 deletions test/next/pages/optimistic/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function Page() {
const hello = useHelloData()

// this is for you, gaearon
const { data, error, time, loading, update, refetch, mutate, aborter, optimistic } = hello
const { data, realData, error, time, loading, update, refetch, mutate, aborter, optimistic } = hello

const onRefreshClick = useCallback(() => {
refetch()
Expand Down Expand Up @@ -50,7 +50,10 @@ export default function Page() {

return <>
<div>
{JSON.stringify(data) ?? "undefined"}
Data: {JSON.stringify(data) ?? "undefined"}
</div>
<div>
Real data: {JSON.stringify(realData) ?? "undefined"}
</div>
<div>
time: {time && ~~(time / 1000)}
Expand Down

1 comment on commit add66c9

@vercel
Copy link

@vercel vercel bot commented on add66c9 Oct 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

xswr-test – ./

xswr-test-hazae41.vercel.app
test.xswr.hazae41.me
xswr-test-git-master-hazae41.vercel.app

Please sign in to comment.