-
Notifications
You must be signed in to change notification settings - Fork 1
Use dom-context for event contract #1
base: master
Are you sure you want to change the base?
Conversation
Ooo love it ... super interested, it's definitely something that's needed in the custom-elements space. I skimmed through the source code in |
I also would be open to deprecating this library and we just add a stencil package to your repo. |
Good questions to think about.
I'll be taking another crack at this tomorrow, focusing on getting some
tests in place.
I like the idea of a stencil wrapper, too. For stencil the key thing is it
needs to get wired into state or props. I was planning to use state,
wormhole uses props, both have some pros and cons.
…On Tue, Sep 8, 2020, 7:51 PM Rahim Alwer ***@***.***> wrote:
I also would be open to deprecating this library and we just add a stencil
package to your repo. @dom-context/wc and @dom-context/stencil maybe? Up
to you.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAI2PXRJ55IJ5VDDSVAJKDTSE3UTVANCNFSM4RAZDRGA>
.
|
I like both approaches (state is cleaner) but my only issue with passing state in directly was that it creates tight coupling and makes it hard to test. I think having both is an ideal approach to cater for different use cases. |
There is also the approach that I feel like a couple helper methods like The thing that For example: import { createContext } from "@dom-context/stencil";
export const UserContext = createContext<User>("context:user");
export const RouterContext = createContext<string>("context:currentRoute");
// Connect to props
RouterContext.connectToProps(Component, "propName1");
// Connect to state
UserContext.connectToState(Component, (comp,context) => comp.statefield = context); I'll be working on this actively at my job for the next few weeks to connect |
@mihar-22 I have an implementation based roughly on your wrapper implementation https://gist.github.com/loganvolkers/9453eb3b92bb69454f874f874f586303 You need access to the Thoughts? |
You've done a really good job everything looks perfect to me, I have nothing to add to the implementation. I only thought twice about: context.connectToProps(StencilConsumer, (c, next) => (c.context = next)); Only because in my own experience I was directly mapping the context to properties most the time, and I wonder if it comes up for others a lot. Maybe including a utility function like so... const mapToProps = (props?: string[]) =>
(component: Constructor<C>, context: T)
=> (props ?? Object.keys(context)).forEach(prop => { component[prop] = context[prop]; }); Usage // 1.
context.connectToProps(StencilConsumer, mapToProps());
// 2.
context.connectToProps(StencilConsumer, mapToProps(['prop1', 'prop2'])); It's mostly developer preference I guess and it's situational, I was doing it mostly to Thanks for the awesome work! |
Not against a convenience method, but some feedback on your proposal: a) This would only work well with object contexts, not if the context is just a number, string, or function I see there being a benefit here for efficient re-rendering. If you're using a large context object, but only listening on a few keys, then you're not going to trigger a stencil re-render on every context change. I don't like the idea of spreading the entire context object into all the props, though ( |
Right... thanks for feedback! For some reason I was reflecting on my implementation when I wrote it, after reading what you said the For the use case where someone has a large context object and wants to avoid inefficient re-renders you suggestion seems the way to go 👍 |
There are a number of libraries that use a similar dom context approach as proposed by Justin Fagnani in 2016, see the readme here: https://github.com/saasquatch/dom-context/tree/v1
Looking to centralize all the libraries on a standard event contract.
Opening this pull request to see if you're open to the collaboration.
The goal is to keep your external API intact so it doesn't create any breaking changes for your consumers.