-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
105 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** @fileoverview TODO */ | ||
|
||
import { ElementRef } from './element-ref.js'; | ||
import { HydroActiveComponent } from './hydroactive-component.js'; | ||
|
||
/** TODO */ | ||
export class ComponentRef { | ||
readonly #host: ElementRef<HydroActiveComponent>; | ||
public get host(): ElementRef<HTMLElement> { return this.#host; } | ||
|
||
readonly #connectedCallbacks: Array<OnConnect> = []; | ||
readonly #disconnectedCallbacks: Array<OnDisconnect> = []; | ||
|
||
private constructor(host: ElementRef<HydroActiveComponent>) { | ||
this.#host = host; | ||
} | ||
|
||
/** TODO */ | ||
public /* internal */ static _from(host: ElementRef<HydroActiveComponent>): | ||
ComponentRef { | ||
const ref = new ComponentRef(host); | ||
|
||
ref.#host.native._registerLifecycleHooks({ | ||
onConnect: () => { | ||
for (const onConnect of ref.#connectedCallbacks) { | ||
ref.#invokeOnConnect(onConnect); | ||
} | ||
}, | ||
|
||
onDisconnect: () => { | ||
for (const onDisconnect of ref.#disconnectedCallbacks) { | ||
onDisconnect(); | ||
} | ||
|
||
ref.#disconnectedCallbacks.splice(0, ref.#disconnectedCallbacks.length); | ||
}, | ||
}); | ||
|
||
return ref; | ||
} | ||
|
||
/** TODO */ | ||
public connected(onConnect: OnConnect): void { | ||
this.#connectedCallbacks.push(onConnect); | ||
|
||
if (this.#host.native.isConnected) this.#invokeOnConnect(onConnect); | ||
} | ||
|
||
/** | ||
* Invokes the given {@link OnConnect} handler and registers its disconnect | ||
* callback if provided. | ||
*/ | ||
#invokeOnConnect(onConnect: OnConnect): void { | ||
const onDisconnect = onConnect(); | ||
if (onDisconnect) this.#disconnectedCallbacks.push(onDisconnect); | ||
} | ||
} | ||
|
||
/** TODO */ | ||
export type OnConnect = () => OnDisconnect | void; | ||
|
||
/** TODO */ | ||
export type OnDisconnect = () => void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
import { component } from 'hydroactive'; | ||
|
||
/** Automatically increments the count over time. */ | ||
export const AutoCounter = component('auto-counter', (host) => { | ||
const label = host.query('span')!; | ||
export const AutoCounter = component('auto-counter', (comp) => { | ||
const label = comp.host.query('span')!; | ||
let count = Number(label.text); | ||
|
||
setInterval(() => { | ||
count++; | ||
label.native.textContent = count.toString(); | ||
}, 1_000); | ||
comp.connected(() => { | ||
const id = setInterval(() => { | ||
count++; | ||
label.native.textContent = count.toString(); | ||
}, 1_000); | ||
|
||
return () => { | ||
clearInterval(id); | ||
}; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { HydrateLifecycle, component } from './component.js'; | ||
export { ComponentRef, OnDisconnect, OnConnect } from './component-ref.js'; | ||
export { ElementRef } from './element-ref.js'; |