|
| 1 | +import type {Controller} from '@coveo/headless'; |
| 2 | +import type {PropertyValues, ReactiveElement} from 'lit'; |
| 3 | +import type {InitializableComponent} from './types'; |
| 4 | + |
| 5 | +type ControllerProperties<T> = { |
| 6 | + [K in keyof T]: T[K] extends Controller ? K : never; |
| 7 | +}[keyof T]; |
| 8 | + |
| 9 | +/** |
| 10 | + * Overrides the shouldUpdate method to prevent triggering an unnecessary updates when the controller state is not yet defined. |
| 11 | + * |
| 12 | + * This function wraps the original shouldUpdate method of a LitElement component. It ensures that the component |
| 13 | + * will only update if the original shouldUpdate method returns true and at least one of the changed properties |
| 14 | + * is not undefined. |
| 15 | + * |
| 16 | + * You can always define a custom shouldUpdate method in your component which will override this one. |
| 17 | + * |
| 18 | + * @param component - The LitElement component whose shouldUpdate method is being overridden. |
| 19 | + * @param shouldUpdate - The original shouldUpdate method of the component. |
| 20 | + */ |
| 21 | +function overrideShouldUpdate( |
| 22 | + component: ReactiveElement, |
| 23 | + shouldUpdate: (changedProperties: PropertyValues) => boolean |
| 24 | +) { |
| 25 | + // @ts-expect-error - shouldUpdate is a protected property |
| 26 | + component.shouldUpdate = function (changedProperties: PropertyValues) { |
| 27 | + return ( |
| 28 | + shouldUpdate.call(this, changedProperties) && |
| 29 | + [...changedProperties.values()].some((v) => v !== undefined) |
| 30 | + ); |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * A decorator that allows the Lit component state property to automatically get updates from a [Coveo Headless controller](https://docs.coveo.com/en/headless/latest/usage/#use-headless-controllers). |
| 36 | + * |
| 37 | + * @example |
| 38 | + * ```ts |
| 39 | + * @bindStateToController('pager') @state() private pagerState!: PagerState; |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * For more information and examples, view the "Utilities" section of the readme. |
| 43 | + * |
| 44 | + * @param controllerProperty The controller property to subscribe to. The controller has to be created inside of the `initialize` method. |
| 45 | + * @param options The configurable `bindStateToController` options. |
| 46 | + * TODO: KIT-3822: add unit tests to this decorator |
| 47 | + */ |
| 48 | +export function bindStateToController<Element extends ReactiveElement>( // TODO: check if can inject @state decorator |
| 49 | + controllerProperty: ControllerProperties<Element>, |
| 50 | + options?: { |
| 51 | + /** |
| 52 | + * Component's method to be called when state is updated. |
| 53 | + */ |
| 54 | + onUpdateCallbackMethod?: string; |
| 55 | + } |
| 56 | +) { |
| 57 | + return < |
| 58 | + T extends Record<ControllerProperties<Element>, Controller> & |
| 59 | + Record<string, unknown>, |
| 60 | + Instance extends Element & T & InitializableComponent, |
| 61 | + K extends keyof Instance, |
| 62 | + >( |
| 63 | + proto: Element, |
| 64 | + stateProperty: K |
| 65 | + ) => { |
| 66 | + const ctor = proto.constructor as typeof ReactiveElement; |
| 67 | + |
| 68 | + ctor.addInitializer((instance) => { |
| 69 | + const component = instance as Instance; |
| 70 | + // @ts-expect-error - shouldUpdate is a protected property |
| 71 | + const {disconnectedCallback, initialize, shouldUpdate} = component; |
| 72 | + |
| 73 | + overrideShouldUpdate(component, shouldUpdate); |
| 74 | + |
| 75 | + component.initialize = function () { |
| 76 | + initialize && initialize.call(this); |
| 77 | + |
| 78 | + if (!component[controllerProperty]) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if ( |
| 83 | + options?.onUpdateCallbackMethod && |
| 84 | + !component[options.onUpdateCallbackMethod] |
| 85 | + ) { |
| 86 | + return console.error( |
| 87 | + `ControllerState: The onUpdateCallbackMethod property "${options.onUpdateCallbackMethod}" is not defined`, |
| 88 | + component |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + const controller = component[controllerProperty]; |
| 93 | + const updateCallback = options?.onUpdateCallbackMethod |
| 94 | + ? component[options.onUpdateCallbackMethod] |
| 95 | + : undefined; |
| 96 | + |
| 97 | + const unsubscribeController = controller.subscribe(() => { |
| 98 | + component[stateProperty] = controller.state as Instance[K]; |
| 99 | + typeof updateCallback === 'function' && updateCallback(); |
| 100 | + }); |
| 101 | + |
| 102 | + component.disconnectedCallback = function () { |
| 103 | + !component.isConnected && unsubscribeController?.(); |
| 104 | + disconnectedCallback && disconnectedCallback.call(component); |
| 105 | + }; |
| 106 | + }; |
| 107 | + }); |
| 108 | + }; |
| 109 | +} |
0 commit comments