Skip to content

Commit

Permalink
use get function to resolve hostref
Browse files Browse the repository at this point in the history
  • Loading branch information
danielleroux committed Feb 12, 2025
1 parent f703c57 commit f160a8b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/client/client-host-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type * as d from '../declarations';
* @returns — true if the element was successfully removed, or false if it was not present.
*/
export const deleteHostRef = (ref: d.RuntimeRef): boolean => {
if (ref.$hostRef$) {
delete ref.$hostRef$;
if (ref.__stencil__getHostRef) {
delete ref.__stencil__getHostRef;
return true;
}

Expand All @@ -26,7 +26,11 @@ export const deleteHostRef = (ref: d.RuntimeRef): boolean => {
* @returns the Host reference (if found) or undefined
*/
export const getHostRef = (ref: d.RuntimeRef): d.HostRef | undefined => {
return ref.$hostRef$;
if (ref.__stencil__getHostRef) {
return ref.__stencil__getHostRef();
}

return undefined;
};

/**
Expand All @@ -37,7 +41,7 @@ export const getHostRef = (ref: d.RuntimeRef): d.HostRef | undefined => {
* @param hostRef that instances `HostRef` object
*/
export const registerInstance = (lazyInstance: any, hostRef: d.HostRef) => {
lazyInstance.$hostRef$ = hostRef;
lazyInstance.__stencil__getHostRef = () => hostRef;
hostRef.$lazyInstance$ = lazyInstance;

if (BUILD.modernPropertyDecls && (BUILD.state || BUILD.prop)) {
Expand Down Expand Up @@ -73,7 +77,8 @@ export const registerHost = (hostElement: d.HostElement, cmpMeta: d.ComponentRun
hostElement['s-rc'] = [];
}

const ref = (hostElement.$hostRef$ = hostRef);
const ref = hostRef;
hostElement.__stencil__getHostRef = () => ref;

if (!BUILD.lazyLoad && BUILD.modernPropertyDecls && (BUILD.state || BUILD.prop)) {
reWireGetterSetter(hostElement, hostRef);
Expand Down
4 changes: 2 additions & 2 deletions src/declarations/stencil-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ export interface HostElement extends HTMLElement {
host?: Element;
forceUpdate?: () => void;

$hostRef$?: HostRef;
__stencil__getHostRef?: () => HostRef;

// "s-" prefixed properties should not be property renamed
// and should be common between all versions of stencil
Expand Down Expand Up @@ -1726,7 +1726,7 @@ export type ComponentRuntimeReflectingAttr = [string, string | undefined];
* keys in a `WeakMap` which maps {@link HostElement} instances to their
* associated {@link HostRef} instance.
*/
export type RuntimeRef = HostElement | { $hostRef$?: HostRef };
export type RuntimeRef = HostElement | { __stencil__getHostRef?: () => HostRef };

/**
* Interface used to track an Element, it's virtual Node (`VNode`), and other data
Expand Down

0 comments on commit f160a8b

Please sign in to comment.