Repository navigation
Chronicles #19 — New Library, New Rules #36
Replies: 1 comment
|
Rule 11 — Every atom says hello. const count = atom(0); count.subscribe(value => { Subscribe to an atom and you get its current value immediately. Because an atom is not just waiting for something to change. It already knows something. If it has a value, the subscriber should not have to ask twice. Rule 12 — A scope keeps its atoms awake. You still close the door. Atoms created inside a scope are subscribed automatically. You do not have to introduce them one by one. But a scope cannot know when your feature, route, session, or component is truly finished. const user = scope({
name: "",
email: "",
});
// later, when its owner is done
user.destroy();The scope keeps the house running. You still have to lock it when you leave. Rule 13 — A pipe method produces an atom too.
It produces a readable atom. const doubled = pipe(
count,
map(value => value * 2),
);So it has a current value. It can feed a It can become the source of another pipe. It can live alongside state and flows without needing a translation ceremony. const label = derived(() => `Doubled: ${doubled.value}`);A pipe is not the end of the reactive story. It is another atom in it. Rule 14 — Atoms multicast. Multiple subscribers do not create multiple flows. They share one atom. const clock = pipe(
interval(1000),
map(() => new Date()),
);
clock.subscribe(renderClock);
clock.subscribe(logClock);One timer. One sequence of values. Many listeners. The atom does the work once, then multicasts each update to everyone who cares. No accidental duplicate requests. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Every library has rules. Some are written in documentation. Some are discovered at 3 AM.
Streamix prefers to write them down early.
Rule 1 — If it has a value, make it an atom.
Don't build a kingdom around a number.
It's a number.
Let it be a number.
Rule 2 — If it can be calculated, don't store it.
Storing
totalseparately means you now have two problems:Congratulations.
Rule 3 — If it takes time, let it flow.
Some things don't simply have a value.
They happen.
Let them happen.
Rule 4 — Don't chase values. Ask for the next one.
No observer hiding behind a curtain.
No mysterious callback waiting to surprise you.
Just:
How demanding.
Rule 5 — When nobody is listening, go home.
No subscribers? No consumers? Nothing to do?
Excellent.
Stop working.
Computers deserve boundaries too.
Rule 6 — If it belongs together, scope it together.
State, computations, flows, cleanup.
One home.
When the home goes away, everybody leaves.
No abandoned timers hiding in the basement.
Rule 7 — Don't compute the universe after every keystroke.
State changes fast. Your CPU doesn't have to keep up with every mood swing.
Rule 8 — Cancellation should not require a philosophy degree.
If you stop asking an iterator for values, it stops producing them.
That's the idea.
No twelve-page lifecycle diagram required.
Rule 9 — Good abstractions should disappear.
The first time:
The tenth time:
Eventually, you stop noticing the abstraction at all.
You just write code.
That's the goal.
Rule 10 — Don't train for complexity.
The best code doesn't make you work for understanding.
If a simple idea requires serious mental effort to follow, the architecture is charging too much.
Good design often looks like restraint:
These rules are unofficial. Just nice to know.
All reactions