/
Node.js
214 lines (136 loc) · 5.63 KB
/
Node.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
Class('KiokuJS.Node', {
use : 'Data.Visitor',
does : 'KiokuJS.Role.Resolvable',
has : {
// stored attributes
ID : null,
className : undefined,
classVersion : undefined,
objTraits : undefined,
isRoot : undefined,
data : null,
// run-time attributes
object : {
is : 'rw'
},
// arbitrary data about object
objectData : null,
typeMap : {
is : 'ro',
lazy : function () { return this.getTypeMapFor(this.className) }
},
resolver : {
is : 'rw',
required : true
},
entry : {
is : 'ro',
lazy : 'this.buildEntry'
},
selfReference : {
is : 'ro',
lazy : 'this.buildSelfReference'
},
// node will produce separate first-class (extrinisic) entry unless it (or its typemap) has `intrinsic` flag set
extrinsic : false,
// node will produce intrinsic entries
intrinsic : false,
// node will be skipped from the collapse result (effectively from all store operations)
immutable : false,
// node will contain a lazy reference
lazy : false
},
methods : {
initialize : function () {
var object = this.object
var className = this.className
if (!className && !object) throw "Either `object` or `className` with `data` must be supplied during instantiation of node [" + this + "]"
if (!className) this.className = this.getClassNameFor(object)
},
isLive : function () {
return this.object != null
},
isFirstClass : function () {
return this.ID != null
},
isIntrinsic : function () {
return this.intrinsic || this.getTypeMap().intrinsic
},
getClass : function () {
return eval(this.className)
},
acquireID : function (desiredId) {
var ID = this.ID
if (ID) {
if (desiredId != null && ID != desiredId) throw "Attempt to redefine the ID of node [" + this + "] from [" + ID + "] to [" + desiredId + "]"
return
}
this.ID = this.getTypeMap().acquireID(this, desiredId)
},
// XXX implement clear/predicate for attribute
clearEntry : function () {
delete this.entry
},
clearInstance : function () {
if (!this.object) throw "Node [" + this + "] doesn't contain an object instance to clear"
this.getTypeMap().clearInstance(this)
},
buildEntry : function () {
var entry = {
className : this.className,
classVersion : this.classVersion,
objTraits : this.objTraits,
isRoot : this.isRoot,
data : this.data
}
if (this.ID != null) entry.ID = this.ID
return entry
},
buildSelfReference : function () {
var ref = {
$ref : this.ID
}
if (this.lazy) ref.type = 'lazy'
return ref
},
collapse : function (collapser) {
this.data = this.getTypeMap().collapse(this, collapser)
},
createEmptyInstance : function () {
if (this.object) throw "Node [" + this + "] already contain an object instance"
return this.object = this.getTypeMap().createEmptyInstance(this)
},
populate : function (expander) {
if (!this.object) throw "Node [" + this + "] doesn't contain the object - can't be expanded"
this.getTypeMap().populate(this, expander)
return this.object
},
consumeOldNode : function (oldNode) {
this.object = oldNode.object
// this.isRoot = oldNode.isRoot seems this will be returned from DB in entry anyway
// XXX also copy `intrinsic, extrinsic, immutable and lazy` ?
// seems it will be recalculated during collapsing anyway?
},
consumeEntry : function (entry) {
this.clearEntry()
}
},
my : {
has : {
HOST : null
},
methods : {
newFromEntry : function (entry, resolver) {
entry.resolver = resolver
return new this.HOST(entry)
},
newFromObject : function (object, resolver) {
Data.Visitor.assignRefAdrTo(object)
return new this.HOST({
object : object,
resolver : resolver
})
}
}
}
})