@tarojs/taro-h5
Version:
Taro h5 framework
144 lines (141 loc) • 5.91 kB
JavaScript
import Taro from '@tarojs/api';
import isNil from 'lodash-es/isNil';
import queryString from 'query-string';
import { temporarilyNotSupport } from '../../../utils/index.js';
import { CallbackManager } from '../../../utils/handler.js';
const unhandledRejectionCallbackManager = new CallbackManager();
const themeChangeCallbackManager = new CallbackManager();
const pageNotFoundCallbackManager = new CallbackManager();
const errorCallbackManager = new CallbackManager();
const appShowCallbackManager = new CallbackManager();
const appHideCallbackManager = new CallbackManager();
const unhandledRejectionListener = (res) => {
unhandledRejectionCallbackManager.trigger(res);
};
let themeMatchMedia = null;
const themeChangeListener = (res) => {
themeChangeCallbackManager.trigger({
theme: res.matches ? 'dark' : 'light'
});
};
const pageNotFoundListener = (res) => {
pageNotFoundCallbackManager.trigger(res);
};
const errorListener = (res) => {
// @ts-ignore
errorCallbackManager.trigger(res.stack || res.message || res);
};
const getApp = () => {
var _a;
const path = (_a = Taro.Current.page) === null || _a === void 0 ? void 0 : _a.path;
return {
/** 小程序切前台的路径 */
path: (path === null || path === void 0 ? void 0 : path.substring(0, path.indexOf('?'))) || '',
/** 小程序切前台的 query 参数 */
query: queryString.parse(location.search),
/** 来源信息。 */
referrerInfo: {},
/** 小程序切前台的[场景值](https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/scene.html) */
scene: 0,
/** shareTicket,详见[获取更多转发信息](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share.html) */
shareTicket: ''
};
};
const appShowListener = () => {
if (document.visibilityState !== 'hidden') {
appShowCallbackManager.trigger(getApp());
}
};
const appHideListener = () => {
if (document.visibilityState === 'hidden') {
appHideCallbackManager.trigger(getApp());
}
};
// 应用级事件
const onUnhandledRejection = callback => {
unhandledRejectionCallbackManager.add(callback);
if (unhandledRejectionCallbackManager.count() === 1) {
window.addEventListener('unhandledrejection', unhandledRejectionListener);
}
};
const onThemeChange = callback => {
themeChangeCallbackManager.add(callback);
if (themeChangeCallbackManager.count() === 1) {
if (isNil(themeMatchMedia)) {
themeMatchMedia = window.matchMedia('(prefers-color-scheme: light)');
}
themeMatchMedia.addEventListener('change', themeChangeListener);
}
};
const onPageNotFound = callback => {
pageNotFoundCallbackManager.add(callback);
if (pageNotFoundCallbackManager.count() === 1) {
Taro.eventCenter.on('__taroRouterNotFound', pageNotFoundListener);
}
};
const onLazyLoadError = /* @__PURE__ */ temporarilyNotSupport('onLazyLoadError');
const onError = callback => {
errorCallbackManager.add(callback);
if (errorCallbackManager.count() === 1) {
window.addEventListener('error', errorListener);
}
};
const onAudioInterruptionEnd = /* @__PURE__ */ temporarilyNotSupport('onAudioInterruptionEnd');
const onAudioInterruptionBegin = /* @__PURE__ */ temporarilyNotSupport('onAudioInterruptionBegin');
const onAppShow = callback => {
appShowCallbackManager.add(callback);
if (appShowCallbackManager.count() === 1) {
window.addEventListener('visibilitychange', appShowListener);
}
};
const onAppHide = callback => {
appHideCallbackManager.add(callback);
if (appHideCallbackManager.count() === 1) {
window.addEventListener('visibilitychange', appHideListener);
}
};
const offUnhandledRejection = callback => {
unhandledRejectionCallbackManager.remove(callback);
if (unhandledRejectionCallbackManager.count() === 0) {
window.removeEventListener('unhandledrejection', unhandledRejectionListener);
}
};
const offThemeChange = callback => {
themeChangeCallbackManager.remove(callback);
if (themeChangeCallbackManager.count() === 0) {
if (isNil(themeMatchMedia)) {
themeMatchMedia = window.matchMedia('(prefers-color-scheme: light)');
}
themeMatchMedia.removeEventListener('change', themeChangeListener);
themeMatchMedia = null;
}
};
const offPageNotFound = callback => {
pageNotFoundCallbackManager.remove(callback);
if (pageNotFoundCallbackManager.count() === 0) {
Taro.eventCenter.off('__taroRouterNotFound', pageNotFoundListener);
}
};
const offLazyLoadError = /* @__PURE__ */ temporarilyNotSupport('offLazyLoadError');
const offError = callback => {
errorCallbackManager.remove(callback);
if (errorCallbackManager.count() === 0) {
window.removeEventListener('error', errorListener);
}
};
const offAudioInterruptionEnd = /* @__PURE__ */ temporarilyNotSupport('offAudioInterruptionEnd');
const offAudioInterruptionBegin = /* @__PURE__ */ temporarilyNotSupport('offAudioInterruptionBegin');
const offAppShow = callback => {
appShowCallbackManager.remove(callback);
if (appShowCallbackManager.count() === 0) {
window.removeEventListener('visibilitychange', appShowListener);
}
};
const offAppHide = callback => {
appHideCallbackManager.remove(callback);
if (appHideCallbackManager.count() === 0) {
window.removeEventListener('visibilitychange', appHideListener);
}
};
export { offAppHide, offAppShow, offAudioInterruptionBegin, offAudioInterruptionEnd, offError, offLazyLoadError, offPageNotFound, offThemeChange, offUnhandledRejection, onAppHide, onAppShow, onAudioInterruptionBegin, onAudioInterruptionEnd, onError, onLazyLoadError, onPageNotFound, onThemeChange, onUnhandledRejection };
//# sourceMappingURL=app-event.js.map