Skip to content
Snippets Groups Projects
Commit 39a778b2 authored by mcmlxxix's avatar mcmlxxix
Browse files

added splice() method to json-client (currently only accepts one "data"...

added splice() method to json-client (currently only accepts one "data" argument, unlike the standard JS Array.splice() method).
TODO: unlocking a write-locked record does not imply that data was written, yet updates are being sent anyway. not a big deal, but probably should be fixed.
parent c9687c14
No related branches found
No related tags found
No related merge requests found
......@@ -185,6 +185,24 @@ function JSONClient(serverAddr,serverPort) {
return this.wait();
}
/* array splice method */
this.splice=function(scope,location,start,num,data,lock) {
this.send({
scope:scope,
func:"QUERY",
oper:"SPLICE",
location:location,
data:{
start:start,
num:num,
data:data
},
lock:lock,
timeout:this.settings.TIMEOUT
});
return this.wait();
}
/* read multiple object data (lock for reading or writing, blocking) */
/* readmulti([['tw2','sector.1',undefined,'sector'],['tw2','planets.1',undefined,'planet']]); */
this.readmulti=function(objects) {
......
......@@ -103,7 +103,8 @@ function JSONdb (fileName, scope) {
STATUS:11,
KEYS:12,
SLICE:13,
KEYTYPES:14
KEYTYPES:14,
SPLICE:15
}
/* error constants */
......@@ -342,6 +343,38 @@ function JSONdb (fileName, scope) {
}
}
/* splice an array */
this.splice = function(request,record) {
var client = request.client;
var data = request.data;
/* if the requested data does not exist, result is undefined */
if(record.data === undefined) {
this.error(client,errors.OBJECT_NOT_FOUND);
return true;
}
/* if this client has this record locked */
else if(record.info.lock[client.id] &&
record.info.lock[client.id].type == locks.WRITE) {
if(record.data[record.property] instanceof Array) {
record.data[record.property].splice(request.data.start,request.data.num,request.data.data);
/* remove existing shadow records that have been replaced by new data */
record.shadow[record.property].splice(request.data.start,request.data.num,new Shadow());
/* populate this object's children with shadow objects */
composite_sketch(record.data[record.property][request.data.start],record.shadow[record.property][request.data.start]);
}
else {
this.error(client,errors.NON_ARRAY);
}
return true;
}
/* if there is no lock for this client, error */
else {
return false;
}
}
/* push a record onto the end of an array */
this.unshift = function(request,record) {
var client = request.client;
......@@ -721,6 +754,9 @@ function JSONdb (fileName, scope) {
case "UNSHIFT":
result=this.unshift(request,record);
break;
case "SPLICE":
result=this.splice(request,record);
break;
case "SLICE":
result=this.slice(request,record);
break;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment