UNPKG

dingtalk-jsapi

Version:

钉钉 模块化 多端统一 API

1 lines 1.83 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.emitEventToDTNative=exports.removeDTEventListener=exports.addDTEventListener=void 0;var eapp_1=require("../lib/bridge/eapp"),eventsHandleMap={},sp=".",genEventName=function(e,t){return""+e+sp+t},splitEventName=function(e){var t=e.split(sp);return{namespace:t[0]||"",eventName:t[1]||""}},tryRegisterEvent=function(){var e=!1;return function(){e||(eapp_1.default("internal.util.registerNativeCall",{onSuccess:function(e){var t=eventsHandleMap[genEventName(e.namespace,e.eventName)];t&&t.forEach(function(t){t(e.data)})}}),dd.addDTCustomEventListener(function(e){var t="",n="",a={};e.namespace&&e.eventName?(t=e.namespace,n=e.eventName,a=e.data):e.data.namespace&&e.data.eventName&&(t=e.data.namespace,n=e.data.eventName,a=e.data.data);var r=eventsHandleMap[genEventName(t,n)];r&&r.forEach(function(e){e(a)})}),e=!0)}}();exports.addDTEventListener=function(e,t){if(tryRegisterEvent(),!e||"function"!=typeof t)throw new Error("eventName is null or listener is not a function, addEAppListener fail");var n=eventsHandleMap[e];n?n.push(t):eventsHandleMap[e]=[t]},exports.removeDTEventListener=function(e,t){if(!e||"function"!=typeof t)throw new Error("eventName is null or listener is not a function, removeEAppListener fail");var n=eventsHandleMap[e];if(Array.isArray(n)){var a=n.indexOf(t);-1!==a&&n.splice(a,1)}},exports.emitEventToDTNative=function(e,t,n,a){if("string"==typeof e&&"object"==typeof t){var r=splitEventName(e);return new Promise(function(e,i){eapp_1.default("internal.util.callExternalNative",{data:t,namespace:r.namespace,eventName:r.eventName,onSuccess:function(t){"function"==typeof n&&n(t),e(t)},onFail:function(e){"function"==typeof a&&a(e),i(e)}})})}throw new Error("eventName is null or data is not a object, emitEventToNative fail")};