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

Passing MakeLogicType into the .extend API #129

Closed
cee-chen opened this issue May 5, 2021 · 3 comments
Closed

Passing MakeLogicType into the .extend API #129

cee-chen opened this issue May 5, 2021 · 3 comments

Comments

@cee-chen
Copy link

cee-chen commented May 5, 2021

Heya Marius, sorry to bother you again!! 👋 Our team has run up into another use case where we'd love to use the .extend() API to share some common listeners/state between 2 similar-ish logic files, but unfortunately we're blocked by typing being fairly broken once we start to use .extend().

Here's an example commit of what I was hoping to use extend for: cee-chen/kibana@e51b21c (I've left a few code comments highlighting the problematic type areas in that link).

For now, I've opted to instead use explicit connections (commit ref: cee-chen/kibana@9e46d0e) which gets the typing working again and works excellently for this use case, where there's less than 5 shared values/actions and the 2 files have a minor amount of things in common.

.extend() would be super useful however for a couple of our other files, where the majority of the values/actions are shared and there's just 1 or two things that differ between views. I was hoping it might be less dev effort to support just MakeLogicType (vs. kea-typegen) for the .extend() API by passing in some Typescript generics, but I'm definitely not a Typescript expert so could be way off base about that.

As always, thanks a million for your awesome work and let me know if I can elaborate further / help at all!

@mariusandra
Copy link
Member

Hey Constance, thanks for the support! :)

I added support for passing the right Type into logic.extend<Type>() in Kea 2.4.1.

However, this might not be the function you want to use. When you call logic.extend(), you're modifying the existing logic. In your case you're adding a bunch of actions/values onto SchemaBaseLogic.

If you want more traditional inheritance a'la class Cat extends Animal, where all the actions/values from SchemaBaseLogic are cloned into SchemaLogic, and an independent set is cloned into all other logics that might inherit from SchemaBaseLogic in the future, then this was possible with a workaround. From Kea 2.4.2 however you can use inherit like so:

const SchemaLogic = kea<SchemaLogicType>({
  inherit: [SchemaBaseLogic],
  path: ['schema'],
  actions: {
    ...
  },
})

and the new SchemaLogic will have a clone of the SchemaBaseLogic actions, values and everything else.

...

However, even then you might want to just connect like you're doing now. That would be the case if SchemaBaseLogic has a listener to make a long API request, and all logics that inherit (like SchemaLogic) need the same data. If you use connect, the request will be made just once. If you use inherit, it'll be made once per logic that inherits.

Hope this helps :)

@cee-chen
Copy link
Author

This looks awesome!!! Thanks so much as always Marius, not least for the super speedy turnaround time ❤️ I'll try to play around with both the new extend typing and awesome-looking inherit API here in a bit, but it may be a while (big release coming up!). Definitely feel free to consider this issue solved/closed for us, and I'll follow up if we run into any other typing/test shenanigans. Thanks again!

@mariusandra
Copy link
Member

That's great, closing the issue :)

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

No branches or pull requests

2 participants