0.1.1 • Published 3 years ago

simate v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

simate

A simate is the core of our state management. Create a simate instance via createSimate. This instance is where the magic happens. All the methods are on the simate instance.

createSimate

Create a simate instance

import { createSimate } from 'simate';

const count = createSimate(69);
// 69 is initial value for `count`. It is optional.

// You can also set initial value by calling a function as the argument.
function initVal() {
  return 420;
}
const count = createSimate(initVal()); // count = 420;

// You can explicitly provide type for your simate as generic.
const mySimate = createSimate<string | number>('Hello World!');
// mySimate can be a string or a number.

Simate instance's methods

.get()

Provides the current value

const value = count.get(); // Simate's value is 69

.set()

Allows you to mutate the value

count.set(420); // Simate's value is now 420
// ----OR----
// You can also pass a function which takes the `prev`(current value of simate)
// as the argument.
// The return value of the function will be the next value of the simate.
count.set((prev) => prev + 1);

.attach()

Attach a change listener to a simate so that it is triggered everytime the simate's value is mutated via .set() method. You can attach as many callback functions you want. This helps us achieve the "reactivity".

NOTE: Do not use this method inside the function that you are attaching.

function doSomethingWhenSimateValueChanges() {
  // ...
}
count.attach(doSomethingWhenSimateValueChanges);

Detach a callback function from a simate

detach()

An object is returned by simate.attach() which has detach() method on it. You can call detach() to stop it from being triggered when the simate's value changes.

function doSomethingWhenSimateValueChanges() {
  // ...
}

const listener = count.attach(doSomethingWhenSimateValueChanges);

listener.detach();
// `doSomethingWhenSimateValueChanges` will no longer be triggered.
0.0.46

3 years ago

0.1.0

3 years ago

0.1.1

3 years ago

0.0.43

3 years ago

0.0.44

3 years ago

0.0.45

3 years ago

0.0.40

3 years ago

0.0.41

3 years ago

0.0.42

3 years ago

0.0.20

3 years ago

0.0.21

3 years ago

0.0.23

3 years ago

0.0.24

3 years ago

0.0.25

3 years ago

0.0.15

3 years ago

0.0.16

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.19

3 years ago

0.0.30

3 years ago

0.0.31

3 years ago

0.0.32

3 years ago

0.0.10

3 years ago

0.0.33

3 years ago

0.0.11

3 years ago

0.0.34

3 years ago

0.0.12

3 years ago

0.0.35

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.1

3 years ago

0.0.0

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.9

3 years ago

0.0.26

3 years ago

0.0.8

3 years ago

0.0.27

3 years ago

0.0.28

3 years ago

0.0.29

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

1.0.0

3 years ago