Skip to content

Commit

Permalink
Fix the thing
Browse files Browse the repository at this point in the history
  • Loading branch information
OEvgeny committed Dec 4, 2024
1 parent 64a52a3 commit 8a97582
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class CodeBlock extends HTMLElement {

#originalFragment: DocumentFragment = null;

connected = false;

get code() {
return this.querySelector('code')?.textContent ?? '';
}
Expand Down Expand Up @@ -42,14 +44,23 @@ class CodeBlock extends HTMLElement {
}

connectedCallback() {
this.connected = true;
this.update();
}

disconnectedCallback() {
this.connected = false;
}

highlight(...args: Parameters<HighlightCodeFn>) {
return defaultHighlightCode(...args);
}

update() {
if (!this.connected) {
return;
}

const { code, language, options, ownerDocument: document } = this;

if (code && !this.highlightedCodeFragment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { createElement, type ComponentType } from 'react';
import { render, unmountComponentAtNode } from 'react-dom';

enum ConnectionState {
DISCONNECTED,
CONNECTED,
PENDING,
DISCONNECTED
PENDING
}


export default function wrapAsCustomElement<Props extends { [key: string]: string | undefined } & { children?: never }>(
component: ComponentType<Props>,
propKeys: (keyof Props)[]
Expand All @@ -22,7 +21,6 @@ export default function wrapAsCustomElement<Props extends { [key: string]: strin
}

#connected = ConnectionState.DISCONNECTED;
#ref = new WeakRef(this);

constructor() {
super();
Expand All @@ -34,7 +32,6 @@ export default function wrapAsCustomElement<Props extends { [key: string]: strin

typeof value === 'string' && propMap.set(key === 'class' ? 'className' : key, value);
}

}

#propMap: Map<keyof Props, string>;
Expand Down Expand Up @@ -75,8 +72,7 @@ export default function wrapAsCustomElement<Props extends { [key: string]: strin

async disconnectedCallback() {
this.#connected = ConnectionState.PENDING;
// eslint-disable-next-line no-restricted-globals
await new Promise(resolve => setTimeout(resolve));
await Promise.resolve();
if (this.#connected === ConnectionState.PENDING) {
this.#connected = ConnectionState.DISCONNECTED;
unmountComponentAtNode(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ export function useUpdater<T>(fn: () => T, deps: DependencyList | undefined): Re

const ref = useRef<T>();

useMemo(() => {
ref.current = fn();
// eslint-disable-next-line react-hooks/exhaustive-deps
const value = useMemo(fn, deps);

if (ref.current !== value) {
ref.current = value;
for (const updater of updaters.current.values()) {
updater();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fn, ...deps]);
}

return useMemo(() => Object.freeze([ref, onTrack]), [ref, onTrack]);
}

0 comments on commit 8a97582

Please sign in to comment.