Comments (12)
HI @pdf ! First, cool username :)
Second, yes, I've been aware of this issue for a while and agree a Mounter interface would resolve it. If you don't mind, I'd like to get into the nitty-gritty details of why you need this. For example, could you point me to the exact JS library / relevant code that you would need to interoperate with?
There are absolutely caveats to working with Vecty on an element, e.g. the case of elements being replaced as you already stated. In your situation, would you have to deal with that element-replacement situation?
I'm OK with adding a Mounter interface to get access to the underlying element. But only if we have a really solid real-world use case that can't be solved some other way more cleanly. (I want to avoid the React situation of having a million different *Mount *Unmount handlers that have subtle differences)
from vecty.
The first example I encountered this with was looking at bringing in some UI enhancements, like Materialize or Material Components, both of which expect to enhance the DOM with JS.
I have it working in a rudimentary fashion via a very naive first pass at Mounter
in 462e338 , though I need to look at it in more detail, especially into how to handle replacement behaviour - I basically just slapped the Mount
call everywhere the DOM was updated to get up and running, though this is almost certainly not correct.
Material Components, for example, requires calling a destroy
function when the element is removed from the DOM, which makes me think maybe we'll be leaking something if replace is performed without Unmount
, though I haven't looked into exactly what they're doing in destroy
.
Hitting replacements seems pretty certain when dealing with dynamic lists and the like from my brief look at the vecty code, but I just started to have a poke at GopherJS/vecty yesterday, so I'm still in exploration mode.
from vecty.
For my use-case, Unmount definitely needs to be called on node replacement, and I've updated my branch to reflect this in b50ec7c
from vecty.
So, the thing that I wanted to uncover here has been uncovered. In short:
- You need
Mount
to be called and give you access to the DOM element -- I'm OK with that. - You need
Unmount
to be called on each node replacement -- but that is incredibly misleading because the component hasn't been 'unmounted' -- it's just had it's element swapped out. I would only expect Unmount to be called when the component itself is no longer being rendered entirely. So this is tricky behavior to nail down...
from vecty.
I agree, what I'm working with currently doesn't feel right at all. If #76 was implemented though, I think replacements would go away entirely, except for when components are actually mounted/unmounted.
from vecty.
Something I've been thinking of over the past couple of houors: I would be OK exposing a more fine-grained API for implementing things like this if it wasn't exposed through the public Go API / was more hidden away as to not mislead new users.
I'm wondering if we could expose it (and allow access to it) only via JS itself. e.g. perhaps via dispatching an event or checking for some global variable (I'm not suggesting this yet, just throwing out ideas). Another option would be maybe like a github.com/gopherjs/vecty/internals
package that allows hooking fine-grained things like this.
We'll def. come up with a solution :)
from vecty.
I think Mount/Unmount would probably be sufficient if node replacements were removed, except where a node is actually replaced with a different type.
from vecty.
Now that I understand more of what's going on during render thanks to #76, I believe ca1025f is pretty much correct, though I need to do some testing - I have a feeling we're already not unmounting everywhere we should.
from vecty.
I believe the current behaviour for Unmounter
is incorrect. In the case where two components return the same HTML tag, the previous component will be replaced without Unmount
being called.
Only way I can see to produce the correct behaviour here is via reflection. I've implemented this in 3df8654, and I'm now seeing consistent Mount/Unmount behaviour when I'd expect it.
I'd like to cache the reflect.TypeOf
result, but I've hit what appears to be a GopherJS bug (see the commented block), though I'm having trouble producing a minimal test case.
from vecty.
If you have any thoughts on the odd GopherJS behaviour above, I'd be keen to hear them. Alternatively, would you be okay with accepting a PR for this implementation, sans-caching? I'd like to get this out of my tree if possible so that I can start pushing some functional code.
from vecty.
@pdf Sorry for the latent response, I've been super busy recently. Also, if possible, let's try to keep issues properly scoped :)
reflect.TypeOf
: If I had to guess, thereflect.TypeOf
result would be useless to cache because it is probably already stored as part of the JS object or can be auto-determined cheaply somehow by GopherJS. Regardless, I'm not really sure / don't work on gopherjs itself. You should open a GopherJS issue for this: github.com/gopherjs/gopherjs/issues/new- Let's not make invalid
Unmounter
behavior be globbed into this issue, I've created #85 just now to track this.
That means that this issue is about:
- A
Mount
callback that provides access to the DOM element after it is created. - A
Unmount
callback that provides access to the DOM element prior to its destruction, unlike the current one.
Which of those two do you believe you will need for your use case? I can prioritize based on that.
from vecty.
I understand, time is the most precious commodity.
-
I've taken a look at the GopherJS reflect code, and it looks like they're doing caching on the first call to
reflect.TypeOf
there, so we can indeed avoid it here. I'll try harder to produce a minimal example of the undefined issue discovered here to report upstream, though I'll have to spend more time in the debugger to work out precisely how this is triggered, as it did not happen in my more trivial tests. -
I can split this into two PRs if you like, however there is some overlap in the code, so one PR will have to depend on the other.
I need both callbacks. However, they're useful for more than just accessing the DOM - I'm using them to manage store subscribe/unsubscribe as an example.
from vecty.
Related Issues (20)
- Add a basic example on usage HOT 5
- allow global panic handler HOT 2
- removeChild crashes if parent node is nil HOT 1
- [feature request] static HTML/CSS components HOT 6
- Examples go.mod file conflict HOT 1
- Add snake videogame example. HOT 4
- question: What is this prop vs. state idea HOT 3
- Instrumenting panic messages HOT 8
- question: initializing rendered HTML nodes via javascript HOT 4
- Porting gopherjs js calls to use with vecty HOT 2
- Document the availability of vecty project template generator HOT 1
- Return type of If function
- prop.Value("") does not produce the correct html on first render
- Vecty needs an official website HOT 10
- The slack invite link in readme is broken
- vecty docs HOT 1
- How to update a rendered element? HOT 10
- Failed Running The Example HOT 8
- Todo MVC example has misleading usage of p.Items HOT 2
- Rerendering List Replaces First ListItem HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vecty.