softvar/super-workers

unable to get example worker: help needed

Scnck opened this issue · 0 comments

Scnck commented

unable to gets this example running any help would be appreciated. Thank you.

`

<title>Super Workers | Home</title> Start a new low-priority task Start a new mediun-priority task Start a new high-priority task
<br />
<button id="get-free">Get next task</button>

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/softvar/super-workers@master/dist/super-workers.min.js"></script>
<script type="text/javascript">

	document.addEventListener('DOMContentLoaded', function(event) {
		var blob = new Blob([document.querySelector('#worker').textContent]);
		blobURL = window.URL.createObjectURL(blob);

		var main = window.main = new SuperWorkers.MainThread({
			url: blobURL,
			maxWorkers: 3, // hardwareConcurrency
			minWorkers: 1,
			killWorkersAfterJob: true
		});

		var prom;


		function add(a, b) {
			var p = SuperWorkers.Promise.defer();

			// p.promise.timeout(1000)
			setTimeout(function () {
				return p.resolve(a + b);
			}, 2000);

			// p.promise.cancel();
			return p.promise;
		}


		document.getElementById('open-low').addEventListener('click', function () {
			prom = window.main.exec('add', [1,2], {
				priority: 'low',
				name: 'add-low'
			}).then(function (val) {
				console.log(val);
			}).catch(function (err) {
				console.log(err)
			});

			/*window.main.workers[0].worker.postMessage({
				id: window.main.workers[0].id,
				action: 'handshake',
				message: 'Hello'
			});*/
		}, false);
		document.getElementById('open-medium').addEventListener('click', function () {
			prom = window.main.exec(add, [2, 3], {
				priority: 'medium',
				name: 'add-medium'
			}).then(function (val) {
				console.log(val);
			}).catch(function (err) {
				console.log(err)
			});
		}, false);
		document.getElementById('open-high').addEventListener('click', function () {
			prom = window.main.exec(add, [4, 5], {
				priority: 'high',
				name: 'add-high'
			}).then(function (val) {
				console.log(val);
			}).catch(function (err) {
				console.log(err)
			});
		}, false);
		document.getElementById('get-free').addEventListener('click', function () {
			console.log(window.main.taskQueue.getNextTask());
		}, false);
	});
</script>
<script id="worker" type="javascript/worker">
	importScripts('https://cdn.jsdelivr.net/gh/softvar/super-workers@master/dist/super-workers.min.js');

	var child = new SuperWorkers.WorkerThread();

	child.exposeMethods({
		add: function (a, b) {
			return a + b;
		}
	});
</script>
`