0.6.3 • Published 10 days ago

mlyn v0.6.3

Weekly downloads
-
License
-
Repository
github
Last release
10 days ago

mlyn

Reactive immutable state.

Inspired by solidjs

For react binding check react-mlyn

create a subject

let create a simple subject by passing in initial state

import { createSubject } from "mlyn";

const subject = createSubject({
  user: {
    firstName: "Adam",
    lastName: "Smith",
  },
});

Now you can access any property by invoking as function corresponding value on subject:

subject.user.firstName(); // Adam

You can retrieve the value on any level of nesting

subject.user().firstName; // Adam

modifying subject

You can modify a property, just in the same way as with plain js object, however this will update full state in immutable fashion:

const stateCopy = subject();
subject.user.firstName = "Abraham";

console.log(subject.user()); // { firstName: "Abraham", lastName: "Smith" }
console.log(subject() === stateCopy); // false, cause root object has changed as well.

Assignments, can be done, on any nesting level (expect to root one):

subject.user = { ...subject.user(), firstName: "Abraham" };

To modify root you can invoke the subject as a function (same as to retrieve a value), but passing in the new value:

subject({ user: { firstName: "Mihas", lastName: "Vaukalak" } });

This approach is applicable on any level on nesting

subject.user.firstName("Abraham");

Btw, this approach will work if you perfrom destructuring or pass a node as parameter to a function:

const upperCase = (value) => {
  value(value().toUpperCase());
}
const { firstName } = subject.user;
upperCase(firstName);
console.log(subject()); // { user: { firstName: "ADAM", lastName: "Smith" } }

Reactive scopes

Since the state is fully immutable, we can detect any update of it (or of any sub-node) using runInReactiveScope api.

const logState = (state) => {
  runInReactiveScope(() => {
    console.log("subject: ", state());
  });
};
logState(subject.user.firstName);
subject.user.firstName = "Abraham";

This will log both Adam and Abraham, because, if you invoke a subject inside callback passed to runInReactiveScope a subscribtion to this object gets created. The best thing of it, is that subscription gets created only on the part of the state, you've asked for:

subject.user.lastName = "Lincoln"; // nothing logged, cause we observe only first name
0.6.3

10 days ago

0.6.2

10 days ago

0.6.1

10 days ago

0.6.0

10 days ago

0.5.21

1 month ago

0.5.22

1 month ago

0.5.23

1 month ago

0.5.20

1 year ago

0.5.16

1 year ago

0.5.12

2 years ago

0.5.13

2 years ago

0.5.3

2 years ago

0.5.0

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.4.2

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.1

3 years ago

0.1.0

3 years ago

0.1.2

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago