github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/server/camlistored/ui/worker_message_router.js (about)

     1  /*
     2  Copyright 2014 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  goog.provide('cam.WorkerMessageRouter');
    18  
    19  goog.require('goog.string');
    20  
    21  // Convenience for sending request/response style messages to and from workers.
    22  // @param {!Worker} worker The DOM worker to wrap.
    23  // @constructor
    24  cam.WorkerMessageRouter = function(worker) {
    25  	this.worker_ = worker;
    26  	this.nextMessageId_ = 1;
    27  
    28  	// name->handler - See registerHandler()
    29  	// @type Object.<string, function(*, function(*))>
    30  	this.handlers_ = {};
    31  
    32  	// messageid->callback - See sendMessage()
    33  	// @type Object.<number, function(*)>
    34  	this.pendingMessages_ = {};
    35  
    36  	this.worker_.addEventListener('message', this.handleMessage_.bind(this));
    37  };
    38  
    39  // Send a message over the worker, optionally expecting a response.
    40  // @param {!string} name The name of the message to send.
    41  // @param {!*} msg The message content
    42  // @param {?function(*)} opt_callback The function to receive the response.
    43  cam.WorkerMessageRouter.prototype.sendMessage = function(name, msg, opt_callback) {
    44  	var messageId = 0;
    45  	if (opt_callback) {
    46  		messageId = this.nextMessageId_++;
    47  		this.pendingMessages_[messageId] = opt_callback;
    48  	}
    49  	this.worker_.postMessage({
    50  		messageId: messageId,
    51  		name: name,
    52  		message: msg
    53  	});
    54  };
    55  
    56  // Registers a function to handle a particular named message type.
    57  // @param {!string} name The name of the message type to handle.
    58  // @param {!function(*, function(*))} handler The function to call to return the reply to the client.
    59  cam.WorkerMessageRouter.prototype.registerHandler = function(name, handler) {
    60  	this.handlers_[name] = handler;
    61  };
    62  
    63  cam.WorkerMessageRouter.prototype.handleMessage_ = function(e) {
    64  	if (!goog.isObject(e.data) || !goog.isDef(e.data.messageId)) {
    65  		return;
    66  	}
    67  
    68  	if (goog.isDef(e.data.name)) {
    69  		this.handleRequest_(e.data);
    70  	} else {
    71  		this.handleReply_(e.data);
    72  	}
    73  };
    74  
    75  cam.WorkerMessageRouter.prototype.handleRequest_ = function(request) {
    76  	var handler = this.handlers_[request.name];
    77  	if (!handler) {
    78  		throw new Error(goog.string.subs('No registered handler with name: %s', request.name));
    79  	}
    80  
    81  	var sendReply = function(reply) {
    82  		if (!request.messageId) {
    83  			return;
    84  		}
    85  		this.worker_.postMessage({
    86  			messageId: request.messageId,
    87  			message: reply
    88  		});
    89  	}.bind(this);
    90  
    91  	handler(request.message, sendReply);
    92  };
    93  
    94  cam.WorkerMessageRouter.prototype.handleReply_ = function(reply) {
    95  	var callback = this.pendingMessages_[reply.messageId];
    96  	if (!callback) {
    97  		throw new Error('Could not find callback for pending message: %s', reply.messageId);
    98  	}
    99  	delete this.pendingMessages_[reply.messageId];
   100  	callback(reply.message);
   101  };