Remote-instance
With this library you can encode and transfer instances remotely 💫.
Remote instances will be just like if they created locally. To create a remote instance all you need to do is:
- Optional - Implement a static method
fromArgumentsList()
on the constructor of the instance. This method will receive an array of arguments as first argument and should return a new instance accordingly. If this method those not exists, the arguments will apply on the instance constrictor (see example bellow). - Implement a method
toArgumentsList()
on the instance which return array of arguments of any kind (including other instances!) to recreate this instance remotely via thefromArgumentsList()
method. - Register the instance constructor on the parser with a unique single byte-code that is well known to each peer (see example bellow).
That's it!
You can use the parser method parser.transform(stream)
to convert a duplex buffer stream to an
object
mode stream transformed by the parser (see API Reference for more details).
Install
npm install remote-instance
Usage
const Parser = ; // Create test class { thisbar = bar; } // Tel the parser how to rebuild this class remotely { return thisbar; } // Create a parser instance and register our classconst parser = ;parser; // create an instance and encode itconst foo = test: 'myThing' ;const buffer = parser; // decode and recreate the instanceconst decodedFoo = parser; // `decodedFoo` is not foo but an exact copy of FoodecodedFoo !== foo; // truedecodedFoo instanceof Foo; // truedecodedFoobar; // { test: 'myThing' }
API Reference
This module is a part of the remote-lib
library.
Here is the relevant documentation for this module:
© 2017 Moshe Simantov
Licensed under the Apache License, Version 2.0.