@waves/event-sender
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -22,3 +22,3 @@ (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
.then(function () { | ||
utils_1.runByPath(data.initializeMethod, [data.apiToken]); | ||
utils_1.runByPath(data.initializeMethod, [data.apiToken, data.initializeOptions]); | ||
adapters.push({ | ||
@@ -25,0 +25,0 @@ type: data.type, |
@@ -1,1 +0,1 @@ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.bus=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var Adapter=function(){function Adapter(){}return Adapter}();exports.Adapter=Adapter},{}],2:[function(require,module,exports){"use strict";var __extends=this&&this.__extends||function(){var extendStatics=function(d,b){extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p]};return extendStatics(d,b)};return function(d,b){extendStatics(d,b);function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)}}();var __assign=this&&this.__assign||function(){__assign=Object.assign||function(t){for(var s,i=1,n=arguments.length;i<n;i++){s=arguments[i];for(var p in s)if(Object.prototype.hasOwnProperty.call(s,p))t[p]=s[p]}return t};return __assign.apply(this,arguments)};Object.defineProperty(exports,"__esModule",{value:true});var Adapter_1=require("./Adapter");var __1=require("..");var WindowProtocol_1=require("../protocols/WindowProtocol");var utils_1=require("../utils/utils");var EMPTY_OPTIONS={origins:[],availableChanelId:[]};var WindowAdapter=function(_super){__extends(WindowAdapter,_super);function WindowAdapter(listen,dispatch,options){var _this=_super.call(this)||this;_this.id=__1.uniqueId("wa");_this.callbacks=[];_this.options=WindowAdapter.prepareOptions(options);_this.listen=listen;_this.dispatch=dispatch;_this.listen.forEach(function(protocol){return protocol.on("message",_this.onMessage,_this)});return _this}WindowAdapter.prototype.addListener=function(cb){this.callbacks.push(cb);__1.console.info("WindowAdapter: Add iframe message listener");return this};WindowAdapter.prototype.send=function(data){var message=__assign({},data,{chanelId:this.chanelId});this.dispatch.forEach(function(protocol){return protocol.dispatch(message)});__1.console.info("WindowAdapter: Send message",message);return this};WindowAdapter.prototype.destroy=function(){this.listen.forEach(function(protocol){return protocol.destroy()});this.dispatch.forEach(function(protocol){return protocol.destroy()});__1.console.info("WindowAdapter: Destroy")};WindowAdapter.prototype.onMessage=function(event){if(this.accessEvent(event)){this.callbacks.forEach(function(cb){try{cb(event.data)}catch(e){__1.console.warn("WindowAdapter: Unhandled exception!",e)}})}};WindowAdapter.prototype.accessEvent=function(event){if(typeof event.data!=="object"||event.data.type==null){__1.console.info("WindowAdapter: Block event. Wrong event format!",event.data);return false}if(!this.options.origins.has("*")&&!this.options.origins.has(event.origin)){__1.console.info('SimpleWindowAdapter: Block event by origin "'+event.origin+'"');return false}if(!this.options.availableChanelId.size){return true}var access=!!(event.data.chanelId&&this.options.availableChanelId.has(event.data.chanelId));if(!access){__1.console.info('SimpleWindowAdapter: Block event by chanel id "'+event.data.chanelId+'"')}return access};WindowAdapter.createSimpleWindowAdapter=function(iframe,options){var _this=this;var origin=this.getContentOrigin(iframe);var myOptions=this.prepareOptions(options);var events=[];if(origin){myOptions.origins.add(origin)}var listen=new WindowProtocol_1.WindowProtocol(window,WindowProtocol_1.WindowProtocol.PROTOCOL_TYPES.LISTEN);var handler=function(event){events.push(event)};listen.on("message",handler);return this.getIframeContent(iframe).then(function(win){var dispatch=new WindowProtocol_1.WindowProtocol(win.win,WindowProtocol_1.WindowProtocol.PROTOCOL_TYPES.DISPATCH);var adapter=new WindowAdapter([listen],[dispatch],_this.unPrepareOptions(myOptions));events.forEach(function(event){adapter.onMessage(event)});listen.off("message",handler);return adapter})};WindowAdapter.prepareOptions=function(options){if(options===void 0){options=EMPTY_OPTIONS}var concat=function(initialValue){return function(list){return list.reduce(function(set,item){return set.add(item)},initialValue)}};var getCollection=function(data,initial){return utils_1.pipe(__1.toArray,concat(initial))(data)};var origins=getCollection(options.origins||[],new __1.UniqPrimitiveCollection([window.location.origin]));var chanelId=getCollection(options.availableChanelId||[],new __1.UniqPrimitiveCollection);return __assign({},options,{origins:origins,availableChanelId:chanelId})};WindowAdapter.unPrepareOptions=function(options){return{origins:options.origins.toArray(),availableChanelId:options.availableChanelId.toArray(),chanelId:options.chanelId}};WindowAdapter.getIframeContent=function(content){if(!content){return Promise.resolve({win:window.opener||window.parent})}if(!(content instanceof HTMLIFrameElement)){return Promise.resolve({win:content})}if(content.contentWindow){return Promise.resolve({win:content.contentWindow})}return new Promise(function(resolve,reject){content.addEventListener("load",function(){return resolve({win:content.contentWindow})},false);content.addEventListener("error",reject,false)})};WindowAdapter.getContentOrigin=function(content){if(!content){try{return new URL(document.referrer).origin}catch(e){return null}}if(!(content instanceof HTMLIFrameElement)){try{return window.top.origin}catch(e){return null}}try{return new URL(content.src).origin||null}catch(e){return null}};return WindowAdapter}(Adapter_1.Adapter);exports.WindowAdapter=WindowAdapter},{"..":5,"../protocols/WindowProtocol":6,"../utils/utils":10,"./Adapter":1}],3:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var utils_1=require("../utils");var EventType;(function(EventType){EventType[EventType["Event"]=0]="Event";EventType[EventType["Action"]=1]="Action";EventType[EventType["Response"]=2]="Response"})(EventType=exports.EventType||(exports.EventType={}));var ResponseStatus;(function(ResponseStatus){ResponseStatus[ResponseStatus["Success"]=0]="Success";ResponseStatus[ResponseStatus["Error"]=1]="Error"})(ResponseStatus=exports.ResponseStatus||(exports.ResponseStatus={}));var Bus=function(){function Bus(adapter,defaultTimeout){var _this=this;this.id=utils_1.uniqueId("bus");this._timeout=defaultTimeout||5e3;this._adapter=adapter;this._adapter.addListener(function(data){return _this._onMessage(data)});this._eventHandlers=Object.create(null);this._activeRequestHash=Object.create(null);this._requestHandlers=Object.create(null);utils_1.console.info('Create Bus with id "'+this.id+'"')}Bus.prototype.dispatchEvent=function(name,data){this._adapter.send(Bus._createEvent(name,data));utils_1.console.info('Dispatch event "'+name+'"',data);return this};Bus.prototype.request=function(name,data,timeout){var _this=this;return new Promise(function(resolve,reject){var id=utils_1.uniqueId(_this.id+"-action");var wait=timeout||_this._timeout;var timer;if((timeout||_this._timeout)!==-1){timer=setTimeout(function(){delete _this._activeRequestHash[id];var error=new Error('Timeout error for request with name "'+name+'" and timeout '+wait+"!");utils_1.console.error(error);reject(error)},wait)}var cancelTimeout=function(){if(timer){clearTimeout(timer)}};_this._activeRequestHash[id]={reject:function(error){cancelTimeout();utils_1.console.error('Error request with name "'+name+'"',error);reject(error)},resolve:function(data){cancelTimeout();utils_1.console.info('Request with name "'+name+'" success resolved!',data);resolve(data)}};_this._adapter.send({id:id,type:1,name:name,data:data});utils_1.console.info('Request with name "'+name+'"',data)})};Bus.prototype.on=function(name,handler,context){return this._addEventHandler(name,handler,context,false)};Bus.prototype.once=function(name,handler,context){return this._addEventHandler(name,handler,context,true)};Bus.prototype.off=function(name,handler){var _this=this;if(!name){Object.keys(this._eventHandlers).forEach(function(name){return _this.off(name,handler)});return this}if(!this._eventHandlers[name]){return this}if(!handler){this._eventHandlers[name].slice().forEach(function(info){_this.off(name,info.handler)});return this}this._eventHandlers[name]=this._eventHandlers[name].filter(function(info){return info.handler!==handler});if(!this._eventHandlers[name].length){delete this._eventHandlers[name]}return this};Bus.prototype.registerRequestHandler=function(name,handler){if(this._requestHandlers[name]){throw new Error("Duplicate request handler!")}this._requestHandlers[name]=handler;return this};Bus.prototype.unregisterHandler=function(name){if(this._requestHandlers[name]){delete this._requestHandlers[name]}return this};Bus.prototype.changeAdapter=function(adapter){var _this=this;var bus=new Bus(adapter,this._timeout);Object.keys(this._eventHandlers).forEach(function(name){_this._eventHandlers[name].forEach(function(info){if(info.once){bus.once(name,info.handler,info.context)}else{bus.on(name,info.handler,info.context)}})});Object.keys(this._requestHandlers).forEach(function(name){bus.registerRequestHandler(name,_this._requestHandlers[name])});return bus};Bus.prototype.destroy=function(){utils_1.console.info("Destroy Bus");this.off();this._adapter.destroy()};Bus.prototype._addEventHandler=function(name,handler,context,once){if(!this._eventHandlers[name]){this._eventHandlers[name]=[]}this._eventHandlers[name].push({handler:handler,once:once,context:context});return this};Bus.prototype._onMessage=function(message){switch(message.type){case 0:utils_1.console.info('Has event with name "'+message.name+'"',message.data);this._fireEvent(message.name,message.data);break;case 1:utils_1.console.info('Start action with id "'+message.id+'" and name "'+message.name+'"',message.data);this._createResponse(message);break;case 2:utils_1.console.info('Start response with name "'+message.id+'" and status "'+message.status+'"',message.content);this._fireEndAction(message);break}};Bus.prototype._createResponse=function(message){var _this=this;var sendError=function(error){utils_1.console.error(error);_this._adapter.send({id:message.id,type:2,status:1,content:String(error)})};if(!this._requestHandlers[message.name]){sendError(new Error('Has no handler for "'+message.name+'" action!'));return void 0}try{var result=this._requestHandlers[message.name](message.data);if(Bus._isPromise(result)){result.then(function(data){_this._adapter.send({id:message.id,type:2,status:0,content:data})},sendError)}else{this._adapter.send({id:message.id,type:2,status:0,content:result})}}catch(e){sendError(e)}};Bus.prototype._fireEndAction=function(message){if(this._activeRequestHash[message.id]){switch(message.status){case 1:this._activeRequestHash[message.id].reject(message.content);break;case 0:this._activeRequestHash[message.id].resolve(message.content);break}delete this._activeRequestHash[message.id]}};Bus.prototype._fireEvent=function(name,value){if(!this._eventHandlers[name]){return void 0}this._eventHandlers[name]=this._eventHandlers[name].slice().filter(function(handlerInfo){try{handlerInfo.handler.call(handlerInfo.context,value)}catch(e){utils_1.console.warn(e)}return!handlerInfo.once});if(!this._eventHandlers[name].length){delete this._eventHandlers[name]}};Bus._createEvent=function(eventName,data){return{type:0,name:eventName,data:data}};Bus._isPromise=function(some){return some&&some.then&&typeof some.then==="function"};return Bus}();exports.Bus=Bus},{"../utils":9}],4:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var config;(function(config){var console;(function(console){console.LOG_LEVEL={PRODUCTION:0,ERRORS:1,VERBOSE:2};console.logLevel=console.LOG_LEVEL.PRODUCTION;console.methodsData={log:{save:false,logLevel:console.LOG_LEVEL.VERBOSE},info:{save:false,logLevel:console.LOG_LEVEL.VERBOSE},warn:{save:false,logLevel:console.LOG_LEVEL.VERBOSE},error:{save:true,logLevel:console.LOG_LEVEL.ERRORS}}})(console=config.console||(config.console={}))})(config=exports.config||(exports.config={}))},{}],5:[function(require,module,exports){"use strict";function __export(m){for(var p in m)if(!exports.hasOwnProperty(p))exports[p]=m[p]}Object.defineProperty(exports,"__esModule",{value:true});__export(require("./bus/Bus"));__export(require("./adapters/Adapter"));__export(require("./adapters/WindowAdapter"));__export(require("./protocols/WindowProtocol"));__export(require("./config"));__export(require("./utils"))},{"./adapters/Adapter":1,"./adapters/WindowAdapter":2,"./bus/Bus":3,"./config":4,"./protocols/WindowProtocol":6,"./utils":9}],6:[function(require,module,exports){"use strict";var __extends=this&&this.__extends||function(){var extendStatics=function(d,b){extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p]};return extendStatics(d,b)};return function(d,b){extendStatics(d,b);function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)}}();Object.defineProperty(exports,"__esModule",{value:true});var typed_ts_events_1=require("typed-ts-events");var WindowProtocol=function(_super){__extends(WindowProtocol,_super);function WindowProtocol(win,type){var _this=_super.call(this)||this;_this.win=win;_this.handler=function(event){_this.trigger("message",event)};if(type===WindowProtocol.PROTOCOL_TYPES.LISTEN){_this.win.addEventListener("message",_this.handler,false)}return _this}WindowProtocol.prototype.dispatch=function(data){this.win.postMessage(data,"*");return this};WindowProtocol.prototype.destroy=function(){this.win.removeEventListener("message",this.handler,false);this.win=WindowProtocol._fakeWin};WindowProtocol._fakeWin=function(){var empty=function(){return null};return{postMessage:empty,addEventListener:empty,removeEventListener:empty}}();return WindowProtocol}(typed_ts_events_1.EventEmitter);exports.WindowProtocol=WindowProtocol;(function(WindowProtocol){WindowProtocol.PROTOCOL_TYPES={LISTEN:"listen",DISPATCH:"dispatch"}})(WindowProtocol=exports.WindowProtocol||(exports.WindowProtocol={}));exports.WindowProtocol=WindowProtocol},{"typed-ts-events":12}],7:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var UniqPrimitiveCollection=function(){function UniqPrimitiveCollection(list){this.size=0;this.hash=Object.create(null);if(list){list.forEach(this.add,this)}}UniqPrimitiveCollection.prototype.add=function(item){this.hash[item]=true;this.size=Object.keys(this.hash).length;return this};UniqPrimitiveCollection.prototype.has=function(key){return key in this.hash};UniqPrimitiveCollection.prototype.toArray=function(){return Object.keys(this.hash)};return UniqPrimitiveCollection}();exports.UniqPrimitiveCollection=UniqPrimitiveCollection},{}],8:[function(require,module,exports){(function(global){"use strict";var __assign=this&&this.__assign||function(){__assign=Object.assign||function(t){for(var s,i=1,n=arguments.length;i<n;i++){s=arguments[i];for(var p in s)if(Object.prototype.hasOwnProperty.call(s,p))t[p]=s[p]}return t};return __assign.apply(this,arguments)};Object.defineProperty(exports,"__esModule",{value:true});var config_1=require("../../config");var utils_1=require("../utils");var consoleModule=function(root){return root.console}(self||global);var storage=Object.create(null);function addNamespace(type){if(!storage[type]){storage[type]=[]}}function saveEvent(type,args){storage[type].push(args)}function generateConsole(){return utils_1.keys(config_1.config.console.methodsData).reduce(function(api,method){api[method]=function(){var args=[];for(var _i=0;_i<arguments.length;_i++){args[_i]=arguments[_i]}if(config_1.config.console.logLevel<config_1.config.console.methodsData[method].logLevel){if(config_1.config.console.methodsData[method].save){addNamespace(method);saveEvent(method,args)}}else{consoleModule[method].apply(consoleModule,args)}};return api},Object.create(null))}exports.console=__assign({},generateConsole(),{getSavedMessages:function(type){return storage[type]||[]}})}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"../../config":4,"../utils":10}],9:[function(require,module,exports){"use strict";function __export(m){for(var p in m)if(!exports.hasOwnProperty(p))exports[p]=m[p]}Object.defineProperty(exports,"__esModule",{value:true});__export(require("./utils"));__export(require("./console"));__export(require("./UniqPrimitiveCollection"))},{"./UniqPrimitiveCollection":7,"./console":8,"./utils":10}],10:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});function keys(o){return Object.keys(o)}exports.keys=keys;var salt=Math.floor(Date.now()*Math.random());var counter=0;function uniqueId(prefix){return prefix+"-"+salt+"-"+counter++}exports.uniqueId=uniqueId;function toArray(some){return Array.isArray(some)?some:[some]}exports.toArray=toArray;function pipe(){var args=[];for(var _i=0;_i<arguments.length;_i++){args[_i]=arguments[_i]}return function(data){return args.reduce(function(acc,cb){return cb(acc)},data)}}exports.pipe=pipe},{}],11:[function(require,module,exports){"use strict";exports.__esModule=true;var EventEmitter=function(){function EventEmitter(){this._events=Object.create(null)}EventEmitter.prototype.trigger=function(eventName,params){if(this._events[eventName]){this._events[eventName]=this._events[eventName].filter(function(data){try{data.handler.call(data.context,params)}catch(e){}return!data.once});if(!this._events[eventName].length){delete this._events[eventName]}}};EventEmitter.prototype.on=function(eventName,handler,context){this._on(eventName,handler,context,false)};EventEmitter.prototype.once=function(eventName,handler,context){this._on(eventName,handler,context,true)};EventEmitter.prototype.off=function(arg1,arg2){var _this=this;var eventName=typeof arg1==="string"?arg1:null;var handler=typeof arg2==="function"?arg2:typeof arg1==="function"?arg1:null;if(!eventName){Object.keys(this._events).forEach(function(eventName){_this.off(eventName,handler)});return void 0}if(!handler){delete this._events[eventName];return void 0}if(eventName in this._events){this._events[eventName]=this._events[eventName].filter(function(item){return item.handler!==handler})}};EventEmitter.prototype._on=function(eventName,handler,context,once){if(!this._events[eventName]){this._events[eventName]=[]}this._events[eventName].push({handler:handler,context:context,once:once})};return EventEmitter}();exports.EventEmitter=EventEmitter},{}],12:[function(require,module,exports){"use strict";function __export(m){for(var p in m)if(!exports.hasOwnProperty(p))exports[p]=m[p]}exports.__esModule=true;__export(require("./EventEmitter"))},{"./EventEmitter":11}]},{},[5])(5)}); | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.bus=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var Adapter=function(){function Adapter(){}return Adapter}();exports.Adapter=Adapter},{}],2:[function(require,module,exports){"use strict";var __extends=this&&this.__extends||function(){var extendStatics=function(d,b){extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p]};return extendStatics(d,b)};return function(d,b){extendStatics(d,b);function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)}}();var __assign=this&&this.__assign||function(){__assign=Object.assign||function(t){for(var s,i=1,n=arguments.length;i<n;i++){s=arguments[i];for(var p in s)if(Object.prototype.hasOwnProperty.call(s,p))t[p]=s[p]}return t};return __assign.apply(this,arguments)};Object.defineProperty(exports,"__esModule",{value:true});var Adapter_1=require("./Adapter");var __1=require("..");var WindowProtocol_1=require("../protocols/WindowProtocol");var utils_1=require("../utils/utils");var EMPTY_OPTIONS={origins:[],availableChanelId:[]};var WindowAdapter=function(_super){__extends(WindowAdapter,_super);function WindowAdapter(listen,dispatch,options){var _this=_super.call(this)||this;_this.id=__1.uniqueId("wa");_this.callbacks=[];_this.options=WindowAdapter.prepareOptions(options);_this.listen=listen;_this.dispatch=dispatch;_this.listen.forEach(function(protocol){return protocol.on("message",_this.onMessage,_this)});return _this}WindowAdapter.prototype.addListener=function(cb){this.callbacks.push(cb);__1.console.info("WindowAdapter: Add iframe message listener");return this};WindowAdapter.prototype.send=function(data){var message=__assign({},data,{chanelId:this.chanelId});this.dispatch.forEach(function(protocol){return protocol.dispatch(message)});__1.console.info("WindowAdapter: Send message",message);return this};WindowAdapter.prototype.destroy=function(){this.listen.forEach(function(protocol){return protocol.destroy()});this.dispatch.forEach(function(protocol){return protocol.destroy()});__1.console.info("WindowAdapter: Destroy")};WindowAdapter.prototype.onMessage=function(event){if(this.accessEvent(event)){this.callbacks.forEach(function(cb){try{cb(event.data)}catch(e){__1.console.warn("WindowAdapter: Unhandled exception!",e)}})}};WindowAdapter.prototype.accessEvent=function(event){if(typeof event.data!=="object"||event.data.type==null){__1.console.info("WindowAdapter: Block event. Wrong event format!",event.data);return false}if(!this.options.origins.has("*")&&!this.options.origins.has(event.origin)){__1.console.info('SimpleWindowAdapter: Block event by origin "'+event.origin+'"');return false}if(!this.options.availableChanelId.size){return true}var access=!!(event.data.chanelId&&this.options.availableChanelId.has(event.data.chanelId));if(!access){__1.console.info('SimpleWindowAdapter: Block event by chanel id "'+event.data.chanelId+'"')}return access};WindowAdapter.createSimpleWindowAdapter=function(iframe,options){var _this=this;var origin=this.getContentOrigin(iframe);var myOptions=this.prepareOptions(options);var events=[];if(origin){myOptions.origins.add(origin)}var listen=new WindowProtocol_1.WindowProtocol(window,WindowProtocol_1.WindowProtocol.PROTOCOL_TYPES.LISTEN);var handler=function(event){events.push(event)};listen.on("message",handler);return this.getIframeContent(iframe).then(function(win){var dispatch=new WindowProtocol_1.WindowProtocol(win,WindowProtocol_1.WindowProtocol.PROTOCOL_TYPES.DISPATCH);var adapter=new WindowAdapter([listen],[dispatch],_this.unPrepareOptions(myOptions));events.forEach(function(event){adapter.onMessage(event)});listen.off("message",handler);return adapter})};WindowAdapter.prepareOptions=function(options){if(options===void 0){options=EMPTY_OPTIONS}var concat=function(initialValue){return function(list){return list.reduce(function(set,item){return set.add(item)},initialValue)}};var getCollection=function(data,initial){return utils_1.pipe(__1.toArray,concat(initial))(data)};var origins=getCollection(options.origins||[],new __1.UniqPrimitiveCollection([window.location.origin]));var chanelId=getCollection(options.availableChanelId||[],new __1.UniqPrimitiveCollection);return __assign({},options,{origins:origins,availableChanelId:chanelId})};WindowAdapter.unPrepareOptions=function(options){return{origins:options.origins.toArray(),availableChanelId:options.availableChanelId.toArray(),chanelId:options.chanelId}};WindowAdapter.getIframeContent=function(content){if(!content){return Promise.resolve(window.opener||window.parent)}if(!(content instanceof HTMLIFrameElement)){return Promise.resolve(content)}if(content.contentWindow){return Promise.resolve(content.contentWindow)}return new Promise(function(resolve,reject){content.addEventListener("load",function(){return resolve(content.contentWindow)},false);content.addEventListener("error",reject,false)})};WindowAdapter.getContentOrigin=function(content){if(!content){try{return new URL(document.referrer).origin}catch(e){return null}}if(!(content instanceof HTMLIFrameElement)){try{return window.top.origin}catch(e){return null}}try{return new URL(content.src).origin||null}catch(e){return null}};return WindowAdapter}(Adapter_1.Adapter);exports.WindowAdapter=WindowAdapter},{"..":5,"../protocols/WindowProtocol":6,"../utils/utils":10,"./Adapter":1}],3:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var utils_1=require("../utils");var EventType;(function(EventType){EventType[EventType["Event"]=0]="Event";EventType[EventType["Action"]=1]="Action";EventType[EventType["Response"]=2]="Response"})(EventType=exports.EventType||(exports.EventType={}));var ResponseStatus;(function(ResponseStatus){ResponseStatus[ResponseStatus["Success"]=0]="Success";ResponseStatus[ResponseStatus["Error"]=1]="Error"})(ResponseStatus=exports.ResponseStatus||(exports.ResponseStatus={}));var Bus=function(){function Bus(adapter,defaultTimeout){var _this=this;this.id=utils_1.uniqueId("bus");this._timeout=defaultTimeout||5e3;this._adapter=adapter;this._adapter.addListener(function(data){return _this._onMessage(data)});this._eventHandlers=Object.create(null);this._activeRequestHash=Object.create(null);this._requestHandlers=Object.create(null);utils_1.console.info('Create Bus with id "'+this.id+'"')}Bus.prototype.dispatchEvent=function(name,data){this._adapter.send(Bus._createEvent(name,data));utils_1.console.info('Dispatch event "'+name+'"',data);return this};Bus.prototype.request=function(name,data,timeout){var _this=this;return new Promise(function(resolve,reject){var id=utils_1.uniqueId(_this.id+"-action");var wait=timeout||_this._timeout;var timer;if((timeout||_this._timeout)!==-1){timer=setTimeout(function(){delete _this._activeRequestHash[id];var error=new Error('Timeout error for request with name "'+name+'" and timeout '+wait+"!");utils_1.console.error(error);reject(error)},wait)}var cancelTimeout=function(){if(timer){clearTimeout(timer)}};_this._activeRequestHash[id]={reject:function(error){cancelTimeout();utils_1.console.error('Error request with name "'+name+'"',error);reject(error)},resolve:function(data){cancelTimeout();utils_1.console.info('Request with name "'+name+'" success resolved!',data);resolve(data)}};_this._adapter.send({id:id,type:1,name:name,data:data});utils_1.console.info('Request with name "'+name+'"',data)})};Bus.prototype.on=function(name,handler,context){return this._addEventHandler(name,handler,context,false)};Bus.prototype.once=function(name,handler,context){return this._addEventHandler(name,handler,context,true)};Bus.prototype.off=function(name,handler){var _this=this;if(!name){Object.keys(this._eventHandlers).forEach(function(name){return _this.off(name,handler)});return this}if(!this._eventHandlers[name]){return this}if(!handler){this._eventHandlers[name].slice().forEach(function(info){_this.off(name,info.handler)});return this}this._eventHandlers[name]=this._eventHandlers[name].filter(function(info){return info.handler!==handler});if(!this._eventHandlers[name].length){delete this._eventHandlers[name]}return this};Bus.prototype.registerRequestHandler=function(name,handler){if(this._requestHandlers[name]){throw new Error("Duplicate request handler!")}this._requestHandlers[name]=handler;return this};Bus.prototype.unregisterHandler=function(name){if(this._requestHandlers[name]){delete this._requestHandlers[name]}return this};Bus.prototype.changeAdapter=function(adapter){var _this=this;var bus=new Bus(adapter,this._timeout);Object.keys(this._eventHandlers).forEach(function(name){_this._eventHandlers[name].forEach(function(info){if(info.once){bus.once(name,info.handler,info.context)}else{bus.on(name,info.handler,info.context)}})});Object.keys(this._requestHandlers).forEach(function(name){bus.registerRequestHandler(name,_this._requestHandlers[name])});return bus};Bus.prototype.destroy=function(){utils_1.console.info("Destroy Bus");this.off();this._adapter.destroy()};Bus.prototype._addEventHandler=function(name,handler,context,once){if(!this._eventHandlers[name]){this._eventHandlers[name]=[]}this._eventHandlers[name].push({handler:handler,once:once,context:context});return this};Bus.prototype._onMessage=function(message){switch(message.type){case 0:utils_1.console.info('Has event with name "'+message.name+'"',message.data);this._fireEvent(message.name,message.data);break;case 1:utils_1.console.info('Start action with id "'+message.id+'" and name "'+message.name+'"',message.data);this._createResponse(message);break;case 2:utils_1.console.info('Start response with name "'+message.id+'" and status "'+message.status+'"',message.content);this._fireEndAction(message);break}};Bus.prototype._createResponse=function(message){var _this=this;var sendError=function(error){utils_1.console.error(error);_this._adapter.send({id:message.id,type:2,status:1,content:String(error)})};if(!this._requestHandlers[message.name]){sendError(new Error('Has no handler for "'+message.name+'" action!'));return void 0}try{var result=this._requestHandlers[message.name](message.data);if(Bus._isPromise(result)){result.then(function(data){_this._adapter.send({id:message.id,type:2,status:0,content:data})},sendError)}else{this._adapter.send({id:message.id,type:2,status:0,content:result})}}catch(e){sendError(e)}};Bus.prototype._fireEndAction=function(message){if(this._activeRequestHash[message.id]){switch(message.status){case 1:this._activeRequestHash[message.id].reject(message.content);break;case 0:this._activeRequestHash[message.id].resolve(message.content);break}delete this._activeRequestHash[message.id]}};Bus.prototype._fireEvent=function(name,value){if(!this._eventHandlers[name]){return void 0}this._eventHandlers[name]=this._eventHandlers[name].slice().filter(function(handlerInfo){try{handlerInfo.handler.call(handlerInfo.context,value)}catch(e){utils_1.console.warn(e)}return!handlerInfo.once});if(!this._eventHandlers[name].length){delete this._eventHandlers[name]}};Bus._createEvent=function(eventName,data){return{type:0,name:eventName,data:data}};Bus._isPromise=function(some){return some&&some.then&&typeof some.then==="function"};return Bus}();exports.Bus=Bus},{"../utils":9}],4:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var config;(function(config){var console;(function(console){console.LOG_LEVEL={PRODUCTION:0,ERRORS:1,VERBOSE:2};console.logLevel=console.LOG_LEVEL.PRODUCTION;console.methodsData={log:{save:false,logLevel:console.LOG_LEVEL.VERBOSE},info:{save:false,logLevel:console.LOG_LEVEL.VERBOSE},warn:{save:false,logLevel:console.LOG_LEVEL.VERBOSE},error:{save:true,logLevel:console.LOG_LEVEL.ERRORS}}})(console=config.console||(config.console={}))})(config=exports.config||(exports.config={}))},{}],5:[function(require,module,exports){"use strict";function __export(m){for(var p in m)if(!exports.hasOwnProperty(p))exports[p]=m[p]}Object.defineProperty(exports,"__esModule",{value:true});__export(require("./bus/Bus"));__export(require("./adapters/Adapter"));__export(require("./adapters/WindowAdapter"));__export(require("./protocols/WindowProtocol"));__export(require("./config"));__export(require("./utils"))},{"./adapters/Adapter":1,"./adapters/WindowAdapter":2,"./bus/Bus":3,"./config":4,"./protocols/WindowProtocol":6,"./utils":9}],6:[function(require,module,exports){"use strict";var __extends=this&&this.__extends||function(){var extendStatics=function(d,b){extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p]};return extendStatics(d,b)};return function(d,b){extendStatics(d,b);function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)}}();Object.defineProperty(exports,"__esModule",{value:true});var typed_ts_events_1=require("typed-ts-events");var WindowProtocol=function(_super){__extends(WindowProtocol,_super);function WindowProtocol(win,type){var _this=_super.call(this)||this;_this.win=win;_this.handler=function(event){_this.trigger("message",event)};if(type===WindowProtocol.PROTOCOL_TYPES.LISTEN){_this.win.addEventListener("message",_this.handler,false)}return _this}WindowProtocol.prototype.dispatch=function(data){this.win.postMessage(data,"*");return this};WindowProtocol.prototype.destroy=function(){this.win.removeEventListener("message",this.handler,false);this.win=WindowProtocol._fakeWin};WindowProtocol._fakeWin=function(){var empty=function(){return null};return{postMessage:empty,addEventListener:empty,removeEventListener:empty}}();return WindowProtocol}(typed_ts_events_1.EventEmitter);exports.WindowProtocol=WindowProtocol;(function(WindowProtocol){WindowProtocol.PROTOCOL_TYPES={LISTEN:"listen",DISPATCH:"dispatch"}})(WindowProtocol=exports.WindowProtocol||(exports.WindowProtocol={}));exports.WindowProtocol=WindowProtocol},{"typed-ts-events":12}],7:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var UniqPrimitiveCollection=function(){function UniqPrimitiveCollection(list){this.size=0;this.hash=Object.create(null);if(list){list.forEach(this.add,this)}}UniqPrimitiveCollection.prototype.add=function(item){this.hash[item]=true;this.size=Object.keys(this.hash).length;return this};UniqPrimitiveCollection.prototype.has=function(key){return key in this.hash};UniqPrimitiveCollection.prototype.toArray=function(){return Object.keys(this.hash)};return UniqPrimitiveCollection}();exports.UniqPrimitiveCollection=UniqPrimitiveCollection},{}],8:[function(require,module,exports){(function(global){"use strict";var __assign=this&&this.__assign||function(){__assign=Object.assign||function(t){for(var s,i=1,n=arguments.length;i<n;i++){s=arguments[i];for(var p in s)if(Object.prototype.hasOwnProperty.call(s,p))t[p]=s[p]}return t};return __assign.apply(this,arguments)};Object.defineProperty(exports,"__esModule",{value:true});var config_1=require("../../config");var utils_1=require("../utils");var consoleModule=function(root){return root.console}(self||global);var storage=Object.create(null);function addNamespace(type){if(!storage[type]){storage[type]=[]}}function saveEvent(type,args){storage[type].push(args)}function generateConsole(){return utils_1.keys(config_1.config.console.methodsData).reduce(function(api,method){api[method]=function(){var args=[];for(var _i=0;_i<arguments.length;_i++){args[_i]=arguments[_i]}if(config_1.config.console.logLevel<config_1.config.console.methodsData[method].logLevel){if(config_1.config.console.methodsData[method].save){addNamespace(method);saveEvent(method,args)}}else{consoleModule[method].apply(consoleModule,args)}};return api},Object.create(null))}exports.console=__assign({},generateConsole(),{getSavedMessages:function(type){return storage[type]||[]}})}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"../../config":4,"../utils":10}],9:[function(require,module,exports){"use strict";function __export(m){for(var p in m)if(!exports.hasOwnProperty(p))exports[p]=m[p]}Object.defineProperty(exports,"__esModule",{value:true});__export(require("./utils"));__export(require("./console"));__export(require("./UniqPrimitiveCollection"))},{"./UniqPrimitiveCollection":7,"./console":8,"./utils":10}],10:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});function keys(o){return Object.keys(o)}exports.keys=keys;var salt=Math.floor(Date.now()*Math.random());var counter=0;function uniqueId(prefix){return prefix+"-"+salt+"-"+counter++}exports.uniqueId=uniqueId;function toArray(some){return Array.isArray(some)?some:[some]}exports.toArray=toArray;function pipe(){var args=[];for(var _i=0;_i<arguments.length;_i++){args[_i]=arguments[_i]}return function(data){return args.reduce(function(acc,cb){return cb(acc)},data)}}exports.pipe=pipe},{}],11:[function(require,module,exports){"use strict";exports.__esModule=true;var EventEmitter=function(){function EventEmitter(){this._events=Object.create(null)}EventEmitter.prototype.trigger=function(eventName,params){if(this._events[eventName]){this._events[eventName]=this._events[eventName].filter(function(data){try{data.handler.call(data.context,params)}catch(e){}return!data.once});if(!this._events[eventName].length){delete this._events[eventName]}}};EventEmitter.prototype.on=function(eventName,handler,context){this._on(eventName,handler,context,false)};EventEmitter.prototype.once=function(eventName,handler,context){this._on(eventName,handler,context,true)};EventEmitter.prototype.off=function(arg1,arg2){var _this=this;var eventName=typeof arg1==="string"?arg1:null;var handler=typeof arg2==="function"?arg2:typeof arg1==="function"?arg1:null;if(!eventName){Object.keys(this._events).forEach(function(eventName){_this.off(eventName,handler)});return void 0}if(!handler){delete this._events[eventName];return void 0}if(eventName in this._events){this._events[eventName]=this._events[eventName].filter(function(item){return item.handler!==handler})}};EventEmitter.prototype._on=function(eventName,handler,context,once){if(!this._events[eventName]){this._events[eventName]=[]}this._events[eventName].push({handler:handler,context:context,once:once})};return EventEmitter}();exports.EventEmitter=EventEmitter},{}],12:[function(require,module,exports){"use strict";function __export(m){for(var p in m)if(!exports.hasOwnProperty(p))exports[p]=m[p]}exports.__esModule=true;__export(require("./EventEmitter"))},{"./EventEmitter":11}]},{},[5])(5)}); |
@@ -21,2 +21,3 @@ declare namespace AnalyticsTypes { | ||
initializeMethod: string; | ||
initializeOptions?: any; | ||
sendMethod: string; | ||
@@ -23,0 +24,0 @@ type: TTargetTypes; |
{ | ||
"name": "@waves/event-sender", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "An interface for sending analytics events from Waves JS applications", | ||
@@ -5,0 +5,0 @@ "main": "dist/analytics.js", |
Sorry, the diff of this file is not supported yet
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
31709
263