-
Notifications
You must be signed in to change notification settings - Fork 110
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
Plugin methods #188
Open
FelixHenninger
wants to merge
4
commits into
main
Choose a base branch
from
plugin-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
Plugin methods #188
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7cd0b92
Pull out and test method name generation
FelixHenninger 128bfcc
Automatically call eponymous methods on plugin events
FelixHenninger 3c0c276
Rename plugin:add and :remove events to pluginAdd and pluginRemove
FelixHenninger 06ae4e1
Attempt inferred method types on Plugin
FelixHenninger 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,63 @@ | ||
import { Component } from './component' | ||
import { Plugin, PluginAPI } from './plugin' | ||
|
||
it('Calls plugin for registration', () => { | ||
const p = new Plugin() | ||
const spy = jest.spyOn(p, 'handle') | ||
const c = new Component({ id: 'c', plugins: [p] }) | ||
|
||
expect(spy).toHaveBeenCalledTimes(1) | ||
expect(spy).toHaveBeenCalledWith(c, 'plugin:add') | ||
}) | ||
|
||
it('Calls plugin handle method on component event', () => { | ||
const p = new Plugin() | ||
const spy = jest.spyOn(p, 'handle') | ||
const c = new Component({ id: 'c', plugins: [p] }) | ||
|
||
c.internals.emitter.trigger('foo', 'bar') | ||
|
||
expect(spy).toHaveBeenCalledTimes(2) | ||
expect(spy).toHaveBeenLastCalledWith(c, 'foo', 'bar') | ||
}) | ||
|
||
it('Default plugin calls handler methods by name', () => { | ||
const p = new Plugin() | ||
|
||
const spyHandle = jest.spyOn(p, 'handle') | ||
//@ts-ignore | ||
p.onPluginAdd = () => {} | ||
//@ts-ignore | ||
const spyPluginAdd = jest.spyOn(p, 'onPluginAdd') | ||
//@ts-ignore | ||
p.onFoo = () => {} | ||
//@ts-ignore | ||
const spyFoo = jest.spyOn(p, 'onFoo') | ||
|
||
const c = new Component({ id: 'c', plugins: [p] }) | ||
expect(spyHandle).toHaveBeenCalledTimes(1) | ||
expect(spyPluginAdd).toHaveBeenCalledTimes(1) | ||
expect(spyPluginAdd).toHaveBeenLastCalledWith(c) | ||
|
||
c.internals.emitter.trigger('foo', 'bar') | ||
expect(spyHandle).toHaveBeenCalledTimes(2) | ||
expect(spyFoo).toHaveBeenCalledTimes(1) | ||
expect(spyFoo).toHaveBeenLastCalledWith(c, 'bar') | ||
}) | ||
|
||
it('Calls plugin handler with appropriate context', () => { | ||
const p = new Plugin() | ||
const c = new Component({ id: 'c', plugins: [p] }) | ||
|
||
let context = undefined | ||
|
||
//@ts-ignore | ||
p.onFoo = function () { | ||
context = this | ||
} | ||
//@ts-ignore | ||
const spy = jest.spyOn(p, 'onFoo') | ||
c.internals.emitter.trigger('foo', 'bar') | ||
|
||
expect(context).toEqual(p) | ||
}) |
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
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,9 @@ | ||
import { getEventMethodName } from './eventName' | ||
|
||
it('Generates basic method names', () => { | ||
expect(getEventMethodName('foo')).toBe('onFoo') | ||
}) | ||
|
||
it('Handles split method names', () => { | ||
expect(getEventMethodName('foo:bar')).toBe('onFooBar') | ||
}) |
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,7 @@ | ||
// Event name splitter functions | ||
const splitter = /(^|:)(\w)/gi | ||
const getEventName = (_m: string, _pre: string, eventName: string) => | ||
eventName.toUpperCase() | ||
|
||
export const getEventMethodName = (event: string) => | ||
`on${event.replace(splitter, getEventName)}` |
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.
The dynamic dispatching implementation here is clever, I think there's a good way to do it in TS!
First off, it might be easier just to hardcode a handful of empty methods in this base class here and manually trigger them from the handle.
However, if you want to go full TypeScript wizard, it is now possible with Template Literal Types to perform string manipulation on keys within types. To say, define a number of
on${Event}
methods computed from a union type of events. Here's a tsplayground where I play around with it: https://www.typescriptlang.org/play?#code/C4TwDgpgBAogbhAdsAcgQwLbQLxQOQBOAronlAD75gERho1mV5IAmeAUO6JLAsgBJpELADYQCAYRFoAzjKi4A3uyiqoAbQDWEEFACWiKAAMA9ogAkiiWjB7gaEXoBeEADzwkqTBAB8AXyMAXQAuKAAKAEoFHyg4Ez0Wdj9OAGNpOV5PAAURIgBzA1cJKAgAD2BWeQAjExMxIQAaWBLyyszkdCwYvQwwMSxkeQ8BIVFxKVl5ZTUoMwAlEkioZRU1ZJmzLJo6GiWVmfW1MxhhPc4DpKggAThere 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.
Whoa, thanks! I had no idea this was possible in TS, this is truly excellent. I'll see if I can incorporate your example, many thanks for that!
Given the limited number of intrinsic string manipulation types it probably makes sense to rename some events, specifically
plugin:add
topluginAdd
etc., but that strikes me as a good idea anyway; as far as I know that's the only place we use the good ol'backbone.js
event syntax. (those were the days!)