Skip to content

Commit

Permalink
Loaded promise
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-harding committed Oct 7, 2024
1 parent 9fa532a commit 66c2ec0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions demo/assets/js/corner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { listenPassive } from "./shared.js";
import { NAME as NAME_DRAG_AREA } from "./drag-area.js";
import { loaded } from "./loading.js";

export class Corner extends HTMLElement {
_isPressed = false;
Expand Down Expand Up @@ -27,7 +27,7 @@ export class Corner extends HTMLElement {
switch (name) {
case "side": {
this._side = newValue;
customElements.whenDefined(NAME_DRAG_AREA).then(() => {
loaded.then(() => {
const event = new CustomEvent("th-corner__register", {
bubbles: true,
});
Expand Down
32 changes: 19 additions & 13 deletions demo/assets/js/demo.js → demo/assets/js/loading.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
const IS_PAINT_SUPPORTED = CSS.supports("background", "paint(id)");

function main() {
loadSquircleComponent();
loadPaintWorklet();
const modulePromises = [
const { promise, resolve } = Promise.withResolvers();
export const loaded = promise;

export async function load() {
await Promise.all([
loadSquircleComponent(),
loadPaintWorklet(),
loadComponents(),
]);
resolve(undefined);
}

async function loadComponents() {
const modules = await Promise.all([
import("./tester.js"),
import("./drag-area.js"),
import("./control.js"),
import("./corner.js"),
];
Promise.all(modulePromises).then((modules) => {
for (const module of modules) {
const { NAME, Component } = module;
customElements.define(NAME, Component);
}
});
]);
for (const module of modules) {
const { NAME, Component } = module;
customElements.define(NAME, Component);
}
}

async function loadPaintWorklet() {
Expand All @@ -32,5 +40,3 @@ async function loadSquircleComponent() {
const module = await promise;
customElements.define("th-squircle", module.default);
}

main();
10 changes: 5 additions & 5 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<script
src="{{ '/assets/js/demo.js' | relative_url }}"
type="module"
async
></script>
<script type="module">
import("{{ '/assets/js/loading.js' | relative_url }}").then(({ load }) =>
load(),
);
</script>

<link
href="https://unpkg.com/[email protected]/index.css"
Expand Down

0 comments on commit 66c2ec0

Please sign in to comment.