Skip to content

Commit

Permalink
perf: remove async option for generateTemplate
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Adapters must now return an element synchronously
  • Loading branch information
erictooth committed Aug 28, 2024
1 parent ed63a9c commit 80dfdef
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/BaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ export const BaseAdapter = (config: SmartIconOptions) =>
this.setAttribute("name", name);
}

generateTemplate(): Node | PromiseLike<Node> | null {
generateTemplate(): Node | null {
return null;
}

update = (): void | Promise<void> => {
return;
};

async connectedCallback() {
const template = await this.generateTemplate();
connectedCallback() {
const template = this.generateTemplate();
if (template) {
this.replaceChildren(template);
}
Expand Down
40 changes: 21 additions & 19 deletions src/adapters/SVGFetchAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,42 @@ export const SVGFetchAdapter =
(options: SVGFetchAdapterOptions = {}) =>
(config: SmartIconOptions) => {

Check warning on line 10 in src/adapters/SVGFetchAdapter.ts

View workflow job for this annotation

GitHub Actions / Test & Build

Missing return type on function
return class SVGFetchAdapter extends BaseAdapter(config) {
async getSvgText(): Promise<Node> {
const svgText = await fetch(this.getPath()).then((res) => res.text());

getSvgText(): Node {
const el = document.createElementNS("http://www.w3.org/2000/svg", "svg");
el.setAttribute("width", "100%");
el.setAttribute("height", "100%");

if (!options.querySelector) {
el.innerHTML = svgText;
return el.children[0];
}
fetch(this.getPath())
.then((res) => res.text())
.then((svgText) => {
if (!options.querySelector) {
el.innerHTML = svgText;
return el.children[0];
}

const fragment = document
.createRange()
.createContextualFragment(svgText)
.querySelector(options.querySelector(this.name));
const fragment = document
.createRange()
.createContextualFragment(svgText)
.querySelector(options.querySelector(this.name));

const viewBox = (fragment && fragment.getAttribute("viewBox")) || "";
const viewBox = (fragment && fragment.getAttribute("viewBox")) || "";

if (viewBox) {
el.setAttribute("viewBox", viewBox);
}
if (viewBox) {
el.setAttribute("viewBox", viewBox);
}

if (fragment) {
el.innerHTML = fragment.innerHTML;
}
if (fragment) {
el.innerHTML = fragment.innerHTML;
}
});

return el;
}
generateTemplate() {
return this.getSvgText();
}
update = async () => {
this.replaceChildren(await this.getSvgText());
this.replaceChildren(this.getSvgText());
};
};
};
4 changes: 2 additions & 2 deletions src/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const createdElems: Record<string, DefineResult> = {};
export const define: DefineFn = (componentName, options) => {
if (!globalThis || !globalThis.customElements) {
return {
update: () => undefined
}
update: () => undefined,
};
}
if (globalThis.customElements.get(componentName)) {
if (createdElems[componentName]) {
Expand Down

0 comments on commit 80dfdef

Please sign in to comment.