This repository has been archived by the owner on Nov 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
135 lines (121 loc) · 2.77 KB
/
worker.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Create a fake worker thread of IE and other browsers
Remember: Only pass in primitives, and there is none of the native
security happening
*/
if(!Worker)
{
var Worker = function ( scriptFile )
{
var self = this ;
var __timer = null ;
var __text = null ;
var __fileContent = null ;
var onmessage ;
self.onerror = null ;
self.onmessage = null ;
// child has run itself and called for it's parent to be notified
var postMessage = function( text )
{
if ( "function" == typeof self.onmessage )
{
return self.onmessage( { "data" : text } ) ;
}
return false ;
} ;
// Method that starts the threading
self.postMessage = function( text )
{
__text = text ;
__iterate() ;
return true ;
} ;
var __iterate = function()
{
// Execute on a timer so we dont block (well as good as we can get in a single thread)
__timer = setTimeout(__onIterate,1);
return true ;
} ;
var __onIterate = function()
{
try
{
if ( "function" == typeof onmessage )
{
onmessage({ "data" : __text });
}
return true ;
}
catch( ex )
{
if ( "function" == typeof self.onerror )
{
return self.onerror( ex ) ;
}
}
return false ;
} ;
self.terminate = function ()
{
clearTimeout( __timer ) ;
return true ;
} ;
/* HTTP Request*/
var getHTTPObject = function ()
{
var xmlhttp;
try
{
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
xmlhttp = false;
}
}
return xmlhttp;
}
var importScripts = function(src)
{
// hack time, this will import the script but not wait for it to load...
var script = document.createElement("SCRIPT") ;
script.src = src ;
script.setAttribute( "type", "text/javascript" ) ;
document.getElementsByTagName("HEAD")[0].appendChild(script)
return true ;
} ;
var http = getHTTPObject()
http.open("GET", scriptFile, false)
http.send(null);
if (http.readyState == 4)
{
var strResponse = http.responseText;
//var strResponse = http.responseXML;
switch (http.status)
{
case 404: // Page-not-found error
alert('Error: Not Found. The requested function could not be found.');
break;
case 500: // Display results in a full window for server-side errors
alert(strResponse);
break;
default:
__fileContent = strResponse ;
// IE functions will become delagates of the instance of Worker
eval( __fileContent ) ;
/*
at this point we now have:
a delagate "onmessage(event)"
*/
break;
}
}
return true ;
} ;
}