mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
deploy dev version
This commit is contained in:
32
dev/js/worker/hash/Hash.js
Normal file
32
dev/js/worker/hash/Hash.js
Normal file
@@ -0,0 +1,32 @@
|
||||
(function () {
|
||||
var ns = $.namespace('pskl.worker.hash');
|
||||
|
||||
ns.Hash = function (str, onSuccess, onStep, onError) {
|
||||
this.str = str;
|
||||
|
||||
this.onStep = onStep;
|
||||
this.onSuccess = onSuccess;
|
||||
this.onError = onError;
|
||||
|
||||
this.worker = pskl.utils.WorkerUtils.createWorker(ns.HashWorker, 'hash');
|
||||
this.worker.onmessage = this.onWorkerMessage.bind(this);
|
||||
};
|
||||
|
||||
ns.Hash.prototype.process = function () {
|
||||
this.worker.postMessage({
|
||||
str : this.str
|
||||
});
|
||||
};
|
||||
|
||||
ns.Hash.prototype.onWorkerMessage = function (event) {
|
||||
if (event.data.type === 'STEP') {
|
||||
this.onStep(event);
|
||||
} else if (event.data.type === 'SUCCESS') {
|
||||
this.onSuccess(event);
|
||||
this.worker.terminate();
|
||||
} else if (event.data.type === 'ERROR') {
|
||||
this.onError(event);
|
||||
this.worker.terminate();
|
||||
}
|
||||
};
|
||||
})();
|
||||
34
dev/js/worker/hash/HashWorker.js
Normal file
34
dev/js/worker/hash/HashWorker.js
Normal file
@@ -0,0 +1,34 @@
|
||||
(function () {
|
||||
var ns = $.namespace('pskl.worker.hash');
|
||||
|
||||
ns.HashWorker = function () {
|
||||
var hashCode = function(str) {
|
||||
var hash = 0;
|
||||
if (str.length !== 0) {
|
||||
for (var i = 0, l = str.length; i < l; i++) {
|
||||
var chr = str.charCodeAt(i);
|
||||
hash = ((hash << 5) - hash) + chr;
|
||||
hash |= 0; // Convert to 32bit integer
|
||||
}
|
||||
}
|
||||
return hash;
|
||||
};
|
||||
|
||||
this.onmessage = function(event) {
|
||||
try {
|
||||
var data = event.data;
|
||||
var str = data.str;
|
||||
var hash = hashCode(str);
|
||||
this.postMessage({
|
||||
type : 'SUCCESS',
|
||||
hash : hash
|
||||
});
|
||||
} catch (e) {
|
||||
this.postMessage({
|
||||
type : 'ERROR',
|
||||
message : e.message
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user