{"size": 1268, "ext": "js", "max_stars_repo_path": "powerCords/browser/SSAppPowerCord.js", "max_stars_repo_name": "PrivateSky/swarm-engine", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "powerCords/browser/SSAppPowerCord.js", "max_issues_repo_name": "PrivateSky/swarm-engine", "max_issues_repo_issues_event_min_datetime": "2021-11-26T12:59:46.000Z", "max_issues_repo_issues_event_max_datetime": "2021-11-26T12:59:46.000Z", "max_forks_repo_path": "powerCords/browser/SSAppPowerCord.js", "max_forks_repo_name": "PrivateSky/swarm-engine", "max_forks_repo_forks_event_min_datetime": "2020-08-24T12:00:15.000Z", "max_forks_repo_forks_event_max_datetime": "2020-09-01T08:30:04.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 2, "avg_line_length": 27.5652173913, "max_line_length": 110, "alphanum_fraction": 0.7011041009}
/*
	This type of PowerCord can be used from outer and inner SSApp in order to facilitate the SWARM communication
	@param reference can be the parent (SSApp or wallet environment) or the iframe in which the SSApp gets loaded
*/
function SSAppPowerCord(reference){

	this.sendSwarm = function (swarmSerialization){
		//console.log("Sending swarm using", reference);
		reference.postMessage(swarmSerialization, "*");
	};

	let receivedMessageHandler  = (event)=>{
		console.log("SSAppPowerCord caught event", event);
		/*if(event.source !== reference){
			console.log("Not my message to handle");
			return;
		}
		console.log("Message received from ssapp", event.source);
		*/
		let swarmSerialization = event.data;
		this.transfer(swarmSerialization);
	};

	let setupConnection = () => {
		if(typeof window.powerCordHandler === "undefined"){
			//console.log("SSAPP PC listener set up");
			window.powerCordHandler = receivedMessageHandler;
			window.addEventListener("message", window.powerCordHandler);
		}else{
			//console.log("SSAPP handler already set.");
		}
	};

	return new Proxy(this, {
		set(target, p, value, receiver) {
			target[p] = value;
			if(p === 'identity') {
				setupConnection();
			}
			return true;
		}
	});
}

module.exports = SSAppPowerCord;
