Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

article: the best shared state pattern with remini #26

Open
betula opened this issue Nov 22, 2022 · 0 comments
Open

article: the best shared state pattern with remini #26

betula opened this issue Nov 22, 2022 · 0 comments

Comments

@betula
Copy link
Member

betula commented Nov 22, 2022

The central part of any complex application is stateful logic. Application has a state and a logic that uses and modifies the state.

For example, according to Redux architecture the one state, and logic inside sagas, thunks, reducers, or action creators.

Remini proposal (suppose) pattern combined great ideas from Redux, Vue, and Angular. In depends on your internal particularities, you can choose one of the supposed implementations. The first one will be better for Redux-familiar developers.

When the center of our stateful logic is the Store.

const User = () => {
  $state = box(initialState);

  const login = () => {
    update($state, state => { ...state, loggedIn: true });
  }

  const logout = () => {
    update($state, state => { ...state, loggedIn: false });
  }

  return Object.assign($state, {
    login,
    logout
  });
}

And the second one is the "flat style". All modules should be reactive transparently, looks like simple usual objects.

const User = () => {
  $name = box('Amelia');
  $loggedIn = box(false);

  const login = () => {
    put($loggedIn, true);
  }

  const logout = () => {
    put($loggedIn, false);
  }

  return {
    get name() {
      return val($name);
    },
    get logginIn() {
      return val($logginIn);
    },

    login,
    logout
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant