|
| 1 | +/* eslint-disable @typescript-eslint/ban-types */ |
| 2 | +// typed-ember has not publihsed types for this yet |
| 3 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 4 | +// @ts-ignore |
| 5 | +import { getValue } from '@glimmer/tracking/primitives/cache'; |
| 6 | +import { assert } from '@ember/debug'; |
| 7 | +// typed-ember has not publihsed types for this yet |
| 8 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 9 | +// @ts-ignore |
| 10 | +import { invokeHelper } from '@ember/helper'; |
| 11 | + |
| 12 | +import { FUNCTION_TO_RUN, FunctionRunner, INITIAL_VALUE } from './resources/function-runner'; |
| 13 | +import { DEFAULT_THUNK, normalizeThunk, proxyClass } from './utils'; |
| 14 | + |
| 15 | +import type { ResourceFn } from './resources/function-runner'; |
| 16 | +import type { Cache, Constructable } from './types'; |
| 17 | + |
| 18 | +type NonReactiveVanilla<Return, Args extends unknown[]> = [object, ResourceFn<Return, Args>]; |
| 19 | +type VanillaArgs<Return, Args extends unknown[]> = [object, ResourceFn<Return, Args>, () => Args]; |
| 20 | +type NonReactiveWithInitialValue<Return, Args extends unknown[]> = [ |
| 21 | + object, |
| 22 | + NotFunction<Return>, |
| 23 | + ResourceFn<Return, Args> |
| 24 | +]; |
| 25 | +type WithInitialValueArgs<Return, Args extends unknown[]> = [ |
| 26 | + object, |
| 27 | + NotFunction<Return>, |
| 28 | + ResourceFn<Return, Args>, |
| 29 | + () => Args |
| 30 | +]; |
| 31 | + |
| 32 | +type NotFunction<T> = T extends Function ? never : T; |
| 33 | + |
| 34 | +type UseFunctionArgs<Return, Args extends unknown[]> = |
| 35 | + | NonReactiveVanilla<Return, Args> |
| 36 | + | NonReactiveWithInitialValue<Return, Args> |
| 37 | + | VanillaArgs<Return, Args> |
| 38 | + | WithInitialValueArgs<Return, Args>; |
| 39 | + |
| 40 | +/** |
| 41 | + * For use in the body of a class. |
| 42 | + * Constructs a cached Resource that will reactively respond to tracked data changes |
| 43 | + * |
| 44 | + * @param {Object} destroyable context, e.g.: component instance aka "this" |
| 45 | + * @param {Function} theFunction the function to run with the return value available on .value |
| 46 | + */ |
| 47 | +export function useFunction<Return, Args extends unknown[]>( |
| 48 | + ...passed: NonReactiveVanilla<Return, Args> |
| 49 | +): { value: Return }; |
| 50 | +/** |
| 51 | + * For use in the body of a class. |
| 52 | + * Constructs a cached Resource that will reactively respond to tracked data changes |
| 53 | + * |
| 54 | + * @param {Object} destroyable context, e.g.: component instance aka "this" |
| 55 | + * @param {Function} theFunction the function to run with the return value available on .value |
| 56 | + * @param {Function} thunk to generate / bind tracked data to the function so that the function can re-run when the tracked data updates |
| 57 | + */ |
| 58 | +export function useFunction<Return, Args extends unknown[]>( |
| 59 | + ...passed: VanillaArgs<Return, Args> |
| 60 | +): { value: Return }; |
| 61 | +/** |
| 62 | + * For use in the body of a class. |
| 63 | + * Constructs a cached Resource that will reactively respond to tracked data changes |
| 64 | + * |
| 65 | + * @param {Object} destroyable context, e.g.: component instance aka "this" |
| 66 | + * @param {Object} initialValue - a non-function that matches the shape of the eventual return value of theFunction |
| 67 | + * @param {Function} theFunction the function to run with the return value available on .value |
| 68 | + * @param {Function} thunk to generate / bind tracked data to the function so that the function can re-run when the tracked data updates |
| 69 | + */ |
| 70 | +export function useFunction<Return, Args extends unknown[]>( |
| 71 | + ...passed: WithInitialValueArgs<Return, Args> |
| 72 | +): { value: Return }; |
| 73 | +/** |
| 74 | + * For use in the body of a class. |
| 75 | + * Constructs a cached Resource that will reactively respond to tracked data changes |
| 76 | + * |
| 77 | + * @param {Object} destroyable context, e.g.: component instance aka "this" |
| 78 | + * @param {Object} initialValue - a non-function that matches the shape of the eventual return value of theFunction |
| 79 | + * @param {Function} theFunction the function to run with the return value available on .value |
| 80 | + */ |
| 81 | +export function useFunction<Return, Args extends unknown[]>( |
| 82 | + ...passed: NonReactiveWithInitialValue<Return, Args> |
| 83 | +): { value: Return }; |
| 84 | + |
| 85 | +export function useFunction<Return, Args extends unknown[]>( |
| 86 | + ...passedArgs: UseFunctionArgs<Return, Args> |
| 87 | +): { value: Return } { |
| 88 | + let [context] = passedArgs; |
| 89 | + let initialValue: Return | undefined; |
| 90 | + let fn: ResourceFn<Return, Args>; |
| 91 | + let thunk: (() => Args) | undefined; |
| 92 | + |
| 93 | + assert( |
| 94 | + `Expected second argument to useFunction to either be an initialValue or the function to run`, |
| 95 | + passedArgs[1] !== undefined |
| 96 | + ); |
| 97 | + |
| 98 | + if (isVanillaArgs(passedArgs)) { |
| 99 | + fn = passedArgs[1]; |
| 100 | + thunk = passedArgs[2]; |
| 101 | + } else { |
| 102 | + initialValue = passedArgs[1]; |
| 103 | + fn = passedArgs[2]; |
| 104 | + thunk = passedArgs[3]; |
| 105 | + } |
| 106 | + |
| 107 | + let target = buildUnproxiedFunctionResource<Return, Args>( |
| 108 | + context, |
| 109 | + initialValue, |
| 110 | + fn, |
| 111 | + (thunk || DEFAULT_THUNK) as () => Args |
| 112 | + ); |
| 113 | + |
| 114 | + // :( |
| 115 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 116 | + return proxyClass<any>(target) as { value: Return }; |
| 117 | +} |
| 118 | + |
| 119 | +function isVanillaArgs<R, A extends unknown[]>( |
| 120 | + args: UseFunctionArgs<R, A> |
| 121 | +): args is VanillaArgs<R, A> | NonReactiveVanilla<R, A> { |
| 122 | + return typeof args[1] === 'function'; |
| 123 | +} |
| 124 | + |
| 125 | +const FUNCTION_CACHE = new WeakMap<ResourceFn<unknown, unknown[]>, Constructable<FunctionRunner>>(); |
| 126 | + |
| 127 | +/** |
| 128 | + * The function is wrapped in a bespoke resource per-function definition |
| 129 | + * because passing a vanilla function to invokeHelper would trigger a |
| 130 | + * different HelperManager, which we want to work a bit differently. |
| 131 | + * See: |
| 132 | + * - function HelperManager in ember-could-get-used-to-this |
| 133 | + * - Default Managers RFC |
| 134 | + * |
| 135 | + */ |
| 136 | +function buildUnproxiedFunctionResource<Return, ArgsList extends unknown[]>( |
| 137 | + context: object, |
| 138 | + initial: Return | undefined, |
| 139 | + fn: ResourceFn<Return, ArgsList>, |
| 140 | + thunk: () => ArgsList |
| 141 | +): { value: Return } { |
| 142 | + let resource: Cache<Return>; |
| 143 | + |
| 144 | + let klass: Constructable<FunctionRunner>; |
| 145 | + |
| 146 | + let existing = FUNCTION_CACHE.get(fn); |
| 147 | + |
| 148 | + if (existing) { |
| 149 | + klass = existing; |
| 150 | + } else { |
| 151 | + klass = class AnonymousFunctionRunner extends FunctionRunner<Return, ArgsList> { |
| 152 | + [INITIAL_VALUE] = initial; |
| 153 | + [FUNCTION_TO_RUN] = fn; |
| 154 | + }; |
| 155 | + |
| 156 | + FUNCTION_CACHE.set(fn, klass); |
| 157 | + } |
| 158 | + |
| 159 | + return { |
| 160 | + get value(): Return { |
| 161 | + if (!resource) { |
| 162 | + resource = invokeHelper(context, klass, () => { |
| 163 | + return normalizeThunk(thunk); |
| 164 | + }) as Cache<Return>; |
| 165 | + } |
| 166 | + |
| 167 | + return getValue<Return>(resource); |
| 168 | + }, |
| 169 | + }; |
| 170 | +} |
0 commit comments