-
Notifications
You must be signed in to change notification settings - Fork 286
/
promise-assembler.js
160 lines (133 loc) · 3.77 KB
/
promise-assembler.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
/**
Original implementation and the idea behind the `PromiseAssembler`,
`Promise` model, and other work related to promise inspection was done
by Stefan Penner (@stefanpenner) thanks to McGraw Hill Education (@mhelabs)
and Yapp Labs (@yapplabs).
*/
import Promise from 'ember-debug/models/promise';
const Ember = window.Ember;
const { Object: EmberObject, Evented, A, computed, RSVP, copy, isNone } = Ember;
let PromiseAssembler = EmberObject.extend(Evented, {
// RSVP lib to debug
RSVP,
all: computed(function() { return A(); }),
promiseIndex: computed(function() { return {}; }),
// injected on creation
promiseDebug: null,
start() {
this.RSVP.configure('instrument', true);
this.promiseChained = e => {
chain.call(this, e);
};
this.promiseRejected = e => {
reject.call(this, e);
};
this.promiseFulfilled = e => {
fulfill.call(this, e);
};
this.promiseCreated = e => {
create.bind(this)(e);
};
this.RSVP.on('chained', this.promiseChained);
this.RSVP.on('rejected', this.promiseRejected);
this.RSVP.on('fulfilled', this.promiseFulfilled);
this.RSVP.on('created', this.promiseCreated);
},
stop() {
this.RSVP.configure('instrument', false);
this.RSVP.off('chained', this.promiseChained);
this.RSVP.off('rejected', this.promiseRejected);
this.RSVP.off('fulfilled', this.promiseFulfilled);
this.RSVP.off('created', this.promiseCreated);
this.get('all').forEach(item => {
item.destroy();
});
this.set('all', A());
this.set('promiseIndex', {});
this.promiseChained = null;
this.promiseRejected = null;
this.promiseFulfilled = null;
this.promiseCreated = null;
},
willDestroy() {
this.stop();
this._super();
},
createPromise(props) {
let promise = Promise.create(props);
let index = this.get('all.length');
this.get('all').pushObject(promise);
this.get('promiseIndex')[promise.get('guid')] = index;
return promise;
},
find(guid) {
if (guid) {
const index = this.get('promiseIndex')[guid];
if (index !== undefined) {
return this.get('all').objectAt(index);
}
} else {
return this.get('all');
}
},
findOrCreate(guid) {
return this.find(guid) || this.createPromise({ guid });
},
updateOrCreate(guid, properties) {
let entry = this.find(guid);
if (entry) {
entry.setProperties(properties);
} else {
properties = copy(properties);
properties.guid = guid;
entry = this.createPromise(properties);
}
return entry;
}
});
export default PromiseAssembler;
function fulfill(event) {
const guid = event.guid;
const promise = this.updateOrCreate(guid, {
label: event.label,
settledAt: event.timeStamp,
state: 'fulfilled',
value: event.detail
});
this.trigger('fulfilled', { promise });
}
function reject(event) {
const guid = event.guid;
const promise = this.updateOrCreate(guid, {
label: event.label,
settledAt: event.timeStamp,
state: 'rejected',
reason: event.detail
});
this.trigger('rejected', { promise });
}
function chain(event) {
let guid = event.guid;
let promise = this.updateOrCreate(guid, {
label: event.label,
chainedAt: event.timeStamp
});
let children = promise.get('children');
let child = this.findOrCreate(event.childGuid);
child.set('parent', promise);
children.pushObject(child);
this.trigger('chained', { promise, child });
}
function create(event) {
const guid = event.guid;
const promise = this.updateOrCreate(guid, {
label: event.label,
createdAt: event.timeStamp,
stack: event.stack
});
// todo fix ordering
if (isNone(promise.get('state'))) {
promise.set('state', 'created');
}
this.trigger('created', { promise });
}