Resource
class
class Resource {
DataGenerator generator;
Function beforeRequestCallback;
Future handleSyncRequest (Map data) {
num watchID = startWatch('${data["action"]}, ${data['collection']}');
var action = data["action"];
var reqVersion = data['version'];
List<String> modifications = ['add', 'change', 'remove'];
Future beforeRequest = new Future.value(null);
if (beforeRequestCallback != null && modifications.contains(action)) {
var value;
if (action == 'add') value = data['data'];
else if (action == 'change') value = data['change'];
else if (action == 'remove') value = {};
beforeRequest = beforeRequestCallback(value, data['args']);
}
DataProvider dp;
return beforeRequest
.then((_) => generator(data['args']))
.then((DataProvider _dp) {
dp = _dp;
if (action == "get_data") {
return dp.data().then((result) {
stopWatch(watchID);
return result;
});
}
else if(action == "get_diff") {
return dp.diffFromVersion(reqVersion)
.then((result) {
stopWatch(watchID);
return result;
});
}
else if (action == "add") {
return dp.add(data['data'], data['author'])
.then((result) {
stopWatch(watchID);
return result;
});
}
else if (action == "change") {
return dp.change(data['_id'], data['change'], data['author'])
.then((result) {
stopWatch(watchID);
return result;
});
}
else if (action == "remove") {
return dp.remove(data['_id'], data['author'])
.then((result) {
stopWatch(watchID);
return result;
});
}
});
}
Resource(this.generator, this.beforeRequestCallback);
}
Constructors
new Resource(DataGenerator generator, Function beforeRequestCallback) #
Creates a new Object instance.
Object instances have no meaningful state, and are only useful
through their identity. An Object instance is equal to itself
only.
Resource(this.generator, this.beforeRequestCallback);
Properties
Function beforeRequestCallback #
Function beforeRequestCallback
Methods
Future handleSyncRequest(Map data) #
Future handleSyncRequest (Map data) {
num watchID = startWatch('${data["action"]}, ${data['collection']}');
var action = data["action"];
var reqVersion = data['version'];
List<String> modifications = ['add', 'change', 'remove'];
Future beforeRequest = new Future.value(null);
if (beforeRequestCallback != null && modifications.contains(action)) {
var value;
if (action == 'add') value = data['data'];
else if (action == 'change') value = data['change'];
else if (action == 'remove') value = {};
beforeRequest = beforeRequestCallback(value, data['args']);
}
DataProvider dp;
return beforeRequest
.then((_) => generator(data['args']))
.then((DataProvider _dp) {
dp = _dp;
if (action == "get_data") {
return dp.data().then((result) {
stopWatch(watchID);
return result;
});
}
else if(action == "get_diff") {
return dp.diffFromVersion(reqVersion)
.then((result) {
stopWatch(watchID);
return result;
});
}
else if (action == "add") {
return dp.add(data['data'], data['author'])
.then((result) {
stopWatch(watchID);
return result;
});
}
else if (action == "change") {
return dp.change(data['_id'], data['change'], data['author'])
.then((result) {
stopWatch(watchID);
return result;
});
}
else if (action == "remove") {
return dp.remove(data['_id'], data['author'])
.then((result) {
stopWatch(watchID);
return result;
});
}
});
}