Skip to content

Commit 706c67d

Browse files
committed
restructure
1 parent 49ad4ca commit 706c67d

24 files changed

+197
-24
lines changed

benchmarks/observer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
console.log('\nObserver\n')
22

3-
var Observer = require('../src/observer/observer')
3+
var Observer = require('../src/observe/observer')
44
var Emitter = require('../src/emitter')
55
var OldObserver = require('../../vue/src/observer')
66
var sideEffect = true

explorations/inheritance.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Observer = require('../src/observer/observer')
1+
var Observer = require('../src/observe/observer')
22
var _ = require('../src/util')
33

44
function Vue (options) {
@@ -87,9 +87,7 @@ function Vue (options) {
8787
// also proxy newly added keys.
8888
var self = this
8989
ob.on('added', function (key) {
90-
if (!self.hasOwnProperty(key)) {
91-
_.proxy(self, scope, key)
92-
}
90+
_.proxy(self, scope, key)
9391
})
9492

9593
}

src/api/asset-register.js

Whitespace-only changes.

src/api/config.js

Whitespace-only changes.

src/instance/data.js src/api/data.js

+16
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,26 @@ exports.$set = function () {
66

77
}
88

9+
exports.$add = function () {
10+
11+
}
12+
13+
exports.$delete = function () {
14+
15+
}
16+
917
exports.$watch = function () {
1018

1119
}
1220

1321
exports.$unwatch = function () {
1422

23+
}
24+
25+
exports.$toJSON = function () {
26+
27+
}
28+
29+
exports.$log = function () {
30+
1531
}

src/instance/dom.js src/api/dom.js

File renamed without changes.
File renamed without changes.

src/api/extend.js

Whitespace-only changes.

src/api/global.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var _ = require('../util')
2+
var config = require('../config')
3+
4+
/**
5+
* Configuration
6+
*/
7+
8+
exports.config = function () {
9+
10+
}
11+
12+
/**
13+
* Class inehritance
14+
*/
15+
16+
exports.extend = function () {
17+
18+
}
19+
20+
/**
21+
* Plugin system
22+
*/
23+
24+
exports.use = function () {
25+
26+
}
27+
28+
/**
29+
* Expose some internal utilities
30+
*/
31+
32+
exports.require = function () {
33+
34+
}
35+
36+
/**
37+
* Define asset registries and registration
38+
* methods on a constructor.
39+
*/
40+
41+
config.assetTypes.forEach(function (type) {
42+
var registry = '_' + type + 's'
43+
exports[registry] = {}
44+
45+
/**
46+
* Asset registration method.
47+
*
48+
* @param {String} id
49+
* @param {*} definition
50+
*/
51+
52+
exports[type] = function (id, definition) {
53+
this[registry][id] = definition
54+
}
55+
})
56+
57+
/**
58+
* This is pretty useful so we expose it as a global method.
59+
*/
60+
61+
exports.nextTick = _.nextTick
File renamed without changes.

src/api/require.js

Whitespace-only changes.

src/api/use.js

Whitespace-only changes.

src/config.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
module.exports = {}
1+
module.exports = {
2+
3+
assetTypes: [
4+
'directive',
5+
'filter',
6+
'partial',
7+
'effect',
8+
'component'
9+
]
10+
11+
}
File renamed without changes.

src/internal/init.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
exports._init = function (options) {
2+
3+
var data = options.data
4+
var parent = options.parent
5+
var scope = this._scope = parent
6+
? Object.create(parent._scope)
7+
: {}
8+
9+
// copy instantiation data into scope
10+
for (var key in data) {
11+
if (key in scope) {
12+
// key exists on the scope prototype chain
13+
// cannot use direct set here, because in the parent
14+
// scope everything is already getter/setter and we
15+
// need to overwrite them with Object.defineProperty.
16+
_.define(scope, key, data[key], true)
17+
} else {
18+
scope[key] = data[key]
19+
}
20+
}
21+
22+
// create observer
23+
// pass in noProto:true to avoid mutating the __proto__
24+
var ob = this._observer = Observer.create(scope, { noProto: true })
25+
var dob = Observer.create(data)
26+
var locked = false
27+
28+
// sync scope and original data.
29+
ob
30+
.on('set', guard(function (key, val) {
31+
data[key] = val
32+
}))
33+
.on('added', guard(function (key, val) {
34+
data.$add(key, val)
35+
}))
36+
.on('deleted', guard(function (key) {
37+
data.$delete(key)
38+
}))
39+
40+
// also need to sync data object changes to scope...
41+
// this would cause cycle updates, so we need to lock
42+
// stuff when one side updates the other
43+
dob
44+
.on('set', guard(function (key, val) {
45+
scope[key] = val
46+
}))
47+
.on('added', guard(function (key, val) {
48+
scope.$add(key, val)
49+
}))
50+
.on('deleted', guard(function (key) {
51+
scope.$delete(key)
52+
}))
53+
54+
function guard (fn) {
55+
return function (key, val) {
56+
if (locked || key.indexOf(Observer.pathDelimiter) > -1) {
57+
return
58+
}
59+
locked = true
60+
fn(key, val)
61+
locked = false
62+
}
63+
}
64+
65+
// relay change events from parent scope.
66+
// this ensures the current Vue instance is aware of
67+
// stuff going on up in the scope chain.
68+
if (parent) {
69+
var po = parent._observer
70+
;['set', 'mutate', 'added', 'deleted'].forEach(function (event) {
71+
po.on(event, function (key, a, b) {
72+
if (!scope.hasOwnProperty(key)) {
73+
ob.emit(event, key, a, b)
74+
}
75+
})
76+
})
77+
}
78+
79+
// proxy everything on self
80+
for (var key in scope) {
81+
_.proxy(this, scope, key)
82+
}
83+
84+
// also proxy newly added keys.
85+
var self = this
86+
ob.on('added', function (key) {
87+
_.proxy(self, scope, key)
88+
})
89+
90+
}
File renamed without changes.
File renamed without changes.

src/observer/observer.js src/observe/observer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Observer.create = function (value, options) {
8585
return value.$observer
8686
} if (_.isArray(value)) {
8787
return new Observer(value, ARRAY, options)
88-
} else if (_.isObject(value)) {
88+
} else if (_.isObject(value) && !value._scope) { // avoid Vue instance
8989
return new Observer(value, OBJECT, options)
9090
}
9191
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/util.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ exports.deepMixin = function (to, from) {
3636
*/
3737

3838
exports.proxy = function (to, from, key) {
39+
if (to.hasOwnProperty(key)) return
3940
Object.defineProperty(to, key, {
4041
enumerable: true,
4142
configurable: true,

src/vue.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
var _ = require('./util')
2-
var Compiler = require('./compiler/compiler')
1+
var _ = require('./util')
32

43
/**
54
* The exposed Vue constructor.
@@ -10,33 +9,31 @@ var Compiler = require('./compiler/compiler')
109
*/
1110

1211
function Vue (options) {
13-
this._compiler = new Compiler(this, options)
12+
this._init(options)
1413
}
1514

15+
var p = Vue.prototype
16+
1617
/**
17-
* Mixin instance methods
18+
* Mixin internal instance methods
1819
*/
1920

20-
var p = Vue.prototype
21-
_.mixin(p, require('./instance/lifecycle'))
22-
_.mixin(p, require('./instance/data'))
23-
_.mixin(p, require('./instance/dom'))
24-
_.mixin(p, require('./instance/events'))
21+
_.mixin(p, require('./internal/init'))
22+
_.mixin(p, require('./internal/compile'))
2523

2624
/**
27-
* Mixin asset registers
25+
* Mixin API instance methods
2826
*/
2927

30-
_.mixin(Vue, require('./api/asset-register'))
28+
_.mixin(p, require('./api/data'))
29+
_.mixin(p, require('./api/dom'))
30+
_.mixin(p, require('./api/events'))
31+
_.mixin(p, require('./api/lifecycle'))
3132

3233
/**
33-
* Static methods
34+
* Mixin global API
3435
*/
3536

36-
Vue.config = require('./api/config')
37-
Vue.use = require('./api/use')
38-
Vue.require = require('./api/require')
39-
Vue.extend = require('./api/extend')
40-
Vue.nextTick = require('./util').nextTick
37+
_.mixin(Vue, require('./api/global'))
4138

4239
module.exports = Vue

0 commit comments

Comments
 (0)