{"size": 1257, "ext": "js", "max_stars_repo_path": "src/shopwrker.js", "max_stars_repo_name": "leeoniya/sweatshop.js", "max_stars_repo_stars_event_min_datetime": "2021-08-25T04:49:41.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-25T04:49:41.000Z", "max_issues_repo_path": "src/shopwrker.js", "max_issues_repo_name": "leeoniya/sweatshop.js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/shopwrker.js", "max_forks_repo_name": "leeoniya/sweatshop.js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 18.7611940299, "max_line_length": 70, "alphanum_fraction": 0.6237072395}
/*
* Copyright (c) 2013, Leon Sorokin
* All rights reserved. (MIT Licensed)
*
* sweatshop.js - parallel processing via web workers
*/

function OkMsg(data, extra) {
	this.jsonrpc = "2.0";
	this.result = data;
	this.id = null;

	// non-standard out-of-band data (like error.data in error responses)
	// use this for debug stuff, etc...
	this.extra = {};

	if (extra) {
		for (var i in extra)
			this.extra[i] = extra[i];
	}
}

function ErrMsg(code, message, data) {
	this.jsonrpc = "2.0";
	this.error = {
		code: code,
		message: message,
		data: data,
	};
	this.id = null;
}

self.onmessage = function(e) {
	var msg,
		d = e.data,
		t = Date.now();

	if (methods[d.method]) {
		try {
			var result = methods[d.method].apply(self, d.params);

			if (!(result instanceof OkMsg || result instanceof ErrMsg))
				msg = new OkMsg(result);
			else
				msg = result;
		}
		catch(e) {
			// assume generic error
			// TODO: switch on Error class and translate to RPC error codes
			if (e instanceof Error)
				msg = new ErrMsg(-32603, e.message);
		}
	}
	else
		msg = new ErrMsg(-32601, "Method '" + d.method + "' not found");

	msg.id = d.id;

	if (msg instanceof OkMsg)
		msg.extra.time	= Date.now() - t;

	postMessage(msg);
};

// callable methods
methods = {};
