Fix cache path problems

This commit is contained in:
haad 2016-11-03 09:35:38 +01:00
parent f7c19eb9b6
commit 1709be97fe
5 changed files with 29 additions and 20 deletions

4
.gitignore vendored
View File

@ -5,11 +5,9 @@ node_modules/
.idea/ .idea/
dump.rdb dump.rdb
Vagrantfile Vagrantfile
orbit-db-cache.json
examples/browser/bundle.js examples/browser/bundle.js
examples/browser/*.map examples/browser/*.map
dist/*.map dist/*.map
orbit-db.json
dist/orbitdb.js dist/orbitdb.js
orbit-db.cache orbit-db/
ipfs/ ipfs/

19
dist/orbitdb.js vendored
View File

@ -4655,11 +4655,15 @@ var Cache = function () {
} }
}, { }, {
key: 'loadCache', key: 'loadCache',
value: function loadCache() { value: function loadCache(cachePath) {
var cacheFile = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'orbit-db.cache'; var cacheFile = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'orbit-db.cache';
cache = {}; cache = {};
store = new BlobStore(cacheFile);
if (!cachePath) return _promise2.default.resolve();
// console.log("cache data:", cachePath)
store = new BlobStore(cachePath);
filePath = cacheFile; filePath = cacheFile;
return new _promise2.default(function (resolve, reject) { return new _promise2.default(function (resolve, reject) {
@ -4688,6 +4692,7 @@ var Cache = function () {
key: 'reset', key: 'reset',
value: function reset() { value: function reset() {
cache = {}; cache = {};
store = null;
} }
}]); }]);
return Cache; return Cache;
@ -25609,17 +25614,17 @@ var OrbitDB = function () {
}, { }, {
key: '_createStore', key: '_createStore',
value: function _createStore(Store, dbname) { value: function _createStore(Store, dbname) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { subscribe: true, cacheFile: 'orbit-db.json' }; var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { subscribe: true };
var store = new Store(this._ipfs, this.user.id, dbname, options); var store = new Store(this._ipfs, this.user.id, dbname, options);
this.stores[dbname] = store; this.stores[dbname] = store;
return this._subscribe(store, dbname, options.subscribe, options); return this._subscribe(store, dbname, options.subscribe, options.cachePath);
} }
}, { }, {
key: '_subscribe', key: '_subscribe',
value: function _subscribe(store, dbname) { value: function _subscribe(store, dbname) {
var subscribe = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var subscribe = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var options = arguments[3]; var cachePath = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : './orbit-db';
store.events.on('data', this._onData.bind(this)); store.events.on('data', this._onData.bind(this));
store.events.on('write', this._onWrite.bind(this)); store.events.on('write', this._onWrite.bind(this));
@ -25629,7 +25634,7 @@ var OrbitDB = function () {
return console.error(e.stack); return console.error(e.stack);
}); });
Cache.loadCache(options.cacheFile).then(function () { Cache.loadCache(cachePath).then(function () {
var hash = Cache.get(dbname); var hash = Cache.get(dbname);
store.loadHistory(hash).catch(function (e) { store.loadHistory(hash).catch(function (e) {
return console.error(e.stack); return console.error(e.stack);

6
dist/orbitdb.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -37,9 +37,14 @@ class Cache {
return cache[key] return cache[key]
} }
static loadCache(cacheFile = 'orbit-db.cache') { static loadCache(cachePath, cacheFile = 'orbit-db.cache') {
cache = {} cache = {}
store = new BlobStore(cacheFile)
if (!cachePath)
return Promise.resolve()
// console.log("cache data:", cachePath)
store = new BlobStore(cachePath)
filePath = cacheFile filePath = cacheFile
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -70,6 +75,7 @@ class Cache {
static reset() { static reset() {
cache = {} cache = {}
store = null
} }
} }

View File

@ -56,13 +56,13 @@ class OrbitDB {
} }
/* Private methods */ /* Private methods */
_createStore(Store, dbname, options = { subscribe: true, cacheFile: 'orbit-db.json' }) { _createStore(Store, dbname, options = { subscribe: true }) {
const store = new Store(this._ipfs, this.user.id, dbname, options) const store = new Store(this._ipfs, this.user.id, dbname, options)
this.stores[dbname] = store this.stores[dbname] = store
return this._subscribe(store, dbname, options.subscribe, options) return this._subscribe(store, dbname, options.subscribe, options.cachePath)
} }
_subscribe(store, dbname, subscribe = true, options) { _subscribe(store, dbname, subscribe = true, cachePath = './orbit-db') {
store.events.on('data', this._onData.bind(this)) store.events.on('data', this._onData.bind(this))
store.events.on('write', this._onWrite.bind(this)) store.events.on('write', this._onWrite.bind(this))
store.events.on('close', this._onClose.bind(this)) store.events.on('close', this._onClose.bind(this))
@ -72,7 +72,7 @@ class OrbitDB {
else else
store.loadHistory().catch((e) => console.error(e.stack)) store.loadHistory().catch((e) => console.error(e.stack))
Cache.loadCache(options.cacheFile).then(() => { Cache.loadCache(cachePath).then(() => {
const hash = Cache.get(dbname) const hash = Cache.get(dbname)
store.loadHistory(hash).catch((e) => console.error(e.stack)) store.loadHistory(hash).catch((e) => console.error(e.stack))
}) })