Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to get example worker: help needed #4

Open
Scnck opened this issue Feb 25, 2021 · 0 comments
Open

unable to get example worker: help needed #4

Scnck opened this issue Feb 25, 2021 · 0 comments

Comments

@Scnck
Copy link

Scnck commented Feb 25, 2021

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>
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant