-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from break-stuff/create-lazy-loader
add custom element lazy-loader
- Loading branch information
Showing
16 changed files
with
1,536 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
let observer; | ||
let components = { | ||
"radio-group": { | ||
importPath: "./dist/radio-group/RadioGroup.js", | ||
dependencies: [], | ||
}, | ||
"radio-button": { | ||
importPath: "./dist/radio-button/RadioButton.js", | ||
dependencies: [], | ||
}, | ||
}; | ||
|
||
export function updateConfig(config) { | ||
if (config.components) { | ||
components = { ...components, ...config.components }; | ||
} | ||
|
||
if (config.rootElement) { | ||
observer.disconnect(); | ||
start(config.rootElement); | ||
} | ||
} | ||
|
||
async function load(root) { | ||
const rootTagName = root instanceof Element ? root.tagName.toLowerCase() : ""; | ||
const tags = [...root.querySelectorAll(":not(:defined)")].map((el) => | ||
el.tagName.toLowerCase() | ||
); | ||
if (rootTagName.includes("-") && !customElements.get(rootTagName)) { | ||
tags.push(rootTagName); | ||
} | ||
const tagsToRegister = [...new Set(tags)]; | ||
await Promise.allSettled(tagsToRegister.map((tagName) => register(tagName))); | ||
} | ||
|
||
function register(tagName) { | ||
if (customElements.get(tagName)) { | ||
cleanUp(component, tagName); | ||
return Promise.resolve(); | ||
} | ||
|
||
const component = components[tagName]; | ||
if (!component) { | ||
return Promise.resolve(); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
import(component.importPath) | ||
.then(() => { | ||
cleanUp(component, tagName); | ||
resolve(); | ||
}) | ||
.catch(() => { | ||
console.error( | ||
`Unable to load <${tagName}> from ${component.importPath}` | ||
); | ||
reject(); | ||
}); | ||
}); | ||
} | ||
|
||
function cleanUp(component, tagName) { | ||
delete components[tagName]; | ||
component.dependencies?.forEach((dependency) => { | ||
delete components[dependency]; | ||
}); | ||
|
||
if (!Object.keys(component).length) { | ||
observer.disconnect(); | ||
} | ||
} | ||
|
||
function start(root = document.body) { | ||
observer = new MutationObserver((mutations) => { | ||
for (const { addedNodes } of mutations) { | ||
for (const node of addedNodes) { | ||
if (node.nodeType === Node.ELEMENT_NODE) { | ||
load(node); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
load(root); | ||
observer.observe(root, { subtree: true, childList: true }); | ||
} | ||
|
||
start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.