Dart Documentationclean_sync.serverPublisher

Publisher class

class Publisher {
 int counter = 0;

 Map<String, Resource> _resources = {};

 void publish(String collection, DataGenerator generator, {beforeRequest: null}) {
   _resources[collection] = new Resource(generator, beforeRequest);
 }

 bool isPublished(String collection) {
   return _resources.containsKey(collection);
 }

 Future handleSyncRequest(ServerRequest request) {
   Map data = request.args;
   logger.finest("REQUEST:  ${data}");

   if(data['args'] == null) {
     data['args'] = {};
   }
   data['args']['_authenticatedUserId'] = request.authenticatedUserId;

   Resource resource = _resources[data['collection']];
   var action = data["action"];

   if (action == "get_id_prefix") {
     return new Future(getIdPrefix).then((prefix) => {'id_prefix': prefix});
   }

   return resource.handleSyncRequest(data).
   catchError((e, s) {
     if(!e.toString().contains("__TEST__")) {
       logger.shout('handle sync request error:', e, s);
     }
     return new Future.value({
       'error': e.toString(),
     });
   });

 }

 String getIdPrefix() {
   String prefix =
       new DateTime.now().millisecondsSinceEpoch.toRadixString(36) +
       prefix_random_part.toRadixString(36) + counter.toRadixString(36);
   counter = (counter + 1) % MAX;
   return prefix;
 }
}

Properties

int counter #

int counter = 0

Methods

String getIdPrefix() #

String getIdPrefix() {
 String prefix =
     new DateTime.now().millisecondsSinceEpoch.toRadixString(36) +
     prefix_random_part.toRadixString(36) + counter.toRadixString(36);
 counter = (counter + 1) % MAX;
 return prefix;
}

Future handleSyncRequest(ServerRequest request) #

Future handleSyncRequest(ServerRequest request) {
 Map data = request.args;
 logger.finest("REQUEST:  ${data}");

 if(data['args'] == null) {
   data['args'] = {};
 }
 data['args']['_authenticatedUserId'] = request.authenticatedUserId;

 Resource resource = _resources[data['collection']];
 var action = data["action"];

 if (action == "get_id_prefix") {
   return new Future(getIdPrefix).then((prefix) => {'id_prefix': prefix});
 }

 return resource.handleSyncRequest(data).
 catchError((e, s) {
   if(!e.toString().contains("__TEST__")) {
     logger.shout('handle sync request error:', e, s);
   }
   return new Future.value({
     'error': e.toString(),
   });
 });

}

bool isPublished(String collection) #

bool isPublished(String collection) {
 return _resources.containsKey(collection);
}

void publish(String collection, DataGenerator generator, {beforeRequest: null}) #

void publish(String collection, DataGenerator generator, {beforeRequest: null}) {
 _resources[collection] = new Resource(generator, beforeRequest);
}