git ssb

0+

Dominic / atomic-file



Commit 4ef6493787e7e9742aad0dd4c8e2fd50d8114509

use default codec consistently

Dominic Tarr committed on 10/9/2016, 4:27:42 AM
Parent: 196d43450df76820243e0f92704062390929bd25

Files changed

index.jschanged
index.jsView
@@ -1,25 +1,26 @@
11 var fs = require('fs')
22
3-var codec = {
3 +var default_codec = {
44 encode: function (obj) {
5- return JSON.stringify(obj)
5 + return JSON.stringify(obj, null, 2)
66 },
77 decode: function (b) {
88 return JSON.parse(b.toString())
99 }
1010 }
1111
12-module.exports = function (filename, suffix) {
12 +module.exports = function (filename, suffix, _codec) {
13 + var codec = _codec || default_codec
1314 suffix = suffix || '~'
1415 var queue = []
1516 var value
1617 return {
1718 get: function (cb) {
1819 if(value) return cb(null, value)
1920 else fs.readFile(filename, 'utf8', function (err, _value) {
2021 if(err) return cb(err)
21- cb(null, value = JSON.parse(_value))
22 + cb(null, value = codec.decode(_value))
2223 })
2324 },
2425 //only allow one update at a time.
2526 set: function put (_value, cb) {

Built with git-ssb-web