Skip to content

Commit

Permalink
feat: Allow skipping all tmp files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Krems committed Sep 11, 2018
1 parent 43dec33 commit ebf18c1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
16 changes: 13 additions & 3 deletions lib/cache.js
Expand Up @@ -154,12 +154,19 @@ tryCache = function(tmpDir) {
});
};

activeLoader = function(meta, loader, tmpDir) {
activeLoader = function(meta, loader, tmpDir, writeCacheFiles) {
var data, fromCache, onDataLoaded, rawData, ref1, tearDownCrashHandler;
rawData = meta.flatMapLatest(loader).map(function(otherData) {
otherData.usingCache = false;
return otherData;
});
if (!writeCacheFiles) {
debug('skip writing a cache, returning raw data stream');
return rawData.distinctUntilChanged(property('data'), isEqual).publish();
}
debug('wrapping data stream in cache', {
tmpDir: tmpDir
});
ref1 = crashRecovery(tmpDir), onDataLoaded = ref1.onDataLoaded, tearDownCrashHandler = ref1.tearDownCrashHandler;
data = rawData.tap(onDataLoaded, tearDownCrashHandler, tearDownCrashHandler);
fromCache = tryCache(tmpDir);
Expand All @@ -170,9 +177,12 @@ passiveLoader = function(tmpDir) {
return latestCacheFile(tmpDir, true).publish();
};

this.cachedLoader = function(meta, loader, tmpDir, active) {
this.cachedLoader = function(meta, loader, tmpDir, active, writeCacheFiles) {
if (writeCacheFiles == null) {
writeCacheFiles = true;
}
debug('cachedLoader(%j)', active);
loader = active ? activeLoader(meta, loader, tmpDir) : passiveLoader(tmpDir);
loader = active ? activeLoader(meta, loader, tmpDir, writeCacheFiles) : passiveLoader(tmpDir);
return Observable.create(function(observer) {
loader.subscribe(observer);
loader.connect();
Expand Down
13 changes: 10 additions & 3 deletions lib/shared-store.js
Expand Up @@ -92,18 +92,25 @@ SharedStore = (function(superClass) {
ref1.dispose();
}
meta = _this._createMeta();
_this.stream = cachedLoader(meta, loader, _this._temp, _this._active);
_this.stream = cachedLoader(meta, loader, _this._temp, _this._active, _this._writeCacheFiles);
return _this.subscription = _this.stream.subscribe(_this._handleUpdate, _this._handleError);
};
})(this);
this._createStream();
this.on('meta', this._handleMetaUpdate);
this._cache = null;
this._writeCacheFiles = !!this._active;
this._retryTimeout = TEN_SECONDS;
}

SharedStore.prototype.setActive = function(_active) {
this._active = _active != null ? _active : true;
SharedStore.prototype.setActive = function(isActive) {
if (isActive == null) {
isActive = true;
}
this._active = !!isActive;
if (isActive && typeof isActive.writeCacheFiles === 'boolean') {
this._writeCacheFiles = isActive.writeCacheFiles;
}
return this._createStream();
};

Expand Down
13 changes: 10 additions & 3 deletions src/cache.coffee
Expand Up @@ -108,13 +108,20 @@ tryCache = (tmpDir) ->
else
Observable.throw error

activeLoader = (meta, loader, tmpDir) ->
activeLoader = (meta, loader, tmpDir, writeCacheFiles) ->
rawData = meta
.flatMapLatest(loader)
.map (otherData) ->
otherData.usingCache = false
otherData

unless writeCacheFiles
debug 'skip writing a cache, returning raw data stream'
return rawData
.distinctUntilChanged property('data'), isEqual
.publish()

debug 'wrapping data stream in cache', { tmpDir }
{onDataLoaded, tearDownCrashHandler} = crashRecovery tmpDir

data = rawData.tap(
Expand All @@ -134,11 +141,11 @@ activeLoader = (meta, loader, tmpDir) ->
passiveLoader = (tmpDir) ->
latestCacheFile(tmpDir, true).publish()

@cachedLoader = (meta, loader, tmpDir, active) ->
@cachedLoader = (meta, loader, tmpDir, active, writeCacheFiles = true) ->
debug 'cachedLoader(%j)', active

loader = if active
activeLoader(meta, loader, tmpDir)
activeLoader(meta, loader, tmpDir, writeCacheFiles)
else
passiveLoader(tmpDir)

Expand Down
8 changes: 6 additions & 2 deletions src/shared-store.coffee
Expand Up @@ -67,15 +67,19 @@ class SharedStore extends EventEmitter
@_createStream = =>
@subscription?.dispose()
meta = @_createMeta()
@stream = cachedLoader meta, loader, @_temp, @_active
@stream = cachedLoader meta, loader, @_temp, @_active, @_writeCacheFiles
@subscription = @stream.subscribe @_handleUpdate, @_handleError
@_createStream()

@on 'meta', @_handleMetaUpdate
@_cache = null
@_writeCacheFiles = !!@_active
@_retryTimeout = TEN_SECONDS

setActive: (@_active=true) ->
setActive: (isActive = true) ->
@_active = !!isActive
if isActive && typeof isActive.writeCacheFiles == 'boolean'
@_writeCacheFiles = isActive.writeCacheFiles
@_createStream()

getCurrent: ->
Expand Down
50 changes: 50 additions & 0 deletions test/shared-store/no-cache.test.coffee
@@ -0,0 +1,50 @@
'use strict'

path = require 'path'
os = require 'os'
fs = require 'fs'

assert = require 'assertive'
{Observable} = require 'rx'
tmp = require 'tmp'

SharedStore = require '../../'

BASE_CONFIG = opt: 'value'

describe 'With cache disabled', ->
before (done) ->
tmp.dir { unsafeCleanup: true }, (err, @tmpDir) => done(err)
return

describe 'with cache disabled', ->
before (done) ->
@overrideFile =
path.join os.tmpdir(), 'shared-store.test.json'
@initialOverrides = override: 'loaded'

fs.writeFile(
@overrideFile, JSON.stringify(@initialOverrides), done
)
return

before (done) ->
@store = new SharedStore {
temp: @tmpDir
active: true
loader: (baseConfig) =>
assert.deepEqual BASE_CONFIG, baseConfig
Observable.just(data: { static: 'data' }, source: 'static')
}
@store.setActive writeCacheFiles: false
@store.init opt: 'value', (err, @initCallbackData) => done(err)
return

it 'returns the initial data', ->
assert.deepEqual {
static: 'data'
}, @store.getCurrent()

it 'writes no cache file', ->
cacheFiles = fs.readdirSync @tmpDir
assert.equal 0, cacheFiles.length

0 comments on commit ebf18c1

Please sign in to comment.