takedown
Version: 
Customizable markdown parser
49 lines (38 loc) • 1.36 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var actionResponse = require('../../lib/action-response.js');
function closer (self)
{
    /**
        Applies this agent to its parent (if existing).
        Remaining `chunk` is passed on to parent or self on 
        successful close.
        @param { String } chunk
            Most recent (rejected) source chunk.
    */
    return chunk =>
    {
        let { model, parent } = self;
        let closed = model.close(model.state);
        if (closed === true)
        {
            if (model.removeSameAncestor)
            {
                let agent = self;
                // find an ancestor with same name
                while (agent = agent.parent) 
                    if (agent.model.name === model.name) 
                        break;
                // abort the ancestor (results in error if root)
                if (agent) return agent.exec(chunk, actionResponse.default.abort());
            }
            // apply to parent if existing
            let agent = parent ? parent.apply(self) : self;
            // if a chunk was provided, pass it on
            return chunk ? agent.next(chunk) : agent;
        }
        // abort on unsuccessful close
        return self.exec(chunk, actionResponse.default.abort(closed));
    }
}
exports.default = closer;