ES6 Metaprogramming, Barn

Alex R. Young





redis browser localStorage ES6 proxies

ES6 Metaprogramming, Barn

Posted by Alex R. Young on .

redis browser localStorage ES6 proxies

ES6 Metaprogramming, Barn

Posted by Alex R. Young on .

ES6 Metaprogramming

The last time I seriously looked at JavaScript metaprogramming I tried all kinds of dubious things like exploiting the with statement. With ECMAScript 6 proxies, the situation might be about to change. In Meta programming with ECMAScript 6 proxies, Dr. Axel Rauschmayer writes about the concept of metaprogramming, the different approaches (introspection, self-modification, intercession), and provides JavaScript examples.

This article includes an overview of proxies and what they're useful for, and shows how they can be used for intercepting method calls. He also mentions how proxies impact performance:

Data binding is a complex topic. Given its popularity and concerns over proxies not being performant enough, a dedicated mechanism has been created for data binding: Object.observe(). It will probably be part of ECMAScript 7 and is already supported by Chrome.

This is an in-depth article that is worth taking some time over, but will influence many people who are creating the next generation of ECMAScript 6-based APIs.


Barn (GitHub: arokor/barn, License: MIT, npm: barn) by Aron Kornhall is an API for localStorage that is based on Redis. The idea is to make operations atomic, so they survive things like a tab being closed or unexpected exceptions.

If you're used to Redis, then you might like this:

var barn = new Barn(localStorage);

barn.set('key', 'val');  
console.log(barn.get('key')); // val

barn.lpush('list', 'val1');  
barn.lpush('list', 'val2');  
console.log(barn.rpop('list')); // val1  
console.log(barn.rpop('list')); // val2  

Internally a mutating flag is used to provide a locking mechanism around the localStorage access. I think this should prevent issues that you might get when performing multiple writes, because the Web Storage specification says writes don't wait until the data has been written to disk.

Barn itself is based on NSStorage, which is also written by Aron. NSStorage is a smaller module that provides namespacing for the Web Storage API.