• Stars
    star
    309
  • Rank 135,306 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The stupidest state management library that works.

PureState.js

A stupid, simple, clean, ridiculously small state management library that is supposed to cover every use case of complex solutions such as Flux/Reflux/etc without the overengineering. See the rant below.

var state = require("./purestate");

// Stateful variables are just JS values wrapped with a `state` call
var x = state(0);

// This reads a stateful variable; read as "console.log(x)"
console.log(x());

// This writes a stateful variable; read as "x = 1"
x(1);

// Stateful variables can depend on other stateful variables 
var y = state(() => x() + 1);
var z = state(() => [x(), y(), x()+y()]);

console.log(x());
console.log(y());
console.log(z());

// Those above output "1", "2", "[1, 2, 3]"

// If you change a stateful vriable, all variables that depend on it are updated.

x(10); // sets x to 10

console.log(x());
console.log(y());
console.log(z());

// Now those above output "10", "11", "[10, 11, 21]"

The idea is simple. 99% of your program should consist of pure functions and values. The 1% that isn't pure should be written as if it was. Then, when you do need to mutate that 1% - just do it. Not indirectly like you do. Just do it, and let PureState deal with recomputing every other value that depends on it, doing the minimal amount of work necessary. Since those "mutations" happen in response to events such as onkeypress, referential transparency isn't broken.

See example_counter.html and example_todo.html for a quick example of how that simple concept is powerful enough to, for example, write interactive MVC web applications in very simple, pure way. That is just to show the idea - of course those examples could be much better with, for example, VirtualDOM or React instead of strings for rendering.

The rant

After so many years, seems like the public is finally learning to appreciate the benefits of purity and immutability. The evolution from the times of jQuery, through Angular, to React, show it. React nails the issue of rendering views, but it still has issues with state. The initial proposal of Flux had clear weakenesses and the market slowly gravitated torwards better solutions.

FRP-inspired answers such as Redux are the hot thing now. I think those are the wrong approach. Folds ("reducers") are merely simulating state through the continuous application of a function to a list-like structure. The fact it is pure under the hoods doesn't make your state less stateful. It just makes it more awkward. Your giant reducer function is no better than just mutating variables on your heap, and zipping streams is just a fancy way to combine variables. Except now you have a reversible history. It is overengineering for no real benefit.

In my understanding, the solution to state is simple: stop fighting it. State is not bad - what is bad is the century-old practice to use state where it is not needed. As long as you do it right - i.e., keep 99% of your application pure, identify the minimal amount of data that needs to be mutated and only alter it - then it is perfectly healthy to treat state as a first-class citizen.

More Repositories

1

WebMonkeys

Massively parallel GPU programming on JavaScript, simple and clean.
JavaScript
1,163
star
2

LJSON

JSON extended with pure functions.
JavaScript
500
star
3

Caramel

A modern syntax for the λ-calculus.
Haskell
405
star
4

forall

Expressive static types and invariant checks for JavaScript.
JavaScript
227
star
5

abstract-algorithm

Optimal evaluator of λ-calculus terms.
JavaScript
222
star
6

Cedille-Core

A minimal proof language.
JavaScript
193
star
7

optlam

An optimal function evaluator written in JavaScript.
JavaScript
115
star
8

Interaction-Type-Theory

Rust
108
star
9

calculus-of-constructions

Minimal, fast, robust implementation of the Calculus of Constructions on JavaScript.
JavaScript
94
star
10

Bitspeak

JavaScript
80
star
11

articles

Thoughts and stuff
JavaScript
65
star
12

UrnaCripto

Referendos criptograficamente incorruptíveis.
JavaScript
51
star
13

lrs

Linkable Ring Signatures on JavaScript and PureScript.
PureScript
46
star
14

lambda-calculus

A simple, clean and fast implementation of the λ-calculus on JavaScript.
JavaScript
44
star
15

heart

heart
JavaScript
41
star
16

ultimate-calculus

TypeScript
36
star
17

absal-rs

Rust
36
star
18

HOC

C
32
star
19

nano-json-stream-parser

A complete, pure JavaScript, streamed JSON parser in less than 1kb.
JavaScript
29
star
20

servify

Microservices in the simplest way conceivable.
JavaScript
28
star
21

optimul

Multiplication on optimal λ-calculus reducers
JavaScript
22
star
22

parallel_lambda_computer_tests

learning cuda
Cuda
18
star
23

nano-ipfs-store

Lightweight library to store and get data to/from IPFS
JavaScript
14
star
24

formality-agda-lib-legacy

Agda libraries relevant to Moonad
Agda
14
star
25

taemoba

13
star
26

unknown_halting_status

Small programs with unknown halting status.
JavaScript
12
star
27

lsign

Quantum-proof, 768-bit signatures for 1-bit messages
JavaScript
11
star
28

reasoning_evals

my reasoning evals
JavaScript
9
star
29

Elementary-Affine-Net-legacy

JavaScript
8
star
30

OpenLegends

An open-source MOBA in Rust
6
star
31

ethereum-offline-signer

Signs an Ethereum transaction from the command line.
JavaScript
6
star
32

coc-with-math-prims

JavaScript
5
star
33

idris-mergesort-benchmark

Benchmark of the new Idris JS backend
JavaScript
5
star
34

uwuchat2_demo

UwUChat2 demo game
TypeScript
5
star
35

LPU

JavaScript
4
star
36

Vote

3
star
37

nano-persistent-memoizer

Caches a function permanently on browser and node.
JavaScript
3
star
38

nano-sha256

Use native Sha256 on both browser and Node.js
JavaScript
3
star
39

ReflexScreenWidget

A widget for Haskell-Reflex that renders a dynamic image to a Canvas in realtime.
Haskell
3
star
40

OSX

Vim script
3
star
41

EthFP

3
star
42

VictorTaelin

3
star
43

ethereum-rpc

JavaScript
2
star
44

ethereum-publisher-dapp

JavaScript
2
star
45

shared-state-machine

JavaScript
2
star
46

Trabalho-IC-UFRJ-2

Python
2
star
47

NeoTaelin

2
star
48

eth-web-tools

Some web Ethereum tools that MyEtherWallet currently lacks
JavaScript
2
star
49

hvm2

Cuda
2
star
50

symmetric-interaction-calculus-benchmarks

SIC benchmarks
JavaScript
2
star
51

talks

C
2
star
52

gpt

2
star
53

agbook

AGDA
Agda
2
star
54

Kind2

Kind refactor based on HVM
2
star
55

kind-react-component

Renders a Kind app as a React component
1
star
56

PongFromScratch

A simple tutorial on how to create a ping-pong game from scratch
1
star
57

tsbook

TypeScript
1
star
58

sketch_bros

Super Sketch Bros!
JavaScript
1
star
59

StupidMinimalistHash

C
1
star
60

MaiaVictor.github.io

HTML
1
star
61

PotS

JavaScript
1
star
62

LamBolt

The ultimate compile target for functional languages
1
star
63

form-login

Um formulário de Login feito com HTML e CSS usando transições
CSS
1
star
64

who-loves-voxels

JavaScript
1
star
65

cuda_rewrite_tests

learning cuda
Cuda
1
star
66

luna-lang-old-abandoned-repo

Typed version of Moon-lang
JavaScript
1
star
67

posts

1
star
68

FPL

A collection of functional JavaScript modules.
JavaScript
1
star
69

PassRhyme

JavaScript
1
star
70

something_calculus

1
star
71

moon-bignum

Minimalistic bignum library compiled from Moon-lang.
JavaScript
1
star
72

see

See inside JS functions
JavaScript
1
star
73

Trabalho-IC-UFRJ

JavaScript
1
star
74

optimal_evaluation_examples

optimal evaluation examples (DUP nodes, SUP nodes)
Haskell
1
star
75

inferno-hello-world-component

JavaScript
1
star
76

inet

JavaScript
1
star