add event type (#1923)

* add event type

* event type literal union of string

* modify ts test for custom events and emit

* core: add comment about literalunion hack
This commit is contained in:
Alexis Hope 2019-11-19 00:49:10 +11:00 committed by Renée Kooi
parent ad74af8b96
commit 9abb4e2ba7
2 changed files with 29 additions and 3 deletions

View file

@ -50,3 +50,22 @@ import DefaultStore = require('@uppy/store-default')
meta: { path: 'path/to/file' }
})
}
{
const uppy = Uppy()
// can emit events with internal event types
uppy.emit('upload')
uppy.emit('complete', () => {})
uppy.emit('error', () => {})
// can emit events with custom event types
uppy.emit('dashboard:modal-closed', () => {})
// can register listners for internal events
uppy.on('upload', () => {})
uppy.on('complete', () => {})
uppy.on('error', () => {})
// can register listners on custom events
uppy.on('dashboard:modal-closed', () => {})
}

View file

@ -99,16 +99,23 @@ declare module Uppy {
totalProgress: number;
}
type LogLevel = 'info' | 'warning' | 'error';
// This hack accepts _any_ string for `Event`, but also tricks VSCode and friends into providing autocompletions
// for the names listed. https://github.com/microsoft/TypeScript/issues/29729#issuecomment-505826972
type LiteralUnion<T extends U, U = string> = T | (U & { });
type Event = LiteralUnion<'file-added' | 'file-removed' | 'upload' | 'upload-progress' | 'upload-success' | 'complete' | 'error' | 'upload-error' |
'upload-retry' | 'info-visible' | 'info-hidden' | 'cancel-all' | 'restriction-failed' | 'reset-progress'>;
class Uppy {
constructor(opts?: Partial<UppyOptions>);
on<TMeta extends IndexedObject<any> = {}>(event: 'upload-success', callback: (file: UppyFile<TMeta>, body: any, uploadURL: string) => void): Uppy;
on<TMeta extends IndexedObject<any> = {}>(event: 'complete', callback: (result: UploadResult<TMeta>) => void): Uppy;
on(event: string, callback: (...args: any[]) => void): Uppy;
off(event: string, callback: any): Uppy;
on(event: Event, callback: (...args: any[]) => void): Uppy;
off(event: Event, callback: any): Uppy;
/**
* For use by plugins only!
*/
emit(event: string, ...args: any[]): void;
emit(event: Event, ...args: any[]): void;
updateAll(state: object): void;
setState(patch: object): void;
getState<TMeta extends IndexedObject<any> = {}>(): State<TMeta>;