more propagator stuff

This commit is contained in:
“chrisshank” 2024-12-18 14:31:34 -08:00
parent 1f98215a3c
commit cd8826d0f0
2 changed files with 11 additions and 10 deletions

View File

@ -1,8 +1,8 @@
import type { PropagatorFunction, PropagatorParser } from './types.ts'; import type { PropagatorFunction, PropagatorParser } from './types.ts';
interface PropagatorOptions { interface PropagatorOptions {
source?: Element | null; source?: EventTarget | null;
target?: Element | null; target?: EventTarget | null;
sourceEvent?: string | null; sourceEvent?: string | null;
targetEvent?: string | null; targetEvent?: string | null;
sourceHandler?: PropagatorFunction | string | null; sourceHandler?: PropagatorFunction | string | null;
@ -16,9 +16,9 @@ interface PropagatorOptions {
* A propagator takes a source and target element and listens for events on both. * A propagator takes a source and target element and listens for events on both.
* When an event is detected on one, it will execute a handler and update the other element. * When an event is detected on one, it will execute a handler and update the other element.
*/ */
export class BiPropagator { export class BidirectionalPropagator {
#source: Element | null = null; #source: EventTarget | null = null;
#target: Element | null = null; #target: EventTarget | null = null;
#sourceEventName: string | null = null; #sourceEventName: string | null = null;
#targetEventName: string | null = null; #targetEventName: string | null = null;
#sourceHandler: PropagatorFunction | null = null; #sourceHandler: PropagatorFunction | null = null;
@ -62,11 +62,11 @@ export class BiPropagator {
* The source element that emits events. * The source element that emits events.
* Setting a new source will automatically update event listeners. * Setting a new source will automatically update event listeners.
*/ */
get source(): Element | null { get source() {
return this.#source; return this.#source;
} }
set source(element: Element | null) { set source(element) {
// Remove listener from old source // Remove listener from old source
if (this.#source && this.#sourceEventName) { if (this.#source && this.#sourceEventName) {
this.#source.removeEventListener(this.#sourceEventName, this.#handleSourceEvent); this.#source.removeEventListener(this.#sourceEventName, this.#handleSourceEvent);
@ -84,11 +84,11 @@ export class BiPropagator {
* The target element that receives propagated changes. * The target element that receives propagated changes.
* Setting a new target will automatically update event listeners. * Setting a new target will automatically update event listeners.
*/ */
get target(): Element | null { get target() {
return this.#target; return this.#target;
} }
set target(element: Element | null) { set target(element) {
// Remove listener from old target // Remove listener from old target
if (this.#target && this.#targetEventName) { if (this.#target && this.#targetEventName) {
this.#target.removeEventListener(this.#targetEventName, this.#handleTargetEvent); this.#target.removeEventListener(this.#targetEventName, this.#handleTargetEvent);

View File

@ -1,2 +1,3 @@
export * from './propagator.ts'; export * from './propagator.ts';
export * from './bipropagator.ts'; export * from './bidirectional-propagator.ts';
export * from './symmetric-propagator.ts';