2015-10-02 05:56:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-08 08:52:37 +00:00
|
|
|
let util = require('./util');
|
|
|
|
|
2015-10-06 19:36:56 +00:00
|
|
|
let DEVICE_HOST = 'localhost:8082';
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-08 08:52:37 +00:00
|
|
|
let idKey = util.idKey;
|
|
|
|
let realmKey = util.realmKey;
|
2015-10-06 19:36:56 +00:00
|
|
|
let typeConverters = {};
|
2015-10-06 20:36:42 +00:00
|
|
|
let XMLHttpRequest = window.XMLHttpRequest;
|
|
|
|
|
|
|
|
// Check if XMLHttpRequest has been overridden, and get the native one if that's the case.
|
|
|
|
if (XMLHttpRequest.__proto__ != window.XMLHttpRequestEventTarget) {
|
|
|
|
let override = XMLHttpRequest;
|
|
|
|
delete window.XMLHttpRequest;
|
|
|
|
XMLHttpRequest = window.XMLHttpRequest;
|
|
|
|
window.XMLHttpRequest = override;
|
|
|
|
}
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-15 01:53:37 +00:00
|
|
|
module.exports = {
|
|
|
|
registerTypeConverter,
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-15 01:53:37 +00:00
|
|
|
createRealm,
|
|
|
|
callRealmMethod,
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-15 01:53:37 +00:00
|
|
|
getObjectProperty,
|
|
|
|
setObjectProperty,
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-15 01:53:37 +00:00
|
|
|
getListItem,
|
|
|
|
setListItem,
|
|
|
|
getListSize,
|
|
|
|
callListMethod,
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-15 01:53:37 +00:00
|
|
|
getResultsItem,
|
|
|
|
getResultsSize,
|
|
|
|
callResultsMethod,
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-15 01:53:37 +00:00
|
|
|
beginTransaction,
|
|
|
|
cancelTransaction,
|
|
|
|
commitTransaction,
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-15 01:53:37 +00:00
|
|
|
clearTestState,
|
|
|
|
};
|
2015-10-15 10:12:28 +00:00
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
function registerTypeConverter(type, handler) {
|
|
|
|
typeConverters[type] = handler;
|
|
|
|
}
|
|
|
|
|
2015-10-15 10:00:13 +00:00
|
|
|
function createRealm(args) {
|
|
|
|
if (args) {
|
|
|
|
args = args.map((arg) => serialize(null, arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendRequest('create_realm', {arguments: args});
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 16:43:38 +00:00
|
|
|
function callRealmMethod(realmId, name, args) {
|
|
|
|
if (args) {
|
|
|
|
args = args.map((arg) => serialize(realmId, arg));
|
|
|
|
}
|
2015-10-08 08:53:22 +00:00
|
|
|
|
2015-10-08 16:43:38 +00:00
|
|
|
let result = sendRequest('call_realm_method', {realmId, name, arguments: args});
|
|
|
|
return deserialize(realmId, result);
|
2015-10-08 08:53:22 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
function getObjectProperty(realmId, objectId, name) {
|
|
|
|
let result = sendRequest('get_property', {realmId, objectId, name});
|
2015-10-08 08:52:37 +00:00
|
|
|
return deserialize(realmId, result);
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setObjectProperty(realmId, objectId, name, value) {
|
2015-10-08 09:00:10 +00:00
|
|
|
value = serialize(realmId, value);
|
2015-10-02 05:56:47 +00:00
|
|
|
sendRequest('set_property', {realmId, objectId, name, value});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getListItem(realmId, listId, index) {
|
|
|
|
let result = sendRequest('get_list_item', {realmId, listId, index});
|
2015-10-08 08:52:37 +00:00
|
|
|
return deserialize(realmId, result);
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setListItem(realmId, listId, index, value) {
|
2015-10-16 00:07:10 +00:00
|
|
|
sendRequest('set_list_item', {realmId, listId, index, value: serialize(realmId, value)});
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getListSize(realmId, listId) {
|
|
|
|
return sendRequest('get_list_size', {realmId, listId});
|
|
|
|
}
|
|
|
|
|
|
|
|
function callListMethod(realmId, listId, name, args) {
|
2015-10-08 08:52:37 +00:00
|
|
|
if (args) {
|
|
|
|
args = args.map((arg) => serialize(realmId, arg));
|
|
|
|
}
|
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
let result = sendRequest('call_list_method', {realmId, listId, name, arguments: args});
|
2015-10-08 08:52:37 +00:00
|
|
|
return deserialize(realmId, result);
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 00:08:19 +00:00
|
|
|
function getResultsItem(realmId, resultsId, index) {
|
|
|
|
let result = sendRequest('get_results_item', {realmId, resultsId, index});
|
2015-10-08 08:52:37 +00:00
|
|
|
return deserialize(realmId, result);
|
2015-10-08 00:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getResultsSize(realmId, resultsId) {
|
|
|
|
return sendRequest('get_results_size', {realmId, resultsId});
|
|
|
|
}
|
|
|
|
|
2015-10-15 01:53:37 +00:00
|
|
|
function callResultsMethod(realmId, resultsId, name, args) {
|
|
|
|
if (args) {
|
|
|
|
args = args.map((arg) => serialize(realmId, arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = sendRequest('call_results_method', {realmId, resultsId, name, arguments: args});
|
|
|
|
return deserialize(realmId, result);
|
|
|
|
}
|
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
function beginTransaction(realmId) {
|
|
|
|
sendRequest('begin_transaction', {realmId});
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancelTransaction(realmId) {
|
|
|
|
sendRequest('cancel_transaction', {realmId});
|
|
|
|
}
|
|
|
|
|
|
|
|
function commitTransaction(realmId) {
|
|
|
|
sendRequest('commit_transaction', {realmId});
|
|
|
|
}
|
|
|
|
|
2015-10-16 01:48:13 +00:00
|
|
|
function clearTestState() {
|
|
|
|
sendRequest('clear_test_state');
|
2015-10-15 10:12:28 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 08:52:37 +00:00
|
|
|
function serialize(realmId, value) {
|
2015-10-15 01:00:21 +00:00
|
|
|
if (typeof value == 'function') {
|
|
|
|
return {type: 'ObjectTypesFUNCTION'};
|
|
|
|
}
|
|
|
|
|
2015-10-08 08:52:37 +00:00
|
|
|
if (!value || typeof value != 'object') {
|
|
|
|
return {value: value};
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = value[idKey];
|
|
|
|
if (id) {
|
|
|
|
if (value[realmKey] != realmId) {
|
|
|
|
throw new Error('Unable to serialize value from another Realm');
|
|
|
|
}
|
|
|
|
|
2015-10-14 23:05:49 +00:00
|
|
|
return {id};
|
2015-10-08 08:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
let array = value.map((item) => serialize(realmId, item));
|
|
|
|
return {value: array};
|
|
|
|
}
|
|
|
|
|
|
|
|
let object = {};
|
|
|
|
for (let key in value) {
|
|
|
|
object[key] = serialize(realmId, value[key]);
|
|
|
|
}
|
|
|
|
return {value: object};
|
|
|
|
}
|
|
|
|
|
|
|
|
function deserialize(realmId, info) {
|
|
|
|
let type = info.type;
|
|
|
|
let handler = type && typeConverters[type];
|
|
|
|
if (handler) {
|
|
|
|
return handler(realmId, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
let value = info.value;
|
|
|
|
if (value && Array.isArray(value)) {
|
|
|
|
return value.map((item) => deserialize(realmId, item));
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendRequest(command, data) {
|
2015-10-15 10:12:28 +00:00
|
|
|
let body = JSON.stringify(data || {});
|
2015-10-06 20:36:42 +00:00
|
|
|
let request = new XMLHttpRequest();
|
2015-10-02 05:56:47 +00:00
|
|
|
let url = 'http://' + DEVICE_HOST + '/' + command;
|
|
|
|
|
|
|
|
request.open('POST', url, false);
|
|
|
|
request.send(body);
|
|
|
|
|
|
|
|
if (request.status != 200) {
|
|
|
|
throw new Error(request.responseText);
|
|
|
|
}
|
|
|
|
|
|
|
|
let response = JSON.parse(request.responseText);
|
2015-10-08 08:52:37 +00:00
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
if (!response || response.error) {
|
|
|
|
throw new Error((response && response.error) || 'Invalid response for "' + command + '"');
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.result;
|
|
|
|
}
|