|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {assertInInjectionContext, inject, Injector} from '../di'; |
| 10 | +import {DestroyRef} from '../linker'; |
| 11 | +import {effect} from '../render3/reactivity/effect'; |
| 12 | +import {signal} from '../render3/reactivity/signal'; |
| 13 | +import {untracked} from '../render3/reactivity/untracked'; |
| 14 | +import {Resource, ResourceSnapshot, type DebouncedOptions} from './api'; |
| 15 | +import {resourceFromSnapshots} from './from_snapshots'; |
| 16 | +import { |
| 17 | + invalidResourceCreationInParams, |
| 18 | + isInParamsFunction, |
| 19 | + rethrowFatalErrors, |
| 20 | + setInParamsFunction, |
| 21 | +} from './resource'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Creates a resource representing a debounced version of the source signal. |
| 25 | + * |
| 26 | + * @param source The source signal to debounce. |
| 27 | + * @param wait The amount of time to wait before calling the source signal, or a function that |
| 28 | + * returns a promise that resolves when the debounced value should be updated. |
| 29 | + * @param options The options to use for the debounced signal. |
| 30 | + * @returns A resource representing the debounced signal. |
| 31 | + * @experimental 22.0 |
| 32 | + */ |
| 33 | +export function debounced<T>( |
| 34 | + source: () => T, |
| 35 | + wait: NoInfer<number | ((value: T, lastValue: ResourceSnapshot<T>) => Promise<void> | void)>, |
| 36 | + options?: NoInfer<DebouncedOptions<T>>, |
| 37 | +): Resource<T> { |
| 38 | + if (isInParamsFunction()) { |
| 39 | + throw invalidResourceCreationInParams(); |
| 40 | + } |
| 41 | + if (ngDevMode && !options?.injector) { |
| 42 | + assertInInjectionContext(debounced); |
| 43 | + } |
| 44 | + const injector = options?.injector ?? inject(Injector); |
| 45 | + |
| 46 | + const state = signal<ResourceSnapshot<T>>({ |
| 47 | + status: 'resolved', |
| 48 | + value: untracked(() => { |
| 49 | + try { |
| 50 | + setInParamsFunction(true); |
| 51 | + return source(); |
| 52 | + } finally { |
| 53 | + setInParamsFunction(false); |
| 54 | + } |
| 55 | + }), |
| 56 | + }); |
| 57 | + |
| 58 | + let active: Promise<void> | void | undefined; |
| 59 | + let pendingValue: T | undefined; |
| 60 | + |
| 61 | + injector.get(DestroyRef).onDestroy(() => { |
| 62 | + active = undefined; |
| 63 | + }); |
| 64 | + |
| 65 | + effect( |
| 66 | + () => { |
| 67 | + // Enter error state if the source throws. |
| 68 | + let value: T; |
| 69 | + try { |
| 70 | + setInParamsFunction(true); |
| 71 | + value = source(); |
| 72 | + } catch (err) { |
| 73 | + rethrowFatalErrors(err); |
| 74 | + state.set({status: 'error', error: err as Error}); |
| 75 | + active = pendingValue = undefined; |
| 76 | + return; |
| 77 | + } finally { |
| 78 | + setInParamsFunction(false); |
| 79 | + } |
| 80 | + |
| 81 | + const currentState = untracked(state); |
| 82 | + |
| 83 | + // Check if the value is the same as the previous one. |
| 84 | + const equal = options?.equal ?? Object.is; |
| 85 | + if (currentState.status === 'reloading') { |
| 86 | + if (equal(value, pendingValue!)) return; |
| 87 | + } else if (currentState.status === 'resolved') { |
| 88 | + if (equal(value, currentState.value!)) return; |
| 89 | + } |
| 90 | + |
| 91 | + const waitFn = |
| 92 | + typeof wait === 'number' |
| 93 | + ? () => new Promise<void>((resolve) => setTimeout(resolve, wait)) |
| 94 | + : wait; |
| 95 | + |
| 96 | + const result = waitFn(value, currentState); |
| 97 | + |
| 98 | + if (result === undefined) { |
| 99 | + // Synchronous case, go straight to resolved. |
| 100 | + state.set({status: 'resolved', value}); |
| 101 | + active = pendingValue = undefined; |
| 102 | + } else { |
| 103 | + // Asynchronous case: |
| 104 | + // If we're in error state or loading state, remain in that state. |
| 105 | + // Otherwise, change to loading state but keep the current value until the new one loads. |
| 106 | + if (currentState.status !== 'loading' && currentState.status !== 'error') { |
| 107 | + state.set({status: 'loading', value: currentState.value}); |
| 108 | + } |
| 109 | + active = result; |
| 110 | + pendingValue = value; |
| 111 | + |
| 112 | + // Once the promise resolves, update the state to resolved. |
| 113 | + result.then(() => { |
| 114 | + if (active === result) { |
| 115 | + state.set({status: 'resolved', value}); |
| 116 | + active = pendingValue = undefined; |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + }, |
| 121 | + {injector}, |
| 122 | + ); |
| 123 | + |
| 124 | + return resourceFromSnapshots(state); |
| 125 | +} |
0 commit comments