Skip to content

Commit

Permalink
chore: added .prettierc
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgiolaga committed May 19, 2024
1 parent 1823af3 commit 5dd7bc5
Show file tree
Hide file tree
Showing 11 changed files with 951 additions and 943 deletions.
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"singleQuote": false,
"useTabs": true,
"tabWidth": 8
}
138 changes: 70 additions & 68 deletions examples/custom-element/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,88 @@ import { Counter, CounterDomain } from "./Counter";
import { ExternalChangeValue } from "./external";

declare global {
// `vite` build replaces `shadowDomMode` with the correct value
export const shadowDomMode: ShadowRootMode | false;
// `vite` plugin replaces `injectCSS` with the correct function
export const injectCSS: (shadow: ShadowRoot) => void;
// `vite` build replaces `shadowDomMode` with the correct value
export const shadowDomMode: ShadowRootMode | false;
// `vite` plugin replaces `injectCSS` with the correct function
export const injectCSS: (shadow: ShadowRoot) => void;
}

class CounterElement extends HTMLElement {
private abortController?: AbortController;
private externalEventTarget = new EventTarget();
private abortController?: AbortController;
private externalEventTarget = new EventTarget();

// This method is called when the element is added to the DOM
connectedCallback() {
const div = document.createElement("div");
if (shadowDomMode) {
const shadow = this.attachShadow({
mode: shadowDomMode,
});
if (typeof injectCSS === "function") {
injectCSS(shadow);
}
shadow.appendChild(div);
} else {
this.appendChild(div);
}
// This method is called when the element is added to the DOM
connectedCallback() {
const div = document.createElement("div");
if (shadowDomMode) {
const shadow = this.attachShadow({
mode: shadowDomMode,
});
if (typeof injectCSS === "function") {
injectCSS(shadow);
}
shadow.appendChild(div);
} else {
this.appendChild(div);
}

// Get the initial value from the attribute or default to 20
const initialValue = Number(this.getAttribute("value") || "20");
// Get the initial value from the attribute or default to 20
const initialValue = Number(this.getAttribute("value") || "20");

// Start the Seqflow app
// Even if `ShadowRoot` is not a `HTMLElement`, we can cast it to `HTMLElement` to make TypeScript happy.
// It works anyway.
this.abortController = start(div, Counter, undefined, {
log: {
error: (l) => console.error(l),
info: (l) => console.info(l),
debug: (l) => console.debug(l),
},
domains: {
// Create the Counter domain with the initial value and the external event target
counter: (et) =>
new CounterDomain(
et,
this.externalEventTarget,
initialValue
),
// `external` domain is a fake domain and used only to ntofy attribute changes
external: () => this.externalEventTarget,
},
});
}
// Start the Seqflow app
// Even if `ShadowRoot` is not a `HTMLElement`, we can cast it to `HTMLElement` to make TypeScript happy.
// It works anyway.
this.abortController = start(div, Counter, undefined, {
log: {
error: (l) => console.error(l),
info: (l) => console.info(l),
debug: (l) => console.debug(l),
},
domains: {
// Create the Counter domain with the initial value and the external event target
counter: (et) =>
new CounterDomain(
et,
this.externalEventTarget,
initialValue
),
// `external` domain is a fake domain and used only to ntofy attribute changes
external: () => this.externalEventTarget,
},
});
}

// We want to be notified when the `value` attribute changes
static get observedAttributes() {
return ["value"];
}
// We want to be notified when the `value` attribute changes
static get observedAttributes() {
return ["value"];
}

// This method is called when the `value` attribute changes
attributeChangedCallback(
name: string,
oldValue: string,
newValue: string
) {
this.externalEventTarget.dispatchEvent(
new ExternalChangeValue({ newValue: Number(newValue) })
);
}
// This method is called when the `value` attribute changes
attributeChangedCallback(
name: string,
oldValue: string,
newValue: string
) {
this.externalEventTarget.dispatchEvent(
new ExternalChangeValue({
newValue: Number(newValue),
})
);
}

// This method is called when the element is removed from the DOM
disconnectedCallback() {
if (this.abortController) {
this.abortController.abort("Unmounting");
}
}
// This method is called when the element is removed from the DOM
disconnectedCallback() {
if (this.abortController) {
this.abortController.abort("Unmounting");
}
}
}

declare module "seqflow-js" {
interface Domains {
counter: CounterDomain;
external: EventTarget;
}
interface Domains {
counter: CounterDomain;
external: EventTarget;
}
}

customElements.define("my-counter-element", CounterElement);
10 changes: 5 additions & 5 deletions examples/empty/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Main } from "./Main";
import "./index.css";

start(document.getElementById("root"), Main, undefined, {
log: {
error: (l) => console.error(l),
info: (l) => console.info(l),
debug: (l) => console.debug(l),
},
log: {
error: (l) => console.error(l),
info: (l) => console.info(l),
debug: (l) => console.debug(l),
},
});
30 changes: 15 additions & 15 deletions examples/random-quote/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import { Main } from "./Main";
import "./index.css";

start(document.getElementById("root"), Main, undefined, {
log: {
error: (l) => console.error(l),
info: (l) => console.info(l),
debug: (l) => console.debug(l),
},
config: {
api: {
baseUrl: "https://api.quotable.io",
},
},
log: {
error: (l) => console.error(l),
info: (l) => console.info(l),
debug: (l) => console.debug(l),
},
config: {
api: {
baseUrl: "https://api.quotable.io",
},
},
});

declare module "seqflow-js" {
interface ApplicationConfiguration {
api: {
baseUrl: string;
};
}
interface ApplicationConfiguration {
api: {
baseUrl: string;
};
}
}
60 changes: 30 additions & 30 deletions examples/random-quote/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,53 @@ import { start } from "seqflow-js";
import { Main } from "../src/Main";

const quotes = [
{ content: "quote 1", author: "Author 1" },
{ content: "quote 2", author: "Author 2" },
{ content: "quote 1", author: "Author 1" },
{ content: "quote 2", author: "Author 2" },
];

let index = 0;
const server = setupServer(
http.get("/random", () => {
return HttpResponse.json(quotes[index++ % quotes.length]);
})
http.get("/random", () => {
return HttpResponse.json(quotes[index++ % quotes.length]);
})
);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

test("should render the quote and refresh it", async () => {
start(document.body, Main, undefined, {
// log: {
// error: (l) => console.error(l),
// info: (l) => console.info(l),
// debug: (l) => console.debug(l),
// },
config: {
api: {
// Route to the mock server
baseUrl: "",
},
},
});
start(document.body, Main, undefined, {
// log: {
// error: (l) => console.error(l),
// info: (l) => console.info(l),
// debug: (l) => console.debug(l),
// },
config: {
api: {
// Route to the mock server
baseUrl: "",
},
},
});

await screen.findByText(/loading/i);
await screen.findByText(/loading/i);

await screen.findByText(new RegExp(quotes[0].content, "i"));
await screen.findByText(new RegExp(quotes[0].author, "i"));
await screen.findByText(new RegExp(quotes[0].content, "i"));
await screen.findByText(new RegExp(quotes[0].author, "i"));

const button = await screen.findByRole("button");
button.click();
const button = await screen.findByRole("button");
button.click();

await screen.findByText(/loading/i);
await screen.findByText(/loading/i);

await screen.findByText(new RegExp(quotes[1].content, "i"));
await screen.findByText(new RegExp(quotes[1].author, "i"));
await screen.findByText(new RegExp(quotes[1].content, "i"));
await screen.findByText(new RegExp(quotes[1].author, "i"));

button.click();
button.click();

await screen.findByText(/loading/i);
await screen.findByText(/loading/i);

await screen.findByText(new RegExp(quotes[0].content, "i"));
await screen.findByText(new RegExp(quotes[0].author, "i"));
await screen.findByText(new RegExp(quotes[0].content, "i"));
await screen.findByText(new RegExp(quotes[0].author, "i"));
});
Loading

0 comments on commit 5dd7bc5

Please sign in to comment.