Skip to content

Latest commit

 

History

History
64 lines (48 loc) · 1.29 KB

README.md

File metadata and controls

64 lines (48 loc) · 1.29 KB

travis

Objects Packer

Save your object links equal server and client states

Short example

// Server
const dataA = {};
const dataB = { a: dataA, b : dataA };

const packer = new Packer();
packer.take(dataB);

const json = JSON.stringify(packer.pack);
// Browser
const packer = new Packer(JSON.parse(json));
const data = packer.get(0);
data.a === data.b; // true

Named stores

// Server
const dataA = {};
const dataB = { a: dataA, b : dataA };

const packer = new Packer();
packer.take(dataB, 'myStore1');
packer.take(dataA, 'myStore2');

const json = JSON.stringify(packer.pack);
// Browser
const packer = new Packer(JSON.parse(json));
const myStore1 = packer.give('myStore1');
myStore1.a === myStore1.b; // true

const myStore2 = packer.give('myStore2'); // Deep equal dataA

IMPORTANT!

You can give as many times as you take for same store. This is feature for cleaning store in runtime for stop memory leaks.

Example

const data = {};
packer.take(data); // returns pos 0
packer.take(data); // returns pos 0

packer.give(0); // returns data
packer.give(0); // returns data
packer.give(0); // returns null