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
Class-based namespaces #43
Conversation
This is an interesting idea. I'll play with this patch a bit before merging it, I think I want to make a minor change to have the namespace object is created, but overall I like the idea. Thanks! |
Thanks. Just for interest, do you see any advantage in creating the |
|
Hm, maybe it is worth thinking about not registering the different handlers in |
This also addresses #41. By overloading the |
Since I needed it right now I implemented the before/after stuff in a quite flexible way. It's in the docs as well under "Event handler middlewares". Even they are not 100% related to class-based namespaces I just added it to this branch, because they are most useful on a per namespace basis. If you generally like it you'll probably want to restructure it in some way, but the basic concept should be visible and is working. |
Oh, I found a bug in this but will fix it later today. |
Well, I think it's ready for revision now. Docs and also tests are written. |
This PR started well, but now I think it got very complicated. I like the idea of a simple class based mechanism, but I don't want to get so much stuff all in a single PR, makes it much more difficult to review and test. I'm not sure I get the idea of middlewares that you implemented. Seems easier to just follow the class-based approach and add the before and after event handlers in there. I was going to make a few minor changes to the PR and merge it, but given that the PR got so big, instead I just coded my own version of the class based namespaces, more in the style of your first implementation, but with some improvements that make it easier to document the |
You are right in that it's generally a bad idea to mix two issues into one PR. Sorry for that. I'm not yet too familiar to Github's features. Maybe there is a way to split it after a specific commit? At least I collapsed the commits belonging to a particular issue. Now there are just two commits left that can easily be differenciated. The second commit deals with the concept of event handler middlewares. I wrote a chapter in the docs about them. Basically it's a way to add before/after event handlers on three layers for fine-grained control: These layers can alter the arguments the event handler will get or its return value transparently or completely abort execution, returning something else.
Finally, when the event is triggered, the I wrote a test for it that is commented and it works well. Hopefully it won't get lost after all because it is so simple and powerful. EDIT: |
And I added all three layers you can add middlewares at to get the maximum flexibility. The cost in both code complexity and performance is not really higher for three than it is for one. |
I just reviewed what your implementation of class-based namespaces looks like. The following points are not yet perfect IMHO:
Maybe you want to take some of my code to change the mentioned points, or maybe you have reasons you preferred the way you went? Apart from that it looks nice. BTW: My middleware implementation would perfectly work on your class-based namespaces as well. |
Now it's even less new code, probably less than 100 lines at all that belongs to middlewares (excluding docs and tests of course). The docs got an update as well to be more detailled. Simply ask when there are questions. |
a0228ba
to
19aac61
Compare
19aac61
to
1783778
Compare
Yes. I documented that to use class based namespaces you have to use events that have legal method names. Having to replace characters in events makes the relationship between events and methods less clear, you can have two events going to the same method, for example. I prefer not to do that.
It's really not private, since another class is using it. I decided to leave it public, but not document it. It's really not a private method, since another class is using it.
Ah, yes, that is a mistake. I had it that way first, then decided against it. I'll fix it.
Because with this you can override an event and have it go to a decorated function, while everything else goes to the class. |
I have a mostly working implementation of the before and after events that I'm currently testing. I have been working on for a while, based on miguelgrinberg/Flask-SocketIO#265. To be consistent with the existing functionality I wanted these events to be decorator based. I think they can also be integrated with the class-based namespaces, but the main functionality is going to be based on decorators. I still don't see the need to have a separate middleware class. That seems overly complicated. |
I'm however not so happy with the event name restrictions. There are often event names having spaces or dots in them that don't work with the current approach. I actually got the middlewares separated from the namespaces. Should I open another PR for this that bases on current master? |
And, I think methods starting with an
|
@efficiosoft It's a minor detail though. I changed it to a As far as different rules for matching events to methods, I have documented the |
Yes, that was no criticism, I'm just not always sure how to use the I'm going to open another PR now for the middlewares. I wrote them compatible to master without conflicts. |
I think you need to wait until I finish my before and after event support. As I said above, I don't find your idea of middlewares that straightforward, and besides, there is already a middleware in this package (a WSGI one), so the choice of name is also not that great. |
Ok, I'll let it in that state now until you make a decision. I find it quite straightforward actually. It reminds me of the middleware system django provides. I've written one that applies encoding and decoding with msgpack, for example, in just a few lines of code. I post it in the new PR as an example. |
Hi,
I've implemented namespace-based grouping of event handlers into classes as a little enhancement to the
Server.on(...)
method which is currently the only available way to register event handlers. As this can become muddled rather quickly I wrote this extension. It also cares about setting default namespaces when callingemit
,disconnect
etc. on such aNamespace
object.Tests and docs are updated as well.
Hope it gets merged and will help some people writing their Socket.IO apps more easily.