{"size": 2279, "ext": "js", "max_stars_repo_path": "src/worker.js", "max_stars_repo_name": "activewidgets/systemjs-unpkg-config", "max_stars_repo_stars_event_min_datetime": "2017-05-30T23:13:09.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-20T14:15:06.000Z", "max_issues_repo_path": "src/worker.js", "max_issues_repo_name": "activewidgets/systemjs-unpkg-config", "max_issues_repo_issues_event_min_datetime": "2017-06-07T12:31:16.000Z", "max_issues_repo_issues_event_max_datetime": "2019-05-20T18:08:18.000Z", "max_forks_repo_path": "src/worker.js", "max_forks_repo_name": "activewidgets/systemjs-unpkg-config", "max_forks_repo_forks_event_min_datetime": "2017-06-09T08:15:56.000Z", "max_forks_repo_forks_event_max_datetime": "2019-08-28T17:22:27.000Z"}
{"max_stars_count": 184, "max_issues_count": 11, "max_forks_count": 16, "avg_line_length": 17.2651515152, "max_line_length": 83, "alphanum_fraction": 0.5397103993}
﻿
define('getlibs/src/worker', [], function(){


	function remote(){

		var port = self;

		self.onmessage = function(event){

			try {
				var request = {};
				request = JSON.parse(event.data);

				port.postMessage(JSON.stringify({
					id: request.id,
					result: self[request.method].apply(self, request.args)
				}));
			}
			catch(err){
				port.postMessage(JSON.stringify({
					id: request.id,
					error: {
						message: err.message,
						stack: err.stack
					}
				}));
			}
		};


		self.onconnect = function(event){
			port = event.ports[0];
			port.onmessage = self.onmessage;
		};
	}


	function createWorker(scripts){

		var source = scripts.concat(remote).map(function(item){

			if (typeof item == 'string'){
				return 'importScripts(' + JSON.stringify(System.normalizeSync(item)) + ');\n';
			}

			if (typeof item == 'function'){
				return '(' + item + ')();\n';
			}
		});

		var blob = new Blob(source, {type: 'application/javascript'});

		return new Worker(URL.createObjectURL(blob));
	}


	return function(scripts){

		var serial = 0,
			tasks = [],
			error = null;


		var worker = createWorker(scripts);


		function handleError(err){

			error = err;

			tasks.forEach(function(task, i){
				if (task) {
					tasks[i] = null;
					task.failure(err);
				}
			});
		}

		worker.onerror = function(event){
			handleError(new Error(event.message, event.filename, event.lineno));
		};


		worker.onmessage = function(e){
			try {
				var msg = JSON.parse(e.data),
					task = tasks[msg.id];

				tasks[msg.id] = null;

				if (msg.error) {
					task.failure(msg.error);
				}
				else {
					task.success(msg.result);
				}
			}
			catch(err){
				handleError(err);
			}
		};


		this.call = function(method){

			if (error){
				return Promise.reject(error);
			}

			var i, args = [], id = ++serial;

			for(i=1; i<arguments.length; i++){
				args[i-1] = arguments[i];
			}

			return new Promise(function(success, failure){

				tasks[id] = {
					success: success,
					failure: failure
				};

				worker.postMessage(JSON.stringify({
					id: id,
					method: method,
					args: args
				}));
			});
		};
	};
});