Skip to content

Commit

Permalink
add event type (#1923)
Browse files Browse the repository at this point in the history
* add event type

* event type literal union of string

* modify ts test for custom events and emit

* core: add comment about literalunion hack
  • Loading branch information
ofhope authored and goto-bus-stop committed Nov 18, 2019
1 parent ad74af8 commit 9abb4e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
19 changes: 19 additions & 0 deletions packages/@uppy/core/types/core-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {})
}
13 changes: 10 additions & 3 deletions packages/@uppy/core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down

0 comments on commit 9abb4e2

Please sign in to comment.