spotifyovercastrssapple-podcasts

elm-spa

We discuss the benefits that using the elm-spa framework bring to your codebase, and how to get started.
September 7, 2020
#12

Getting Started Resources

Transcript

[00:00:00]
Hello, Jeroen.
[00:00:02]
Hello, Dillon.
[00:00:04]
How are you today?
[00:00:06]
I'm doing very well. How about you?
[00:00:08]
Very well on my end too.
[00:00:10]
And what are we talking about today?
[00:00:12]
And how do we pronounce what we're talking about today, most importantly?
[00:00:16]
I think we're going to talk about Elmspa...
[00:00:18]
No, um...
[00:00:20]
Elmspa...
[00:00:22]
Elmspa...
[00:00:24]
Yeah, let's go with Elmspa.
[00:00:26]
That sounds better.
[00:00:28]
Let's put that controversy to bed.
[00:00:30]
We're just going to go with Elmspa once and for all.
[00:00:34]
Yeah, even if Elmspa sounds a bit more relaxing.
[00:00:38]
To some, to some.
[00:00:42]
Elmspa!
[00:00:44]
What is this Elmspa tool, Jeroen?
[00:00:46]
Yeah, so Elmspa is a tool that Ryan Haskell Graz made,
[00:00:50]
which is meant to create single page applications in Elm.
[00:00:54]
That's what the SBA stands for, single page applications.
[00:00:58]
So making single page applications is a recurring question
[00:01:02]
from beginners when they come to the Elm community,
[00:01:05]
or even the front end community, I guess.
[00:01:08]
So the canonical example was Elmspa example,
[00:01:11]
which was made by Richard Feldman.
[00:01:13]
And Ryan built something else that is called Elmspa,
[00:01:18]
which helps you to do this.
[00:01:21]
Right, yeah, and I guess it's worth mentioning that Elm 19
[00:01:27]
gave us browser.application, which sort of makes it simpler
[00:01:31]
to build an SBA in just regular Elm.
[00:01:35]
But still, you have to wire in, essentially,
[00:01:38]
every single page that you create,
[00:01:40]
there's a nice API for routing, the Elm routing API.
[00:01:45]
You route it to a custom type.
[00:01:47]
You do a case statement on the type of page that you have
[00:01:51]
based on the route that you parse.
[00:01:53]
I think of it like a telephone operator,
[00:01:55]
you know, those old school telephone operators
[00:01:58]
that kind of take the wires and they route calls manually from,
[00:02:02]
you know, somebody calls in, then they patch them to the caller.
[00:02:05]
I have seen them on TV shows, but that's it.
[00:02:09]
That's what I feel like when I'm writing the top level code
[00:02:14]
that's sort of delegating to an individual page
[00:02:18]
from the top level.
[00:02:20]
And, you know, so you have to wire in every message.
[00:02:23]
You say if a message comes in, if that message is a homepage message
[00:02:29]
or if that message is a user settings page message,
[00:02:34]
then you have to route it to settings page.update.
[00:02:39]
And same thing for view and same thing for init, whatever.
[00:02:43]
Yeah.
[00:02:44]
That's right.
[00:02:45]
And then there's this kind of thing that always feels a bit odd to me,
[00:02:48]
which is what happens if you're on the user settings page.
[00:02:53]
The model type that you have is user settings,
[00:02:55]
but the message that comes in is a different message.
[00:02:59]
Then you have to do like a wild card clause
[00:03:02]
and just throw away that message, which also, to me, it feels too low level.
[00:03:08]
Yeah.
[00:03:09]
So that can happen when you're on the homepage, for instance.
[00:03:12]
And you make a HTTP request.
[00:03:15]
And before it finishes, you switch pages.
[00:03:18]
So now you're on the account page and you get that previous message back.
[00:03:23]
Well, then who should handle that message?
[00:03:27]
Well, maybe the homepage, but you don't have access to the homepage model anymore.
[00:03:32]
So that's odd.
[00:03:35]
So people usually just end up ignoring it.
[00:03:39]
Right.
[00:03:40]
That's right.
[00:03:41]
Exactly.
[00:03:42]
Exactly.
[00:03:43]
So that's how it was just with vanilla Elm.
[00:03:46]
You have to do all of this wiring to do an SPA.
[00:03:48]
So Elm 19 gave us some great helpers to build an SPA,
[00:03:53]
but there's still a lot of work involved in just wiring things to the right place.
[00:03:58]
And that said, we're humans.
[00:04:00]
So if we're doing all of that work to wire things into place ourselves,
[00:04:04]
there may be some inconsistencies.
[00:04:06]
Yeah.
[00:04:07]
There are also certain patterns that tend to emerge.
[00:04:10]
And you have to make a choice which pattern you're going to use.
[00:04:14]
There's been some controversy around this sort of Elm taco,
[00:04:19]
or I guess it has a different...
[00:04:20]
Elm shared states.
[00:04:22]
Elm shared state is now the name.
[00:04:24]
Yeah.
[00:04:25]
I mean, Elm taco was the name because it was basically like,
[00:04:27]
let's not give this a real name because it's just an idea or an experiment or something.
[00:04:32]
And it stuck and people talk about it a lot.
[00:04:35]
So there are different patterns for managing state in a single page application in Elm.
[00:04:42]
And there are different ways to...
[00:04:45]
How do you communicate from a child page to the global state?
[00:04:50]
Yeah.
[00:04:51]
If you have like a modal window, some people do like a...
[00:04:54]
They send back a triplet.
[00:04:57]
Instead of a tuple, they'll send a triple back that has a model message
[00:05:02]
and then some sort of thing out message or a thing for the parent for the global state.
[00:05:08]
Yeah. They have to handle for instance to close a dialogue or something like that.
[00:05:14]
Yes. Exactly. Yeah.
[00:05:16]
So that's another thing that Elm SPA gives you is it's just sort of an opinionated pattern
[00:05:21]
of how you're going to handle that communication between the parent and child pages.
[00:05:26]
Yeah.
[00:05:27]
I shouldn't say parent and child pages.
[00:05:28]
Yeah.
[00:05:29]
Pages and the global state.
[00:05:30]
So there's one level.
[00:05:32]
There's what Ryan in Elm SPA calls a page component, like the pages he calls components.
[00:05:38]
And then there's the global state and that's it.
[00:05:41]
There's no like sub children under that.
[00:05:44]
You can still have sub children for inside pages, but that's different.
[00:05:48]
Yeah. The rest is left to you.
[00:05:50]
But as far as Elm SPA is concerned, the only first class concepts that exist are the shared state
[00:05:56]
or the global state, the page components and that's it.
[00:06:00]
Yeah. So maybe let's go into how it works.
[00:06:04]
Mm hmm.
[00:06:05]
So basically in V5, which we're going to talk about today.
[00:06:10]
Yeah.
[00:06:11]
Elm SPA consists of CLI that you can get on the NPM, which is called Elm dash SPA.
[00:06:17]
And what that project with that tool will do for you is create a sample project.
[00:06:23]
You don't have to go through that, but you can use it.
[00:06:26]
And it sets up a few files, including a main file or shared file.
[00:06:32]
And then some files related to the SPA concepts like document, page and URL.
[00:06:39]
Yeah. So those are files that is going to make when it creates a project.
[00:06:45]
And then you can go and modify those if you want.
[00:06:49]
And then it's going to generate a few files for you.
[00:06:52]
Basically, it's going to generate those every time you run it.
[00:06:57]
And it's going to generate the wiring needed for the pages that you create.
[00:07:03]
Right. And when you say it's going to generate it every time you run it,
[00:07:07]
the source of truth for the stuff it's generating is the pages folder.
[00:07:13]
So there's source slash pages, you know, like a lot of different web frameworks
[00:07:19]
like next JS and like next and these types of tools do file path based routing.
[00:07:27]
Yeah, which we'll get into. But but the the main point here is that it's going to generate code
[00:07:33]
based on the Elm modules that you have in source slash pages.
[00:07:38]
So as you update that, it actually has a watcher that will regenerate the code as you change those things.
[00:07:43]
Yeah, because you can run it as a server with which runs Elm live under the hood.
[00:07:49]
So you will have as many pages as you have Elm files in the pages folder and subfolders.
[00:07:55]
Exactly. So should we talk about the file path based routing a little bit?
[00:08:00]
Yeah, I think it makes sense.
[00:08:02]
You know, it's interesting. I think that different browsers have different logic that they use for case sensitivity for URLs.
[00:08:09]
But in in general, conceptually, I think a URL is in many browsers considered to be case insensitive, I believe.
[00:08:19]
Yeah, I think so, too. Except maybe like I don't know if query parameters is an exception to that or not.
[00:08:24]
I don't know too much about the specifics of that spec.
[00:08:27]
But in Elm S.P.A., you don't get to choose your casing and stuff like that, which I think is is a feature.
[00:08:34]
And so, you know, if you do a source pages slash home dot Elm, then that's going to give you slash home.
[00:08:43]
It's going to give you that route with a lowercase h and it uses the camel casing.
[00:08:48]
It turns that into kebab casing.
[00:08:51]
Yeah, meaning it's got hyphens in between the words.
[00:08:55]
Hyphens or dashes? Is that the same thing?
[00:08:58]
Same thing. OK. Yeah.
[00:09:00]
Yeah. So it's it's a kebab because you have a skewer. A skewer. Thank you.
[00:09:05]
Skewer going through the words. Yep. Yep. Yeah. It's a it's a pretty nice, nice metaphor.
[00:09:11]
I like it. So that's like the basic version.
[00:09:15]
You can you can nest folders to create sort of more slashes in the route.
[00:09:21]
What about like dynamic routes?
[00:09:24]
Yeah. So dynamic routes is sometimes you're going to have URLs like your site dot com slash users slash an ID, a user ID and then maybe slash something.
[00:09:36]
So you will have a user folder and inside that you would have a route with a dynamic component to it.
[00:09:46]
Yeah. What you would then do is you name that Elm file or folder if there is a path in it.
[00:09:53]
Name underscore string or ID underscore string.
[00:09:58]
So ID is the name of the property that you will have in your pages flags or model.
[00:10:05]
I think it's probably a flag and string would be the type of that value.
[00:10:11]
So you can have either strings or ints. Right.
[00:10:15]
At the moment, I don't know if if you plan to support more, but I think those are pretty safe to start with, at least.
[00:10:22]
Yeah. And I mean, if you if you want to parse it into some more nuanced structure, then you take the string and you parse it into something and validate it.
[00:10:33]
Yeah. And you can always show a not found error or something like that if you parse it and it doesn't work out.
[00:10:40]
Yeah, exactly.
[00:10:41]
I think it's worth noting for people considering using Elm S.P.A. on a project.
[00:10:47]
That's something that Elm S.P.A. is opinionated about and goes all in on is this idea that there is a one to one mapping between, you know, your routes and your file structure.
[00:10:59]
And I think that's quite nice for a couple of reasons.
[00:11:03]
For one thing, it's nice because you don't have to write that sort of route parser logic.
[00:11:09]
You know, you do that by the way you structure your code.
[00:11:12]
But I think more importantly, if somebody is trying to navigate through your code and figure out how the app is structured, where does this page live?
[00:11:21]
There's a one to one mapping. So in terms of navigating code, it makes that a lot easier because you just look at the URL and you can very quickly navigate to that page.
[00:11:33]
Yeah. Usually what I do to find which page I need to modify to modify something is I find a string close to where.
[00:11:41]
Exactly. Exactly.
[00:11:43]
Yeah, class name or a string and then I copy paste that and find it in my editor.
[00:11:47]
And usually I find it quite quickly in the project I'm working on.
[00:11:51]
But exactly. Yeah.
[00:11:53]
But having this routing be very one to one makes it much easier, I think.
[00:12:01]
Yeah, I think that's quite nice.
[00:12:02]
Both between the file path based routing and the sort of opinionated way of structuring an S.P.A. that S.P.A. gives you.
[00:12:12]
I think it makes it really easy to navigate through an app, whether you're looking at your own S.P.A. that you maintain every single day or looking at somebody else's code that uses the same tool.
[00:12:25]
It makes it very predictable where things are going to live and how things are going to be structured.
[00:12:29]
There's no sort of like, oh, well, we were experimenting with this way of sort of having pages for this one part of it handled over here.
[00:12:39]
And then the update for these other pages comes over here.
[00:12:42]
And so some of the code is like this and some of the other code is like this.
[00:12:45]
It's like, no, there's, you know, the top level thing that calls the page that has the init and update and all those things for page is going to be defined in this module.
[00:12:56]
You know exactly where to find that.
[00:12:57]
Yeah, you say it's opinionated and yeah, it is.
[00:13:01]
It's a pretty uncontroversial.
[00:13:03]
Yeah, it sounds very controversial.
[00:13:05]
I agree.
[00:13:06]
Like the only downside I see to that is that if you want to have two pages that look very much alike, like you have a list of users and then when you click on it, you go to the same page with slash that I.D.
[00:13:18]
But you keep the same layout.
[00:13:21]
You just see the user details on the right side of the page.
[00:13:25]
Then you just have to make the components that that contain that are contained in those pages shared.
[00:13:33]
Yeah, that's pretty much it.
[00:13:35]
There's not much you have to generalize to make it work.
[00:13:40]
Yeah, I completely agree.
[00:13:42]
I think that it's fairly uncontroversial.
[00:13:44]
It's nice because, you know, LMSPA came, I think, at a time when these concepts have been pretty well formed.
[00:13:53]
I mean, Richard Feldman has even gone through a few design iterations on the LMSPA example.
[00:14:01]
I mean, he hasn't drastically changed things, but I think with the 19 upgrade, he changed a lot of things, not just to upgrade directly to 19, but he did some design changes.
[00:14:12]
So it's been able to really mature.
[00:14:14]
And a lot of people have used that as a template for how they structure their their SPAs.
[00:14:20]
And so it's a pretty battle tested pattern.
[00:14:23]
Yeah. And so it's pretty uncontroversial and works very well.
[00:14:26]
Yeah. So the LMSPA example from Richard is also using that same pattern.
[00:14:31]
Yeah, that's right.
[00:14:32]
I mean, it's heavily influenced by the LMSPA example real world repository that Richard made.
[00:14:40]
And for anyone who doesn't know, that's just, you know, there's this spec of a real world app, which is like the medium blog platform.
[00:14:48]
It's like a clone of that tool.
[00:14:51]
So it's just a sort of non trivial application that gives you a sense of different front end languages.
[00:14:57]
So one thing that I think we should also mention in terms of just the mechanics of LMSPA is that so there are these different sort of constructor functions for different types of pages.
[00:15:09]
So as Ryan calls them, these page components.
[00:15:12]
So Elm browser has browser application browser sandbox, right, these different levels of complexity.
[00:15:18]
If you want like a browser application gives you the ability to hook into the routing of an Elm map, whereas browser sandbox doesn't.
[00:15:29]
And it just takes over a single element, whereas browser application takes over the whole page.
[00:15:33]
There are these different levels of complexity that you can use to, you know, if you're learning Elm,
[00:15:39]
then it's nice to not deal with all that complexity or if you're just wiring something into a little part of the page,
[00:15:45]
then you don't have to deal with the complexity of the routing because you don't need it.
[00:15:49]
So it's worth noting that LMSPA uses a similar pattern, except it's not browser application for the entire application, but it's for these page components.
[00:16:00]
So you have page static gives you something that just has a view and it doesn't have an update message.
[00:16:09]
Yeah, it's the equivalent of just returning an HTML in Elm.
[00:16:14]
Yes, that's exactly what it is.
[00:16:16]
Yep, it's a non dynamic page.
[00:16:19]
You have sandbox, which has an init update view.
[00:16:23]
You have element, which gets subscriptions and you have application.
[00:16:28]
Now that's the juiciest one.
[00:16:31]
Application introduces another concept that's unique to LMSPA, which is save and load.
[00:16:38]
Oh, with their toes then.
[00:16:40]
So save and load.
[00:16:42]
Yeah, there's a really interesting design and I had a good chat with Ryan.
[00:16:47]
I actually, I was picking his brain because I'm working on a design similar to LMSPA for Elm pages where it generates some code for you.
[00:16:56]
It's different because in Elm pages, it's based on this notion of a template, like a blog post template, for example.
[00:17:04]
But it's been great kind of taking inspiration from Ryan's work.
[00:17:09]
And I got to chat with him about some of his design ideas.
[00:17:14]
And one of the ones I was most curious to hear about was the design he went with for save and load.
[00:17:19]
So I think it's worth kind of mentioning what would be the alternative to save and load and what problems it's all.
[00:17:26]
Yeah, definitely.
[00:17:28]
I think the problem it solves is if you have an individual page, you know, let's say you have a user settings page,
[00:17:36]
then if there's local state for that page, you know, for example, if you're editing a username in the user settings page,
[00:17:46]
perhaps you also have some global state that maybe shows the username in the nav bar.
[00:17:53]
So how do you send that information to tell the global state to change when the username has changed, for example?
[00:18:02]
Yeah.
[00:18:03]
So one way you could do that is if you have the ability to trigger global messages.
[00:18:08]
And so, for example, you could do that by attaching a global message to something.
[00:18:14]
So global message would be a message designed for something higher than a page, but just for the whole system.
[00:18:22]
Exactly. Some state that's shared by all the pages, which actually is why Ryan uses the term shared, not global.
[00:18:29]
So that was a definitely nice, nice point that he made.
[00:18:33]
He was telling me that he used to call it global and it's now called shared.
[00:18:38]
And the reason is because he was explaining it to people.
[00:18:40]
He's like, oh, global is just this place where you can share state. It's like a shared state.
[00:18:45]
He's like, why don't I just call it that if that's how I'm explaining it to people, which I think makes a lot of sense.
[00:18:50]
Yeah. The way I see it is when you switch pages, you don't want to reload all data that you previously computed or fetched.
[00:18:58]
So you can store it in the shared state, shared model, and then the new page just takes it from there.
[00:19:08]
Yes, that's right. Because you need to persist certain bits of state.
[00:19:11]
For example, if a user is logged in, it wouldn't make sense to recreate that state every single time.
[00:19:19]
I mean, you're making an HTTP request to authenticate each time and what are you even using to authenticate it because you've lost.
[00:19:27]
Do you have to ask them to log in every time they go to a new page?
[00:19:30]
So obviously you need some sort of shared state.
[00:19:33]
So you could persist that state by allowing the views of different page components to send a global or shared message.
[00:19:44]
To do that, you would actually need to pass in a to message and to global message to allow you to wrap messages in either the page's local message type.
[00:19:54]
Yeah, to message would be to message, not to the number two.
[00:19:59]
That's right.
[00:20:00]
I also call it the translator pattern.
[00:20:04]
Yeah, Richard sometimes calls it the how to message pattern, I think.
[00:20:09]
Teach me how to message.
[00:20:11]
So that's one way to approach that problem.
[00:20:14]
And Ryan went a different direction and his thinking was essentially that he didn't want to couple the individual pages to the shared messages.
[00:20:23]
And he also wanted to make the way that you share state between an individual page and the shared state be very explicit.
[00:20:31]
Yeah.
[00:20:32]
So that's the sort of rationale behind the load and save design.
[00:20:36]
So what do they do?
[00:20:38]
Load and save are really just a hook that you define when you when you define, you know, you say the source slash pages slash settings dot elm.
[00:20:50]
And then you define your page component by saying page dot application.
[00:20:55]
And so you pass it in it update view subscriptions as we're kind of used to.
[00:21:01]
You define a message and a model type.
[00:21:04]
And then you also have these two other functions that you pass to page dot application.
[00:21:08]
And that's load and save.
[00:21:10]
And what those do is load allows you to you receive the shared state every time it changes.
[00:21:18]
And you can take that shared state and update your page component state so you can update a settings page.
[00:21:26]
Yeah. So you get you got the shared model, the model, and then you turn the model along with some commands that you can also trigger.
[00:21:36]
Yes, exactly.
[00:21:37]
Local messages.
[00:21:39]
That's right. And then you've got the save hook, which is again, it's another function that you pass to page dot application.
[00:21:47]
And that allows you to take the local model.
[00:21:51]
Given a change that you've made to your local model, it allows you to update the shared model.
[00:21:57]
And so in that way, you can communicate between the shared state and the local state.
[00:22:03]
And one of the reasons that Ryan chose this design is because it allows you to be very explicit about how you're sharing state.
[00:22:11]
So if you're looking to how do I understand how the shared state is going to change?
[00:22:16]
Well, you look in the top level update function for your shared component for your top level shared state.
[00:22:23]
And then you look in the save functions in your page components.
[00:22:29]
And between those, you can understand how the shared state could possibly change.
[00:22:35]
Yeah. So you also have a update function inside the shared module, which can also modify the shared model.
[00:22:43]
It will mostly be through the safe functions as far as I understand it.
[00:22:47]
Yeah, I mean, it could. So this actually is another good point is that shared has a view and the shared view allows you to sort of wrap the individual page view with some sort of header and footer, for example, as many apps do.
[00:23:05]
Now, if you if you wanted to, your header could trigger a shared message.
[00:23:10]
Yeah. Right. Because in shared view, that function has the ability to make messages of the shared message type, which can change the shared state.
[00:23:21]
So if you wanted to, you know, have a hamburger menu on your top level pages, if it's in mobile, you can track that open closed state of the hamburger menu in your shared model.
[00:23:33]
And you can directly send messages to toggle that state through the shared view, for example.
[00:23:41]
I do wonder whether that's a good idea. Because like, if you have a website where in all pages, you have the header, and all the pages, you have the footer, then okay, if you have some pages, like, I don't know, a settings page or admin page, where you don't have the same header or no header at all.
[00:24:01]
Mm hmm. Then you need to do things differently. And maybe you need to move all those headers and footers in the visual pages or say, oh, if you're on this page, then do not display the header display this other header and do that in shared view.
[00:24:19]
Yeah, there are different ways to structure this. So with Elm pages, the design that I'm working on is the type of your sort of shared view can be any type, which means it could be a custom type.
[00:24:31]
So the way I would solve that with the design that I'm working on the prototype I'm working on is that you could have your shared view type be a custom type that's, you know, standalone window, or a window with a header and footer.
[00:24:47]
And your individual pages can specify which type of page they're going to be if they're going to be one with a header and footer or one without.
[00:24:56]
Yeah, but and they would specify it through their shared model, they would save.
[00:25:01]
They would specify it through the well that that would be one way you could do it.
[00:25:06]
That's definitely one way you could do it with this design I'm working on and on pages, the view can be any type. So you could have a view that's a custom type that's either with header and footer or plain, and that wraps, you know, whatever view type element or HTML, and you return that type of view from the individual pages.
[00:25:27]
Well, I think this is this is an interesting thing. In Elm SPA version four, you had a CLI which generated a lot of code.
[00:25:37]
Yeah, the wiring, boilerplate, but basically everything, even the main functions, even the main files, as far as I remember.
[00:25:45]
But version five decided to only have a CLI and this is something that didn't notice when it was released.
[00:25:53]
There's no underlying Elm package. Yeah, there's no underlying Elm package.
[00:25:58]
When you create an Elm SPA project, it creates the main file, the SPA page documents, URL files, but those you can afterwards modify to your liking.
[00:26:11]
So if you wanted to, you could now specify, oh, I don't want to view to return a document with a title and a body. I want it to return a custom type.
[00:26:22]
That's right. And you couldn't do this before because that's right. It was a package and it controlled what you did.
[00:26:30]
Now it only sets the backbone of the application. Right. And you can go and modify it if you need to suit your needs.
[00:26:37]
Right. It basically gives you this SPA slash generated slash pages and slash route file.
[00:26:46]
Yeah. So the things in that that generated module namespace, that generated folder, you cannot modify or you'll be very unhappy if you do.
[00:26:57]
And the other things you can modify. Yeah. Yeah, you're right, though. You could you could approach that in those two different ways.
[00:27:03]
You could approach it by updating your shared state to say on the initial load of a page component, you could update the shared state to say whether or not there should be a header and footer.
[00:27:14]
Or you could modify the view type to be a custom type and do a case statement on that.
[00:27:19]
Yeah. I don't know about the history of why V5 was turned out to only be SLI compared to V4 where it was a CLI and a package.
[00:27:28]
I'm thinking that it's because people wanted to customize all those things, all the main file, the shared file.
[00:27:36]
And you get them, but you can modify them. I think that's true. I didn't I didn't talk to Ryan specifically about that, but I think I think.
[00:27:43]
You had three hours. I did. I did. It was a very long conversation.
[00:27:48]
I also just had a lot I wanted to ask him about as I was working through this Elm pages prototype.
[00:27:53]
It was great. It was really fun. But another thing that that it helps with is it gives you the ability to see I have a sense of these things because I've I've gone through the experience of trying to build it and like you run into a wall and then you turn the other direction.
[00:28:10]
It's interesting to compare notes. So one of the things that it gives you when you when you generate those files as well is you don't have a bajillion type variables everywhere because you can hard code it for the specific types in the code base.
[00:28:23]
So when you publish an Elm package, you can't depend on types that are specific to the user's code base.
[00:28:31]
When you generate code, you can. Yeah. I remember that V1 I think had like nine type variables for the page type or something.
[00:28:41]
Yeah. And I think it was more coupled to the idea of like specifically having an Elm UI based or Elm HTML based app, which, you know, when you when you can generalize those things, it's a sign that it's a good design, I think.
[00:28:57]
Oh, sorry. It's not nine. It's eleven in V1. Page had eleven type variables.
[00:29:05]
And I think. Oh, nice. In V4, he did some nice improvements. I didn't follow to him.
[00:29:12]
But it was some good improvements where it was only like three type variables and the rest was generated.
[00:29:21]
The earlier versions also tied in some specifics about, you know, doing page transition animations and things like that, which he, you know, he later sort of backed out of that direction and said, like, I don't want to couple the framework to those decisions too much.
[00:29:38]
I want to let the user code be more in charge of that. Yeah. And you can still do it yourself. You can still do page transitions.
[00:29:45]
Exactly. So I think the design he's come up with is very nice in its simplicity. It hits it. I think that's what makes this a really nice framework, is that it hits at the key design problem and it doesn't try to do anything more than that.
[00:29:59]
Yeah. You have simplicity and you have features. Like if I want to have page transitions, now I have to write them myself.
[00:30:10]
That's right. In my case, I don't know how to do that. So it's nice when the tool does it for you. But when you do too many things for the user, then they can't back out of it.
[00:30:22]
They can't like, oh, you're using LMSPA. Oh, you want that kind of transition. Oh, yeah. You need to stop using LMSPA.
[00:30:30]
That's right. Exactly. Because it makes certain assumptions then when it's building in these very specific features. So, yeah, Ryan's done a very nice job making the right assumptions that are safe, fairly uncontroversial assumptions to make about how you structure an SPA and assumptions that give you real benefit and value like file path based routing.
[00:30:54]
You know, like each page component is, you know, initialized in exactly the same way. And it can do all these things for you based on those assumptions. But then the more assumptions you make, the more it can limit you.
[00:31:08]
So it gives, like you said, an assumption can give you like a nice feature, but it can also limit things that you might not be able to do because of that constraint.
[00:31:17]
Another thing that I like is because everything is generated or committed to the project, you don't have a package which limits you in other ways.
[00:31:27]
And one side effect of this is that if you, for some reason, don't like LMSPA, like you created your project with it, but you just decide to stop using it, then you just stop using it. You commit the generated files.
[00:31:42]
And now you do just regular Elm. And that is really nice that you can back out of it easily.
[00:31:51]
It's a really great point. Yeah. Yeah. You're not even coupled to an Elm package. You own that code.
[00:31:56]
Yeah. You still have the whole architecture that it built for you, which is, I think, pretty good.
[00:32:01]
Yeah, it's actually pretty readable code. Yeah. Perhaps even more readable than your average SPA code base because a lot of thought has been put into how to design it and structure it.
[00:32:11]
Yeah. And it's very consistent. Yeah. It's mostly what you would do yourself, except that it does something with view and subscriptions, which it bundles together, which I don't do in my normal code.
[00:32:23]
But it's not very hard to understand, I think.
[00:32:26]
And by the way, I don't think we've mentioned yet that the CLI provides this command you can run, which is LMSPA add.
[00:32:36]
Yeah. If you do that, it will walk you through interactively choosing which type of page component do you want to use.
[00:32:44]
Do you want to have a static sandbox element or application type of page component?
[00:32:50]
Then it will ask you for a name and then it generates that starting point.
[00:32:53]
So that's quite handy because it takes care of that wiring for you.
[00:32:57]
One thing that's kind of interesting to note is I think it does depend on you exposing certain things. Right.
[00:33:03]
But when you use the CLI to generate the files, you don't have to think about that so much.
[00:33:08]
Yeah, because it will give you errors if you messed up somehow.
[00:33:12]
And it generates it for you. So you don't have to do it by hand.
[00:33:15]
If you were doing it by hand, you know, and it's the kind of thing like you have to remember to expose message and model and that they're named that same thing.
[00:33:23]
But usually people will do that. But when you generate it with the CLI using LMSPA add, you don't even have to think about that.
[00:33:32]
Yeah, actually even forgot about the feature. And when I played with it, I copy pasted another page.
[00:33:39]
Yeah, that's that's a fair way to do it, too.
[00:33:41]
Which in my case, I really just wanted to copy paste.
[00:33:44]
But for some reason. So what are the problems with the load save paradigm or idea?
[00:33:51]
Because there is a trap in the sense that you can forget to load things and you can forget to save things.
[00:34:00]
Exactly. I think those are the only downsides that I really see with it.
[00:34:05]
Yeah. As with any LMSPA code, things work so nicely when you have a single source of truth for data.
[00:34:11]
And when you start duplicating that source of truth, then things can get out of sync.
[00:34:17]
But in general, if you approach writing LMSPA code in this way where there's a single source of truth, then you just don't even think about out of sync errors because they don't happen.
[00:34:28]
It's interesting with like an object oriented language where, you know, you have mutable objects.
[00:34:35]
You can just update an object and the data stays in sync.
[00:34:38]
But with immutable languages, you can have something that's the same object, but it diverges in state because you're you're passing around these immutable data types, which can then have new references to different values.
[00:34:51]
And so they can get out of sync. So you do you do run into that issue.
[00:34:55]
And that's something you have to be aware of. Yeah.
[00:34:58]
And it's only something that you will notice during page transitions and maybe only some page transitions, like when you leave a given page, which forgets to save things or when you enter a specific page, which loads some other data.
[00:35:13]
That's right. And on the flip side of that, you can intentionally choose to update things selectively.
[00:35:19]
Yeah. So you can choose. I want to forget whatever was before. Like when you go to the logout page, you can say, oh, user equals nothing.
[00:35:28]
That's right. Yeah. Yeah. I do think that Elm Review could help with this, like not forgetting to.
[00:35:35]
Yeah. To load and save things that are named the same as in the shared model, for instance.
[00:35:42]
But I do think that there's a lot of edge cases where you might not want to. You might have intended.
[00:35:50]
Yeah. So keep them out of sync. Yeah. I'd be interested in a thing that's working. But yeah, it's it's an idea.
[00:35:59]
That's an interesting point. So so in terms of the you know, the value from from using Elm S.P.A., you know, let's let's say, you know, using it in a production code base,
[00:36:10]
using it for a team. We kind of hit on a few points. We talked about the value of having a one to one mapping of a of a route to a particular place in the file system.
[00:36:21]
Yeah. We didn't say that pages are much simpler because you don't have to handle all the wiring.
[00:36:27]
That's right. It takes care of a lot of wiring for you. And it does it in a very consistent way because it's like if you don't, it won't get wired in for you or there will be an error.
[00:36:37]
You just use Elm S.P.A. add and it, you know, it uses a particular structure and it and it auto generates these pieces for you that are going to be done very consistently.
[00:36:47]
Yeah. Basically, unless you're using a page dot application, you can see every page as a single program.
[00:36:56]
Exactly. And even for application is much simpler than the wiring that you would do yourself, I think.
[00:37:02]
Right. Exactly. So that's huge. Just just the ease of navigating through a code base and being able to predict how things are going to be wired up, where you're going to find things.
[00:37:11]
That's extremely valuable, I think. Another benefit of using Elm S.P.A., which I think is really valuable to to consider as you're as you're using it,
[00:37:21]
is how it enables you to think about just a slice of, you know, think about one page at a time, but also think about, you know, if it's if it's built with page static, then you don't have to think about the global state or the local state.
[00:37:37]
You know, if it's page sandbox, then you don't have to think about subscription.
[00:37:42]
So, you know, you get these more nuanced ways of looking at a page and you know, is this going to touch shared state? And you can simply look through for is it a page dot application?
[00:37:54]
If it's not, you know, it doesn't touch the shared state. So this is one of the amazing things about Elm is your ability to look at type signatures and know what it could possibly do.
[00:38:07]
If it doesn't get wired up to an update function that sends a command, then it doesn't do side effects and you don't have to think about that.
[00:38:14]
If it doesn't get wired up to update, it doesn't change the state. If it doesn't depend on this data, then it's not using it. If it doesn't return this data, then it's not affecting it.
[00:38:24]
I think we could link to Richard Feldman's scaling Elm apps.
[00:38:29]
Yes, exactly. Yeah, Richard Feldman gave an Elm Europe keynote one year and it's yes, it is a treasure trove of great advice on how to approach building Elm apps.
[00:38:40]
And basically the headline of that talk, the advice he gives is make your Elm code as simple as possible and no simpler in terms of what it depends on and what it can change.
[00:38:53]
So that, you know, if you're structuring your Elm code to make it depend on as few things as it needs to and be coupled to things that it can possibly do and change as little as it possibly can,
[00:39:06]
it makes it so much easier to trace where problems are coming from because you can completely discard certain things as possibilities by just glancing at the type signature.
[00:39:15]
And that's a superpower as an Elm developer to be able to read things that way and to structure things so it's easier to do that, which definitely watch Richard's scaling Elm apps talk to learn more about that.
[00:39:27]
In the context of Elm SPA, it gives you that same thing as a framework.
[00:39:32]
You have these sort of four core ways of building an app, page.static, page.sandbox, page.element, page.application, and that gives you a very clear understanding of what it can possibly do in terms of affecting global state.
[00:39:47]
Does it have its own state? I think that's very valuable.
[00:39:50]
Yeah.
[00:39:51]
And since those are generated for you when you create the project, you can modify those. So if you want an application but that can't update the shared model, then you create a new variant of application where you don't have the save function, for instance.
[00:40:06]
Right.
[00:40:07]
And when you use that, you know, oh, okay, that one can't update the shared model.
[00:40:12]
Okay, so it is generated for you, but it's not in the generated folder. And so you could change it, but it's also going to call.
[00:40:21]
Oh, I guess you would have to change your your upgrade functions. But the upgrade functions are generated. Are you sure you can change those?
[00:40:30]
Yeah.
[00:40:31]
Are you sure it doesn't make assumptions based on that?
[00:40:33]
No, you just created a new variant of application where the wiring is different. Like you say, save is always identity, meaning it never changes the shared model.
[00:40:47]
I gotcha.
[00:40:48]
And those files are committed to the project.
[00:40:51]
Gotcha. So there's like this page type. I guess you couldn't change the page type.
[00:40:56]
Sorry?
[00:40:57]
There's this like type alias page, which defines all of the, you know, init, update, view, subscription, save, load.
[00:41:05]
I guess the generated code depends on assumptions about that, right?
[00:41:11]
Hmm.
[00:41:12]
I'm guessing it would be unhappy if you changed that. I'm guessing it's assuming that the type of page is that.
[00:41:20]
I wonder about that, actually.
[00:41:22]
Yeah, I guess it wants to have an init or an update function and view or bundle.
[00:41:30]
Yeah, exactly. Because the generated code is passing specific types of data types into page.init.
[00:41:40]
And so I think things would break if you changed that type alias, which actually maybe that's something that could be made more clear by that being in like the generated folder.
[00:41:51]
Well, I do think it's useful not to have it be generated.
[00:41:56]
But as long as you keep having pages generate this, the current page alias, then it's fine.
[00:42:04]
Right. Yeah. He may have kept the page type alias out of the generated folder to avoid some circular dependencies.
[00:42:12]
But also, yeah.
[00:42:14]
It's nice when you can edit anything outside of the generated folder as long as you keep things consistent between your own code and don't get any compiler errors.
[00:42:25]
And then just have the generated stuff depend on only generated stuff.
[00:42:29]
Yeah. And the downside is if you change those types too much is that if someone is used to MSPA but not to your project and they come into your project,
[00:42:40]
then they will have a bit of a harder time to understand things. But generally, it will stay probably the same thing.
[00:42:47]
Right. I think. Yeah. I think there's a lot of value to having the ability to customize these types of things for an application. Yeah.
[00:42:55]
But also to keep things pretty standard.
[00:42:58]
Yes. Yeah. Two sides of the...
[00:43:01]
Mm hmm. Right.
[00:43:03]
So, yeah, there's one thing that I want to pick your brain on is about boilerplate.
[00:43:10]
So in Elm, we don't like boilerplate, but we embrace it more than we do than other communities do.
[00:43:18]
Yeah. And in this case, the boilerplate is handled for you. And I'm thinking that in this case, it's fine.
[00:43:25]
I usually don't like it when there's magic involved because you can't see how things are working. And when you're a beginner, it's hard to understand.
[00:43:35]
But in this case, I think it's fine because the code is generated and you can trace it back and you can understand the whole system.
[00:43:43]
Yeah. Is that your feeling too or?
[00:43:46]
Yeah. One way that I think about it.
[00:43:48]
So first of all, I think that the reason that in the Elm community, we tend to hear people talking about boilerplate and we say,
[00:43:57]
hmm, are they thinking about the right considerations for designing something?
[00:44:01]
Is because what the tradeoff often is, is you can have boilerplate or you can have implicit magic code.
[00:44:08]
Yeah. And we don't like that in Elm. So we don't like boilerplate in the Elm community.
[00:44:13]
But the sensibility is we like implicit magic much less than we like boilerplate.
[00:44:21]
And so we'll take that tradeoff. And that's very frequently the experience I have when I use things in the JavaScript ecosystem or in the Ruby Rails community, for example.
[00:44:32]
There's a lot of code that does things for you, which is convenient.
[00:44:37]
But then what I find when I'm working with code like that is that means that's more for my brain to process.
[00:44:44]
So it's like this framework does this work for me, but my brain still has to understand it.
[00:44:50]
So it's actually adding more cognitive load. That's the way I think about it.
[00:44:54]
So how can we make our code more predictable and make it cheaper to process what's going on and make it easier to trace back bugs and understand code and understand how to change things?
[00:45:05]
So that's the goal. And being explicit helps with that goal. There's less work for our brain to do.
[00:45:11]
Even if there's more work for our fingers to do to initially type it, that's OK.
[00:45:16]
We want to optimize for our brains doing less work and our brains being able to better predict how things will change or where problems are coming from.
[00:45:24]
So that said, when we were talking about the boilerplate of an SPA in Elm, you can create a very simple set of rules that the code is generating for you.
[00:45:35]
And now if you think about that question, how much effort does this add for my brain to understand and how well can my brain predict what changes are going to do?
[00:45:46]
It actually isn't a problem for those considerations because there's a very predictable deterministic mechanism for how the stuff you write is going to map to this lower level code.
[00:46:02]
So what you're actually doing is you're learning a higher level mechanism and mapping that understanding to the lower level pieces of what that's going to do.
[00:46:11]
So your brain can still understand it just as quickly, if not quicker, because you know it's not going to deviate in all the low level wiring.
[00:46:18]
You know it's going to be predictably wired. You know exactly how to map it to what it's going to do and how the code is going to change when you change something in a particular spot.
[00:46:27]
So I think it actually, yes, there are a couple of concepts that you have to learn.
[00:46:33]
But once you learn those concepts, I think those concepts serve to help you understand code more quickly with less effort.
[00:46:40]
So in my book, that is definitely a win. And I think this framework does a very good job of balancing those trade offs.
[00:46:47]
Well, I think anyone would say as long as you learn the concepts, it makes everything much easier.
[00:46:52]
And yes, be that for language, be that for a framework.
[00:46:56]
And yeah, it's true. But sometimes the learning curve is very hard.
[00:47:01]
And in this case, it's pretty simple. So it's OK.
[00:47:05]
But otherwise, be careful of having to teach people new ways of thinking that you can't trace back, that you can't understand without looking at the source code or something like that.
[00:47:18]
Exactly. And I think frameworks have to, you know, framework authors have to seriously consider how many concepts they're introducing to users because it becomes too many concepts to juggle.
[00:47:30]
So if you have too many concepts that you're introducing, then users can't have a clear mental model.
[00:47:35]
There's a steep learning curve. And even if they do learn it, they can't keep it straight in their head because there are too many concepts and it's confusing.
[00:47:42]
So I think another important point is that the concepts and the mapping of the code you write to the generated code, it's actually fairly simple.
[00:47:52]
Now, you know, earlier versions, when you have page transitions and things like that, that's more specific concepts. That's more specific understanding that you have to gain in.
[00:48:02]
What is this code doing for me? And so I think that, you know, this has gotten, you know, the design has gotten better and better in that regard of just doing very little, but doing things that have a huge win.
[00:48:17]
So learning this new concept, you get your money's worth.
[00:48:22]
Back to creating new functions in the SPA page module, like static application sandbox and application.
[00:48:33]
I said you can change them, but I just noticed if you do MSPA add and you change them, you will get incorrect code to solve, which can be fine, but you will not have as much help as you would have expected.
[00:48:50]
Yeah, that's interesting. I wonder how much changing those helpers is in line with Ryan's vision and intention for the framework.
[00:48:59]
Probably not much.
[00:49:01]
We'll see. Maybe. I mean, I think it's the kind of thing too that as people use it more, this is the type of thing that could potentially evolve.
[00:49:09]
You know, maybe the CLI, you know, will introspect this file and give you those options based on that. Who knows? But yeah.
[00:49:18]
Oh, I want to try that out now.
[00:49:21]
Yeah, I want to hear more of Ryan's thoughts on that. I'll tweet about it and report back.
[00:49:28]
So if people want to get started, how should they learn about it?
[00:49:32]
Okay, so Ryan made a very helpful website, which is his documentation since he doesn't have an Elm package anymore.
[00:49:40]
So there's no entry on the packages, the Elm package registry website, at least not for this version.
[00:49:47]
So you can go to elm dash SBA dot dev and you can follow his guide. So you got something that looks kind of like the official Elm guide remotely.
[00:50:01]
We kind of learn some concepts of Elm or architecture.
[00:50:06]
I still do recommend the official guide first because it doesn't teach you Elm.
[00:50:13]
Yeah, but it's a very good tutorial. He introduces the concepts very nicely.
[00:50:18]
So yeah, go there and you will have a getting started section, which is basically install Elm SBA via NPM and do Elm dash SBA in it.
[00:50:31]
Yep, right. If you have questions, there is a Elm dash SBA dash users on the Slack channel.
[00:50:43]
You have a channel on Slack. Yeah.
[00:50:46]
Yeah, people seem to be responsive there and there's a lot of conversation happening in that channel.
[00:50:51]
Yeah. Definitely a good place to look.
[00:50:54]
And then there's Ryan recently released Elm SBA real world or as he as he considered naming it Elm SBA example example.
[00:51:07]
It's a rewrite of the real world app, which we mentioned at the top.
[00:51:11]
It's Richard Feldman did a great demo repository that builds this sort of medium blog platform clone in Elm as an SBA to show off how sort of medium scale app looks.
[00:51:25]
So Ryan built that using the Elm SBA framework. That's definitely worth a look.
[00:51:30]
If you haven't looked at Richard's original Elm SBA example, that's also worth a look just for some of the patterns he uses there.
[00:51:38]
And actually, Richard walks through that.
[00:51:41]
So he has an Oslo Elm Day keynote where he walked through some details about that Elm SBA example repo and his front end masters courses talk about it heavily as well.
[00:51:53]
So those are also great resources.
[00:51:56]
Even if you're using Ryan's Elm SBA framework, you'll be served well by by learning more about SBAs and Elm in general.
[00:52:06]
Yeah, I would really like a section in the guide to how to migrate your projects to using Elm SBA.
[00:52:15]
Yeah. The advice I would give is see the code that is being generated by Elm SBA or for new packages and try to make your your pages fit that model one by one.
[00:52:27]
And then when you have something very much like SBA, switch to using Elm SBA is my guess advice.
[00:52:37]
Yeah, I'm trying to think if there would be because you can't really create two different Elm browser applications side by side.
[00:52:48]
And Elm SBA uses browser application.
[00:52:51]
So I'd be curious to hear how people have been approaching that.
[00:52:54]
I know some people have used some large scale projects and migrated over to Elm SBA and they show their, you know, diff for the pull request.
[00:53:03]
And it's, you know, thousands of lines deleted compared to the lines added.
[00:53:07]
And it's quite cool. But I'm curious, do they just go for it and start wiring it in?
[00:53:13]
Or do they do it sort of stepwise where they they have sort of the Elm SBA version and their original versions side by side and both working in production?
[00:53:24]
That's always my leaning is to try to do that if possible.
[00:53:27]
But maybe people are just going for it and doing the wiring because they're confident that it'll work.
[00:53:33]
I don't know. Yeah. Well, I guess if you have a small application, then, yeah, sure.
[00:53:37]
But it really depends on the size. All right.
[00:53:43]
Well, are you going to are you going to use it, Jeroen?
[00:53:45]
I don't know if you want me to say yes or if you want me to say no, I'm going to use Elm Pages.
[00:53:52]
Oh, well, that's that's an option, too. But I mean, they serve very different purposes, I think.
[00:53:57]
But no, I'm curious. Are you are you going to be using it for any apps that you've got?
[00:54:02]
Well, at my workplace, we are gradually migrating to something like Elm SBA.
[00:54:07]
We were actually pretty inspired by the SBA before. Yes. Oh, OK.
[00:54:13]
So we were going into that direction and then V5 came out.
[00:54:17]
So I'm still a bit curious about how the load and save will turn out in production.
[00:54:24]
Yeah. Yeah. Which is the big change in V5, in my opinion.
[00:54:29]
But yeah, I find it very interesting. So, yeah, I guess we'll try it out.
[00:54:33]
And then I don't really have any of my own projects, which have their own websites.
[00:54:38]
But yeah, if I ever do want to probably do something like Elm Pages or Elm SBA.
[00:54:43]
Yeah, definitely. Or depending on size, just my using my own bullet platey code.
[00:54:51]
Right. I definitely think that code generation is one of these things that, you know,
[00:54:57]
you do have to use it somewhat sparingly and be careful how you do it.
[00:55:01]
Maybe that's a topic for a whole episode, but you want to make a very clear mapping between the source of truth of the code.
[00:55:07]
Ideally, it's some Elm code and what it's going to do with that as the input.
[00:55:12]
But code generation is certainly a very powerful technique in Elm when it's used sparingly and in the strategic places.
[00:55:20]
And I definitely think that Elm SBA is one of those examples. So, well, great stuff.
[00:55:26]
Yeah, we did talk about code generation in the episode called Extending Elm, episode seven.
[00:55:34]
We don't often go back to our previous episodes, but hey, go listen to episode seven.
[00:55:39]
That's right. Great stuff. Also, if people haven't done so yet, we would love to get ratings in the iTunes podcast app.
[00:55:51]
So if you go to the Elm Radio podcast in iTunes or in Apple podcasts, all you have to do is go to the podcast and tap in a star rating.
[00:56:01]
And that helps people find the podcast.
[00:56:04]
The rating the most on the right.
[00:56:06]
Oh, that's right. Yeah, yeah. Just press the fifth star and you'll be good.
[00:56:10]
Also, tweet us your questions or go to elm.radio.com slash question.
[00:56:16]
Yeah, we also have a Twitter account now which is called Elm Radio Podcast.
[00:56:22]
Yeah, let us know what you want to hear about and let us know if you're using Elm SBA. We'd love to hear about it.
[00:56:28]
Yeah. All right. Well, thanks again, Jeroen. And I'll talk to you next time.
[00:56:32]
See you next time.