2015-10-02 05:56:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-06 19:36:56 +00:00
|
|
|
let DEVICE_HOST = 'localhost:8082';
|
2015-10-02 05:56:47 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
exports.registerTypeConverter = registerTypeConverter;
|
|
|
|
|
|
|
|
exports.createRealm = createRealm;
|
|
|
|
exports.createObject = createObject;
|
2015-10-08 00:08:19 +00:00
|
|
|
exports.getObjects = getObjects;
|
2015-10-02 05:56:47 +00:00
|
|
|
|
|
|
|
exports.getObjectProperty = getObjectProperty;
|
|
|
|
exports.setObjectProperty = setObjectProperty;
|
|
|
|
|
|
|
|
exports.getListItem = getListItem;
|
|
|
|
exports.setListItem = setListItem;
|
|
|
|
exports.getListSize = getListSize;
|
|
|
|
exports.callListMethod = callListMethod;
|
|
|
|
|
2015-10-08 00:08:19 +00:00
|
|
|
exports.getResultsItem = getResultsItem;
|
|
|
|
exports.getResultsSize = getResultsSize;
|
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
exports.beginTransaction = beginTransaction;
|
|
|
|
exports.cancelTransaction = cancelTransaction;
|
|
|
|
exports.commitTransaction = commitTransaction;
|
|
|
|
|
|
|
|
function registerTypeConverter(type, handler) {
|
|
|
|
typeConverters[type] = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createRealm(config) {
|
|
|
|
return sendRequest('create_realm', config);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createObject(realmId, type, values) {
|
|
|
|
return sendRequest('create_object', {realmId, type, values});
|
|
|
|
}
|
|
|
|
|
2015-10-08 00:08:19 +00:00
|
|
|
function getObjects(realmId, type, predicate) {
|
|
|
|
return sendRequest('get_objects', {realmId, type, predicate});
|
|
|
|
}
|
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
function getObjectProperty(realmId, objectId, name) {
|
|
|
|
let result = sendRequest('get_property', {realmId, objectId, name});
|
2015-10-08 01:40:09 +00:00
|
|
|
return convert(realmId, result);
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setObjectProperty(realmId, objectId, name, value) {
|
|
|
|
sendRequest('set_property', {realmId, objectId, name, value});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getListItem(realmId, listId, index) {
|
|
|
|
let result = sendRequest('get_list_item', {realmId, listId, index});
|
2015-10-08 01:40:09 +00:00
|
|
|
return convert(realmId, result);
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setListItem(realmId, listId, index, value) {
|
|
|
|
sendRequest('set_list_item', {realmId, listId, index, value});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getListSize(realmId, listId) {
|
|
|
|
return sendRequest('get_list_size', {realmId, listId});
|
|
|
|
}
|
|
|
|
|
|
|
|
function callListMethod(realmId, listId, name, args) {
|
|
|
|
let result = sendRequest('call_list_method', {realmId, listId, name, arguments: args});
|
2015-10-08 01:40:09 +00:00
|
|
|
return convert(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 01:40:09 +00:00
|
|
|
return convert(realmId, result);
|
2015-10-08 00:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getResultsSize(realmId, resultsId) {
|
|
|
|
return sendRequest('get_results_size', {realmId, resultsId});
|
|
|
|
}
|
|
|
|
|
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});
|
|
|
|
}
|
|
|
|
|
|
|
|
function convert(realmId, info) {
|
|
|
|
let handler = typeConverters[info.type];
|
|
|
|
return handler ? handler(realmId, info) : info.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendRequest(command, data) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (!response || response.error) {
|
|
|
|
throw new Error((response && response.error) || 'Invalid response for "' + command + '"');
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.result;
|
|
|
|
}
|