-
Notifications
You must be signed in to change notification settings - Fork 759
Commit
* passes tests * Remove _StripeEvent class * Convert flows test to TS * Add PlatformFunctions test * Node 14 woes * Fix Node version check * fix tests * Make base PlatformFunction class * more reviewer comments * More createEmitter tests * wip * Build and actually resolve * Throw error in web workers for streaming files * Remove checkForStream * Add tests * Update src/Webhooks.ts Co-authored-by: pakrym-stripe <[email protected]> * reviewer comments * better types * more cleanup --------- Co-authored-by: pakrym-stripe <[email protected]>
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @private | ||
* (For internal use in stripe-node.) | ||
* Wrapper around the Event Web API. | ||
*/ | ||
class _StripeEvent extends Event { | ||
data?: RequestEvent | ResponseEvent; | ||
constructor(eventName: string, data: any) { | ||
super(eventName); | ||
this.data = data; | ||
} | ||
} | ||
|
||
type Listener = (...args: any[]) => any; | ||
type ListenerWrapper = (event: _StripeEvent) => void; | ||
|
||
/** Minimal EventEmitter wrapper around EventTarget. */ | ||
class StripeEmitter { | ||
eventTarget: EventTarget; | ||
listenerMapping: Map<Listener, ListenerWrapper>; | ||
|
||
constructor() { | ||
this.eventTarget = new EventTarget(); | ||
this.listenerMapping = new Map(); | ||
} | ||
|
||
on(eventName: string, listener: Listener): void { | ||
const listenerWrapper: ListenerWrapper = (event: _StripeEvent): void => { | ||
listener(event.data); | ||
}; | ||
this.listenerMapping.set(listener, listenerWrapper); | ||
return this.eventTarget.addEventListener(eventName, listenerWrapper); | ||
} | ||
|
||
removeListener(eventName: string, listener: Listener): void { | ||
const listenerWrapper = this.listenerMapping.get(listener); | ||
this.listenerMapping.delete(listener); | ||
return this.eventTarget.removeEventListener(eventName, listenerWrapper!); | ||
} | ||
|
||
once(eventName: string, listener: Listener): void { | ||
const listenerWrapper: ListenerWrapper = (event: _StripeEvent): void => { | ||
listener(event.data); | ||
}; | ||
this.listenerMapping.set(listener, listenerWrapper); | ||
return this.eventTarget.addEventListener(eventName, listenerWrapper, { | ||
once: true, | ||
}); | ||
} | ||
|
||
emit(eventName: string, data: RequestEvent | ResponseEvent): boolean { | ||
return this.eventTarget.dispatchEvent(new _StripeEvent(eventName, data)); | ||
} | ||
} | ||
|
||
export = StripeEmitter; |