cross-mp-power
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -0,1 +1,4 @@ | ||
declare const memoize: <T extends (...args: any[]) => any = any>(func: T) => (...args: Parameters<T>) => ReturnType<T>; | ||
declare const uuid: () => string; | ||
declare const getApiVar: () => any; | ||
@@ -38,2 +41,11 @@ declare const getApiVarName: () => string; | ||
} | ||
interface EventHandler { | ||
(type: string, data?: any): any; | ||
} | ||
interface CrossEvent { | ||
from: string; | ||
target: string; | ||
name: string; | ||
data?: any; | ||
} | ||
@@ -73,2 +85,28 @@ declare const hookApiMethodCallback: (apiName: string, onSuccess: AnyFunction, onFail: AnyFunction, args: any[]) => any[]; | ||
export { type AnyFunction, type CrossMpClientRect, type CrossMpEnvInfo, type CrossMpEnvVersion, type CrossMpStorageInfo, type CrossMpSystemInfo, checkDebugEnabled, clearStorage, getApiVar, getApiVarName, getCurrentAppId, getCurrentAppVersion, getCurrentEnvVersion, getStorage, getStorageInfo, getSystemInfo, hookApiMethodCallback, nextTick, promisifyApi, removeStorage, selectAllBoundingClientRect, selectBoundingClientRect, setClipboardData, setStorage, showActionSheet, showToast, supportSelectOwnerComponent }; | ||
declare const getGlobalObject: () => any; | ||
declare class EventEmitter { | ||
private events; | ||
constructor(); | ||
once(type: string, _handler: EventHandler): void; | ||
destroy(): void; | ||
on(type: string, handler: EventHandler): void; | ||
off(type: string, handler?: EventHandler): void; | ||
emit(type: string, data?: any): void; | ||
} | ||
declare class CrossEventSystem { | ||
static Ebus: EventEmitter; | ||
private hash; | ||
private parentHash; | ||
private eventHandler; | ||
private onHandler?; | ||
constructor(); | ||
destroy(): void; | ||
emit(name: string, data?: any): void; | ||
on(handler: (e: CrossEvent) => void): void; | ||
getHash(): string; | ||
setParentHash(val: string): void; | ||
} | ||
export { type AnyFunction, type CrossEvent, CrossEventSystem, type CrossMpClientRect, type CrossMpEnvInfo, type CrossMpEnvVersion, type CrossMpStorageInfo, type CrossMpSystemInfo, EventEmitter, type EventHandler, checkDebugEnabled, clearStorage, getApiVar, getApiVarName, getCurrentAppId, getCurrentAppVersion, getCurrentEnvVersion, getGlobalObject, getStorage, getStorageInfo, getSystemInfo, hookApiMethodCallback, memoize, nextTick, promisifyApi, removeStorage, selectAllBoundingClientRect, selectBoundingClientRect, setClipboardData, setStorage, showActionSheet, showToast, supportSelectOwnerComponent, uuid }; |
294
esm/index.js
@@ -12,2 +12,9 @@ const memoize = (func)=>{ | ||
}; | ||
const uuid = ()=>{ | ||
return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, (c)=>{ | ||
const r = Math.random() * 16 | 0; | ||
const v = c === 'x' ? r : r & 0x3 | 0x8; | ||
return v.toString(16); | ||
}); | ||
}; | ||
@@ -206,3 +213,4 @@ // eslint-disable-next-line complexity | ||
}); | ||
const getEnvInfo = (()=>{ | ||
// eslint-disable-next-line complexity | ||
const getEnvInfo = memoize(()=>{ | ||
let res; | ||
@@ -223,57 +231,56 @@ const d = (val)=>{ | ||
}; | ||
// eslint-disable-next-line complexity | ||
return ()=>{ | ||
if (res !== undefined) { | ||
return res; | ||
if (BUILD_TARGET === 'wx') { | ||
if ('getAccountInfoSync' in wx) { | ||
var _wx_getAccountInfoSync; | ||
res = (_wx_getAccountInfoSync = wx.getAccountInfoSync()) === null || _wx_getAccountInfoSync === void 0 ? void 0 : _wx_getAccountInfoSync.miniProgram; | ||
} | ||
if (BUILD_TARGET === 'wx') { | ||
if ('getAccountInfoSync' in wx) { | ||
var _wx_getAccountInfoSync; | ||
res = (_wx_getAccountInfoSync = wx.getAccountInfoSync()) === null || _wx_getAccountInfoSync === void 0 ? void 0 : _wx_getAccountInfoSync.miniProgram; | ||
} | ||
if (!res && typeof __wxConfig === 'object') { | ||
var __wxConfig_accountInfo; | ||
return d({ | ||
envVersion: __wxConfig.envVersion, | ||
appId: ((__wxConfig_accountInfo = __wxConfig.accountInfo) === null || __wxConfig_accountInfo === void 0 ? void 0 : __wxConfig_accountInfo.appId) || '?', | ||
version: '?' | ||
}); | ||
} | ||
return d(res); | ||
if (!res && typeof __wxConfig === 'object') { | ||
var __wxConfig_accountInfo; | ||
return d({ | ||
envVersion: __wxConfig.envVersion, | ||
appId: ((__wxConfig_accountInfo = __wxConfig.accountInfo) === null || __wxConfig_accountInfo === void 0 ? void 0 : __wxConfig_accountInfo.appId) || '?', | ||
version: '?' | ||
}); | ||
} | ||
if (BUILD_TARGET === 'qq') { | ||
if ('getAccountInfoSync' in qq) { | ||
var _qq_getAccountInfoSync; | ||
res = (_qq_getAccountInfoSync = qq.getAccountInfoSync()) === null || _qq_getAccountInfoSync === void 0 ? void 0 : _qq_getAccountInfoSync.miniProgram; | ||
} | ||
if (!res && typeof __wxConfig === 'object') { | ||
let cfg; | ||
if ((typeof __wxConfig === 'object' ? cfg = __wxConfig : false) || (typeof __qqConfig === 'object' ? cfg = __qqConfig : false)) { | ||
var _cfg_accountInfo; | ||
return d({ | ||
envVersion: cfg.envVersion, | ||
appId: ((_cfg_accountInfo = cfg.accountInfo) === null || _cfg_accountInfo === void 0 ? void 0 : _cfg_accountInfo.appId) || '?', | ||
version: '?' | ||
}); | ||
} | ||
} | ||
return d(res); | ||
return d(res); | ||
} | ||
if (BUILD_TARGET === 'qq') { | ||
if ('getAccountInfoSync' in qq) { | ||
var _qq_getAccountInfoSync; | ||
res = (_qq_getAccountInfoSync = qq.getAccountInfoSync()) === null || _qq_getAccountInfoSync === void 0 ? void 0 : _qq_getAccountInfoSync.miniProgram; | ||
} | ||
if (BUILD_TARGET === 'my') { | ||
if ('getAccountInfoSync' in my) { | ||
var _my_getAccountInfoSync_miniProgram, _my_getAccountInfoSync; | ||
res = (_my_getAccountInfoSync = my.getAccountInfoSync()) === null || _my_getAccountInfoSync === void 0 ? void 0 : (_my_getAccountInfoSync_miniProgram = _my_getAccountInfoSync.miniProgram) === null || _my_getAccountInfoSync_miniProgram === void 0 ? void 0 : _my_getAccountInfoSync_miniProgram.envVersion; | ||
} | ||
if (!res && typeof __appxStartupParams === 'object') { | ||
if (!res && typeof __wxConfig === 'object') { | ||
let cfg; | ||
if ((typeof __wxConfig === 'object' ? cfg = __wxConfig : false) || (typeof __qqConfig === 'object' ? cfg = __qqConfig : false)) { | ||
var _cfg_accountInfo; | ||
return d({ | ||
envVersion: __appxStartupParams.envVersion || (my.isIDE ? 'develop' : '?'), | ||
appId: __appxStartupParams.appId || '?', | ||
envVersion: cfg.envVersion, | ||
appId: ((_cfg_accountInfo = cfg.accountInfo) === null || _cfg_accountInfo === void 0 ? void 0 : _cfg_accountInfo.appId) || '?', | ||
version: '?' | ||
}); | ||
} | ||
return d(res); | ||
} | ||
return d(); | ||
}; | ||
})(); | ||
return d(res); | ||
} | ||
if (BUILD_TARGET === 'my') { | ||
if ('getAccountInfoSync' in my) { | ||
var _my_getAccountInfoSync_miniProgram, _my_getAccountInfoSync; | ||
res = (_my_getAccountInfoSync = my.getAccountInfoSync()) === null || _my_getAccountInfoSync === void 0 ? void 0 : (_my_getAccountInfoSync_miniProgram = _my_getAccountInfoSync.miniProgram) === null || _my_getAccountInfoSync_miniProgram === void 0 ? void 0 : _my_getAccountInfoSync_miniProgram.envVersion; | ||
} | ||
if (!res && typeof __appxStartupParams === 'object') { | ||
return d({ | ||
envVersion: __appxStartupParams.envVersion || (my.isIDE ? 'develop' : '?'), | ||
appId: __appxStartupParams.appId || '?', | ||
version: '?' | ||
}); | ||
} | ||
return d(res); | ||
} | ||
const apiVar = getApiVar(); | ||
if ('getAccountInfoSync' in apiVar) { | ||
var _apiVar_getAccountInfoSync_miniProgram, _apiVar_getAccountInfoSync; | ||
res = (_apiVar_getAccountInfoSync = apiVar.getAccountInfoSync()) === null || _apiVar_getAccountInfoSync === void 0 ? void 0 : (_apiVar_getAccountInfoSync_miniProgram = _apiVar_getAccountInfoSync.miniProgram) === null || _apiVar_getAccountInfoSync_miniProgram === void 0 ? void 0 : _apiVar_getAccountInfoSync_miniProgram.envVersion; | ||
} | ||
return d(res); | ||
}); | ||
/** 获取小程序环境版本,可选值及含义: | ||
@@ -421,3 +428,4 @@ * develop=开发/预览环境版本; | ||
try { | ||
apiVar.createSelectorQuery()[mode](selector).boundingClientRect((rect)=>{ | ||
apiVar.createSelectorQuery()[mode](selector).boundingClientRect().exec((rect)=>{ | ||
rect = rect === null || rect === void 0 ? void 0 : rect[0]; | ||
if (rect) { | ||
@@ -440,3 +448,3 @@ clearTimeout(timer); | ||
setTimeout(fire, retryDelay || 0); | ||
}).exec(); | ||
}); | ||
} catch (error) { | ||
@@ -457,3 +465,187 @@ clearTimeout(timer); | ||
export { checkDebugEnabled, clearStorage, getApiVar, getApiVarName, getCurrentAppId, getCurrentAppVersion, getCurrentEnvVersion, getStorage, getStorageInfo, getSystemInfo, hookApiMethodCallback, nextTick, promisifyApi, removeStorage, selectAllBoundingClientRect, selectBoundingClientRect, setClipboardData, setStorage, showActionSheet, showToast, supportSelectOwnerComponent }; | ||
const getGlobalObject = memoize(function() { | ||
if (typeof global === 'object' && global) { | ||
return global; | ||
} | ||
if (typeof globalThis === 'object' && globalThis) { | ||
return globalThis; | ||
} | ||
if (typeof this === 'object' && this) { | ||
return this; | ||
} | ||
const key = '__crmpGlobal__'; | ||
let apiVar; | ||
if (apiVar = getApiVar()) { | ||
apiVar[key] = apiVar[key] || {}; | ||
return apiVar[key]; | ||
} | ||
if (typeof getApp === 'function') { | ||
const app = getApp({ | ||
allowDefault: true | ||
}); | ||
app[key] = app[key] || {}; | ||
return app[key]; | ||
} | ||
return {}; | ||
}); | ||
function _define_property$1(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
class EventEmitter { | ||
once(type, _handler) { | ||
const handler = (...args)=>{ | ||
_handler(...args); | ||
this.off(type, handler); | ||
}; | ||
this.on(type, handler); | ||
} | ||
destroy() { | ||
this.emit('destroy'); | ||
// eslint-disable-next-line guard-for-in | ||
for(const prop in this.events){ | ||
delete this.events[prop]; | ||
} | ||
} | ||
on(type, handler) { | ||
if (!this.events[type]) { | ||
this.events[type] = []; | ||
} | ||
if (this.events[type].indexOf(handler) === -1) { | ||
this.events[type].push(handler); | ||
} | ||
} | ||
off(type, handler) { | ||
if (this.events[type]) { | ||
if (handler) { | ||
const index = this.events[type].indexOf(handler); | ||
if (index !== -1) { | ||
this.events[type].splice(index, 1); | ||
} | ||
} else { | ||
delete this.events[type]; | ||
} | ||
} | ||
} | ||
emit(type, data) { | ||
const currentIsFireHandlers = []; | ||
const fire = (handleType = type)=>{ | ||
let needReload; | ||
if (this.events[type]) { | ||
const list = this.events[type]; | ||
for(let i = 0, len = list.length; i < len; i++){ | ||
if (!this.events[type]) { | ||
break; | ||
} | ||
if (this.events[type] && i in list && list[i]) { | ||
const handler = list[i]; | ||
if (currentIsFireHandlers.indexOf(handler) !== -1) { | ||
continue; | ||
} | ||
const nextHandler = list[i + 1]; | ||
currentIsFireHandlers.push(handler); | ||
handler(handleType, data); | ||
if (!list[i] || list[i] !== handler || !list[i + 1] || list[i + 1] !== nextHandler) { | ||
needReload = true; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
needReload && fire(); | ||
}; | ||
if (type !== 'all') { | ||
fire(); | ||
const old = type; | ||
// eslint-disable-next-line no-param-reassign | ||
type = 'all'; | ||
fire(old); | ||
} | ||
} | ||
constructor(){ | ||
_define_property$1(this, "events", {}); | ||
[ | ||
'on', | ||
'off', | ||
'emit' | ||
].forEach((prop)=>{ | ||
this[prop] = this[prop].bind(this); | ||
}); | ||
} | ||
} | ||
function _define_property(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
class CrossEventSystem { | ||
destroy() { | ||
CrossEventSystem.Ebus.off('CrossEvent', this.eventHandler); | ||
} | ||
emit(name, data) { | ||
CrossEventSystem.Ebus.emit('CrossEvent', { | ||
from: this.hash, | ||
target: this.parentHash, | ||
name, | ||
data | ||
}); | ||
} | ||
on(handler) { | ||
this.onHandler = handler; | ||
} | ||
getHash() { | ||
return this.hash; | ||
} | ||
setParentHash(val) { | ||
this.parentHash = val; | ||
} | ||
constructor(){ | ||
_define_property(this, "hash", void 0); | ||
_define_property(this, "parentHash", void 0); | ||
_define_property(this, "eventHandler", void 0); | ||
_define_property(this, "onHandler", void 0); | ||
this.hash = uuid(); | ||
this.parentHash = ''; | ||
if (!CrossEventSystem.Ebus) { | ||
const G = getGlobalObject(); | ||
if (!G.Ebus) { | ||
G.Ebus = new EventEmitter(); | ||
} | ||
CrossEventSystem.Ebus = G.Ebus; | ||
} | ||
this.eventHandler = (type, e)=>{ | ||
if (e.target === this.hash) { | ||
try { | ||
var _this_onHandler, _this; | ||
(_this_onHandler = (_this = this).onHandler) === null || _this_onHandler === void 0 ? void 0 : _this_onHandler.call(_this, e); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
}; | ||
CrossEventSystem.Ebus.on('CrossEvent', this.eventHandler); | ||
} | ||
} | ||
_define_property(CrossEventSystem, "Ebus", void 0); | ||
export { CrossEventSystem, EventEmitter, checkDebugEnabled, clearStorage, getApiVar, getApiVarName, getCurrentAppId, getCurrentAppVersion, getCurrentEnvVersion, getGlobalObject, getStorage, getStorageInfo, getSystemInfo, hookApiMethodCallback, memoize, nextTick, promisifyApi, removeStorage, selectAllBoundingClientRect, selectBoundingClientRect, setClipboardData, setStorage, showActionSheet, showToast, supportSelectOwnerComponent, uuid }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "cross-mp-power", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "跨平台小程序能力封装", | ||
@@ -5,0 +5,0 @@ "main": "esm/index.js", |
Sorry, the diff of this file is not supported yet
75674
736