|
| 1 | +import { deepEqual } from './utils/equals' |
| 2 | +import { QueryKeyArray } from './queryKey' |
| 3 | +import { createEmitter, Emitter } from '@vuehooks/core' |
| 4 | + |
| 5 | +export enum QueryStatus { |
| 6 | + Idle = 'idle', |
| 7 | + Loading = 'loading', |
| 8 | + Error = 'error', |
| 9 | + Success = 'success' |
| 10 | +} |
| 11 | + |
| 12 | +export type QueryFunction<TResult> = ( |
| 13 | + ...args: any[] |
| 14 | +) => TResult | Promise<TResult> |
| 15 | + |
| 16 | +export type RetryFunction<TError> = ( |
| 17 | + failureCount: number, |
| 18 | + error: TError |
| 19 | +) => boolean |
| 20 | + |
| 21 | +/** |
| 22 | + * Query Configs |
| 23 | + */ |
| 24 | +export interface BaseQueryConfig<TResult, TError = unknown> { |
| 25 | + queryFn?: QueryFunction<TResult> |
| 26 | + isDataEqual?: (newData: unknown, oldData: unknown) => boolean |
| 27 | + retry?: boolean | number | RetryFunction<TError> |
| 28 | +} |
| 29 | + |
| 30 | +export interface QueryConfig<TResult, TError = unknown> |
| 31 | + extends BaseQueryConfig<TResult, TError> {} |
| 32 | + |
| 33 | +export interface QueryInitConfig<TResult, TError = unknown> { |
| 34 | + queryKey: QueryKeyArray |
| 35 | + queryHash: string |
| 36 | + config: QueryConfig<TResult, TError> |
| 37 | +} |
| 38 | + |
| 39 | +export interface QueryState<TResult, TError = unknown> { |
| 40 | + data: TResult | undefined |
| 41 | + status: QueryStatus |
| 42 | + error?: TError |
| 43 | +} |
| 44 | + |
| 45 | +export type SubscriptionHandler<TResult, TError> = ( |
| 46 | + status: QueryState<TResult, TError> |
| 47 | +) => void |
| 48 | + |
| 49 | +export class Query<TResult, TError> { |
| 50 | + readonly queryKey: QueryKeyArray |
| 51 | + readonly queryHash: string |
| 52 | + readonly config: QueryConfig<TResult, TError> |
| 53 | + |
| 54 | + state: QueryState<TResult, TError> |
| 55 | + |
| 56 | + private emitter: Emitter |
| 57 | + private promise?: Promise<TResult | undefined> |
| 58 | + |
| 59 | + constructor(init: QueryInitConfig<TResult, TError>) { |
| 60 | + this.config = { isDataEqual: deepEqual, ...init.config } |
| 61 | + this.queryKey = init.queryKey |
| 62 | + this.queryHash = init.queryHash |
| 63 | + this.emitter = createEmitter() |
| 64 | + |
| 65 | + // Default state |
| 66 | + this.state = { data: undefined, status: QueryStatus.Idle } |
| 67 | + } |
| 68 | + |
| 69 | + async fetch(): Promise<TResult | undefined> { |
| 70 | + let queryFn = this.config.queryFn |
| 71 | + |
| 72 | + if (!queryFn) return |
| 73 | + if (this.promise) return this.promise |
| 74 | + |
| 75 | + this.promise = (async () => { |
| 76 | + try { |
| 77 | + this.dispatch({ status: QueryStatus.Loading }) |
| 78 | + const data = await queryFn!() |
| 79 | + |
| 80 | + if (!this.config.isDataEqual!(this.state.data, data)) |
| 81 | + this.dispatch({ status: QueryStatus.Success, data }) |
| 82 | + delete this.promise |
| 83 | + |
| 84 | + return data |
| 85 | + } catch (error) { |
| 86 | + this.dispatch({ status: QueryStatus.Error }) |
| 87 | + delete this.promise |
| 88 | + throw error |
| 89 | + } |
| 90 | + })() |
| 91 | + |
| 92 | + return this.promise |
| 93 | + } |
| 94 | + |
| 95 | + private dispatch(state: Partial<QueryState<TResult, TError>>) { |
| 96 | + this.state = { ...this.state, ...state } |
| 97 | + this.emitter.emit('*', { state: this.state }) |
| 98 | + } |
| 99 | + |
| 100 | + subscribe(handler: SubscriptionHandler<TResult, TError>) { |
| 101 | + this.emitter.on('*', handler) |
| 102 | + } |
| 103 | + |
| 104 | + unsubscribe(handler: SubscriptionHandler<TResult, TError>) { |
| 105 | + this.emitter.off('*', handler) |
| 106 | + } |
| 107 | +} |
0 commit comments