Skip to content

Commit

Permalink
🐛 allow mnml.listen('ready') to work when called by an async script
Browse files Browse the repository at this point in the history
  • Loading branch information
dryan committed Apr 6, 2023
1 parent 39d4315 commit 343eccc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"test": "echo \"Error: no test specified\" && exit 0",
"clean": "rimraf dist",
"prebuild": "npm run clean",
"build": "tsc --project tsconfig.json && uglifyjs dist/mnml.js -o dist/mnml.min.js --compress --comments --source-map \"url=mnml.min.js.map,includeSources\"",
"postbuild": "MNML_VERSION=$(node -pe \"require('./package.json').version\") && cat dist/mnml.js | sed -E \"s/@version/@version $MNML_VERSION/g\" | tee dist/mnml.js > /dev/null && cat dist/mnml.min.js | sed -E \"s/@version/@version $MNML_VERSION/g\" | tee dist/mnml.min.js > /dev/null",
"watch": "npm run build -- --watch",
"build": "tsc --project tsconfig.json",
"postbuild": "uglifyjs dist/mnml.js -o dist/mnml.min.js --compress --comments --source-map \"url=mnml.min.js.map,includeSources\" && MNML_VERSION=$(node -pe \"require('./package.json').version\") && cat dist/mnml.js | sed -E \"s/@version/@version $MNML_VERSION/g\" | tee dist/mnml.js > /dev/null && cat dist/mnml.min.js | sed -E \"s/@version/@version $MNML_VERSION/g\" | tee dist/mnml.min.js > /dev/null",
"dev": "npm run build -- --watch",
"prepublish": "npm run build"
},
"author": "dryan <[email protected]>",
Expand Down
8 changes: 3 additions & 5 deletions src/mnml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,12 @@ export const mnml = (() => {

const _readyListener = (callback: MnmlEventCallback, priority?: number): any => {
priority = priority || 10;
if (_readyListener.loaded) {
return callback();
if (document.readyState === "loading") {
return _readyListener.queue.push([callback, priority]);
}
_readyListener.queue.push([callback, priority]);
return callback();
};
_readyListener.queue = [] as Array<[MnmlEventCallback, number]>;
_readyListener.loaded = false;
document.addEventListener("DOMContentLoaded", () => {
_readyListener.queue = _readyListener.queue.sort((a, b) => a[1] - b[1]);
while (_readyListener.queue.length) {
Expand All @@ -150,7 +149,6 @@ export const mnml = (() => {
cb[0]();
}
}
_readyListener.loaded = true;
});

function listen(
Expand Down

0 comments on commit 343eccc

Please sign in to comment.