diff --git a/packages/uni-mp-kuaishou/dist/index.js b/packages/uni-mp-kuaishou/dist/index.js index e6b7bea879f051f5cadcfef3e5cd0be73d223387..984f60d1483d504fdd168afbfb3b49e23b882971 100644 --- a/packages/uni-mp-kuaishou/dist/index.js +++ b/packages/uni-mp-kuaishou/dist/index.js @@ -356,6 +356,143 @@ var baseApi = /*#__PURE__*/Object.freeze({ interceptors: interceptors }); +class EventChannel { + constructor (id, events) { + this.id = id; + this.listener = {}; + this.emitCache = {}; + if (events) { + Object.keys(events).forEach(name => { + this.on(name, events[name]); + }); + } + } + + emit (eventName, ...args) { + const fns = this.listener[eventName]; + if (!fns) { + return (this.emitCache[eventName] || (this.emitCache[eventName] = [])).push(args) + } + fns.forEach(opt => { + opt.fn.apply(opt.fn, args); + }); + this.listener[eventName] = fns.filter(opt => opt.type !== 'once'); + } + + on (eventName, fn) { + this._addListener(eventName, 'on', fn); + this._clearCache(eventName); + } + + once (eventName, fn) { + this._addListener(eventName, 'once', fn); + this._clearCache(eventName); + } + + off (eventName, fn) { + const fns = this.listener[eventName]; + if (!fns) { + return + } + if (fn) { + for (let i = 0; i < fns.length;) { + if (fns[i].fn === fn) { + fns.splice(i, 1); + i--; + } + i++; + } + } else { + delete this.listener[eventName]; + } + } + + _clearCache (eventName) { + const cacheArgs = this.emitCache[eventName]; + if (cacheArgs) { + for (; cacheArgs.length > 0;) { + this.emit.apply(this, [eventName].concat(cacheArgs.shift())); + } + } + } + + _addListener (eventName, type, fn) { + (this.listener[eventName] || (this.listener[eventName] = [])).push({ + fn, + type + }); + } +} + +const eventChannels = {}; + +const eventChannelStack = []; + +let id = 0; + +function initEventChannel (events, cache = true) { + id++; + const eventChannel = new EventChannel(id, events); + if (cache) { + eventChannels[id] = eventChannel; + eventChannelStack.push(eventChannel); + } + return eventChannel +} + +function getEventChannel (id) { + if (id) { + const eventChannel = eventChannels[id]; + delete eventChannels[id]; + return eventChannel + } + return eventChannelStack.shift() +} + +var navigateTo = { + args (fromArgs, toArgs) { + const id = initEventChannel(fromArgs.events).id; + if (fromArgs.url) { + fromArgs.url = fromArgs.url + (fromArgs.url.indexOf('?') === -1 ? '?' : '&') + '__id__=' + id; + } + }, + returnValue (fromRes, toRes) { + fromRes.eventChannel = getEventChannel(); + } +}; + +function findExistsPageIndex (url) { + const pages = getCurrentPages(); + let len = pages.length; + while (len--) { + const page = pages[len]; + if (page.$page && page.$page.fullPath === url) { + return len + } + } + return -1 +} + +var redirectTo = { + name (fromArgs) { + if (fromArgs.exists === 'back' && fromArgs.delta) { + return 'navigateBack' + } + return 'redirectTo' + }, + args (fromArgs) { + if (fromArgs.exists === 'back' && fromArgs.url) { + const existsPageIndex = findExistsPageIndex(fromArgs.url); + if (existsPageIndex !== -1) { + const delta = getCurrentPages().length - 1 - existsPageIndex; + if (delta > 0) { + fromArgs.delta = delta; + } + } + } + } +}; + var previewImage = { args (fromArgs) { let currentIndex = parseInt(fromArgs.current); @@ -390,7 +527,9 @@ var previewImage = { } }; -const protocols = { +const protocols = { + navigateTo, + redirectTo, previewImage }; const todos = [ @@ -464,12 +603,17 @@ function wrapper (methodName, method) { } arg1 = processArgs(methodName, arg1, options.args, options.returnValue); - - const args = [arg1]; - if (typeof arg2 !== 'undefined') { - args.push(arg2); + + const args = [arg1]; + if (typeof arg2 !== 'undefined') { + args.push(arg2); } - const returnValue = ks[options.name || methodName].apply(ks, args); + if (isFn(options.name)) { + methodName = options.name(arg1); + } else if (isStr(options.name)) { + methodName = options.name; + } + const returnValue = ks[methodName].apply(ks, args); if (isSyncApi(methodName)) { // 同步 api return processReturnValue(methodName, returnValue, options.returnValue, isContextApi(methodName)) } @@ -1137,10 +1281,10 @@ function handleEvent (event) { eventArray[2], isCustom, methodName - ) || []; + ); // 参数尾部增加原始事件对象用于复杂表达式内获取额外数据 // eslint-disable-next-line no-sparse-arrays - ret.push(handler.apply(handlerCtx, params.concat([, , , , , , , , , , event]))); + ret.push(handler.apply(handlerCtx, (Array.isArray(params) ? params : []).concat([, , , , , , , , , , event]))); } }); } @@ -1316,6 +1460,20 @@ function parseApp (vm) { } function createApp (vm) { + Vue.prototype.getOpenerEventChannel = function () { + if (!this.__eventChannel__) { + this.__eventChannel__ = new EventChannel(); + } + return this.__eventChannel__ + }; + const callHook = Vue.prototype.__call_hook; + Vue.prototype.__call_hook = function (hook, args) { + if (hook === 'onLoad' && args && args.__id__) { + this.__eventChannel__ = getEventChannel(args.__id__); + delete args.__id__; + } + return callHook.call(this, hook, args) + }; App(parseApp(vm)); return vm }