-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ID-913 ZkEvm provider events [NO-CHANGELOG] #580
Changes from all commits
944846c
b9cf1bf
08e9be5
ac1ffcc
9a5f2c6
cddf95d
a5a6c8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import TypedEventEmitter from './typedEventEmitter'; | ||
|
||
type TestEvents = { | ||
testEvent1: [Array<number>]; | ||
testEvent2: [{ id: number }], | ||
}; | ||
|
||
describe('TypedEventEmitter', () => { | ||
it('should be able to emit and listen to events', () => { | ||
const eventEmitter = new TypedEventEmitter<TestEvents>(); | ||
|
||
const testEvent1Handler = jest.fn(); | ||
const testEvent2Handler = jest.fn(); | ||
|
||
eventEmitter.on('testEvent1', testEvent1Handler); | ||
eventEmitter.on('testEvent2', testEvent2Handler); | ||
|
||
eventEmitter.emit('testEvent1', [1, 2, 3]); | ||
eventEmitter.emit('testEvent2', { id: 1 }); | ||
|
||
expect(testEvent1Handler).toHaveBeenCalledWith([1, 2, 3]); | ||
expect(testEvent2Handler).toHaveBeenCalledWith({ id: 1 }); | ||
|
||
eventEmitter.removeListener('testEvent1', testEvent1Handler); | ||
eventEmitter.removeListener('testEvent2', testEvent2Handler); | ||
|
||
eventEmitter.emit('testEvent1', [4, 5, 6]); | ||
eventEmitter.emit('testEvent2', { id: 2 }); | ||
|
||
expect(testEvent1Handler).toHaveBeenCalledTimes(1); | ||
expect(testEvent2Handler).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { EventEmitter } from 'events'; | ||
|
||
export default class TypedEventEmitter<TEvents extends Record<string, any>> { | ||
private emitter = new EventEmitter(); | ||
|
||
emit<TEventName extends keyof TEvents & string>( | ||
eventName: TEventName, | ||
...eventArg: TEvents[TEventName] | ||
) { | ||
this.emitter.emit(eventName, ...(eventArg as [])); | ||
} | ||
|
||
on<TEventName extends keyof TEvents & string>( | ||
eventName: TEventName, | ||
handler: (...eventArg: TEvents[TEventName]) => void, | ||
) { | ||
this.emitter.on(eventName, handler as any); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Types look off in this class, can we avoid There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class comes from here, and the reason is:
|
||
} | ||
|
||
removeListener<TEventName extends keyof TEvents & string>( | ||
eventName: TEventName, | ||
handler: (...eventArg: TEvents[TEventName]) => void, | ||
) { | ||
this.emitter.removeListener(eventName, handler as any); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -65,14 +65,26 @@ export interface JsonRpcResponsePayload { | |||||
} | ||||||
|
||||||
export type Provider = { | ||||||
request: (request: RequestArguments) => Promise<any> | ||||||
request: (request: RequestArguments) => Promise<any>; | ||||||
sendAsync: ( | ||||||
request: JsonRpcRequestPayload | JsonRpcRequestPayload[], | ||||||
callback: JsonRpcRequestCallback, | ||||||
) => void | ||||||
) => void; | ||||||
send: ( | ||||||
request: string | JsonRpcRequestPayload | JsonRpcRequestPayload[], | ||||||
callbackOrParams?: JsonRpcRequestCallback | Array<any>, | ||||||
callback?: JsonRpcRequestCallback, | ||||||
) => void | ||||||
) => void; | ||||||
on: (event: string, listener: (...args: any[]) => void) => void; | ||||||
removeListener: (event: string, listener: (...args: any[]) => void) => void; | ||||||
}; | ||||||
|
||||||
export enum ProviderEventNames { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
ACCOUNTS_CHANGED = 'accountsChanged', | ||||||
} | ||||||
|
||||||
export type AccountsChangedEvent = Array<string>; | ||||||
|
||||||
export interface ProviderEvents extends Record<string, any> { | ||||||
[ProviderEventNames.ACCOUNTS_CHANGED]: [AccountsChangedEvent], | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './registerZkEvmUser'; | ||
export * from './loginZkEvmUser'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥