-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
Pass live api through context not a prop #70
base: master
Are you sure you want to change the base?
Conversation
First impression is that I like this, haven't looked at the code but the idea is sound. Nested components might need access to the live api, currently you need to pass it manually through every prop, or use a context yourself. This solves that. Also, conceptually it's a more sound idea than using props. I also prefer the latter solution, using a context still feels like it's data instead of code. Let me think about it a bit again before committing to this :) |
As a counterargument, Using it as a library ( |
Of course, no pressure, I just saw Component api page for unrelated reason and when it came to CommonJS support is a blocker for the util right now (as I see it), but I feel like |
Definitely. I'm working on a project (https://territoriez.io/) that uses LiveSvelte quite deeply and I've struggled a bit with SSR. For example it seems there's some memory leakage going on. So I've disabled it for now. #36 should be an all encompassing issue that replaces it with something better. So I'm invested in fixing it at some point. But also waiting it out just a little until it's the right time to tackle it. Working with it in production is giving me a good feel of what the pain points still are for LiveSvelte. |
Added half-way implementation for // typed basic
import {getContext} from 'svelte'
import {Live} from 'live-svelte'
const live: Live = getContext("live-svelte")
// typed util
import {getContext} from 'svelte'
import {getLive} from 'svelte-live'
const live = getLive(getContext) |
fb44190
to
784042d
Compare
I like this. Eliminating the Svelte warnings is a big win IMO since the fewer warnings my team has to ignore, the less likely they are to miss the important ones. SSR, among its other issues (speed, scalability, another server-side library), is unstable for me and I submitted an issue about it. I don't understand how it could be working for other LiveSvelte users. I don't see myself using SSR with LiveSvelte because if I didn't require heavy-duty client-side code, I would use vanilla Phoenix. |
SSR definitely needs a rework. It can be slow and seems to have some memory issues. I'm not 100% convinced of the proposed solution though. If we can do something like this: import {getLive} from "live-svelte"
const live = getLive() I'd be happy to integrate that. |
For that a change of ssr node provider needs to happen first. To one that supports esm since new svelte does not support cjs. Also would recommend to add js build artifacts to gitignore. When this package will start to import svelte it will increase the size of the js output. I don't think there is much gain from committing those builds, but they do pollute the history and increase size of the repo. Or am I missing something about why they need to be shown? |
That's right, gitignored and removed them. |
784042d
to
9daf2da
Compare
Hm. How about this: |
9daf2da
to
d031c27
Compare
Nah, scrap this. Back to context only. Made this work: import {getLive} from "live-svelte"
const live = getLive() For that added import {getContext} from "svelte"
import {getHooks, setupLive} from "live_svelte"
setupLive(getContext) Could have done it from the start but wanted to avoid usage of global state. But, since svelte itself uses it and there can only be one svelte, should be ok. |
d031c27
to
17440cb
Compare
I've updated the description with current state of the PR. Do you have any reservations about current implementation? |
Saw this but not a 100% sure, if we can get rid of manually passing around the context in the setup then we're good to go. |
vonagam Why doesn't live_svelte do |
Svelte 4 does not support cjs and current runtime expects cjs.
I don't think we can get better than this at the moment. It is just 2 lines in the template. |
I think we should replace Node with Bun before we do this. Bun has reached 1.0 and should be compatible. This will avoid the cjs stuff. This should probably be a separate package inside the Elixir ecosystem. Bun is not available as a library, only as an executable. It has support for ffi and it might be possible to call Elixir code from javascript... There's a huge opportunity here if someone feels like picking up this... my spidey sense it tingling, some really cool things could be built from this I feel. Worth to explore. |
Right so I have had a look at this today because Svelte 5 beta has dropped and I think its going to pair even better with LiveView. It could even solve the slot issue as they have been deprecated in favour of Bun can easily replace Getting the Svelte 5 to render a basic component was pretty easy, just had to handle the breaking changes with components not being classes anymore: https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes |
Sorry to resurrect this old discussion, but some of the earlier posts were sort of relevant to what I've been experimenting on 😅 With some minor tweaking, I've managed to get both SSR and client-side working with ES-modules (still using nodejs), using esbuild to compile each svelte component/page entry point. I've also decoupled the es-modules from the LV-hooks (currently live_svelte needs to import all of them when setting up the hooks), instead importing them dynamically by using a data-attribute with the location of the respective js-module. The same approach should also work fine when Svelte 5 lands. I can create a repo demonstrating all of this if you're interested @woutdp, and if you like what you see I might be able to set off enough time to create a PR sometime in the not too distant future as well 😅 |
@Torkan Awesome work, repo showing this off is useful! Would definitely be up for merging a PR that implements this too :) |
So, I'm interested in plans regarding this change @woutdp. Elixir's node package now supports ESM so there should not be a problem with adding a dependency on Svelte. But it also can work without any dependency - like in the current state of PR - with a single setup call in Either way it can be a breaking change or a configurable one. I see that you have PR for Svelte 5, will it be a major update or a minor? In case it is a major - does it make sense to make this change at the same time then? |
@vonagam import {pushEvent} from "live_svelte" I'd be more in favor of that approach. I feel that's very clean and egonomic to use, no real boilerplate and should take care of the warnings. And I'd be fine with depracating the Regarding the Svelte 5 PR, I'm fine either way. Either inlcude it in that one or to have another release for this. Svelte 5 will just be another version update like 0.x |
Hm... I don't think <script>
// a)
import {getLive} from "live_svelte"
const live = getLive()
// b)
export let live
export let number = 1
function increase() {
live.pushEvent("set_number", {number: number + 1}, () => {})
}
function decrease() {
live.pushEvent("set_number", {number: number - 1}, () => {})
}
</script>
<p>The number is {number}</p>
<button on:click={increase}>+</button>
<button on:click={decrease}>-</button> In both cases through prop or context a user gets access to an instance of a live component with corresponding apis. But with bare import {pushEvent} from "live_svelte"
function decrease() {
pushEvent("set_number", {number: number - 1}, () => {})
} |
Right now live api is exposed as a prop. And we pass it to all rendered components, even those who do not ask for it, which causes svelte to complain about it.
A viable alternative is to pass it not as a prop, but through context. Which will remove the warnings and ease access for nested components to live api. It is a breaking change through.
getLive
is typed which is also a bonus.To make it work, two and half lines with
setupLive
call have to be added toapp.js
(so no action from new users, but migrating users need to add them):Edit: updated the description to reflect current state.