Skip to content

Commit

Permalink
perf: use a shallowRef for data
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Feb 15, 2024
1 parent 885d854 commit aae0c70
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
27 changes: 12 additions & 15 deletions src/data-fetching_new/createDataLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TypeEqual, expectType } from 'ts-expect'
import { Ref, ShallowRef, UnwrapRef, effectScope, ref } from 'vue'
import { type ShallowRef, effectScope, shallowRef } from 'vue'
import { IS_USE_DATA_LOADER_KEY, STAGED_NO_VALUE } from './meta-extensions'
import { _Awaitable } from '../core/utils'
import { _PromiseMerged } from './utils'
Expand All @@ -14,12 +14,10 @@ export interface DataLoaderEntryBase<
isLazy extends boolean = boolean,
Data = unknown
> {
// state

/**
* Data stored in the entry.
*/
data: Ref<_DataMaybeLazy<Data, isLazy>>
data: ShallowRef<_DataMaybeLazy<Data, isLazy>>

/**
* Error if there was an error.
Expand All @@ -29,7 +27,7 @@ export interface DataLoaderEntryBase<
/**
* Whether there is an ongoing request.
*/
isLoading: Ref<boolean>
isLoading: ShallowRef<boolean>

options: DefineDataLoaderOptionsBase<isLazy>

Expand All @@ -54,6 +52,10 @@ export interface DataLoaderEntryBase<
*/
staged: Data | typeof STAGED_NO_VALUE

/**
* Error that was staged by a loader. This is used to avoid showing the old error while the new data is loading.
* Calling the internal `commit()` function will replace the error with the staged error.
*/
stagedError: any | null

// entry instance
Expand All @@ -77,10 +79,10 @@ export function createDataLoader<Context extends DataLoaderContextBase>({
}: CreateDataLoaderOptions<Context>): DefineDataLoader<Context> {
const defineLoader: DefineDataLoader<Context> = (dataLoader, options) => {
const { isLoading, error, data } = effectScope(true).run(() => ({
isLoading: ref(false),
isLoading: shallowRef(false),
// TODO: allow generic for error type
error: ref<unknown | null>(null),
data: ref<unknown | undefined>(),
error: shallowRef<unknown | null>(null),
data: shallowRef<unknown | undefined>(),
}))!

const useDataLoader: UseDataLoader<boolean, unknown> = (() => {
Expand Down Expand Up @@ -282,17 +284,12 @@ export interface UseDataLoaderResult<
/**
* Data returned by the loader. If the data loader is lazy, it will be undefined until the first load.
*/
data: Ref<UnwrapRef<_DataMaybeLazy<Data, isLazy>>>
data: ShallowRef<_DataMaybeLazy<Data, isLazy>>

/**
* Whether there is an ongoing request.
*/
isLoading: Ref<boolean>

/**
* Whether the loader is running or not. TODO: change pending to this and make pending wait until commit is called
*/
// fetching: Ref<boolean>
isLoading: ShallowRef<boolean>

/**
* Error if there was an error.
Expand Down
6 changes: 3 additions & 3 deletions src/data-fetching_new/defineColadaLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
setCurrentContext,
trackRoute,
} from './utils'
import { Ref, ShallowRef, ref, shallowRef, watch } from 'vue'
import { type ShallowRef, shallowRef, watch } from 'vue'
import { NavigationResult } from './navigation-guard'
import {
UseQueryKey,
Expand Down Expand Up @@ -100,8 +100,8 @@ export function defineColadaLoader<Data, isLazy extends boolean>(
const route = shallowRef<_RouteLocationNormalizedLoaded>(to)
entries.set(loader, {
// force the type to match
data: ref() as Ref<_DataMaybeLazy<Data, isLazy>>,
isLoading: ref(false),
data: shallowRef<_DataMaybeLazy<Data, isLazy>>(),
isLoading: shallowRef(false),
error: shallowRef<any>(),

options,
Expand Down
6 changes: 3 additions & 3 deletions src/data-fetching_new/defineLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
STAGED_NO_VALUE,
} from './meta-extensions'
import { IS_CLIENT, getCurrentContext, setCurrentContext } from './utils'
import { Ref, ref, shallowRef } from 'vue'
import { shallowRef } from 'vue'
import { NavigationResult } from './navigation-guard'

/**
Expand Down Expand Up @@ -90,8 +90,8 @@ export function defineBasicLoader<Data, isLazy extends boolean>(
if (!entries.has(loader)) {
entries.set(loader, {
// force the type to match
data: ref() as Ref<_DataMaybeLazy<Data, isLazy>>,
isLoading: ref(false),
data: shallowRef<_DataMaybeLazy<Data, isLazy>>(),
isLoading: shallowRef(false),
error: shallowRef<any>(),

options,
Expand Down

0 comments on commit aae0c70

Please sign in to comment.