45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
|
import { onMounted, onBeforeUnmount } from 'vue';
|
||
|
import { isBrowser } from './utils';
|
||
|
let fontsReady;
|
||
|
let isFontReady;
|
||
|
const init = () => {
|
||
|
var _a, _b;
|
||
|
fontsReady = isBrowser ? (_b = (_a = document) === null || _a === void 0 ? void 0 : _a.fonts) === null || _b === void 0 ? void 0 : _b.ready : undefined;
|
||
|
isFontReady = false;
|
||
|
/* istanbul ignore if */
|
||
|
if (fontsReady !== undefined) {
|
||
|
void fontsReady.then(() => {
|
||
|
isFontReady = true;
|
||
|
});
|
||
|
}
|
||
|
else {
|
||
|
isFontReady = true;
|
||
|
}
|
||
|
};
|
||
|
init();
|
||
|
// For testing
|
||
|
export { init };
|
||
|
/**
|
||
|
* Call callback on fontsReady is resolved. If fontsReady is already resolved,
|
||
|
* callback won't be called.
|
||
|
*/
|
||
|
export default function onFontsReady(cb) {
|
||
|
/* istanbul ignore next */
|
||
|
if (isFontReady)
|
||
|
return;
|
||
|
let deactivated = false;
|
||
|
onMounted(() => {
|
||
|
/* istanbul ignore next */
|
||
|
if (!isFontReady) {
|
||
|
fontsReady === null || fontsReady === void 0 ? void 0 : fontsReady.then(() => {
|
||
|
if (deactivated)
|
||
|
return;
|
||
|
cb();
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
onBeforeUnmount(() => {
|
||
|
deactivated = true;
|
||
|
});
|
||
|
}
|