-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: support nested event methods #5
Open
andiwils
wants to merge
6
commits into
main
Choose a base branch
from
feat/nested-event-methods
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c459f47
feat: support nested event methods
andiwils 0c29991
fix(tests): update tests for consistency
andiwils 76b61aa
fix(tests): update comment location after refactor
andiwils e10786c
chore(readme): add documentation about nested events
andiwils c038c3c
chore: update readme
andiwils 1bd3838
fix: update readme
andiwils File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,10 +22,45 @@ type eventHandlerType<Type> = Type extends TypedEvent<infer X> ? X : never; | |
* - The values are of typed 'TypedEvent'. | ||
* | ||
* @param Base - The class which will be extended with event subscription methods. | ||
* @param path - The path to the TypedEvent member in the Base class. This is optional and only needed | ||
* if the events are not directly on the Base class. | ||
* @returns A subclass of Base with event subscription methods added. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export function AddEvents<TBase extends Constructor, U>(Base: TBase) { | ||
export function AddEvents<TBase extends Constructor, U>(Base: TBase, path?: string) { | ||
/** | ||
* Get the event object from the given instance. If a path is given, the object will be traversed | ||
* using the path. | ||
* | ||
* @param instance - The instance to get the event object from. | ||
* @returns The event object. | ||
*/ | ||
function getEventObject(instance: any) { | ||
if (!path) { | ||
return instance; | ||
} | ||
return path.split('.').reduce((obj, key) => (obj ? obj[key] : undefined), instance); | ||
} | ||
|
||
/** | ||
* Get the event object from the given instance. If a path is given, the object will be traversed | ||
* using the path. If the event object is not found, an error will be thrown. | ||
* | ||
* @param instance - The instance to get the event object from. | ||
* @param eventName - The name of the event to get. | ||
* @returns The event. | ||
*/ | ||
function getEvent<K extends keyof U>(instance: any, eventName: K) { | ||
const eventsObject = getEventObject(instance); | ||
const event = eventsObject?.[eventName]; | ||
if (!event) { | ||
// Construct the full path for the error message | ||
const fullPath = path ? `${path}.${String(eventName)}` : String(eventName); | ||
throw new Error(`Event "${fullPath}" is not defined`); | ||
} | ||
return event; | ||
} | ||
|
||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
return class WithEvents extends Base { | ||
/** | ||
|
@@ -37,7 +72,7 @@ export function AddEvents<TBase extends Constructor, U>(Base: TBase) { | |
on<K extends keyof U, E extends eventHandlerType<U[K]>>(eventName: K, handler: E) { | ||
// Even though we bypass type safety in the call (casting this as any), we've enforced it in the | ||
// method signature above, so it's still safe. | ||
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 comment looks like it isn't really relevant here anymore...maybe better as a note on 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. Good catch. Done. |
||
(this as any)[eventName].on(handler); | ||
getEvent(this, eventName).on(handler); | ||
} | ||
|
||
/** | ||
|
@@ -49,7 +84,7 @@ export function AddEvents<TBase extends Constructor, U>(Base: TBase) { | |
* @param handler - The handler. | ||
*/ | ||
once<K extends keyof U, E extends eventHandlerType<U[K]>>(eventName: K, handler: E) { | ||
(this as any)[eventName].once(handler); | ||
getEvent(this, eventName).once(handler); | ||
} | ||
|
||
/** | ||
|
@@ -61,7 +96,7 @@ export function AddEvents<TBase extends Constructor, U>(Base: TBase) { | |
* @param handler - The handler. | ||
*/ | ||
off<K extends keyof U, E extends eventHandlerType<U[K]>>(eventName: K, handler: E) { | ||
(this as any)[eventName].off(handler); | ||
getEvent(this, eventName).off(handler); | ||
} | ||
}; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: for consistency, can you extract these out into an interface like the test above does? with them matching it makes it easier to see that what's different here is the path argument.
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.
Sure thing. Done.