foam-framework
Version:
MVC metaprogramming framework
120 lines (114 loc) • 3.23 kB
JavaScript
/**
* @license
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
CLASS({
name: 'WorkerDelegate',
package: 'foam.core.dao',
help: 'The client side of a web-worker DAO',
properties: [
{
name: 'dao',
label: 'DAO',
type: 'DAO',
required: 'true',
postSet: function(oldVal, val) {
if (oldVal) oldVal.unlisten(this);
val.listen(this);
}
}
],
methods: {
init: function() {
this.SUPER();
self.addEventListener('message', this.onMessage);
},
put: function(obj) {
self.postMessage({
method: "put",
obj: ObjectToJSON.visitObject(obj)
});
},
remove: function(obj) {
self.postMessage({
method: "remove",
key: obj.id
});
}
},
listeners: [
{
name: 'onMessage',
code: function(e) {
// This is a nightmare of a function, clean it up.
var message = e.data;
if ( !message.method ) return;
var me = this;
var params = message.params.model_ ?
JSONToObject.visitObject(message.params) :
message.params;
if (message.method == "put") {
this.dao.put(params, {
put: function() {
self.postMessage({
request: message.request
});
},
error: function() {
self.postMessage({
request: message.request,
error: true
});
}
});
} else if(message.method == "remove") {
this.dao.remove(params, {
remove: function() {
self.postMessage({
request: message.request
});
},
error: function() {
self.postMessage({
request: message.request,
error: true
});
}
});
} else if(message.method == "select") {
var request = JSONToObject.visit(message.params);
var mysink = {
__proto__: request.sink,
eof: function() {
request.sink.eof && request.sink.eof();
self.postMessage({
request: message.request,
sink: ObjectToJSON.visit(this.__proto__)
});
},
error: function() {
request.sink.error && request.sink.error();
self.postMessage({
request: message.request,
error: true
});
}
};
this.dao.select(mysink, request.options);
}
}
}
]
});