message2call
Version:
Convert Send Message and on Message to asynchronous get and call style
7 lines (6 loc) • 17.9 kB
JavaScript
/**!
* message2call.js v2.0.3
* Homepage: https://github.com/lyswhut/message2call#readme
* License: MIT
*/
var Message2call=function(e){"use strict";function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function r(e,t,r,n,o,a,i){try{var s=e[a](i),u=s.value}catch(e){return void r(e)}s.done?t(u):Promise.resolve(u).then(n,o)}function n(e){return function(){var t=this,n=arguments;return new Promise((function(o,a){var i=e.apply(t,n);function s(e){r(i,o,a,s,u,"next",e)}function u(e){r(i,o,a,s,u,"throw",e)}s(void 0)}))}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function a(e,t,r){return t&&function(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,l(n.key),n)}}(e.prototype,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function i(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=f(e))||t){r&&(e=r);var n=0,o=function(){};return{s:o,n:function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,i=!0,s=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return i=e.done,e},e:function(e){s=!0,a=e},f:function(){try{i||null==r.return||r.return()}finally{if(s)throw a}}}}function s(e,t,r){return(t=l(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function u(){u=function(){return t};var e,t={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(e,t,r){e[t]=r.value},a="function"==typeof Symbol?Symbol:{},i=a.iterator||"@@iterator",s=a.asyncIterator||"@@asyncIterator",c=a.toStringTag||"@@toStringTag";function l(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{l({},"")}catch(e){l=function(e,t,r){return e[t]=r}}function f(e,t,r,n){var a=t&&t.prototype instanceof g?t:g,i=Object.create(a.prototype),s=new A(n||[]);return o(i,"_invoke",{value:R(e,r,s)}),i}function h(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}t.wrap=f;var p="suspendedStart",v="suspendedYield",d="executing",y="completed",m={};function g(){}function b(){}function w(){}var E={};l(E,i,(function(){return this}));var k=Object.getPrototypeOf,x=k&&k(k(j([])));x&&x!==r&&n.call(x,i)&&(E=x);var S=w.prototype=g.prototype=Object.create(E);function L(e){["next","throw","return"].forEach((function(t){l(e,t,(function(e){return this._invoke(t,e)}))}))}function P(e,t){function r(o,a,i,s){var u=h(e[o],e,a);if("throw"!==u.type){var c=u.arg,l=c.value;return l&&"object"==typeof l&&n.call(l,"__await")?t.resolve(l.__await).then((function(e){r("next",e,i,s)}),(function(e){r("throw",e,i,s)})):t.resolve(l).then((function(e){c.value=e,i(c)}),(function(e){return r("throw",e,i,s)}))}s(u.arg)}var a;o(this,"_invoke",{value:function(e,n){function o(){return new t((function(t,o){r(e,n,t,o)}))}return a=a?a.then(o,o):o()}})}function R(t,r,n){var o=p;return function(a,i){if(o===d)throw Error("Generator is already running");if(o===y){if("throw"===a)throw i;return{value:e,done:!0}}for(n.method=a,n.arg=i;;){var s=n.delegate;if(s){var u=_(s,n);if(u){if(u===m)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===p)throw o=y,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=d;var c=h(t,r,n);if("normal"===c.type){if(o=n.done?y:v,c.arg===m)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o=y,n.method="throw",n.arg=c.arg)}}}function _(t,r){var n=r.method,o=t.iterator[n];if(o===e)return r.delegate=null,"throw"===n&&t.iterator.return&&(r.method="return",r.arg=e,_(t,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),m;var a=h(o,t.iterator,r.arg);if("throw"===a.type)return r.method="throw",r.arg=a.arg,r.delegate=null,m;var i=a.arg;return i?i.done?(r[t.resultName]=i.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,m):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,m)}function O(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function C(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function A(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(O,this),this.reset(!0)}function j(t){if(t||""===t){var r=t[i];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,a=function r(){for(;++o<t.length;)if(n.call(t,o))return r.value=t[o],r.done=!1,r;return r.value=e,r.done=!0,r};return a.next=a}}throw new TypeError(typeof t+" is not iterable")}return b.prototype=w,o(S,"constructor",{value:w,configurable:!0}),o(w,"constructor",{value:b,configurable:!0}),b.displayName=l(w,c,"GeneratorFunction"),t.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===b||"GeneratorFunction"===(t.displayName||t.name))},t.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,w):(e.__proto__=w,l(e,c,"GeneratorFunction")),e.prototype=Object.create(S),e},t.awrap=function(e){return{__await:e}},L(P.prototype),l(P.prototype,s,(function(){return this})),t.AsyncIterator=P,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new P(f(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},L(S),l(S,c,"Generator"),l(S,i,(function(){return this})),l(S,"toString",(function(){return"[object Generator]"})),t.keys=function(e){var t=Object(e),r=[];for(var n in t)r.push(n);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=j,A.prototype={constructor:A,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(C),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function o(n,o){return s.type="throw",s.arg=t,r.next=n,o&&(r.method="next",r.arg=e),!!o}for(var a=this.tryEntries.length-1;a>=0;--a){var i=this.tryEntries[a],s=i.completion;if("root"===i.tryLoc)return o("end");if(i.tryLoc<=this.prev){var u=n.call(i,"catchLoc"),c=n.call(i,"finallyLoc");if(u&&c){if(this.prev<i.catchLoc)return o(i.catchLoc,!0);if(this.prev<i.finallyLoc)return o(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return o(i.catchLoc,!0)}else{if(!c)throw Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return o(i.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=t&&t<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=e,i.arg=t,a?(this.method="next",this.next=a.finallyLoc,m):this.complete(i)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),m},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),C(r),m}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;C(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:j(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),m}},t}function c(e){return function(e){if(Array.isArray(e))return t(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||f(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e,"string");return"symbol"==typeof t?t:t+""}function f(e,r){if(e){if("string"==typeof e)return t(e,r);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(e,r):void 0}}var h;!function(e){e[e.REQUEST=0]="REQUEST",e[e.RESPONSE=1]="RESPONSE",e[e.CALLBACK_REQUEST=2]="CALLBACK_REQUEST",e[e.CALLBACK_RESPONSE=3]="CALLBACK_RESPONSE"}(h||(h={}));var p="function"==typeof setImmediate?setImmediate:"function"==typeof queueMicrotask?queueMicrotask:function(e){Promise.resolve().then(e)},v=function(){return Math.random().toString(36).slice(2)},d=new Map,y=function(){return a((function e(t,r){var n;o(this,e),s(this,"sendResponse",void 0),s(this,"isSendErrorStack",void 0),this.sendResponse=r,this.isSendErrorStack=null!==(n=t.isSendErrorStack)&&void 0!==n&&n}),[{key:"handleCallbackRequest",value:(e=n(u().mark((function e(t,r){var n,o;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=d.get(t)){e.next=4;break}return this.sendResponse([h.CALLBACK_RESPONSE,t,{message:"".concat(t," is released")}]),e.abrupt("return");case 4:return e.prev=4,e.next=7,n.apply(void 0,c(r));case 7:o=e.sent,e.next=14;break;case 10:return e.prev=10,e.t0=e.catch(4),this.sendResponse([h.CALLBACK_RESPONSE,t,{message:e.t0.message,stack:this.isSendErrorStack?e.t0.stack:void 0}]),e.abrupt("return");case 14:this.sendResponse([h.CALLBACK_RESPONSE,t,null,o]);case 15:case"end":return e.stop()}}),e,this,[[4,10]])}))),function(t,r){return e.apply(this,arguments)})}]);var e}(),m=function(){return a((function e(t,r){var n=t.exposeObj,a=t.events,i=t.timeout,u=t.onCallBeforeParams,c=t.isSendErrorStack;o(this,e),s(this,"events",void 0),s(this,"exposeObj",void 0),s(this,"sendResponse",void 0),s(this,"timeout",void 0),s(this,"onCallBeforeParams",void 0),s(this,"isSendErrorStack",void 0),this.exposeObj=n,this.events=a,this.timeout=i,this.onCallBeforeParams=u,this.isSendErrorStack=c,this.sendResponse=r}),[{key:"handleCallbackData",value:(t=n(u().mark((function e(t,r,n){var o=this;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",new Promise((function(e,a){var i=function(r,n){if(i.timeout&&clearTimeout(i.timeout),o.events.delete(t),null==r)e(n);else{var s=new Error(r.message);s.stack=r.stack,a(s)}};o.events.set(t,i),r&&(i.timeout=setTimeout((function(){i.timeout=null,i({message:"call remote timeout"})}),r)),o.sendResponse([h.CALLBACK_REQUEST,t,n])})));case 1:case"end":return e.stop()}}),e)}))),function(e,r,n){return t.apply(this,arguments)})},{key:"handleRequest",value:(e=n(u().mark((function e(t,r,o,a){var s,c,l,f,p,v,d,y,m,g=this;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:s=this.exposeObj,c=r.pop(),l=i(r),e.prev=3,l.s();case 5:if((f=l.n()).done){e.next=13;break}if(p=f.value,void 0!==(s=s[p])){e.next=11;break}return this.sendResponse([h.RESPONSE,t,{message:"".concat(p," is not defined")}]),e.abrupt("return");case 11:e.next=5;break;case 13:e.next=18;break;case 15:e.prev=15,e.t0=e.catch(3),l.e(e.t0);case 18:return e.prev=18,l.f(),e.finish(18);case 21:if("function"!=typeof s[c]){e.next=53;break}if(!a.length){e.next=39;break}d=i(a),e.prev=24,m=u().mark((function e(){var t,r,a;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:t=y.value,r=g,a=o[t],o.splice(t,1,n(u().mark((function e(){var t,n,o,i=arguments;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:for(t=i.length,n=new Array(t),o=0;o<t;o++)n[o]=i[o];return e.abrupt("return",r.handleCallbackData(a,r.timeout,n));case 2:case"end":return e.stop()}}),e)}))));case 4:case"end":return e.stop()}}),e)})),d.s();case 27:if((y=d.n()).done){e.next=31;break}return e.delegateYield(m(),"t1",29);case 29:e.next=27;break;case 31:e.next=36;break;case 33:e.prev=33,e.t2=e.catch(24),d.e(e.t2);case 36:return e.prev=36,d.f(),e.finish(36);case 39:return e.prev=39,this.onCallBeforeParams&&(o=this.onCallBeforeParams(o)),e.next=43,s[c].apply(s,o);case 43:v=e.sent,e.next=50;break;case 46:return e.prev=46,e.t3=e.catch(39),this.sendResponse([h.RESPONSE,t,{message:e.t3.message,stack:this.isSendErrorStack?e.t3.stack:void 0}]),e.abrupt("return");case 50:this.sendResponse([h.RESPONSE,t,null,v]),e.next=54;break;case 53:void 0===s[c]?this.sendResponse([h.RESPONSE,t,{message:"".concat(c," is not defined")}]):this.sendResponse([h.RESPONSE,t,null,s[c]]);case 54:case"end":return e.stop()}}),e,this,[[3,15,18,21],[24,33,36,39],[39,46]])}))),function(t,r,n,o){return e.apply(this,arguments)})}]);var e,t}(),g=function(){return a((function e(t,r){o(this,e),s(this,"events",void 0),s(this,"remoteGroups",void 0),s(this,"sendRequest",void 0),s(this,"onError",void 0),s(this,"timeout",void 0),this.remoteGroups=new Map,this.events=t.events,this.timeout=t.timeout,this.onError=t.onError,this.sendRequest=r}),[{key:"handleGroupNextTask",value:function(e,t){var r=this;p((function(){var n=r.remoteGroups.get(e);n.handling=!1,n.queue.length&&(null==t?n.queue.shift()[0]():n.queue.shift()[1](t))}))}},{key:"waitQueue",value:(r=n(u().mark((function e(t,r,n){var o=this;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!t.handling){e.next=3;break}return e.next=3,new Promise((function(e,a){t.queue.push([e,function(e){var t;a(e),null===(t=o.onError)||void 0===t||t.call(o,e,n,r),o.handleGroupNextTask(r,e)}])}));case 3:t.handling=!0;case 4:case"end":return e.stop()}}),e)}))),function(e,t,n){return r.apply(this,arguments)})},{key:"handleData",value:(t=n(u().mark((function e(t,r,n,o){var a,i=this;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a="".concat(r.join("."),"_").concat(v()),e.abrupt("return",new Promise((function(e,s){var u=function(n,o){if(u.timeout&&clearTimeout(u.timeout),i.events.delete(a),null==n)e(o);else{var c,l=new Error(n.message);l.stack=n.stack,null===(c=i.onError)||void 0===c||c.call(i,l,r,t),s(l)}},c=[];o=o.map((function(e,t){if("function"==typeof e){if(!e.__msg2call_cbname__)throw new Error("callback is not a proxy callback");return c.push(t),e.__msg2call_cbname__}return e})),i.events.set(a,u),n&&(u.timeout=setTimeout((function(){u.timeout=null,u({message:"call remote timeout"})}),n)),i.sendRequest([h.REQUEST,a,r,o,c])})));case 2:case"end":return e.stop()}}),e)}))),function(e,r,n,o){return t.apply(this,arguments)})},{key:"getData",value:(e=n(u().mark((function e(t,r,n){var o,a,i,s,c=this;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(o=this.timeout,a=!1,null==t){e.next=9;break}if(null!=(i=this.remoteGroups.get(t)).options.timeout&&(o=i.options.timeout),!i.options.queue){e.next=9;break}return a=!0,e.next=9,this.waitQueue(i,t,r);case 9:return s=this.handleData(t,r,o,n),a&&(s=s.then((function(e){return c.handleGroupNextTask(t),e})).catch((function(e){throw c.handleGroupNextTask(t,e),e}))),e.abrupt("return",s);case 12:case"end":return e.stop()}}),e,this)}))),function(t,r,n){return e.apply(this,arguments)})},{key:"createProxy",value:function(e,t){var r,o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],a=new Proxy((function(){}),{get:function(r,n,a){var i=n.toString();if("then"==n&&o.length){var s=e.getData(t,o,[]);return s.then.bind(s)}return e.createProxy(e,t,[].concat(c(o),[i]))},apply:(r=n(u().mark((function r(n,a,i){return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.abrupt("return",e.getData(t,o,i));case 1:case"end":return r.stop()}}),r)}))),function(e,t,n){return r.apply(this,arguments)})});return a}},{key:"createRemoteGroup",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return this.remoteGroups.set(e,{handling:!1,queue:[],options:t}),this.createProxy(this,e)}}]);var e,t,r}();return e.createMessage2Call=function(e){var t,r,o=new Map,a=null!==(t=e.timeout)&&void 0!==t?t:12e4,s=null!==(r=e.isSendErrorStack)&&void 0!==r&&r,c=new g({events:o,timeout:a,onError:e.onError},(function(t){e.sendMessage(t)})),l=new y(e,(function(t){e.sendMessage(t)})),f=new m({events:o,timeout:a,isSendErrorStack:s,onCallBeforeParams:e.onCallBeforeParams,exposeObj:e.exposeObj},(function(t){e.sendMessage(t)})),p=function(){var e=n(u().mark((function e(t,r,n){var a;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:"function"==typeof(a=o.get(t))&&a(r,n);case 2:case"end":return e.stop()}}),e)})));return function(t,r,n){return e.apply(this,arguments)}}();return{remote:c.createProxy(c,null),createRemoteGroup:c.createRemoteGroup.bind(c),message:function(e){if(!Array.isArray(e))throw new Error("message is not array");var t=e;switch(t[0]){case h.REQUEST:f.handleRequest(t[1],t[2],t[3],t[4]);break;case h.CALLBACK_REQUEST:l.handleCallbackRequest(t[1],t[2]);break;case h.RESPONSE:case h.CALLBACK_RESPONSE:p(t[1],t[2],t[3])}},destroy:function(){var e,t=i(o.values());try{for(t.s();!(e=t.n()).done;){(0,e.value)({message:"destroy"})}}catch(e){t.e(e)}finally{t.f()}}}},e.createProxyCallback=function(e){var t=e.__msg2call_cbname__="func_".concat(d.size,"_").concat(v());return d.set(t,e),e.releaseProxy=function(){d.delete(t)},e},e.releaseAllProxyCallback=function(){d.clear()},e}({});