Skip to content

Instantly share code, notes, and snippets.

@mmirolim
Created July 4, 2014 00:12
Show Gist options
  • Save mmirolim/1b6a7b5a315a3fd83f4a to your computer and use it in GitHub Desktop.
Save mmirolim/1b6a7b5a315a3fd83f4a to your computer and use it in GitHub Desktop.
Offline Gear
/**
* Caching service via pouchDb
* @link http://pouchdb.com/
* @author Mirolim Mirzakhmedov mirolim777 at gmail.com
* @date 03.07.2014
*/
app.factory('CacheDB', ['$http', 'API',
function ($http, API) {
var db = new PouchDB('cachedb');
// listen to socket connect and sync pending data (tasks)
io.socket.on('connect', function () {
db.allDocs({startkey: 'backup/task/pending/', endkey: 'backup/tasks/pending_', include_docs: true})
.then(function (o) {
console.log(o, 'Object from PouchDB');
var totalDocs = o.rows.length;
// increment to now when promises end
var i = 0;
o.rows.forEach(function (r) {
console.log(r.doc, 'R.doc');
i++;
// @todo think maybe match string with hash tag
if (typeof r.doc.id === 'number') {
// if id is number then task already created in backend and should be updated
socket.put(API.tasks + '/' + r.doc.id, r.doc, function (r) {
console.log(r, 'Response while syncing');
var _id = 'backup/task/updated/pending/' + r.id;
db.get(_id, function (err, doc) {
if (!err) {
db.remove(doc, function (err, r) {
if (!err) {
console.log(r, 'Object synced and deleted from cachedb');
} else {
console.log(err);
}
})
} else {
console.log(err);
}
});
if (i === totalDocs) {
new Notification(' Tasks Sync completed', {body: totalDocs + ' Tasks synced'})
}
}, function (e, status) {
new Notification('During syncing there was problem', {body: e});
});
}
});
});
});
return {
put: function (obj) {
// @todo support all types of models
var _id = '';
// check if new obj
if (!obj.id) {
obj.id = 't#' + Date.now();
// if created updates it should use same doc
_id = 'backup/task/created/pending/' + obj.id;
db.put(obj, _id)
.then(function (r) {
devPanel.log(r);
});
} else {
// should be only existing one updated
_id = 'backup/task/updated/pending/' + obj.id;
db.get(_id, function (err, doc) {
if (!err) {
db.put(obj, _id, doc._rev, function (err, doc) {
if (!err) {
devPanel.log(doc);
} else {
console.log(err);
}
});
} else {
db.put(obj, _id, function (err, doc) {
if (!err) {
console.log(doc, "Cached");
} else {
console.log(err);
}
});
}
});
}
},
get: function (id) {
db.get(id)
.then(function (r) {
console.log(r, 'Result from get');
return r;
});
},
query: function (map) {
db.query({map: map}, {include_docs: true})
.then(function (r) {
return r;
})
},
getAll: function (startKey, endKey) {
var criteria = {
include_docs: true
};
if (typeof startKey !== 'undefined') {
criteria.startkey = startKey;
}
if (typeof endKey !== 'undefined') {
criteria.endkey = endKey;
}
db.allDocs(criteria)
.then(function (r) {
return r
})
}
}
}
]);
// this goes to appropriate places
// also delete should be added
save: function (task) {
var deffered = $q.defer();
// attach offline gear
if (devPanel.offlineGearEnabled) {
CacheDB.put(task);
}
if (!task.id) {
task.status = STATUS.inbox.name;
socket.post(API.tasks, task, function (r) {
deffered.resolve(r);
}, function (e) {
deffered.reject(e);
});
} else {
delete task.$$hashKey;
delete task.dueDateOpened;
socket.put(API.tasks + '/' + task.id, task, function (r) {
deffered.resolve(r);
}, function (e, status) {
deffered.reject(e, status);
});
}
return deffered.promise;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment