// #region Imports
import { AsyncDirective } from 'lit/async-directive.js';
import { directive } from 'lit/directive.js';
// #endregion
/**
 * A directive that renders a signal and subscribes to it, updating the part when the signal changes.
 *
 * @private
 */
class WatchDirective extends AsyncDirective {
    // #region Fields
    _signal;
    _dispose;
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    render(signal, converter) {
        if (signal !== this._signal) {
            this._dispose?.();
            this._signal = signal;
            let internalUpdate = true;
            this._dispose = signal.subscribe((value) => {
                if (!internalUpdate) {
                    this.setValue(converter ? converter(value) : value);
                }
            });
            internalUpdate = false;
        }
        return signal.get();
    }
    /**
     * @protected
     * @override
     */
    disconnected() {
        this._dispose?.();
    }
    /**
     * @protected
     * @override
     */
    reconnected() {
        this._dispose = this._signal?.subscribe((value) => {
            this.setValue(value);
        });
    }
}
/**
 * Renders a signal and subscribes to it, updating the part when the signal changes.
 *
 * @public
 */
export const watch = directive(WatchDirective);
//# sourceMappingURL=WatchDirective.js.map