-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.js
242 lines (164 loc) · 5.85 KB
/
service.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// ----- Ember modules -----
import Service from 'ember-service'
import {assert} from 'ember-metal/utils'
import getOwner from 'ember-owner/get'
import computed from 'ember-computed'
// import get from 'ember-metal/get'
// import {A} from 'ember-array/utils'
// import on from 'ember-evented/on'
// ----- Third-party libraries -----
import RSVP from 'rsvp'
// ----- Own modules -----
import Node from 'ember-zen/node'
import NodeArray from 'ember-zen/node-array'
export default Service.extend({
// ----- Computed properties -----
owner : computed(function () { return getOwner(this) }),
// ----- Overridden methods -----
// ----- Public methods -----
dispatch (nodeOrPath, message, callback, params) {
const node = this._getNode(nodeOrPath)
if (node.get('isDestroying') || node.get('isDestroyed')) return
node.set('_isDispatchInProgress', true)
callback()
this.logStateChangeOnNode(node, message, params)
node.set('_isDispatchInProgress', false)
},
dispatchAction (nodeOrPath, actionName, ...args) {
const node = this._getNode(nodeOrPath)
const message = `action "${actionName}"`
const action = () => node.send(actionName, ...args)
this.dispatch(nodeOrPath, message, action, {actionName, args})
},
dispatchSet (nodeOrPath, message, key, value) {
const node = this._getNode(nodeOrPath)
message = message || `set \`${key}\``
assert(
`Attempted to dispatchSet ${key} on ${node.get('nodeName')}, but ${key} is not an attribute on ${node.get('nodeName')}`,
node._hasAttr(key)
)
const action = () => node._setAttr(key, value)
this.dispatch(node, message, action, {key, value})
},
dispatchSetProperties (nodeOrPath, message, obj) {
const node = this._getNode(nodeOrPath)
const keys = Object.keys(obj)
keys.forEach(key => {
assert(
`Attempted to dispatchSetProperties ${key} on ${node.get('nodeName')}, but ${key} is not an attribute on ${node.get('nodeName')}`,
node._hasAttr(key)
)
})
message = message || "set `" + keys.join("`, `") + "`"
const action = () => node._setAttrs(obj)
this.dispatch(node, message, action, {obj})
},
dispatchPromise (nodeOrPath, name, callback) {
const node = this._getNode(nodeOrPath)
const keyIsPending = `${name}IsPending`
const keyIsRejected = `${name}IsRejected`
const keyIsFulfilled = `${name}IsFulfilled`
const keyIsSettled = `${name}IsSettled`
const keyResponse = `${name}Response`
const keyError = `${name}Error`
const keyPromise = `${name}Promise`
const isPending = node.get(keyIsPending)
if (isPending) throw new Error(`Can't dispatch promise ${name} on node ${node.get('nodePath')} as it's already pending`)
this.dispatchSetProperties(node, `starting promise "${name}"`, {
[keyIsPending] : true,
[keyIsRejected] : false,
[keyIsFulfilled] : false,
[keyIsSettled] : false,
})
const promise = callback()
.then(response => {
this.dispatchSetProperties(node, `fulfilling promise "${name}"`, {
[keyIsPending] : false,
[keyIsRejected] : false,
[keyIsFulfilled] : true,
[keyIsSettled] : true,
[keyResponse] : response,
})
return response
})
.catch(error => {
this.dispatchSetProperties(node, `rejecting promise "${name}"`, {
[keyIsPending] : false,
[keyIsRejected] : true,
[keyIsFulfilled] : false,
[keyIsSettled] : true,
[keyError] : error,
})
return RSVP.reject(error)
})
this.set(keyPromise, promise)
return promise
},
createNode (nodeTypeName, props) {
const newNode = this._lookupNodeType(nodeTypeName)
assert(`Expecting instance of a Node or NodeArray (sub)class`,
newNode instanceof Node
|| newNode instanceof NodeArray
)
if (props) newNode.setProperties(props)
return newNode
},
logStateChangeOnNode (nodeOrPath, message, params = {}) {
// debugger
const node = this._getNode(nodeOrPath)
const nodePath = node.get('nodePath')
message = `[zen] ${nodePath}: ${message}`
const result = {
node,
nodePath,
nodeName : node.get('nodeName'),
nodeSnapshot : node.valueOf(),
stackTrace : this.captureStackTrace(),
...params,
}
const rootNode = node.get('rootNode')
if (node !== rootNode) {
const rootNodeName = rootNode.get('nodeName')
result.rootNode = rootNode
result.rootNodeName = rootNodeName
result.rootNodeSnapshot = rootNode.valueOf()
}
console.info(message, result)
},
restore (node, snapshot) {
return node.restore(snapshot)
},
captureStackTrace () {
// https://github.com/chaijs/assertion-error/blob/48599d08c8aeaebc048939f6bf4ff732cd74a093/index.js#L68-L79
const obj = {}
if (Error.captureStackTrace) {
Error.captureStackTrace(obj)
} else {
try {
throw new Error()
} catch (e) {
obj.stack = e.stack
}
}
return obj.stack
},
// ----- Private methods -----
_getNode (nodeOrPath) {
if (nodeOrPath instanceof Node || nodeOrPath instanceof NodeArray) return nodeOrPath
assert(`Must be either node or path to node, "${nodeOrPath}" given`, typeof nodeOrPath === 'string')
const node = this.get(nodeOrPath)
assert(`Node not found: "${nodeOrPath}"`, node)
assert(
'Node must be an instance of Node or NodeArray',
node instanceof Node || node instanceof NodeArray
)
return node
},
_lookupNodeType (nodeTypeName) {
const owner = this.get('owner')
const moduleName = `node:${nodeTypeName}`
const node = owner.lookup(moduleName)
assert(`Node class not found: "${nodeTypeName}"`, node)
return node
},
})