<commit_before>var Rx = require("rx/dist/rx");
var pathSyntax = require("falcor-path-syntax");

module.exports = function deref(boundPathArg) {

    var model = this;
    var modelRoot = model._root;
    var pathsIndex = -1;
    var pathsCount = arguments.length - 1;
    var paths = new Array(pathsCount);

    var boundPath = pathSyntax.fromPath(boundPathArg);

    while (++pathsIndex < pathsCount) {
        paths[pathsIndex] = pathSyntax.fromPath(arguments[pathsIndex + 1]);
    }

    if (modelRoot.syncRefCount <= 0 && pathsCount === 0) {
        throw new Error("Model#deref requires at least one value path.");
    }

    return Rx.Observable.defer(function() {
        var value;
        var errorHappened = false;
        try {
            ++modelRoot.syncRefCount;
            value = model._derefSync(boundPath);
        } catch (e) {
            value = e;
            errorHappened = true;
        } finally {
            --modelRoot.syncRefCount;
        }
        return errorHappened ?
            Rx.Observable.throw(value) :
            Rx.Observable.return(value);
    }).
    flatMap(function(boundModel) {
        if (Boolean(boundModel)) {
            if (pathsCount > 0) {
                return boundModel.get.
                    apply(boundModel, paths).
                    map(function() {
                        return boundModel;
                    }).
                    catch(Rx.Observable.empty());
            }
            return Rx.Observable.return(boundModel);
        } else if (pathsCount > 0) {
            return model.
                get.apply(model, paths.map(function(path) {
                    return boundPath.concat(path);
                })).
                map(function() {
                    return model.deref(boundPath);
                }).
                mergeAll();
        }
        return Rx.Observable.empty();
    });
};<commit_msg>Fix deref completing without onNext'ing, if preload paths fail<commit_after>var Rx = require("rx/dist/rx");
var pathSyntax = require("falcor-path-syntax");

module.exports = function deref(boundPathArg) {

    var model = this;
    var modelRoot = model._root;
    var pathsIndex = -1;
    var pathsCount = arguments.length - 1;
    var paths = new Array(pathsCount);

    var boundPath = pathSyntax.fromPath(boundPathArg);

    while (++pathsIndex < pathsCount) {
        paths[pathsIndex] = pathSyntax.fromPath(arguments[pathsIndex + 1]);
    }

    if (modelRoot.syncRefCount <= 0 && pathsCount === 0) {
        throw new Error("Model#deref requires at least one value path.");
    }

    return Rx.Observable.defer(function() {
        var value;
        var errorHappened = false;
        try {
            ++modelRoot.syncRefCount;
            value = model._derefSync(boundPath);
        } catch (e) {
            value = e;
            errorHappened = true;
        } finally {
            --modelRoot.syncRefCount;
        }
        return errorHappened ?
            Rx.Observable.throw(value) :
            Rx.Observable.return(value);
    }).
    flatMap(function(boundModel) {
        if (Boolean(boundModel)) {
            if (pathsCount > 0) {

                return boundModel.get.
                    apply(boundModel, paths).
                    map(function() {
                        return boundModel;
                    }).
                    catch(Rx.Observable.of(boundModel)).
                    take(1);

            }
            return Rx.Observable.return(boundModel);
        } else if (pathsCount > 0) {
            return model.
                get.apply(model, paths.map(function(path) {
                    return boundPath.concat(path);
                })).
                map(function() {
                    return model.deref(boundPath);
                }).
                mergeAll();
        }
        return Rx.Observable.empty();
    });
};
{'fraction_non_alphanumeric': 0.11236821805091284, 'fraction_numerical': 0.0038570326562098224, 'mean_word_length': 1.5881570192947438, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7346963', 'n_tokens_mistral': 1099, 'n_tokens_neox': 1025, 'n_words': 217}