Skip to content
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

event: overhaul api #196

Merged
merged 48 commits into from
Apr 2, 2022
Merged

event: overhaul api #196

merged 48 commits into from
Apr 2, 2022

Conversation

200sc
Copy link
Contributor

@200sc 200sc commented Mar 12, 2022

  • adds type safe bindings / triggers
  • decreases api surface considerably
  • loops replaced with concurrent executions

TODO before completion:

  • Rewrite tests
  • Rewrite all other oak package callers to use the new package API
  • Finish re-documenting the package
  • Evaluate if there are needed helper methods for ease of use

- adds type safe bindings / triggers
- decreases api surface considerably
- loops replaced with concurrent executions
@200sc 200sc added this to the 4.0 milestone Mar 12, 2022
@200sc 200sc self-assigned this Mar 12, 2022
@200sc 200sc linked an issue Mar 12, 2022 that may be closed by this pull request
event/bind.go Outdated
eb.Bind(name, 0, fn)
// A Bindable is a strongly typed callback function to be executed on Trigger. It must be paired
// with an event registered via RegisterEvent.
type Bindable[Payload any, C any] func(C, Payload) Response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate to say it but I think we should change this to not flip args.
When newer implementers are using this the error messages are a bit confusing due to the flip.
Take for example this error message I got while going through the click propagation example:
.\main.go:56:48: type func(me *"github.com/oakmound/oak/v3/mouse".Event, box *hoverButton) event.Response of func(me *mouse.Event, box hoverButton) event.Response {…} does not match inferred type event.Bindable["github.com/oakmound/oak/v3/mouse".Event, *hoverButton] for event.Bindable[vent, *hoverButton] for event.Bindable[Payload, C]

After writing a few its a pretty well known paradigm but it feels like if we are to make this switch it would be now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, you're suggesting:

--- a/event/bind.go
+++ b/event/bind.go
@@ -81,9 +81,9 @@ func (bus *Bus) Unbind(loc Binding) {

 // A Bindable is a strongly typed callback function to be executed on Trigger. It must be paired
 // with an event registered via RegisterEvent.
-type Bindable[Payload any, C any] func(C, Payload) Response
+type Bindable[C any, Payload any] func(C, Payload) Response

-func Bind[Payload any, C Caller](b Handler, ev EventID[Payload], c C, fn Bindable[Payload, C]) Binding {
+func Bind[C Caller, Payload any](b Handler, ev EventID[Payload], c C, fn Bindable[C, Payload]) Binding {
        return b.UnsafeBind(ev.UnsafeEventID, c.CID(), func(c CallerID, h Handler, payload interface{}) Response {
                typedPayload := payload.(Payload)
                ent := h.GetCallerMap().GetEntity(c)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly!
How do you feel about that rewrite?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good improvement; it won't require any refactoring either, as the type arguments should be implied from the inputs.

Implausiblyfun and others added 19 commits April 2, 2022 10:45
@codecov-commenter
Copy link

codecov-commenter commented Apr 2, 2022

Codecov Report

Merging #196 (e27b187) into release/v4.0.0 (135b087) will decrease coverage by 0.84%.
The diff coverage is 83.05%.

❗ Current head e27b187 differs from pull request most recent head 8dd2b59. Consider uploading reports for the commit 8dd2b59 to get more accurate results

@@                Coverage Diff                 @@
##           release/v4.0.0     #196      +/-   ##
==================================================
- Coverage           92.39%   91.54%   -0.85%     
==================================================
  Files                 141      138       -3     
  Lines                6456     6222     -234     
==================================================
- Hits                 5965     5696     -269     
- Misses                430      459      +29     
- Partials               61       67       +6     
Impacted Files Coverage Δ
config.go 97.82% <ø> (-0.41%) ⬇️
default.go 0.00% <0.00%> (ø)
lifecycle.go 72.05% <ø> (ø)
mouse/events.go 0.00% <0.00%> (ø)
scene/map.go 100.00% <ø> (ø)
scene/scene.go 100.00% <ø> (ø)
debugstream/scopeHelper.go 75.90% <25.00%> (ø)
inputTracker.go 62.06% <58.82%> (-14.86%) ⬇️
inputLoop.go 41.42% <61.53%> (-20.34%) ⬇️
window.go 59.64% <61.76%> (-7.31%) ⬇️
... and 38 more

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@200sc 200sc marked this pull request as ready for review April 2, 2022 18:12
@200sc 200sc merged commit fc32cea into release/v4.0.0 Apr 2, 2022
@200sc 200sc deleted the feature/v4-events branch April 2, 2022 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generic, type safe event handling with Go 1.18
3 participants