usrlocalben / pydux

Pydux = Python + Redux
MIT License
120 stars 12 forks source link

Make store a class instance rather than dict? #4

Open kasbah opened 8 years ago

kasbah commented 8 years ago

So you can:

store.subscribe(listener)

rather than:

store['subscribe'](listener)
usrlocalben commented 8 years ago

I've considered using objects to reduce the use of quotes/strings, but I think I prefer the literal translation of the original code. However, this would be the primary candidate for it

kasbah commented 8 years ago

I would argue that there isn't a literal translation of the Javascript object in Python as it sits somewhere between Python's dict and object.

For instance I would translate:

a = {b: function() {console.log(this)}}
a.b()
// outputs {b: [function]}

to


class A():
    def b(self):
        print(self)

a = A()
a.b()
#outputs <__main__.A instance at ...>

There is no way to express this with dict.

subscribe and dispatch look like class methods to me and the way to express this in Python would be to write a class. This is why I balked at the store methods but didn't blink an eye that dispatch takes a dict.

Aside from this I am much more interested in adapting Redux to Python than following the Javascript implementation to the letter. Maybe it would be best to create a fork if our ideas differs in that regard?

usrlocalben commented 8 years ago

I'm going to try it out. If it doesn't create problems in the way middleware is applied, then I think I'm onboard.

kasbah commented 8 years ago

I am going to try and implement this now.

usrlocalben commented 8 years ago

I spent some time on this, but replacing the apply_middleware() system is pretty hairy. Maybe you will come up with something better. It seems to me that any middleware function will need to accept a self/instance argument so that it can be composed and turned into a bound-function at runtime, and that's quite a deviation from the original implementation.

A simpler approach that gets you the same syntax, albeit without the real class you were looking for can be found here: https://github.com/benjamin9999/pydux/compare/store-as-dict-subclass

kasbah commented 8 years ago

Hmm, I should have really started there given what you said before. I haven't really used middleware and just reading over it now I think I will have some trouble wrapping my head around how to implement it.

I can't really justify spending time on it right now. Are you happy with the dict subclass solution?

kasbah commented 8 years ago

Any inspiration from aioredux?

usrlocalben commented 8 years ago

I merged the subclass version into master. I'd like to revisit this in the future, but my time is limited right now. This gives the desired syntax, and the api for callers shouldn't change later if it is changed.