Skip to content

Commit

Permalink
fix: replace eventbus with document query to force icons to re-render
Browse files Browse the repository at this point in the history
  • Loading branch information
erictooth committed Aug 27, 2024
1 parent 7fa58cd commit 4fd6e4f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 25 deletions.
10 changes: 2 additions & 8 deletions src/BaseAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Events } from "./events";
import { SmartIconOptions, EventBus } from "./types";
import { SmartIconOptions } from "./types";

export const BaseAdapter = (config: SmartIconOptions, eventBus: EventBus) =>
export const BaseAdapter = (config: SmartIconOptions) =>

Check warning on line 3 in src/BaseAdapter.ts

View workflow job for this annotation

GitHub Actions / Test & Build

Missing return type on function
class SmartIcon extends HTMLElement {
constructor() {
super();
Expand Down Expand Up @@ -40,16 +39,11 @@ export const BaseAdapter = (config: SmartIconOptions, eventBus: EventBus) =>
if (template) {
this.replaceChildren(template);
}
eventBus.addEventListener(Events.UPDATED, this.update);
}

attributeChangedCallback(attrName: string): void {
if (this.children[0] && attrName === "name") {
this.update();
}
}

disconnectedCallback(): void {
eventBus.removeEventListener(Events.UPDATED, this.update);
}
};
6 changes: 3 additions & 3 deletions src/adapters/SVGFetchAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SmartIconOptions, EventBus } from "../types";
import { SmartIconOptions } from "../types";
import { BaseAdapter } from "../BaseAdapter";

type SVGFetchAdapterOptions = {
Expand All @@ -7,8 +7,8 @@ type SVGFetchAdapterOptions = {

export const SVGFetchAdapter =
(options: SVGFetchAdapterOptions = {}) =>
(config: SmartIconOptions, eventBus: EventBus) => {
return class SVGFetchAdapter extends BaseAdapter(config, eventBus) {
(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());

Expand Down
6 changes: 3 additions & 3 deletions src/adapters/SVGUseAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SmartIconOptions, EventBus } from "../types";
import { SmartIconOptions } from "../types";
import { BaseAdapter } from "../BaseAdapter";

export const SVGUseAdapter = (config: SmartIconOptions, eventBus: EventBus) =>
class SVGUseAdapter extends BaseAdapter(config, eventBus) {
export const SVGUseAdapter = (config: SmartIconOptions) =>

Check warning on line 4 in src/adapters/SVGUseAdapter.ts

View workflow job for this annotation

GitHub Actions / Test & Build

Missing return type on function
class SVGUseAdapter extends BaseAdapter(config) {
generateTemplate() {
const href = this.getPath();
const el = document.createElementNS("http://www.w3.org/2000/svg", "svg");
Expand Down
13 changes: 7 additions & 6 deletions src/define.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Events } from "./events";
import { DefineOptions, SmartIconOptions, EventBus } from "./types";
import { DefineOptions, SmartIconOptions } from "./types";

type DefineResult = {
update: (
Expand Down Expand Up @@ -34,13 +33,15 @@ export const define: DefineFn = (componentName, options) => {
aliases: options.aliases || {},
};

const eventBus: EventBus = new Comment("smart-icon-events");

const triggerUpdate = (): void => {
eventBus.dispatchEvent(new CustomEvent(Events.UPDATED));
for (const el of document.querySelectorAll(componentName)) {
if ("update" in el && typeof el.update === "function") {
el.update();
}
}
};

globalThis.customElements.define(componentName, options.adapter(config, eventBus));
globalThis.customElements.define(componentName, options.adapter(config));

const result: DefineResult = {
update: (options) => {
Expand Down
3 changes: 0 additions & 3 deletions src/events.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ export type DefineOptions = {
aliases?: SmartIconOptions["aliases"];
resolvePath: SmartIconOptions["resolvePath"];
};

export type EventBus = Comment;

0 comments on commit 4fd6e4f

Please sign in to comment.