transmittable 0.1.0 copy "transmittable: ^0.1.0" to clipboard
transmittable: ^0.1.0 copied to clipboard

outdatedDart 1 only

Provides a simple means of registering types with string keys to allow them to be transparently serialized into a Json-like string. These types can be used on a shallow transmittable object to transf [...]

#Transmittable Build Status

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 explicitly implement an interface for this object, but do not implement any of the interfaces getters/setters which you would like to transmit. It is a requirement that classes extending off Transmittable implement a default constructor, meaning it is not a named constructor and it takes no arguments.

bool _animalTranTypesRegistered = false;
void registerAnimalTranTypes(){
  if(_animalTranTypesRegistered){ return; }
  _animalTranTypesRegistered = true;
  registerTranTypes('Animal', 'a', (){
    registerTranSubtype('cat', Cat);
  });
}

class Cat extends Transmittable implements ICat{}
abstract class ICat{
  String name;
  int age;
}

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, int, bool, String, DateTime, Duration, RegExp, 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 simply register them using the top level function:

registerTranCodec(String key, Type type, TranEncode encode, TranDecode decode)
//where
typedef String TranEncode<T>(T obj);
typedef T TranDecode<T>(String str);

Remember this method call must be made on both the client side and the server side with the same arguments. This is usually best achieved by both server and client side libraries referencing a common interface library which contains a method wrapping all your required calls to registerTranCodec.

Additionally for Transmittable to be able to recreate an actual instance of your extended type you must register that type too with registerTranSubtype.

0
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Provides a simple means of registering types with string keys to allow them to be transparently serialized into a Json-like string. These types can be used on a shallow transmittable object to transfer objects with type information over http requests, so they can be fully deserialized on the receiving end to the correct types.

Repository
View/report issues

License

unknown (license)

More

Packages that depend on transmittable