forked from persvr/pintura
-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker-grid.js
54 lines (52 loc) · 1.75 KB
/
worker-grid.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
/**
* This store funnels requests from multiple workers to a single store instance in a shared worker. This is
* important for stores that must be single threaded and/or share memory.
*/
var Remote = require("store/remote").Remote,
when = require("promised-io/promise").when,
workerClient = require("jsgi-worker").client,
Replicated = require("store/replicated").Replicated,
replicas = [];
try{
// TODO: Check to see if we really need to create a new shared worker if the request is not multi-process
// try to get a shared worker if it is available
var SharedWorker = require("worker").SharedWorker;
// TODO: Use an existing worker like the HTTP server or something if we can to avoid creating extra workers
var trackWorkers = new SharedWorker("util/track-workers", "track-workers");
trackWorkers.port.postMessage({
method:"subscribe",
name: name
});
trackWorkers.port.onmessage = function(e){
replicas.push(Remote(workerClient(e.port)));
};
}
catch(e){
// not all platforms have shared workers, proceed anyway
}
exports.WorkerGrid = function(store, id){
if(!SharedWorker){
return store;
}
var restStoreApp = require("../jsgi/error").ErrorHandler(
require("../jsgi/transactional").Transactional(require("stores"),
require("../jsgi/rest-store").RestStore({})));
global.onconnect = function (e) {
e.port.onmessage = function(event){
var request = event.data;
request.store = store;
request.jsgi = {version: [0,3],
errors: system.stderr,
multithread: false,
multiprocess: true,
async: true,
runOnce: false
};
when(restStoreApp(request),
function(){
e.port.postMessage
}, e.port.postMessage);
};
};
return Replicated(store, replicas);
};