Permalink
Browse files

narrowed down delete problem to supermarket

  • Loading branch information...
substack committed Dec 6, 2010
1 parent 0ba50b6 commit 5bb829105615c73ccde5a5dbdcd67f9a59a2856e
Showing with 38 additions and 31 deletions.
  1. +27 −19 index.js
  2. +11 −12 test/pf.js
View
@@ -30,48 +30,56 @@ function load (db, def, cb) {
rows.map(function (r) { return r.value })
);
- rows.forEach(function (row) {
- if (row.key == '') return;
- var pkey = row.key.split('.').slice(0,-1).join('.');
- var key = row.key.split('.').slice(-1)[0];
- keyed[pkey][key] = keyed[row.key];
- });
+ rows
+ .sort(function (a,b) {
+ // so that children don't get set before their parents
+ return a.key.length - b.key.length
+ })
+ .forEach(function (row) {
+ if (row.key == '') return;
+ var pkey = row.key.split('.').slice(0,-1).join('.');
+ var key = row.key.split('.').slice(-1)[0];
+ keyed[pkey][key] = keyed[row.key];
+ })
+ ;
var root = keyed[''];
var em = new EventEmitter;
em.on('set', function set (ps, value) {
var key = ps.join('.');
if (typeof value != 'object' || value === null) {
- keyed[key] = value;
db.set(key, value);
}
else if (Array.isArray(value)) {
db.set(key, []);
- keyed[key] = [];
value.forEach(function (x, i) {
- var ps_ = ps.concat(i);
- set(ps_, x);
- keyed[key][i] = keyed[ps_.join('.')];
+ set(ps.concat(i), x);
});
}
else {
db.set(key, {});
- keyed[key] = {};
Hash(value).forEach(function (x, k) {
- var ps_ = ps.concat(k);
- set(ps_, x);
- keyed[key][k] = keyed[ps_.join('.')];
+ set(ps.concat(k), x);
});
}
});
- em.on('delete', function rm (ps) {
+ em.on('delete', function rm (ps, obj) {
var key = ps.join('.');
- delete keyed[key];
var name = ps[ps.length - 1];
- delete keyed[ps.slice(0,-1).join('.')][name];
db.remove(key);
+
+ if (Array.isArray(obj)) {
+ obj.forEach(
+ function (x,k) { rm(ps.concat(k), x) }
+ );
+ }
+ else if (typeof obj == 'object') {
+ Hash(obj).forEach(
+ function (x, k) { rm(ps.concat(k), x) }
+ );
+ }
});
if (rows.length === 0) {
@@ -122,7 +130,7 @@ function Wrapper (obj, path, em) {
},
delete : function (name) {
if (obj.propertyIsEnumerable(name)) {
- em.emit('delete', path.concat(name));
+ em.emit('delete', path.concat(name), obj[name]);
}
delete obj[name];
return true;
View
@@ -13,30 +13,29 @@ exports['set attrs'] = function (assert) {
obj.b = 3;
obj.a ++;
obj.c = [3,4];
- setTimeout(function () {
- pf(filename, function (err, obj_) {
- assert.eql(obj_, { a : 2, b : 3, c : [3,4] });
- });
- }, 500);
});
+
+ setTimeout(function () {
+ pf(filename, function (err, obj) {
+ assert.eql(obj, { a : 2, b : 3, c : [3,4] });
+ });
+ }, 5000);
};
exports.remove = function (assert) {
var filename = tmpfile();
- console.log(filename);
pf(filename, {}, function (err, obj) {
obj.a = 3;
obj.b = [ 4, 5 ];
obj.c = [ 6, 7, { d : 8, f : 9 } ];
delete obj.b[1];
delete obj.c;
-
- setTimeout(function () {
- pf(filename, {}, function (err, obj_) {
- assert.eql(obj_, { a : 3, b : [4] });
- });
- }, 500);
});
+ setTimeout(function () {
+ pf(filename, {}, function (err, obj) {
+ assert.eql(obj, { a : 3, b : [4] });
+ });
+ }, 10000);
};

0 comments on commit 5bb8291

Please sign in to comment.