提交 9f5aee35 编写于 作者: Q qiang

refactor: socket callback

上级 41e8dc5c
......@@ -13167,9 +13167,9 @@ class SocketTask {
const res = name === "message" ? {
data: event2.data
} : {};
this._callbacks[name].forEach((callback22) => {
this._callbacks[name].forEach((callback3) => {
try {
callback22(res);
callback3(res);
} catch (e2) {
console.error(`thirdScriptError
${e2};at socketTask.on${shared.capitalize(name)} callback function
......@@ -13214,9 +13214,9 @@ ${e2};at socketTask.on${shared.capitalize(name)} callback function
throw new Error("SocketTask.readyState is not OPEN");
}
ws.send(data);
this._callback(options, "sendSocketMessage:ok");
callback(options, "sendSocketMessage:ok");
} catch (error) {
this._callback(options, `sendSocketMessage:fail ${error}`);
callback(options, `sendSocketMessage:fail ${error}`);
}
}
close(options = {}) {
......@@ -13229,30 +13229,9 @@ ${e2};at socketTask.on${shared.capitalize(name)} callback function
} else {
ws.close(code);
}
this._callback(options, "closeSocket:ok");
callback(options, "closeSocket:ok");
} catch (error) {
this._callback(options, `closeSocket:fail ${error}`);
}
}
_callback({
success,
fail,
complete
} = {}, errMsg) {
const data = {
errMsg
};
if (/:ok$/.test(errMsg)) {
if (typeof success === "function") {
success(data);
}
} else {
if (typeof fail === "function") {
fail(data);
}
}
if (typeof complete === "function") {
complete(data);
callback(options, `closeSocket:fail ${error}`);
}
}
onOpen(callback2) {
......
......@@ -13164,9 +13164,9 @@ class SocketTask {
const res = name === "message" ? {
data: event2.data
} : {};
this._callbacks[name].forEach((callback22) => {
this._callbacks[name].forEach((callback3) => {
try {
callback22(res);
callback3(res);
} catch (e2) {
console.error(`thirdScriptError
${e2};at socketTask.on${capitalize(name)} callback function
......@@ -13211,9 +13211,9 @@ ${e2};at socketTask.on${capitalize(name)} callback function
throw new Error("SocketTask.readyState is not OPEN");
}
ws.send(data);
this._callback(options, "sendSocketMessage:ok");
callback(options, "sendSocketMessage:ok");
} catch (error) {
this._callback(options, `sendSocketMessage:fail ${error}`);
callback(options, `sendSocketMessage:fail ${error}`);
}
}
close(options = {}) {
......@@ -13226,30 +13226,9 @@ ${e2};at socketTask.on${capitalize(name)} callback function
} else {
ws.close(code);
}
this._callback(options, "closeSocket:ok");
callback(options, "closeSocket:ok");
} catch (error) {
this._callback(options, `closeSocket:fail ${error}`);
}
}
_callback({
success,
fail,
complete
} = {}, errMsg) {
const data = {
errMsg
};
if (/:ok$/.test(errMsg)) {
if (typeof success === "function") {
success(data);
}
} else {
if (typeof fail === "function") {
fail(data);
}
}
if (typeof complete === "function") {
complete(data);
callback(options, `closeSocket:fail ${error}`);
}
}
onOpen(callback2) {
......
......@@ -14,6 +14,7 @@ import {
API_TYPE_CLOSE_SOCKET,
CloseSocketProtocol,
} from '@dcloudio/uni-api'
import { callback } from '../../../helpers/utils'
type eventName = keyof WebSocketEventMap
......@@ -116,9 +117,9 @@ class SocketTask implements UniApp.SocketTask {
throw new Error('SocketTask.readyState is not OPEN')
}
ws.send(data)
this._callback(options, 'sendSocketMessage:ok')
callback(options, 'sendSocketMessage:ok')
} catch (error) {
this._callback(options, `sendSocketMessage:fail ${error}`)
callback(options, `sendSocketMessage:fail ${error}`)
}
}
......@@ -137,40 +138,11 @@ class SocketTask implements UniApp.SocketTask {
} else {
ws.close(code)
}
this._callback(options, 'closeSocket:ok')
callback(options, 'closeSocket:ok')
} catch (error) {
this._callback(options, `closeSocket:fail ${error}`)
callback(options, `closeSocket:fail ${error}`)
}
}
/**
* 通用回调处理
*/
_callback(
{
success,
fail,
complete,
}: UniApp.SendSocketMessageOptions | UniApp.CloseSocketOptions = {},
errMsg: string
) {
const data = {
errMsg,
}
if (/:ok$/.test(errMsg)) {
if (typeof success === 'function') {
success(data)
}
} else {
if (typeof fail === 'function') {
fail(data)
}
}
if (typeof complete === 'function') {
complete(data)
}
}
onOpen(callback: (result: any) => void) {
this._callbacks.open.push(callback)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册