|
| 1 | +import type { ComputedRef, Ref, WatchOptions, WatchSource } from 'vue'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Any function |
| 5 | + */ |
| 6 | +export type Fn = () => void; |
| 7 | + |
| 8 | +/** |
| 9 | + * A ref that allow to set null or undefined |
| 10 | + */ |
| 11 | +export type RemovableRef<T> = Omit<Ref<T>, 'value'> & { |
| 12 | + get value(): T; |
| 13 | + set value(value: T | null | undefined); |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * @deprecated Use `RemovableRef` |
| 18 | + */ |
| 19 | +export type RemoveableRef<T> = RemovableRef<T>; |
| 20 | + |
| 21 | +/** |
| 22 | + * Maybe it's a ref, or a plain value |
| 23 | + * |
| 24 | + * ```ts |
| 25 | + * type MaybeRef<T> = T | Ref<T> |
| 26 | + * ``` |
| 27 | + */ |
| 28 | +export type MaybeRef<T> = T | Ref<T>; |
| 29 | + |
| 30 | +/** |
| 31 | + * Maybe it's a ref, or a plain value, or a getter function |
| 32 | + * |
| 33 | + * ```ts |
| 34 | + * type MaybeComputedRef<T> = (() => T) | T | Ref<T> | ComputedRef<T> |
| 35 | + * ``` |
| 36 | + */ |
| 37 | +export type MaybeComputedRef<T> = MaybeReadonlyRef<T> | MaybeRef<T>; |
| 38 | + |
| 39 | +/** |
| 40 | + * Maybe it's a computed ref, or a getter function |
| 41 | + * |
| 42 | + * ```ts |
| 43 | + * type MaybeReadonlyRef<T> = (() => T) | ComputedRef<T> |
| 44 | + * ``` |
| 45 | + */ |
| 46 | +export type MaybeReadonlyRef<T> = (() => T) | ComputedRef<T>; |
| 47 | + |
| 48 | +/** |
| 49 | + * Make all the nested attributes of an object or array to MaybeRef<T> |
| 50 | + * |
| 51 | + * Good for accepting options that will be wrapped with `reactive` or `ref` |
| 52 | + * |
| 53 | + * ```ts |
| 54 | + * UnwrapRef<DeepMaybeRef<T>> === T |
| 55 | + * ``` |
| 56 | + */ |
| 57 | +export type DeepMaybeRef<T> = T extends Ref<infer V> |
| 58 | + ? MaybeRef<V> |
| 59 | + : T extends Array<any> | object |
| 60 | + ? { [K in keyof T]: DeepMaybeRef<T[K]> } |
| 61 | + : MaybeRef<T>; |
| 62 | + |
| 63 | +/** |
| 64 | + * Infers the element type of an array |
| 65 | + */ |
| 66 | +export type ElementOf<T> = T extends (infer E)[] ? E : never; |
| 67 | + |
| 68 | +export type ShallowUnwrapRef<T> = T extends Ref<infer P> ? P : T; |
| 69 | + |
| 70 | +export type Awaitable<T> = Promise<T> | T; |
| 71 | + |
| 72 | +export type ArgumentsType<T> = T extends (...args: infer U) => any ? U : never; |
| 73 | + |
| 74 | +export interface Pausable { |
| 75 | + /** |
| 76 | + * A ref indicate whether a pausable instance is active |
| 77 | + */ |
| 78 | + isActive: Ref<boolean>; |
| 79 | + |
| 80 | + /** |
| 81 | + * Temporary pause the effect from executing |
| 82 | + */ |
| 83 | + pause: Fn; |
| 84 | + |
| 85 | + /** |
| 86 | + * Resume the effects |
| 87 | + */ |
| 88 | + resume: Fn; |
| 89 | +} |
| 90 | + |
| 91 | +export interface Stoppable { |
| 92 | + /** |
| 93 | + * A ref indicate whether a stoppable instance is executing |
| 94 | + */ |
| 95 | + isPending: Ref<boolean>; |
| 96 | + |
| 97 | + /** |
| 98 | + * Stop the effect from executing |
| 99 | + */ |
| 100 | + stop: Fn; |
| 101 | + |
| 102 | + /** |
| 103 | + * Start the effects |
| 104 | + */ |
| 105 | + start: Fn; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * @deprecated Use `Stoppable` |
| 110 | + */ |
| 111 | +export type Stopable = Stoppable; |
| 112 | + |
| 113 | +export interface ConfigurableFlush { |
| 114 | + /** |
| 115 | + * Timing for monitoring changes, refer to WatchOptions for more details |
| 116 | + * |
| 117 | + * @default 'pre' |
| 118 | + */ |
| 119 | + flush?: WatchOptions['flush']; |
| 120 | +} |
| 121 | + |
| 122 | +export interface ConfigurableFlushSync { |
| 123 | + /** |
| 124 | + * Timing for monitoring changes, refer to WatchOptions for more details. |
| 125 | + * Unlike `watch()`, the default is set to `sync` |
| 126 | + * |
| 127 | + * @default 'sync' |
| 128 | + */ |
| 129 | + flush?: WatchOptions['flush']; |
| 130 | +} |
| 131 | + |
| 132 | +// Internal Types |
| 133 | +export type MapSources<T> = { |
| 134 | + [K in keyof T]: T[K] extends WatchSource<infer V> ? V : never; |
| 135 | +}; |
| 136 | +export type MapOldSources<T, Immediate> = { |
| 137 | + [K in keyof T]: T[K] extends WatchSource<infer V> |
| 138 | + ? Immediate extends true |
| 139 | + ? V | undefined |
| 140 | + : V |
| 141 | + : never; |
| 142 | +}; |
0 commit comments