Code Monkey home page Code Monkey logo

mobx_experimentation's Introduction

mobx_experimentation

A project to play with the mobX package.

This project aims to experiment when there are dependencies between different Stores. It helps to understand the concept of @computed even if the observable comes from an other store. The behavior between observableList and reaction is tested. There are logs in the view to see which part of the code is rebuilt when the observables change.

State

abstract class _StateA with Store {
  @observable
  bool state = false;

  @action
  void switchState() {
    state = !state;
  }
}

abstract class _StateA with Store {
  _StateA({@required StateA stateA}) : _stateA = stateA;

  final StateA _stateA;

  @computed
  bool get stateAState => _stateA.state;
}

Count

class CountStore = _CountStore with _$CountStore;

abstract class _CountStore with Store {
  @observable
  int count = 0;

  @action
  void increment() {
    count++;
  }

  String get countString => 'Total without computed : $count';

  @computed
  String get computedCountString => 'Computed total : $count';

  Color get color {
    if (count > 10) {
      return Colors.red;
    } else {
      return Colors.green;
    }
  }

  @computed
  Color get computedColor {
    if (count > 10) {
      return Colors.red;
    } else {
      return Colors.green;
    }
  }
}

List

class ListStore = _ListStore with _$ListStore;

abstract class _ListStore with Store {
  @observable
  List<String> list = [];

  @observable
  ObservableList<String> observableList = ObservableList.of([]);

  @action
  void addItem(String item) {
    list = [
      ...list,
      item,
    ];
    observableList.add(item);
  }
}

class ReactionStore extends _ReactionStore with _$ReactionStore {
  ReactionStore({@required ListStore listStore}) : super(listStore: listStore);
}

abstract class _ReactionStore with Store {
  _ReactionStore({@required ListStore listStore}) : _listStore = listStore {
    _reactionListDisposer = reaction<List<String>>(
      (_) => _listStore.list,
      updateCount,
      fireImmediately: true,
    );
    _reactionObservableListDisposer = reaction<List<String>>(
      (_) => _listStore.observableList,
      updateCount,
      fireImmediately: true,
    );
  }

  ReactionDisposer _reactionListDisposer;
  ReactionDisposer _reactionObservableListDisposer;

  final ListStore _listStore;

  @observable
  int listCount;

  @observable
  int observableListCount;

  @computed
  int get computedListCount => _listStore.list.length;

  @computed
  int get computedObservableListCount => _listStore.list.length;

  void updateCount(List<String> list) {
    listCount = list.length;
  }

  void updateObservableListCount(List<String> observableList) {
    observableListCount = observableList.length;
  }

  void dispose() {
    _reactionListDisposer();
    _reactionObservableListDisposer();
  }
}

mobx_experimentation's People

Contributors

zaraclaj avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.