126 lines
3.5 KiB
JavaScript
126 lines
3.5 KiB
JavaScript
'use strict';
|
|
|
|
let DEVICE_HOST = 'localhost:8082';
|
|
|
|
let typeConverters = {};
|
|
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;
|
|
}
|
|
|
|
exports.registerTypeConverter = registerTypeConverter;
|
|
|
|
exports.createRealm = createRealm;
|
|
exports.createObject = createObject;
|
|
exports.getObjects = getObjects;
|
|
|
|
exports.getObjectProperty = getObjectProperty;
|
|
exports.setObjectProperty = setObjectProperty;
|
|
|
|
exports.getListItem = getListItem;
|
|
exports.setListItem = setListItem;
|
|
exports.getListSize = getListSize;
|
|
exports.callListMethod = callListMethod;
|
|
|
|
exports.getResultsItem = getResultsItem;
|
|
exports.getResultsSize = getResultsSize;
|
|
|
|
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});
|
|
}
|
|
|
|
function getObjects(realmId, type, predicate) {
|
|
return sendRequest('get_objects', {realmId, type, predicate});
|
|
}
|
|
|
|
function getObjectProperty(realmId, objectId, name) {
|
|
let result = sendRequest('get_property', {realmId, objectId, name});
|
|
return convert(realmId, result);
|
|
}
|
|
|
|
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});
|
|
return convert(realmId, result);
|
|
}
|
|
|
|
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});
|
|
return convert(realmId, result);
|
|
}
|
|
|
|
function getResultsItem(realmId, resultsId, index) {
|
|
let result = sendRequest('get_results_item', {realmId, resultsId, index});
|
|
return convert(realmId, result);
|
|
}
|
|
|
|
function getResultsSize(realmId, resultsId) {
|
|
return sendRequest('get_results_size', {realmId, resultsId});
|
|
}
|
|
|
|
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);
|
|
let request = new XMLHttpRequest();
|
|
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;
|
|
}
|