Skip to content

Commit

Permalink
Removes ElementRef from ComponentRef.
Browse files Browse the repository at this point in the history
This was only really used for the `host` property which is no longer needed now that an `ElementAccessor` is directly provided to users.
  • Loading branch information
dgp1130 committed Sep 4, 2024
1 parent 193f4a6 commit 1d57844
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 35 deletions.
7 changes: 2 additions & 5 deletions src/component-ref.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import './testing/noop-component.js';

import { ComponentRef, type OnDisconnect, type OnConnect } from './component-ref.js';
import { ElementRef } from './element-ref.js';
import { signal } from './signals.js';

describe('component-ref', () => {
Expand All @@ -14,10 +13,8 @@ describe('component-ref', () => {
describe('ComponentRef', () => {
describe('_from', () => {
it('constructs a `ComponentRef` instance', () => {
const el = document.createElement('noop-component');
const ref = ComponentRef._from(ElementRef.from(el));

expect(ref.host.native).toBe(el);
const ref = ComponentRef._from(() => true);
expect(ref).toBeInstanceOf(ComponentRef);
});
});

Expand Down
26 changes: 14 additions & 12 deletions src/component-ref.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ElementRef } from './element-ref.js';
import { HydroActiveComponent } from './hydroactive-component.js';
import { effect } from './signals.js';
import { UiScheduler } from './signals/schedulers/ui-scheduler.js';

Expand All @@ -18,12 +16,8 @@ export type OnDisconnect = () => void;
* the component it references (not shared with other components).
*/
export class ComponentRef {
readonly #host: ElementRef<HydroActiveComponent>;
readonly #scheduler = UiScheduler.from();

/** The custom element hosting the HydroActive component. */
public get host(): ElementRef<HydroActiveComponent> { return this.#host; }

/** All callbacks to invoke when the component is connected to the DOM. */
readonly #connectedCallbacks: Array<OnConnect> = [];

Expand All @@ -35,8 +29,14 @@ export class ComponentRef {
*/
readonly #disconnectedCallbacks: Array<OnDisconnect> = [];

private constructor(host: ElementRef<HydroActiveComponent>) {
this.#host = host;
/**
* Returns whether or not the associated component is currently connected to a
* document.
*/
readonly #isConnected: () => boolean;

private constructor(isConnected: () => boolean) {
this.#isConnected = isConnected;
}

/**
Expand All @@ -45,10 +45,12 @@ export class ComponentRef {
* INTERNAL ONLY: Do not call directly. HydroActive should always provide a
* {@link ComponentRef} to you, it should never be necessary to create one
* manually.
*
* @param isConnected Returns whether or not the associated component is
* currently connected to a document.
*/
public /* internal */ static _from(host: ElementRef<HydroActiveComponent>):
ComponentRef {
return new ComponentRef(host);
public /* internal */ static _from(isConnected: () => boolean): ComponentRef {
return new ComponentRef(isConnected);
}

public /* internal */ _onConnect(): void {
Expand Down Expand Up @@ -101,7 +103,7 @@ export class ComponentRef {
public connected(onConnect: OnConnect): void {
this.#connectedCallbacks.push(onConnect);

if (this.#host.native.isConnected) this.#invokeOnConnect(onConnect);
if (this.#isConnected()) this.#invokeOnConnect(onConnect);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/component.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type HydrateLifecycle, defineComponent } from './component.js';
import { ComponentAccessor } from './component-accessor.js';
import { ComponentRef } from './component-ref.js';
import { ElementRef } from './element-ref.js';
import { HydroActiveComponent } from './hydroactive-component.js';
import { testCase, useTestCases } from './testing/test-cases.js';

Expand Down Expand Up @@ -61,7 +60,7 @@ describe('component', () => {

expect(hydrate).toHaveBeenCalledOnceWith(
ComponentAccessor.fromComponent(comp),
ComponentRef._from(ElementRef.from(comp)),
ComponentRef._from(() => comp.isConnected),
);
});

Expand Down
3 changes: 1 addition & 2 deletions src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { ComponentAccessor } from './component-accessor.js';
import { ComponentRef } from './component-ref.js';
import { ElementRef } from './element-ref.js';
import { HydroActiveComponent } from './hydroactive-component.js';

/** The type of the lifecycle hook invoked when the component hydrates. */
Expand All @@ -19,7 +18,7 @@ export function defineComponent(tagName: string, hydrate: HydrateLifecycle):
Class<HydroActiveComponent> {
const Component = class extends HydroActiveComponent {
public override hydrate(): void {
const ref = ComponentRef._from(ElementRef.from(this));
const ref = ComponentRef._from(() => this.isConnected);
this._registerComponentRef(ref);
hydrate(ComponentAccessor.fromComponent(this), ref);
}
Expand Down
2 changes: 1 addition & 1 deletion src/element-accessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe('element-accessor', () => {
ElementAccessor.from(el).listen(comp, 'click', handler);
expect(handler).not.toHaveBeenCalled();

comp.host.native.click();
el.click();

expect(handler).toHaveBeenCalledOnceWith(jasmine.any(Event));
});
Expand Down
15 changes: 7 additions & 8 deletions src/hydroactive-component.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import './testing/noop-component.js';

import { ComponentRef, OnConnect, OnDisconnect } from './component-ref.js';
import { ElementRef } from './element-ref.js';
import { HydroActiveComponent } from './hydroactive-component.js';
import { testCase, useTestCases } from './testing/test-cases.js';

Expand All @@ -20,7 +19,7 @@ describe('hydroactive-component', () => {
it('hydrates on upgrade when already connected to the DOM', testCase('already-rendered', () => {
const hydrate = jasmine.createSpy<Hydrate>('hydrate')
.and.callFake(function (this: HydroActiveComponent): void {
const ref = ComponentRef._from(ElementRef.from(this));
const ref = ComponentRef._from(() => this.isConnected);
this._registerComponentRef(ref);
});

Expand All @@ -37,7 +36,7 @@ describe('hydroactive-component', () => {
it('hydrates on connect', () => {
const hydrate = jasmine.createSpy<Hydrate>('hydrate')
.and.callFake(function (this: HydroActiveComponent): void {
const ref = ComponentRef._from(ElementRef.from(this));
const ref = ComponentRef._from(() => this.isConnected);
this._registerComponentRef(ref);
});

Expand All @@ -59,7 +58,7 @@ describe('hydroactive-component', () => {
it('does not hydrate a second time when moved around the DOM', () => {
const hydrate = jasmine.createSpy<Hydrate>('hydrate')
.and.callFake(function (this: HydroActiveComponent): void {
const ref = ComponentRef._from(ElementRef.from(this));
const ref = ComponentRef._from(() => this.isConnected);
this._registerComponentRef(ref);
});

Expand All @@ -84,7 +83,7 @@ describe('hydroactive-component', () => {
it('defers hydration', testCase('deferred', (el) => {
const hydrate = jasmine.createSpy<Hydrate>('hydrate')
.and.callFake(function (this: HydroActiveComponent): void {
const ref = ComponentRef._from(ElementRef.from(this));
const ref = ComponentRef._from(() => this.isConnected);
this._registerComponentRef(ref);
});

Expand Down Expand Up @@ -135,7 +134,7 @@ describe('hydroactive-component', () => {
it('hydrates when `defer-hydration` is removed while disconnected from the DOM', testCase('disconnected-hydration', (el) => {
const hydrate = jasmine.createSpy<Hydrate>('hydrate')
.and.callFake(function (this: HydroActiveComponent): void {
const ref = ComponentRef._from(ElementRef.from(this));
const ref = ComponentRef._from(() => this.isConnected);
this._registerComponentRef(ref);
});

Expand Down Expand Up @@ -196,7 +195,7 @@ describe('hydroactive-component', () => {

const el =
document.createElement('lifecycle-comp') as HydroActiveComponent;
const ref = ComponentRef._from(ElementRef.from(el));
const ref = ComponentRef._from(() => el.isConnected);
el._registerComponentRef(ref);

ref.connected(onConnect);
Expand Down Expand Up @@ -233,7 +232,7 @@ describe('hydroactive-component', () => {

const el = document.createElement('hydration-connect') as
HydroActiveComponent;
const ref = ComponentRef._from(ElementRef.from(el));
const ref = ComponentRef._from(() => el.isConnected);
el._registerComponentRef(ref);
ref.connected(onConnect);
expect(onConnect).not.toHaveBeenCalled();
Expand Down
1 change: 0 additions & 1 deletion src/hydroactive-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export abstract class HydroActiveComponent extends HTMLElement {
protected abstract hydrate(): void;

public /* internal */ _registerComponentRef(ref: ComponentRef): void {
if (ref.host.native !== this) throw new Error('Registered `ComponentRef` must be associated with this component.');
if (this.#ref) throw new Error('Already registered a `ComponentRef`.');

this.#ref = ref;
Expand Down
3 changes: 1 addition & 2 deletions src/signal-accessors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,7 @@ describe('signal-accessors', () => {
`));
const outerComp = outerHost.element.getComponentRef();

const innerHost = ElementAccessor.from(
outerComp.host.query('noop-component').native);
const innerHost = outerHost.query('noop-component').access(NoopComponent);
const innerComp = innerHost.element.getComponentRef();

bind(
Expand Down
4 changes: 2 additions & 2 deletions src/testing/noop-component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentRef, ElementRef } from 'hydroactive';
import { ComponentRef } from '../component-ref.js';
import { HydroActiveComponent } from '../hydroactive-component.js';

/**
Expand All @@ -14,7 +14,7 @@ export class NoopComponent extends HydroActiveComponent {
public constructor() {
super();

this.#ref = ComponentRef._from(ElementRef.from(this));
this.#ref = ComponentRef._from(() => this.isConnected);
this._registerComponentRef(this.#ref);
}

Expand Down

0 comments on commit 1d57844

Please sign in to comment.