-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
139 lines (113 loc) · 3.13 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var resolve = require('./path-resolve')
var parseDeps = require('./parse-dependencies')
var Box = require('./box')
module.exports = App
function App () {}
App.prototype.use = function (fn, var_args) {
fn.apply(this, [].slice.call(arguments, 1))
return this
}
App.prototype.on = function (path, deps, fn) {
if (typeof deps == 'function') {
fn = deps
deps = fn.deps || parseDeps(fn)
}
fn = fn || NOOP
deps = deps || []
var p = this._resolve(path)
this['_box_proto_' + p] = new Box(p, deps, fn)
return this
}
App.prototype.run = function (path) {
var instance = Object.create(this)
path && instance.eval(this._resolve(path))
return instance
}
App.prototype.eval = function (path, cb) {
var box = this._box(this._resolve(path))
cb = cb || NOOP
if (box.isReady) {
cb(box.val)
return
}
box.eval(this, cb)
return this
}
App.prototype._box = function (p) {
var box = this['_box_' + p]
if (box) return box
var proto = this['_box_proto_' + p]
if (!proto) throw new Error('Box ' + p + ' is not defined')
return this['_box_' + p] = Object.create(proto)
}
App.prototype.get = function (path) {
var p = this._resolve(path)
var box = this['_box_' + p] || this['_box_proto_' + p]
if (!box) return
if (!box.isReady) throw new Error('Box ' + p + 'is not yet evaluated')
return box.val
}
App.prototype.set = function (path, val) {
this['_box_' + this._resolve(path)] = new Box.Evaluated(val)
return this
}
App.prototype.isReady = function (path) {
var key = '_box_' + this._resolve(path)
return !!this[key] && this[key].isReady
}
App.prototype.onerror = function (path, fn) {
if (typeof path == 'function') {
fn = path
path = '&'
}
this['_onerror_' + this._resolve(path)] = fn
return this
}
App.prototype._raise = function (p, e) {
var handler, parent = p, self = this
do {
p = parent
handler = this['_onerror_' + p]
parent = p.replace(/\/[^\/]*$/g, '') // trim last path segment
if (parent == p) parent = ''
} while (!handler)
var proxy = p ? new Proxy(this, p) : this
function raise (err) {
if (err == null) return
if (p == parent) throw err
self._raise(parent, err)
}
try {
handler.call(proxy, e, raise)
} catch (err) {
raise(err)
}
}
App.prototype._onerror_ = function (e) {
throw e
}
App.prototype._resolve = function (path) {
return resolve('', path)
}
App.prototype.prefix = function (p) {
return new Proxy(this, p)
}
function Proxy (app, prefix) {
this.prefix = prefix
this.app = app
}
;['get', 'set', 'isReady'].forEach(function (meth) {
Proxy.prototype[meth] = function (path) {
path = resolve(this.prefix, path)
return this.app[meth].apply(this.app, arguments)
}
})
;['on', 'onerror', 'eval'].forEach(function (meth) {
Proxy.prototype[meth] = function (path) {
path = resolve(this.prefix, path)
this.app[meth].apply(this.app, arguments)
return this
}
})
Proxy.prototype.use = App.prototype.use
function NOOP () {}