@mpxjs/api-proxy
Advanced tools
Comparing version 2.8.23-alpha to 2.8.25-alpha.9
{ | ||
"name": "@mpxjs/api-proxy", | ||
"version": "2.8.23-alpha", | ||
"version": "2.8.25-alpha.9", | ||
"description": "convert miniprogram API at each end", | ||
@@ -42,3 +42,3 @@ "module": "src/index.js", | ||
}, | ||
"gitHead": "3a6dff432fd46bab36a9866f92cffb6501e69909" | ||
"gitHead": "d72375368adb6722365c623816fd14c28026f836" | ||
} |
@@ -0,19 +1,12 @@ | ||
const notifyCenter = Hummer.notifyCenter | ||
const { Memory } = __GLOBAL__ | ||
// 通过Memory 和 notifyCenter实现跨页面事件通道 | ||
// FIXME:可能存在的问题once订阅的移除 emit事件传参数量 | ||
class EventChannel { | ||
constructor () { | ||
this.listener = {} | ||
} | ||
emit (eventName, ...args) { | ||
const cbs = this.listener[eventName] | ||
if (cbs) { | ||
cbs.forEach((item, index) => { | ||
try { | ||
item.fn.apply(this, args) | ||
} catch (e) { | ||
console.log(`event "${eventName}" error ${e}`) | ||
} | ||
if (item.type === 'once') { | ||
cbs.splice(index, 1) | ||
} | ||
}) | ||
notifyCenter.triggerEvent(eventName, args) | ||
if (Memory.exist(`_ENENT_ONCE_${eventName}`)) { | ||
// 订阅和发送可能不是一个上下文 暂时只能全部移除 | ||
this.off(eventName) | ||
Memory.remove(`_ENENT_ONCE_${eventName}`) | ||
} | ||
@@ -23,28 +16,26 @@ } | ||
off (eventName, EventCallback) { | ||
if (EventCallback) { | ||
const cbs = this.listener[eventName] | ||
const copyCbs = [] | ||
if (cbs) { | ||
cbs.forEach((item, index) => { | ||
if (item.fn !== EventCallback) { | ||
copyCbs.push(item) | ||
} | ||
}) | ||
} | ||
this.listener[eventName] = copyCbs | ||
} else { | ||
this.listener[eventName] && (this.listener[eventName].length = 0) | ||
} | ||
notifyCenter.removeEventListener(eventName, EventCallback) | ||
} | ||
on (eventName, EventCallback) { | ||
(this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: EventCallback, type: 'on' }) | ||
notifyCenter.addEventListener(eventName, EventCallback) | ||
} | ||
once (eventName, EventCallback) { | ||
(this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: EventCallback, type: 'once' }) | ||
notifyCenter.addEventListener(eventName, EventCallback) | ||
Memory.set(`_ENENT_ONCE_${eventName}`, 1) | ||
} | ||
_addListener (eventName, EventCallback, type) { | ||
(this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: EventCallback, type }) | ||
switch (type) { | ||
case 'on': | ||
this.on(eventName, EventCallback) | ||
break | ||
case 'once': | ||
this.once(eventName, EventCallback) | ||
break | ||
default: | ||
this.on(eventName, EventCallback) | ||
break | ||
} | ||
} | ||
@@ -55,3 +46,3 @@ | ||
Object.keys(events).forEach((eventName) => { | ||
(this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: events[eventName], type: 'on' }) | ||
this.on(eventName, events[eventName]) | ||
}) | ||
@@ -58,0 +49,0 @@ } |
@@ -5,5 +5,28 @@ import { webHandleSuccess } from '../../../common/js' | ||
function handleUrl (url) { | ||
const [urlString, queryString] = url.split('?') | ||
const queryObj = {} | ||
if (!queryString) { | ||
return { | ||
query: queryObj, | ||
url: urlString | ||
} | ||
} | ||
const paramsArray = queryString.split('&') | ||
for (const pair of paramsArray) { | ||
const [key, value] = pair.split('=') | ||
queryObj[key] = decodeURIComponent(value) | ||
} | ||
return { | ||
query: queryObj, | ||
url: urlString | ||
} | ||
} | ||
function redirectTo (options = {}) { | ||
const { url, query } = handleUrl(options.url || '') | ||
if (Navigator) { | ||
Navigator.__mpxAction = { type: 'redirect' } | ||
return new Promise((resolve, reject) => { | ||
@@ -13,3 +36,5 @@ // 关闭本页面的跳转 | ||
{ | ||
url: options.url, | ||
url, | ||
animated: false, | ||
params: query, | ||
closeSelf: true | ||
@@ -28,21 +53,18 @@ }, | ||
function navigateTo (options = {}) { | ||
const { url, query } = handleUrl(options.url || '') | ||
const events = options.events | ||
if (Navigator) { | ||
const eventChannel = new EventChannel() | ||
Navigator.__mpxAction = { | ||
type: 'to', | ||
eventChannel | ||
if (events) { | ||
eventChannel._addListeners(events) | ||
} | ||
if (options.events) { | ||
eventChannel._addListeners(options.events) | ||
} | ||
return new Promise((resolve, reject) => { | ||
// 不关闭本页面的跳转 | ||
Navigator.openPage( | ||
{ | ||
url: options.url | ||
}, | ||
// 执行环境变了 得不到执行的机会 故回调无效 | ||
() => {} | ||
) | ||
const res = { errMsg: 'redirectTo:ok' } | ||
Navigator.openPage({ | ||
url, | ||
animated: true, | ||
params: query | ||
}, () => {}) | ||
const res = { errMsg: 'redirectTo:ok', eventChannel } | ||
webHandleSuccess(res, options.success, options.complete) | ||
@@ -57,8 +79,5 @@ resolve(res) | ||
const delta = options.delta || 1 | ||
Navigator.__mpxAction = { | ||
type: 'back', | ||
delta | ||
} | ||
// popBack方法 | ||
Navigator.popBack(delta, { animated: true }) | ||
Navigator.popBack(delta, { | ||
animated: true | ||
}) | ||
const res = { errMsg: 'navigateBack:ok' } | ||
@@ -65,0 +84,0 @@ webHandleSuccess(res, options.success, options.complete) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
167616
4287