Skip to content

Commit

Permalink
allow specifying fs for ObservFile and ObservDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
mmckegg committed Dec 30, 2017
1 parent 3a6834a commit fb37ecc
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/file-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function FileObject (parentContext) {
loading = true
initialized = false
onceIdle(() => { initialized = true })
obs.file = ObservFile(path)
obs.file = ObservFile(path, context.fs)
releaseRename = watch(obs.file.path, obs.path.set)
context.cwd.set(getDirName(path))
releaseClose = obs.file.onClose(onClose)
Expand Down
4 changes: 2 additions & 2 deletions lib/observ-audio-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function ObservAudioBuffer (context) {

context.fs.exists(path, function (exists) {
if (exists) {
var file = ObservFile(path, 'arraybuffer')
var file = ObservFile(path, context.fs, 'arraybuffer')
parseAudioBuffer(file, obs.currentValue.set, context.audio)
releases.push(file.close)
} else {
Expand All @@ -36,7 +36,7 @@ function ObservAudioBuffer (context) {

context.fs.exists(timePath, function (exists) {
if (exists) {
var file = ObservFile(timePath, 'arraybuffer')
var file = ObservFile(timePath, context.fs, 'arraybuffer')
parseFloat32Array(file, obs.cuePoints.set)
releases.push(file.close)
} else {
Expand Down
7 changes: 3 additions & 4 deletions lib/observ-directory.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
var Observ = require('mutant/value')
var join = require('path').join
var fs = require('fs')
var Event = require('geval')
var MutantMap = require('mutant/map')

module.exports = ObservDirectory

function ObservDirectory (path, cb) {
function ObservDirectory (path, fs, cb) {
var entries = Observ([])
var refreshing = false
var watcher = null
Expand Down Expand Up @@ -57,7 +56,7 @@ function ObservDirectory (path, cb) {
function refresh (init) {
refreshing = false
var current = entries()
getDirectories(path, (err, dirs) => {
getDirectories(path, fs, (err, dirs) => {
if (err) return init && cb && cb(err)
if (dirs.length !== current.length || !dirs.every(x => current.includes(x))) {
dirs.sort((a, b) => a.localeCompare(b))
Expand All @@ -70,7 +69,7 @@ function ObservDirectory (path, cb) {
}
}

function getDirectories (path, cb) {
function getDirectories (path, fs, cb) {
fs.readdir(path, (err, files) => {
if (err) return cb && cb(err)
var remaining = files.length
Expand Down
7 changes: 3 additions & 4 deletions lib/observ-file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var Value = require('mutant/value')
var Event = require('geval')
var fs = require('fs')
var join = require('path').join
var getDirectory = require('path').dirname
var onceIdle = require('mutant/once-idle')
Expand All @@ -12,7 +11,7 @@ var readQueue = []
var caches = {}
var encodings = ['utf8', 'arraybuffer', 'buffer']

function ObservFile (path, encoding, cb) {
function ObservFile (path, fs, encoding, cb) {
encoding = encodings.includes(encoding) ? encoding : encodings[0]
var cacheKey = encoding + ':' + path
var cache = caches[cacheKey]
Expand Down Expand Up @@ -139,7 +138,7 @@ function read (item) {
})
})
} else if (item.encoding === 'arraybuffer') {
item.fs.readFile(item.path, function (err, result) {
item.fs.readFile(item.path, 'binary', function (err, result) {
onceIdle(() => {
item.reading = false
result = result ? result.buffer : null
Expand All @@ -149,7 +148,7 @@ function read (item) {
})
})
} else {
item.fs.readFile(item.path, function (err, result) {
item.fs.readFile(item.path, 'binary', function (err, result) {
onceIdle(() => {
item.reading = false
item.lastValue = result
Expand Down
2 changes: 1 addition & 1 deletion nodes/external-chunk/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function External (parentContext) {
initialized = false
onceIdle(() => { initialized = true })
loading = true
obs.file = ObservFile(path)
obs.file = ObservFile(path, context.fs)
updateFile = JsonFile(obs.file, updateNode)
context.cwd.set(Path.dirname(path))
fileReleases.push(
Expand Down
4 changes: 2 additions & 2 deletions nodes/project/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ function Project (parentContext) {
obs.renaming = Observ(false)
obs.duplicating = Observ(false)

obs.entries = ObservDirectory(resolve(context.cwd))
obs.entries = ObservDirectory(resolve(context.cwd), context.fs)

obs.recordingEntries = ObservDirectory(resolvePath(resolve(context.cwd), '~recordings'))
obs.recordingEntries = ObservDirectory(resolvePath(resolve(context.cwd), '~recordings'), context.fs)

obs.outputRms = ObservRms(masterOutput)

Expand Down

0 comments on commit fb37ecc

Please sign in to comment.