JavaScript Services

From WebOS Internals
Revision as of 22:22, 20 October 2014 by Reeder.29 (talk | contribs) (Added errorCodes returned by PalmBus itself.)
Jump to navigation Jump to search

Some notes to flesh out the old primary documentation and the new primary documentation

Node versions

  • webOS 1.x: node not present
  • webOS 2.x: node v0.2.3
  • webOS 3.0: node v0.4.12
  • webOS TV: ?
  • LuneOS (webOS 3.5): node v0.10.25


Values returned over the PalmBus

To return a success, set future.result to an object with the desired properties (where future is the future passed to the Command Assistant).

To return a failure, set future.exception to an object (often some kind of Error). Best practice is to set both message and errorCode properties. (If you set the errorCode property of the object, that will be the errorCode returned over PalmBus, and the errorText will be copied from the message property. Otherwise, errorCode will be -9999 and errorText will be "MojoService: no errorCode supplied "+(reply.errorText||"")) errorCode is nominally an integer, but some system services return a string.


While the Command Assistant run() method is called "inside" the future, it's not clear that you can compose the passed future with others.


Reserved errorCode Values

The following errorCode value should not be used by your service, because they are used by the PalmBus itself:

  • -1: missing service
  • 504: command timeout
  • -9999: no errorCode returned by JS service


Service Initialization

If you have common setup which must be done, regardless of which command is called first, define a Service Assistant

If you return a Future from the Service Assistant's setup method your Command Assistant will not be called until that Future is resolved. If that Future fails, your Command Assistant will never be called, and the service call will eventually time out. So, ensure the Future always succeeds (storing an error if necessary):

MyServiceAssistant.prototype.setup = function () {
	return this.myAsyncMethod().then(function (f) {
		console.log("MyServiceAssistant.setup overall success", f.result);
		return f.result = f.result;
	}, function (f) {
		console.error("MyServiceAssistant.setup overall failure", f.exception);
		f.result = {foo: "bar"};
	});
};