Hacker Newsnew | past | comments | ask | show | jobs | submit | naansequitur's commentslogin

Author here, those are fair points.

For the part about increasing potential for bugs at scale, my mind was on scaling one or more teams and projects. Might have been better to use some other word than scale since it's so overloaded.

I find it difficult to think of patterns to introduce that would help teams align on when and how to use reactive `$` statements.

It's not a Svelte-specific problem by any means, but I do think Svelte's reactive statements would cause more pain than it would help ease as teams and projects get larger.


I thought about it some more after writing my reply. I think a first approximation is that $: gets confusing almost as soon as you bring in more than one level of branching control flow. It’s better to do that with stores or encapsulate the logic in plain old functions.

But if you mostly have some values that need to be recomputed when other values change, or an effect that needs to re-run, it’s a convenient tool to use.


Hi there. IMO, there are three issues there: familiarity, mental model, and caveats (using $ and not getting reactive updates in some cases).

Focusing on mental model, maybe my mental model can inspire yours? Here it goes.

- For me, `$: dependent variable = expression(independent variables)`, is an equation that Svelte guarantees will hold across the single-file component (SFC). So whenever an independent variable change, the dependent variable is also updated to maintained the equation. - Caveat: In the statement `$: dependent variable = expression(independent variables)`, the expression language is JavaScript but the semantics of the statement are Svelte's (i.e. *Svelte's* scoping rules apply, not JavaScript's). SvelteScript is so close from JavaScript that it feels intuitive to use but confusing when the two differ. In a poor analogy, just like using a word that means something in French, another in Spanish, and wrongly guessing the source language (hence the meaning) that applies.

Then: - `$: {some statements here featuring independent variables}`. *Svelte's* scoping rules apply (only the variables visible in the block are targeted by Svelte's reactivity). This is not an equation anymore, it is the expression of an effect triggered by change in dependent variables.

Why this is natural to me? At the specification level, reactive systems are specified with three core syntactic constructs:

1. event -> reaction 2. dependent variable = function (independent variable) 3. dependent variable <- function (independent variables)

The first item means is where you specify what happens when an event occurs. for instance button click -> (counter <- counter + 1; render new counter)

The second item is the same as lambda abstraction. Say `area = f(length, width)`. That's true all the time and allows using `area` everywhere needed instead of `f(length, width)`. But by referential equality rules, you could absolutely do away with `area` - at the expense of course of less clear and more verbose code (and probably less performant too)

The third item is assignment, and is used to describe changes in the state of the component. As a rule, (reaction, new state) = f(event, current state). So the third item describes how to get new state from current state. The first item describes how to get the reaction from the event and current state. The second item is optional but helps a lot readability, conciseness, and performance (not computing `area` twice if you use it twice).

In Svelte syntax: 1 is $: {reaction code here} (event is some change in value of variables scoped in the code) 1 bis: <some html code here> is the same case as 1 with a different syntax when the reaction is a (re)render. Whenever the dependent variables in scope change, a rerender reaction is executed. 2 is $: x = f(a,b,c,...) (Note that the right hand is a single variable name) 3 is any assignment occurring in the SFC.

Not sure if that helps, but well, that's how I stay away from the pitfalls of mixing SvelteScript and JavaScript. Identify events, state variables, the reaction to the events, and the state changes. Then translate that into SvelteScript.


Author here, thanks for the context.

Added an update to the end of the article linking to this comment - https://tyhopp.com/notes/thoughts-on-svelte#update-2023-03-2...


Hacker News comments and "fun to read". Who are you, sir?

:)


Author here, this is a great point.

Added an update to the end of the article linking to this comment - https://tyhopp.com/notes/thoughts-on-svelte#update-2023-03-2...


The forward looking statements disclaimer at the end of their announcement post is such a sharp tone shift it's almost funny - https://www.figma.com/blog/a-new-collaboration-with-adobe/


Thanks for the kind words!


I take that approach too if the site is small enough.

The challenge is if the site starts to get into the hundreds of pages and you want to be able to reuse templates and/or render lists of content. Then you reach for a static site generator.


Hi HN,

This is an open source project I've worked on over weekends for a long time. The goal is to create a tool well suited for websites you expect to be around for 5 or 10+ years.

Would love any thoughts you have on it.

The reason why I decided to build this is all other (JS-based) static site generators I tried had one or of these problems:

- Built on an underlying framework like React, Vue, etc.

- Relies on complex build tools like Webpack, Babel, etc.

- Depends on a massive tree of modules that force constant maintenance

- Has interfaces, source code and documentation that cannot be understood in one sitting

- Requires that your site source be organized in a way that looks nothing like your output

- Forces a huge leap from hello world to a real world implementation

Thanks,

naansequitur

EDIT - List formatting


Really like what you are going for here. I've actually been working on a similar system of a minimalist static site generator off and on for about a year. Looking forward to having a more in depth look at your approach.. and congrats for putting it out there, I've been letting my imposter syndrome keep me from publicly showing my system off. I really dig your templating system, I ended up going with Handlebars but I always planned on removing it at some point. Are you using grey matter for parsing the markdown metadata or did you create your own parser?


Hey thanks! Can definitely empathize with the imposter syndrome, it's a tough hurdle to clear.

For the templating system I decided to also add an option to define your own if you don't like the [] syntax (https://prpl.dev/api#options). Figured since the system is regex-based instead of AST-based there's no reason not to expose it as an option to users.

For the metadata I implemented a basic parser. Here's a link to that part of the source code, it's not the most efficient nor does it cover all the edge cases, but it's simple enough it can be easily updated (https://github.com/tyhopp/prpl/blob/master/packages/core/src...).

Good luck and hope I can see your system on HN someday too


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: