From 343eccc71cca0c620bf65b2e958fc13049bd2b75 Mon Sep 17 00:00:00 2001 From: "Dan Ryan (GitHub Noreply Key)" Date: Thu, 6 Apr 2023 11:12:17 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20allow=20mnml.listen('ready')=20t?= =?UTF-8?q?o=20work=20when=20called=20by=20an=20async=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 6 +++--- src/mnml.ts | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 67b2b5e..845aeff 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/mnml.ts b/src/mnml.ts index d508ba1..a146d53 100644 --- a/src/mnml.ts +++ b/src/mnml.ts @@ -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) { @@ -150,7 +149,6 @@ export const mnml = (() => { cb[0](); } } - _readyListener.loaded = true; }); function listen(