|
| 1 | +import { assert } from '@ember/debug'; |
| 2 | +// @ts-expect-error |
| 3 | +import { setModifierManager } from '@ember/modifier'; |
| 4 | + |
| 5 | +import { resourceFactory } from '../index'; |
| 6 | +import FunctionBasedModifierManager from './manager'; |
| 7 | + |
| 8 | +import type { resource } from '../index'; |
| 9 | +import type { ArgsFor, ElementFor, EmptyObject } from '[core-types]'; |
| 10 | +import type { ModifierLike } from '@glint/template'; |
| 11 | + |
| 12 | +// Provide a singleton manager. |
| 13 | +const MANAGER = new FunctionBasedModifierManager(); |
| 14 | + |
| 15 | +type PositionalArgs<S> = S extends { Args?: object } ? ArgsFor<S['Args']>['Positional'] : []; |
| 16 | +type NamedArgs<S> = S extends { Args?: object } |
| 17 | + ? ArgsFor<S['Args']>['Named'] extends object |
| 18 | + ? ArgsFor<S['Args']>['Named'] |
| 19 | + : EmptyObject |
| 20 | + : EmptyObject; |
| 21 | + |
| 22 | +type ArgsForFn<S> = S extends { Args?: object } |
| 23 | + ? ArgsFor<S['Args']>['Named'] extends EmptyObject |
| 24 | + ? [...PositionalArgs<S>] |
| 25 | + : [...PositionalArgs<S>, NamedArgs<S>] |
| 26 | + : []; |
| 27 | + |
| 28 | +/** |
| 29 | + * A resource-based API for building modifiers. |
| 30 | + * |
| 31 | + * You can attach this to an element, and use a `resource` to manage |
| 32 | + * the state, add event listeners, remove event listeners on cleanup, etc. |
| 33 | + * |
| 34 | + * Using resources for modifiers provides a clear and concise API with |
| 35 | + * easy to read concerns. |
| 36 | + * |
| 37 | + * |
| 38 | + * The signature for the modifier here is _different_ from `ember-modifier`, where positional args and named args are grouped together into an array and object respectively. |
| 39 | +
|
| 40 | + * This signature for ember-resource's `modifier` follows the [plain function invocation](https://blog.emberjs.com/plain-old-functions-as-helpers/) signature. |
| 41 | + * |
| 42 | + * ```js |
| 43 | + * import { resource } from 'ember-resources'; |
| 44 | + * import { modifier } from 'ember-resources/modifier'; |
| 45 | + * |
| 46 | + * const wiggle = modifier((element, arg1, arg2, namedArgs) => { |
| 47 | + * return resource(({ on }) => { |
| 48 | + * let animation = element.animate([ |
| 49 | + * { transform: `translateX(${arg1}px)` }, |
| 50 | + * { transform: `translateX(-${arg2}px)` }, |
| 51 | + * ], { |
| 52 | + * duration: 100, |
| 53 | + * iterations: Infinity, |
| 54 | + * }); |
| 55 | + * |
| 56 | + * on.cleanup(() => animation.cancel()); |
| 57 | + * }); |
| 58 | + * }); |
| 59 | + * |
| 60 | + * <template> |
| 61 | + * <div {{wiggle 2 5 named="hello"}}>hello</div> |
| 62 | + * </template> |
| 63 | + * ``` |
| 64 | + * |
| 65 | + */ |
| 66 | +export function modifier<El extends Element, Args extends unknown[] = unknown[]>( |
| 67 | + fn: (element: El, ...args: Args) => void |
| 68 | +): ModifierLike<{ |
| 69 | + Element: El; |
| 70 | + Args: { |
| 71 | + Named: EmptyObject; |
| 72 | + Positional: Args; |
| 73 | + }; |
| 74 | +}>; |
| 75 | + |
| 76 | +/** |
| 77 | + * A resource-based API for building modifiers. |
| 78 | + * |
| 79 | + * You can attach this to an element, and use a `resource` to manage |
| 80 | + * the state, add event listeners, remove event listeners on cleanup, etc. |
| 81 | + * |
| 82 | + * Using resources for modifiers provides a clear and concise API with |
| 83 | + * easy to read concerns. |
| 84 | + * |
| 85 | + * |
| 86 | + * The signature for the modifier here is _different_ from `ember-modifier`, where positional args and named args are grouped together into an array and object respectively. |
| 87 | +
|
| 88 | + * This signature for ember-resource's `modifier` follows the [plain function invocation](https://blog.emberjs.com/plain-old-functions-as-helpers/) signature. |
| 89 | + * |
| 90 | + * ```js |
| 91 | + * import { resource } from 'ember-resources'; |
| 92 | + * import { modifier } from 'ember-resources/modifier'; |
| 93 | + * |
| 94 | + * const wiggle = modifier((element, arg1, arg2, namedArgs) => { |
| 95 | + * return resource(({ on }) => { |
| 96 | + * let animation = element.animate([ |
| 97 | + * { transform: `translateX(${arg1}px)` }, |
| 98 | + * { transform: `translateX(-${arg2}px)` }, |
| 99 | + * ], { |
| 100 | + * duration: 100, |
| 101 | + * iterations: Infinity, |
| 102 | + * }); |
| 103 | + * |
| 104 | + * on.cleanup(() => animation.cancel()); |
| 105 | + * }); |
| 106 | + * }); |
| 107 | + * |
| 108 | + * <template> |
| 109 | + * <div {{wiggle 2 5 named="hello"}}>hello</div> |
| 110 | + * </template> |
| 111 | + * ``` |
| 112 | + * |
| 113 | + */ |
| 114 | +export function modifier<S extends { Element?: Element }>( |
| 115 | + fn: (element: ElementFor<S>, ...args: ArgsForFn<S>) => ReturnType<typeof resource> |
| 116 | +): ModifierLike<S>; |
| 117 | +/** |
| 118 | + * A resource-based API for building modifiers. |
| 119 | + * |
| 120 | + * You can attach this to an element, and use a `resource` to manage |
| 121 | + * the state, add event listeners, remove event listeners on cleanup, etc. |
| 122 | + * |
| 123 | + * Using resources for modifiers provides a clear and concise API with |
| 124 | + * easy to read concerns. |
| 125 | + * |
| 126 | + * |
| 127 | + * The signature for the modifier here is _different_ from `ember-modifier`, where positional args and named args are grouped together into an array and object respectively. |
| 128 | +
|
| 129 | + * This signature for ember-resource's `modifier` follows the [plain function invocation](https://blog.emberjs.com/plain-old-functions-as-helpers/) signature. |
| 130 | + * |
| 131 | + * ```js |
| 132 | + * import { resource } from 'ember-resources'; |
| 133 | + * import { modifier } from 'ember-resources/modifier'; |
| 134 | + * |
| 135 | + * const wiggle = modifier((element, arg1, arg2, namedArgs) => { |
| 136 | + * return resource(({ on }) => { |
| 137 | + * let animation = element.animate([ |
| 138 | + * { transform: `translateX(${arg1}px)` }, |
| 139 | + * { transform: `translateX(-${arg2}px)` }, |
| 140 | + * ], { |
| 141 | + * duration: 100, |
| 142 | + * iterations: Infinity, |
| 143 | + * }); |
| 144 | + * |
| 145 | + * on.cleanup(() => animation.cancel()); |
| 146 | + * }); |
| 147 | + * }); |
| 148 | + * |
| 149 | + * <template> |
| 150 | + * <div {{wiggle 2 5 named="hello"}}>hello</div> |
| 151 | + * </template> |
| 152 | + * ``` |
| 153 | + * |
| 154 | + */ |
| 155 | +export function modifier<S extends { Args?: object }>( |
| 156 | + fn: (element: ElementFor<S>, ...args: ArgsForFn<S>) => ReturnType<typeof resource> |
| 157 | +): ModifierLike<S>; |
| 158 | +/** |
| 159 | + * A resource-based API for building modifiers. |
| 160 | + * |
| 161 | + * You can attach this to an element, and use a `resource` to manage |
| 162 | + * the state, add event listeners, remove event listeners on cleanup, etc. |
| 163 | + * |
| 164 | + * Using resources for modifiers provides a clear and concise API with |
| 165 | + * easy to read concerns. |
| 166 | + * |
| 167 | + * |
| 168 | + * The signature for the modifier here is _different_ from `ember-modifier`, where positional args and named args are grouped together into an array and object respectively. |
| 169 | +
|
| 170 | + * This signature for ember-resource's `modifier` follows the [plain function invocation](https://blog.emberjs.com/plain-old-functions-as-helpers/) signature. |
| 171 | + * |
| 172 | + * ```js |
| 173 | + * import { resource } from 'ember-resources'; |
| 174 | + * import { modifier } from 'ember-resources/modifier'; |
| 175 | + * |
| 176 | + * const wiggle = modifier((element, arg1, arg2, namedArgs) => { |
| 177 | + * return resource(({ on }) => { |
| 178 | + * let animation = element.animate([ |
| 179 | + * { transform: `translateX(${arg1}px)` }, |
| 180 | + * { transform: `translateX(-${arg2}px)` }, |
| 181 | + * ], { |
| 182 | + * duration: 100, |
| 183 | + * iterations: Infinity, |
| 184 | + * }); |
| 185 | + * |
| 186 | + * on.cleanup(() => animation.cancel()); |
| 187 | + * }); |
| 188 | + * }); |
| 189 | + * |
| 190 | + * <template> |
| 191 | + * <div {{wiggle 2 5 named="hello"}}>hello</div> |
| 192 | + * </template> |
| 193 | + * ``` |
| 194 | + * |
| 195 | + */ |
| 196 | +export function modifier<S extends { Element?: Element; Args?: object }>( |
| 197 | + fn: (element: ElementFor<S>, ...args: ArgsForFn<S>) => ReturnType<typeof resource> |
| 198 | +): ModifierLike<S>; |
| 199 | + |
| 200 | +export function modifier(fn: (element: Element, ...args: unknown[]) => void): ModifierLike<{ |
| 201 | + Element: Element; |
| 202 | + Args: { |
| 203 | + Named: {}; |
| 204 | + Positional: []; |
| 205 | + }; |
| 206 | +}> { |
| 207 | + assert(`modifier() must be invoked with a function`, typeof fn === 'function'); |
| 208 | + setModifierManager(() => MANAGER, fn); |
| 209 | + resourceFactory(fn); |
| 210 | + |
| 211 | + return fn as unknown as ModifierLike<{ |
| 212 | + Element: Element; |
| 213 | + Args: { |
| 214 | + Named: {}; |
| 215 | + Positional: []; |
| 216 | + }; |
| 217 | + }>; |
| 218 | +} |
| 219 | + |
| 220 | +/** |
| 221 | + * @internal |
| 222 | + */ |
| 223 | +export type FunctionBasedModifierDefinition<S> = ( |
| 224 | + element: ElementFor<S>, |
| 225 | + positional: PositionalArgs<S>, |
| 226 | + named: NamedArgs<S> |
| 227 | +) => void; |
0 commit comments