forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstub-item.js
124 lines (105 loc) · 2.99 KB
/
stub-item.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
import {Emitter, CompositeDisposable} from 'event-kit';
let key = 0;
export default class StubItem {
// StubItems should only be created by `create` and never constructed directly.
static create(name, props, uri = '') {
const stub = new StubItem(name, props, uri);
const override = {
_getStub: () => stub,
getElement: () => stub.getElement(),
destroy: stub.destroy.bind(stub),
};
const proxy = new Proxy(override, {
get(target, propName) {
const item = stub.getRealItem();
if (Reflect.has(target, propName)) {
return target[propName];
} else if (item && Reflect.has(item, propName)) {
let val = item[propName];
if (typeof val === 'function') {
val = val.bind(item);
}
return val;
} else {
let val = stub[propName];
if (typeof val === 'function') {
val = val.bind(stub);
}
return val;
}
},
});
return proxy;
}
constructor(name, props = {}, uri) {
this.emitter = new Emitter();
this.subscriptions = new CompositeDisposable();
this.name = name;
this.props = props;
this.uri = uri;
this.key = ++key;
this.element = document.createElement('div');
this.element.classList.add(`github-StubItem-${name}`);
this.realItem = null;
this.realItemPromise = new Promise(res => {
this.resolveRealItemPromise = res;
});
}
setRealItem(item) {
this.realItem = item;
if (this.realItem.getRealItemPromise) {
this.realItem.getRealItemPromise().then(this.resolveRealItemPromise);
} else {
this.resolveRealItemPromise(this.realItem);
}
this.emitter.emit('did-change-title');
this.emitter.emit('did-change-icon');
if (item.onDidChangeTitle) {
this.subscriptions.add(item.onDidChangeTitle((...args) => this.emitter.emit('did-change-title', ...args)));
}
if (item.onDidChangeIcon) {
this.subscriptions.add(item.onDidChangeIcon((...args) => this.emitter.emit('did-change-icon', ...args)));
}
if (item.onDidDestroy) {
this.subscriptions.add(item.onDidDestroy((...args) => {
this.realItem = null;
this.emitter.emit('did-destroy', ...args);
}));
}
}
getRealItemPromise() {
return this.realItemPromise;
}
getRealItem() {
return this.realItem;
}
getURI() {
return this.uri;
}
getTitle() {
return this.props.title || null;
}
getIconName() {
return this.props.iconName || null;
}
onDidChangeTitle(cb) {
return this.emitter.on('did-change-title', cb);
}
onDidChangeIcon(cb) {
return this.emitter.on('did-change-icon', cb);
}
getElement() {
return this.element;
}
onDidDestroy(cb) {
return this.emitter.on('did-destroy', cb);
}
destroy() {
this.resolveRealItemPromise(null);
this.subscriptions.dispose();
this.emitter.dispose();
if (this.realItem) {
this.realItem.destroy && this.realItem.destroy();
}
}
}