Code Monkey home page Code Monkey logo

mota's Introduction

logo

npm NPM Version Coverage Status npm

Overview

Mota is a "lightweight and responsive" state management library, which is less than 5KB. Developers can use it to write "almost framework independent pure js/ts business models", and then use Mota to simply let react components respond to model changes.

Install

Install through NPM as follows

$ npm install mota --save

Usage

Example 1: Hello Mota

import { observable, observer } from "mota";

const model = observable({ count: 0 });
const add = ()=>model.count++;

const View1 = observer(() => {
  return <div>{model.count}</div>;
});

const View2 = observer(() => {
  return <div>
    <span>{model.count}</span>
    <button onClick={add}>click<button>
  </div>;
});

Example 2: Using useObservable

import { observer, useObservable } from "mota";

const View = observer(() => {
  const model = useObservable({ count: 0 });
  return <div>
    <span>{model.count}</span>
    <button onClick={()=>model.count++}>click<button>
  </div>;
});

Example 3: Using useComputed

import { observer, observable, useComputed } from "mota";

const user = observable({ 
  firstName: 'Feng',
  lastName: 'Hou'
});

const View = observer(() => {
  // The fullName will be cached and responsive
  const fullName = useComputed(()=>{
    return `${user.firstName} ${user.lastName}`;
  });
  return <div>{fullName}</div>;
});

Example 4: Using useAutoRun

import { observer, observable, useAutoRun } from "mota";

const model = observable({ count: 0 });

const View = observer(() => {
  // When the count changes, 
  // it will be automatically re executed and printed 'count: n'
  useAutoRun(()=>{
    console.log('count:', model.count);
  });
  return <div>{model.count}</div>;
});

Example 5: Using useWatch

import { observer, observable, useWatch } from "mota";

const model = observable({ count: 0 });

const View = observer(() => {
  // When the result of the evaluation function changes,
  // the processing function is re executed.
  useWatch(()=>model.count%10, (oldValue, newValue)=>{
    console.log(`old: ${oldValue}, new: ${newValue}`);
  });
  return <div>{model.count}</div>;
});

Example 6: Using in class components

import { observer, observable, autorun, watch } from "mota";

const model = observable({ count: 0 });

@observer
class View extends React.Component {
  add = ()=> model.count++;

  componentDidMount(){
    this.destroyAutoRun = autorun(()=>{
      console.log('autorun count: ', model.count);
    });
    this.destroyWatch = watch(()=> model.count%10, ()=>{
      console.log('autorun count: ', model.count);
    });
  }

  componentWillUnmount(){
    this.destroyAutoRun();
    this.destroyWatch();
  }

  @computed get message(){
    return `count: ${model.count}`;
  }

  render() {
    return <div>
      <span>{this.message}</span>
      <button onClick={this.add}>click<button>
    </div>;
  }
}

Example 7: Using multiple instances in class components

import { observer, observable, autorun, watch } from "mota";

@observable
class Model {
  count = 0;
  add () {
    this.count++;
  }
  @computed get message(){
    return `count: ${model.count}`;
  }
}

@observer
class View extends React.Component {
  model = new Model();
  render() {
    return <div>
      <span>{this.model.message}</span>
      <button onClick={()=>this.model.add()}>click<button>
    </div>;
  }
}

mota's People

Contributors

houfeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mota's Issues

Mditor will crash when file is big.

MAC OSX 10.12.5, mditor: 1.0.0
If file is big, press delete key in keyboard when editing, mditor will crash.
the file size is only 11kB
一般在文章的后半段出现这个现象。

图片不支持相对路径

通过工具栏上的图片插入功能插入本地图片后使用的是绝对路径,改成相对路径时图片无法正常预览。

  • 希望预览功能增加相对路径支持。
  • 希望编辑器支持复制粘贴,直接将剪切板的图片保存到指定目录下,并以相对路径方式插入到编辑器中

例如,我的markdown文档都通过git管理,我一般将图片放在image目录下,然后在markdown文件中通过相对路径引用图片。本地编辑完后直接提交到github,github也是支持相对路径引用同一仓库中的图片文件的。

新版本1.0.0Beta2无法下载

该版本无法下载,目前使用的mac桌面版1.0.0.beta1,在编写的过程中,光标定位不准确,出现跳跃的情况,官网无法下载1.0.0.beta2的dmg安装文件。

mac下存在输入法乱跳的问题

mac下,编辑到款出现编辑器滚动条是,当光标出现在文档中间,搜狗输入法在快速输入词语的时候,输入法光标会乱跳到文档的最末尾去,此时在原光标的位置为拼音输入的情况,文档最末尾为需要输入的文字,其他输入法貌似也存在同样的情况(mac自带输入法)

不能编译

系统:macOS Sierra
node: 7.4.0

执行‘npm run dev’ 报错:

NODE_ENV: dev
Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.output.path: The provided value "./build/" is not an absolute path!

npm ERR! Darwin 16.4.0
npm ERR! argv "/Users/hui/.nvm/versions/node/v7.4.0/bin/node" "/Users/hui/.nvm/versions/node/v7.4.0/bin/npm" "run" "dev"
npm ERR! node v7.4.0
npm ERR! npm  v4.3.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: `./bin/watch.sh`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev script './bin/watch.sh'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the mditor-desktop package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     ./bin/watch.sh
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs mditor-desktop
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls mditor-desktop
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/hui/.npm/_logs/2017-04-10T11_27_19_895Z-debug.log

查看webpack配置:

module.exports = {
  context: __dirname,
  entry: {
    bundle: `./src/window/index.js`
  },
  output: {
    path: './build/',
    filename: 'js/[name].js'
  },
  devtool: 'source-map',
  module: {
    loaders: loaders
  },
  plugins: plugins
};

** bundle**这个配置可能有中英文的问题,修改了后还是报以上的问题。

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.