expose EventEmitter in our API, fixes #2359

This commit is contained in:
Johannes Rieken 2016-01-27 12:18:11 +01:00
parent fb96feb3f6
commit 6250b6531e
3 changed files with 39 additions and 17 deletions

View file

@ -6,7 +6,7 @@
'use strict'; 'use strict';
import * as assert from 'assert'; import * as assert from 'assert';
import {workspace, TextDocument, window, Position, Uri, CancellationTokenSource, Disposable} from 'vscode'; import {workspace, TextDocument, window, Position, Uri, EventEmitter, CancellationTokenSource, Disposable} from 'vscode';
import {createRandomFile, deleteFile, cleanUp, pathEquals} from './utils'; import {createRandomFile, deleteFile, cleanUp, pathEquals} from './utils';
import {join, basename} from 'path'; import {join, basename} from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
@ -286,26 +286,16 @@ suite('workspace-namespace', () => {
test('registerTextDocumentContentProvider, change event', function() { test('registerTextDocumentContentProvider, change event', function() {
let callCount = 0; let callCount = 0;
let listeners: Function[] = []; let emitter = new EventEmitter<Uri>();
let registration = workspace.registerTextDocumentContentProvider('foo', { let registration = workspace.registerTextDocumentContentProvider('foo', {
onDidChange(callback, thisArg, disposables) { onDidChange: emitter.event,
let actual = thisArg ? callback.bind(thisArg) : callback;
listeners.push(actual);
let subscription = new Disposable(() => {
const idx = listeners.indexOf(actual);
listeners.splice(idx, 1);
});
if (Array.isArray(disposables)) {
disposables.push(subscription);
}
return subscription;
},
provideTextDocumentContent(uri) { provideTextDocumentContent(uri) {
return 'call' + (callCount++); return 'call' + (callCount++);
} }
}); });
const uri = Uri.parse('foo://testing/path2'); const uri = Uri.parse('foo://testing/path3');
return workspace.openTextDocument(uri).then(doc => { return workspace.openTextDocument(uri).then(doc => {
@ -320,7 +310,7 @@ suite('workspace-namespace', () => {
resolve(); resolve();
}); });
listeners.forEach(l => l(doc.uri)); emitter.fire(doc.uri);
registration.dispose(); registration.dispose();
}); });

29
src/vs/vscode.d.ts vendored
View file

@ -999,6 +999,35 @@ declare namespace vscode {
(listener: (e: T) => any, thisArgs?: any, disposables?: Disposable[]): Disposable; (listener: (e: T) => any, thisArgs?: any, disposables?: Disposable[]): Disposable;
} }
/**
* An event emitter can be used to create and manage an [event](#Event) for others
* to subscribe to. One emitter always owns one event.
*
* Use this class if you want to provide event from within your extension, for instance
* inside a [TextDocumentContentProvider](#TextDocumentContentProvider)mor when providing
* API to other extensions.
*/
export class EventEmitter<T> {
/**
* The event listeners can subscribe to.
*/
event: Event<T>;
/**
* Notify all subscribers of the [event](EventEmitter#event). Failure
* of one or more listener will not fail this function call.
*
* @param data The event object.
*/
fire(data?: T): void;
/**
* Dispose this object and free resources.
*/
dispose(): void;
}
/** /**
* A file system watcher notifies about changes to files and folders * A file system watcher notifies about changes to files and folders
* on disk. * on disk.

View file

@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
'use strict'; 'use strict';
import {Emitter} from 'vs/base/common/event';
import {IBracketElectricCharacterContribution} from 'vs/editor/common/modes/supports'; import {IBracketElectricCharacterContribution} from 'vs/editor/common/modes/supports';
import {score} from 'vs/editor/common/modes/languageSelector'; import {score} from 'vs/editor/common/modes/languageSelector';
import {Remotable, IThreadService} from 'vs/platform/thread/common/thread'; import {Remotable, IThreadService} from 'vs/platform/thread/common/thread';
@ -71,6 +72,7 @@ export class ExtHostAPIImplementation {
Range: typeof vscode.Range; Range: typeof vscode.Range;
Selection: typeof vscode.Selection; Selection: typeof vscode.Selection;
CancellationTokenSource: typeof vscode.CancellationTokenSource; CancellationTokenSource: typeof vscode.CancellationTokenSource;
EventEmitter: typeof vscode.EventEmitter;
Hover: typeof vscode.Hover; Hover: typeof vscode.Hover;
DocumentHighlightKind: typeof vscode.DocumentHighlightKind; DocumentHighlightKind: typeof vscode.DocumentHighlightKind;
DocumentHighlight: typeof vscode.DocumentHighlight; DocumentHighlight: typeof vscode.DocumentHighlight;
@ -106,7 +108,8 @@ export class ExtHostAPIImplementation {
this.Uri = URI; this.Uri = URI;
this.Location = extHostTypes.Location; this.Location = extHostTypes.Location;
this.Diagnostic = <any> extHostTypes.Diagnostic; this.Diagnostic = <any> extHostTypes.Diagnostic;
this.DiagnosticSeverity = <any> extHostTypes.DiagnosticSeverity; this.DiagnosticSeverity = <any>extHostTypes.DiagnosticSeverity;
this.EventEmitter = Emitter;
this.Disposable = extHostTypes.Disposable; this.Disposable = extHostTypes.Disposable;
this.TextEdit = extHostTypes.TextEdit; this.TextEdit = extHostTypes.TextEdit;
this.WorkspaceEdit = extHostTypes.WorkspaceEdit; this.WorkspaceEdit = extHostTypes.WorkspaceEdit;