-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
made contexts default to new Set() #96
made contexts default to new Set() #96
Conversation
…ts is checked for value
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
Thanks for the PR. With your help, we might find a use case where it needs a change. However, the real problem is with Line 157 in d6eecaa
contexts is defined in deps.
Please look at my explanation in #94 why I thought that this condition is not required. It would be great if you could create a test case where the library throws an error without your change. I couldn't find a situation where it can be a problem. |
Thanks for the quick response, I can see where you are on this with a little more clarity. I'm not sure if this is something that should be encouraged, but I may have found a use case: I'm playing around with a hybrid component currently, and I wanted to have a property that was connected directly to the styling of a particular node in the template. Ergo, I created a "styleProperty" factory that upon updating, will immediately change the styling in the template. The only problem is if I update one of these properties and I also update a regular property, contexts is undefined and the error throws. As a side note, is there any benefit to initializing contexts as undefined instead of just an empty set? Initializing as an empty set gets rid of needing a guard anywhere, and to my knowledge it doesn't look like the code checks for contexts unless we are looping through it (which, if the set is empty, does nothing). Let me know what you think, I really like this project and would love to help or understand it more :) |
The main reason is memory consumption. The cache mechanism is attached to every property of every instance of web components created with the library. Although the Thanks for the code example. I will try to run it and debug the place where the problem occurs.
I've found https://github.com/michaelairola/HighlightHelper/blob/4e7a03f30e726b5acc84582c1fba36c8293a6eba/src/helper.js#L55 :) |
Thanks to you I have found a problematic use case. The cache throws when property, which is going to be observed is called before In your case, you are calling Sorry for not letting you be a contributor, but this problem also required changes in |
No problem, I'm glad the use case is now functional! Thanks for plugging the fix :) |
When I debug the problem I looked at your code, and I think some of the patterns might be done easier. The most complex structure is for applying styles. The template engine supports passing styles to the elements within the template. You can make those values as properties, so they can be manipulated from outside. The rest "static" styles should be set inside of the For example, you can write code like this: const HighlightHelper = {
opacity: 0,
top: 0,
left: 0,
...,
render: ({ opacity, top, left }) => html`
<style>
:host {
position: absolute;
boxShadow: 0 30px 90px -20px rgba(0,0,0,.3), 0 0 1px 1px rgba(0,0,0,.5);
fontSize: 14px;
}
#HelperBox {
overflow: hidden;
}
</style>
<div id="HelperBox" style="${{ opacity, top, left }}">
<div id="PageWrapper">
...
</div>
</div>
`,
}; Your code calls On another hand, you can omit passing styles at all, and use class change for "on/off" effect with smooth animation using transition or animation in CSS. It then would all you to just set bool or other simple value as property which would trigger displaying popup: html`
<style>
#PageWrapper.on { ... }
</style>
<div id="PageWrapper" class="${{ on: !!text }}">...</div>
` At last but not least, I would recommend creating a more declarative way of initializing the component. For example, it could be a custom element, which should wrap a subtree in DOM, where the highlight feature should work. Then put children of those elements in element inside of the element template. You can create a switch property for controlling if the popup is visible or not: function toggleHelper(host) {
host.text = window.getSelection().toString(); // It might be a trigger for display
}
const HighlightHelper = {
render: ({ text }) => html`
<style> ... </style>
<div id="HelperBox" class="${ on: !!text }">
...
</div>
<slot onmouseup="${toggleHelper}"></slot>
`,
; Then you can use your highlight-helper everywhere you want, without the worry of unique tag name: <body>
<highlight-helper>
<section>
Some text
</section>
</highlight-helper>
...
</body> |
I mean, when you put it like that... hahahaha Yeah, I really have no defense of my code. Looking back, I guess I was trying to use class-based solutions in a framework based on objects and functions... Not my best side project implementation Thanks for the help! I do appreciate it :) |
This fixes a bug I was having. "contexts" was undefined upon initialization, and while setting certain properties of the hybrid model, the cache function was trying to delete a value out of the contexts value (which was undefined). very small code change.