1 |
/** |
2 |
* Joose.Gears is a meta class for classes that want to delegate work to gears workers |
3 |
* @name Joose.Gears |
4 |
* @extends Joose.Class |
5 |
* @constructor |
6 |
*/ |
7 |
Class("Joose.Gears", { |
8 |
isa: Joose.Class, |
9 |
has: { |
10 |
wp: { }, |
11 |
calls: { init: {} }, |
12 |
callIndex: { init: 0 } |
13 |
}, |
14 |
|
15 |
methods: { |
16 |
initialize: function () { |
17 |
JooseGearsInitializeGears() |
18 |
if(this.canGears()) { |
19 |
this.wp = google.gears.factory.create('beta.workerpool'); |
20 |
var me = this; |
21 |
this.wp.onmessage = function (a,b,message) { |
22 |
me.handleGearsMessage(message) |
23 |
} |
24 |
} |
25 |
}, |
26 |
handleGearsMessage: function (message) { |
27 |
var paras = JSON.parse(message.text); |
28 |
var cbName = paras.to; |
29 |
var ret = paras.ret; |
30 |
var object = this.calls[paras.index]; |
31 |
object[cbName].call(object, ret) |
32 |
delete this.calls[paras.index] |
33 |
}, |
34 |
|
35 |
canGears: function () { |
36 |
return window.google && window.google.gears && window.google.gears.factory |
37 |
}, |
38 |
|
39 |
/** |
40 |
* Adds a worker to the class |
41 |
* @function |
42 |
* @name addWorker |
43 |
* @param {string} Name of the worker |
44 |
* @param {function} Function body of the worker |
45 |
* @param {props} Optional properties for the created method (ignored) |
46 |
* @memberof Joose.Gears |
47 |
*/ |
48 |
addWorker: function (name, func, props) { |
49 |
|
50 |
var cbName = "on"+Joose.S.uppercaseFirst(name) |
51 |
|
52 |
// No gears, then work inline |
53 |
if(!this.canGears()) { |
54 |
var wrapped = function () { |
55 |
var ret = func.apply(this, arguments); |
56 |
this[cbName].call(this, ret) |
57 |
} |
58 |
this.addMethod(name, wrapped, props) |
59 |
return |
60 |
} |
61 |
|
62 |
// OK, we have gears support |
63 |
|
64 |
var json = new Joose.SimpleRequest().getText("json2.js") |
65 |
|
66 |
var source = "function aClass () {}; aClass.prototype."+name+" = "+func.toString()+"\n\n"+ |
67 |
"var wp = google.gears.workerPool\n" + |
68 |
"wp.onmessage = function (a,b,message) {\n"+ |
69 |
"var paras = JSON.parse(message.text)\n"+ |
70 |
"var o = new aClass(); var ret = o."+name+".apply(o, paras.args); wp.sendMessage(JSON.stringify({ ret: ret, to: paras.cbName, index: paras.index }), message.sender)"+ |
71 |
"\n}\n\n"; |
72 |
|
73 |
|
74 |
source += json |
75 |
|
76 |
var wp = this.wp; |
77 |
|
78 |
var childId = wp.createWorker(source) |
79 |
|
80 |
var me = this |
81 |
|
82 |
var wrapped = function () { |
83 |
var message = JSON.stringify({ args: arguments, cbName: cbName, index: me.callIndex }) |
84 |
wp.sendMessage(message, childId); |
85 |
me.calls[me.callIndex] = this |
86 |
me.callIndex++ |
87 |
|
88 |
} |
89 |
this.addMethod(name, wrapped, props) |
90 |
|
91 |
} |
92 |
} |
93 |
}) |
94 |
|
95 |
// Copyright 2007, Google Inc. |
96 |
// |
97 |
// Redistribution and use in source and binary forms, with or without |
98 |
// modification, are permitted provided that the following conditions are met: |
99 |
// |
100 |
// 1. Redistributions of source code must retain the above copyright notice, |
101 |
// this list of conditions and the following disclaimer. |
102 |
// 2. Redistributions in binary form must reproduce the above copyright notice, |
103 |
// this list of conditions and the following disclaimer in the documentation |
104 |
// and/or other materials provided with the distribution. |
105 |
// 3. Neither the name of Google Inc. nor the names of its contributors may be |
106 |
// used to endorse or promote products derived from this software without |
107 |
// specific prior written permission. |
108 |
// |
109 |
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
110 |
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
111 |
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
112 |
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
113 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
114 |
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
115 |
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
116 |
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
117 |
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
118 |
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
119 |
// |
120 |
// Sets up google.gears.*, which is *the only* supported way to access Gears. |
121 |
// |
122 |
// Circumvent this file at your own risk! |
123 |
// |
124 |
// In the future, Gears may automatically define google.gears.* without this |
125 |
// file. Gears may use these objects to transparently fix bugs and compatibility |
126 |
// issues. Applications that use the code below will continue to work seamlessly |
127 |
// when that happens. |
128 |
|
129 |
// Sorry Google for modifying this :) |
130 |
function JooseGearsInitializeGears() { |
131 |
// We are already defined. Hooray! |
132 |
if (window.google && google.gears) { |
133 |
return; |
134 |
} |
135 |
|
136 |
var factory = null; |
137 |
|
138 |
// Firefox |
139 |
if (typeof GearsFactory != 'undefined') { |
140 |
factory = new GearsFactory(); |
141 |
} else { |
142 |
// IE |
143 |
try { |
144 |
factory = new ActiveXObject('Gears.Factory'); |
145 |
// privateSetGlobalObject is only required and supported on WinCE. |
146 |
if (factory.getBuildInfo().indexOf('ie_mobile') != -1) { |
147 |
factory.privateSetGlobalObject(this); |
148 |
} |
149 |
} catch (e) { |
150 |
// Safari |
151 |
if (navigator.mimeTypes["application/x-googlegears"]) { |
152 |
factory = document.createElement("object"); |
153 |
factory.style.display = "none"; |
154 |
factory.width = 0; |
155 |
factory.height = 0; |
156 |
factory.type = "application/x-googlegears"; |
157 |
document.documentElement.appendChild(factory); |
158 |
} |
159 |
} |
160 |
} |
161 |
|
162 |
// *Do not* define any objects if Gears is not installed. This mimics the |
163 |
// behavior of Gears defining the objects in the future. |
164 |
if (!factory) { |
165 |
return; |
166 |
} |
167 |
|
168 |
// Now set up the objects, being careful not to overwrite anything. |
169 |
// |
170 |
// Note: In Internet Explorer for Windows Mobile, you can't add properties to |
171 |
// the window object. However, global objects are automatically added as |
172 |
// properties of the window object in all browsers. |
173 |
if (!window.google) { |
174 |
google = {}; |
175 |
} |
176 |
|
177 |
if (!google.gears) { |
178 |
google.gears = {factory: factory}; |
179 |
} |
180 |
} |
181 |
|
182 |
|