-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.js
68 lines (51 loc) · 1.53 KB
/
js.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var myApp = angular.module('myApp',[]);
myApp.factory('workerFunctionFactory', function () {
'use strict';
function workerFunction() {
var self = this;
self.onmessage = function(event) {
var splitedString = event.data.split(',');
self.postMessage(splitedString);
}
};
return {
getWebworkerFunction: function () {
return workerFunction;
}
}
});
myApp.factory('workerFactory', function ($window) {
'use strict';
function createWorker(workerFunction) {
var dataObj = '(' + workerFunction + ')();';
var blob = new $window.Blob([dataObj.replace('"use strict";', '')]);
var blobURL = ($window.URL ? URL : webkitURL).createObjectURL(blob, {
type: 'application/javascript; charset=utf-8'
});
return new Worker(blobURL);
};
return {
createWorker: createWorker
}
});
myApp.controller('MyCtrl', function MyCtrl($timeout, workerFactory, workerFunctionFactory) {
'use strict';
var worker;
var self = this;
this.inputText = '';
this.data = '';
this.sendMessage = function () {
worker.postMessage(self.inputText)
};
function setData(data) {
self.data = data;
}
(function init() {
worker = workerFactory.createWorker(workerFunctionFactory.getWebworkerFunction());
worker.onmessage = function (message) {
$timeout(function () {
setData(message.data)
}, 0);
}
}());
});