This commit is contained in:
Connor Peet 2017-06-21 21:16:16 -07:00
Родитель 0c2b57e6e5
Коммит 898cfdf7ed
3 изменённых файлов: 17 добавлений и 13 удалений

Просмотреть файл

@ -244,7 +244,7 @@ export class Lease extends EventEmitter {
// far past the end of our key's TTL, there's no way we're going to be
// able to renew it. Fire a "lost".
if (Date.now() - this.lastKeepAlive > 2 * 1000 * this.ttl) {
this.emit('lost', new GRPCConnectFailedError('We lost connect to etcd and our lease has expired.'));
this.emit('lost', new GRPCConnectFailedError('We lost connection to etcd and our lease has expired.'));
return this.close();
}

Просмотреть файл

@ -157,13 +157,23 @@ export function forOwn<T>(obj: T, iterator: <K extends keyof T>(value: T[K], key
* fire on the emitter.
*/
export function onceEvent(emitter: EventEmitter, ...events: string[]): Promise<any> {
return new Promise(resolve => {
const handler = (data: any) => {
events.forEach(name => emitter.removeListener(name, handler));
resolve(data);
return new Promise((resolve, reject) => {
const teardown: (() => void)[] = [];
const handler = (data: any, event: string) => {
teardown.forEach(t => t());
if (event === 'error') {
reject(data);
} else {
resolve(data);
}
};
events.forEach(event => emitter.once(event, handler));
events.forEach(event => {
const fn = (data: any) => handler(data, event);
teardown.push(() => emitter.removeListener(event, fn));
emitter.once(event, fn);
});
});
}

Просмотреть файл

@ -321,13 +321,7 @@ export class WatchBuilder {
this.namespace.applyToRequest(this.request),
);
return onceEvent(watcher, 'connected', 'error').then(res => {
if (res instanceof Error) {
throw res;
}
return watcher;
});
return onceEvent(watcher, 'connected').then(() => watcher);
}
}