| 1 |
"use strict"; |
| 2 |
|
| 3 |
var invoke = require("es5-ext/function/invoke") |
| 4 |
, assign = require("es5-ext/object/assign") |
| 5 |
, isValue = require("es5-ext/object/is-value") |
| 6 |
, forEach = require("es5-ext/object/for-each") |
| 7 |
, deferred = require("deferred") |
| 8 |
, dirname = require("path").dirname; |
| 9 |
|
| 10 |
// eslint-disable-next-line no-empty-function |
| 11 |
var FindRoot = function () {}; |
| 12 |
FindRoot.prototype = { |
| 13 |
known: false, |
| 14 |
close: function () { |
| 15 |
if (this.promises) { |
| 16 |
forEach(this.promises, invoke("close")); |
| 17 |
this.known = true; |
| 18 |
delete this.promises; |
| 19 |
} |
| 20 |
if (!this.promise.resolved) { |
| 21 |
this.reject(new Error("Find root action cancelled")); |
| 22 |
} |
| 23 |
}, |
| 24 |
onvalue: function (value) { |
| 25 |
if (this.known) { |
| 26 |
return; |
| 27 |
} |
| 28 |
if (value) { |
| 29 |
this.known = true; |
| 30 |
if (this.watch && this.promise.resolved) { |
| 31 |
this.promise.value = this.path; |
| 32 |
this.promise.emit("change", this.path); |
| 33 |
} else { |
| 34 |
this.resolve(this.path); |
| 35 |
} |
| 36 |
} else { |
| 37 |
this.down(); |
| 38 |
} |
| 39 |
}, |
| 40 |
next: function () { |
| 41 |
var isRoot; |
| 42 |
isRoot = this.isRoot(this.path); |
| 43 |
if (this.watch) { |
| 44 |
this.promises[this.path] = isRoot; |
| 45 |
isRoot.on("change", this.onevent); |
| 46 |
} |
| 47 |
isRoot.done(this.onvalue); |
| 48 |
}, |
| 49 |
down: function () { |
| 50 |
var dir = dirname(this.path); |
| 51 |
if (dir === this.path) { |
| 52 |
this.known = true; |
| 53 |
this.path = ""; |
| 54 |
if (this.watch && this.promise.resolved) { |
| 55 |
this.promise.value = null; |
| 56 |
this.promise.emit("change", null); |
| 57 |
} else { |
| 58 |
this.resolve(null); |
| 59 |
} |
| 60 |
return; |
| 61 |
} |
| 62 |
this.path = dir; |
| 63 |
this.next(); |
| 64 |
}, |
| 65 |
onevent: function (value, path) { |
| 66 |
var dir; |
| 67 |
if (value) { |
| 68 |
if (!isValue(path)) throw new TypeError("Path must be provided"); |
| 69 |
this.path = path; |
| 70 |
dir = dirname(path); |
| 71 |
while (this.promises[dir]) { |
| 72 |
this.promises[dir].close(); |
| 73 |
delete this.promises[dir]; |
| 74 |
dir = dirname(dir); |
| 75 |
} |
| 76 |
this.known = true; |
| 77 |
// Race condition may occur, double check |
| 78 |
if (this.promise.value !== this.path) { |
| 79 |
this.promise.value = this.path; |
| 80 |
this.promise.emit("change", this.path); |
| 81 |
} |
| 82 |
} else { |
| 83 |
if (!this.known || path !== this.path) { |
| 84 |
return; |
| 85 |
} |
| 86 |
this.known = false; |
| 87 |
this.down(); |
| 88 |
} |
| 89 |
} |
| 90 |
}; |
| 91 |
module.exports = function (isRoot, path/*, options*/) { |
| 92 |
var findRoot, options; |
| 93 |
options = arguments[2]; |
| 94 |
findRoot = new FindRoot(); |
| 95 |
findRoot.isRoot = isRoot; |
| 96 |
findRoot.path = path; |
| 97 |
findRoot.onvalue = findRoot.onvalue.bind(findRoot); |
| 98 |
findRoot.onevent = findRoot.onevent.bind(findRoot); |
| 99 |
assign(findRoot, deferred()); |
| 100 |
findRoot.watch = options && options.watch; |
| 101 |
if (findRoot.watch) { |
| 102 |
findRoot.promises = {}; |
| 103 |
findRoot.promise.close = findRoot.close.bind(findRoot); |
| 104 |
} |
| 105 |
findRoot.next(); |
| 106 |
return findRoot.promise; |
| 107 |
}; |