transmittable 0.8.0 transmittable: ^0.8.0 copied to clipboard
Provides a simple means of registering types with codecs to allow them to be transparently serialized to strings. These types can then be transmitted over http connections with their type information [...]
Transmittable provides a simple way of transferring named and typed properties across http connections whilst also giving the benefit of static type checking during development.
##How To Use:
Extend off of Transmittable to make an object transmittable accross a http connection, then implement getters and setters as appropriate.
void registerAnimalTranTypes = generateRegistrar(
'Animal', 'a', [
new TranRegistration.subtype(Cat, () => new Cat())
]);
class Cat extends Transmittable{
String get name => get('name');
void set name (String o){set('name', o);}
int get age => get('age');
void set age (int o){set('age', o);}
}
void main(){
registerAnimalTranTypes();
Cat c1 = new Cat()
..name = 'felix'
..age = 3;
var tranStr = c1.toTranString(); // turn this cat into a transmittable string
// send down http connection and then deserialise back into the cat object
Cat c2 = new Transmittable.fromTranString(tranStr);
print(c2 is Cat) // true
print(c2.name); // felix
print(c2.age); // 3
}
##Registered Types
Transmittable can handle, null, num, int, double, bool, String, DateTime, Duration, RegExp, Type, List, Set and Map out of the box without any need for further effort on the users part.
If you would like to add additional types to be transmittable or to be subtype
of Transmittable simply register them using the same pattern as below. The generateRegistrar
function takes a full namespace description, in this case 'transmittable'
since it is from the
transmittable package, the second argument is the short namespace that will be used in the string
serializations, in this case the empty string as these are the most common types that will be serialized,
the last argument is a List<TranRegistration>
which defines all the types that
will be transmittable and how they are de/serialized to/from string/actual form, generateRegistrar
returns a Registrar
function which is then called to register all of the types:
Registrar _registerTranTranTypes = generateRegistrar(
'transmittable', '', [
new TranRegistration.codec(null, (o)=> '', (s) => null),
new TranRegistration.codec(_InternalPointer, (_InternalPointer ip) => ip._uniqueValueIndex.toString(), (String s) => new _InternalPointer(int.parse(s))),
new TranRegistration.codec(num, (num n) => n.toString(), (String s) => num.parse(s)),
new TranRegistration.codec(int, (int i) => i.toString(), (String s) => int.parse(s)),
new TranRegistration.codec(double, (double f) => f.toString(), (String s) => double.parse(s)),
new TranRegistration.codec(String, (String s) => s, (String s) => s),
new TranRegistration.codec(bool, (bool b) => b ? 't' : '', (String s) => s == 't' ? true : false),
new TranRegistration.codec(List, _processIterableToString, (String s) => _processStringBackToListOrSet(new List(), s)),
new TranRegistration.codec(Set, _processIterableToString, (String s) => _processStringBackToListOrSet(new Set(), s)),
new TranRegistration.codec(Map, _processMapToString, _processStringBackToMap),
new TranRegistration.codec(RegExp, _processRegExpToString, _processStringBackToRegExp),
new TranRegistration.codec(Type, (Type t) => _processTypeToString(t),(String s) => _tranCodecsByKey[s]._type),
new TranRegistration.codec(DateTime, (DateTime d) => d.toString(), (String s) => DateTime.parse(s)),
new TranRegistration.codec(Duration, (Duration dur) => dur.inMilliseconds.toString(), (String s) => new Duration(milliseconds: num.parse(s))),
new TranRegistration.subtype(Transmittable, () => new Transmittable())
]);
Remember this method call must be made on both the client side and the server side. This is usually best achieved by both server and client side libraries referencing a common library which contains this function.