var cadence = require('cadence')
var Staccato = require('staccato')
var Cache = require('magazine')

var Signal = require('signal')

var restrictor = require('restrictor')

var Turnstile = require('turnstile')

var fs = require('fs')
var path = require('path')

function Journalist (directory, sheaf) {
    this._magazine = new Cache().createMagazine()
    this._directory = directory
    this._sheaf = sheaf
    this.turnstile = new Turnstile
}

Journalist.prototype.hold = function (parts) {
    var filename = path.resolve.apply(path, [ this._directory ].concat(parts))
    var cartridge = this._magazine.hold(filename, null)
    if (cartridge.value == null) {
        var stream = fs.createWriteStream(filename, { flags: 'a' })
        cartridge.value = new Staccato.Writable(stream)
    }
    return cartridge
}

Journalist.prototype.close = cadence(function (async, parts) {
    var filename = path.resolve.apply(path, [ this._directory ].concat(parts))
    var cartridge = this._magazine.hold(filename, null)
    async(function () {
        if (cartridge.value != null) {
            cartridge.value.end(async())
        }
    }, function () {
        cartridge.release()
    })
})

Journalist.prototype.load = restrictor.enqueue('canceled', cadence(function (async, id) {
    console.log('>>>', id)
    var cartridge = this._sheaf.hold(id, null)
    async([function () {
        cartridge.release()
    }], function () {
        if (cartridge.value != null) {
            return [ async.return ]
        }
        var filename = path.resolve(this._directory, 'pages', String(id))
        var items = []
        async(function () {
            fs.readFile(filename, 'utf8', async())
        }, function (entries) {
            console.log('here', filename)
            entries = entries.split('\n')
            entries.pop()
            entries = entries.map(function (entry) { return JSON.parse(entry) })
            entries.forEach(function (entry) {
                console.log('!', entry)
                switch (entry.method) {
                case 'add':
                    items.splice(entry.index, 0, entry.value)
                    break
                case 'remove':
                    items.splice(entry.index, 1)
                    break
                }
            })
            cartridge.value = { id: id, leaf: id % 2 == 1, items: items, ghosts: 0 }
            console.log('exiting')
            return []
        })
    })
}))

module.exports = Journalist

@@ -444,751 +444,8 @@
 %0A%7D%0A%0A
-Journalist.prototype.hold = function (parts) %7B%0A    var filename = path.resolve.apply(path, %5B this._directory %5D.concat(parts))%0A    var cartridge = this._magazine.hold(filename, null)%0A    if (cartridge.value == null) %7B%0A        var stream = fs.createWriteStream(filename, %7B flags: 'a' %7D)%0A        cartridge.value = new Staccato.Writable(stream)%0A    %7D%0A    return cartridge%0A%7D%0A%0AJournalist.prototype.close = cadence(function (async, parts) %7B%0A    var filename = path.resolve.apply(path, %5B this._directory %5D.concat(parts))%0A    var cartridge = this._magazine.hold(filename, null)%0A    async(function () %7B%0A        if (cartridge.value != null) %7B%0A            cartridge.value.end(async())%0A        %7D%0A    %7D, function () %7B%0A        cartridge.release()%0A    %7D)%0A%7D)%0A%0A
 Jour

Remove `Journalist` queue.