MCPcopy Index your code
hub / github.com/Houfeng/mota

github.com/Houfeng/mota @8.2.4

Chat with this repo
repository ↗ · DeepWiki ↗ · release 8.2.4 ↗ · + Follow
58 symbols 144 edges 24 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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 

{model.count}

;
});

const View2 = observer(() => {
  return 


    <span>{model.count}</span>
    <button onClick={add}>click<button>


;
});

Example 2: Using useObservable

import { observer, useObservable } from "mota";

const View = observer(() => {
  const model = useObservable({ count: 0 });
  return 


    <span>{model.count}</span>
    <button onClick={()=>model.count++}>click<button>


;
});

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 

{fullName}

;
});

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 

{model.count}

;
});

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 

{model.count}

;
});

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 


      <span>{this.message}</span>
      <button onClick={this.add}>click<button>


;
  }
}

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 


      <span>{this.model.message}</span>
      <button onClick={()=>this.model.add()}>click<button>


;
  }
}

Core symbols most depended-on inside this repo

$
called by 16
test/helpers/renderer.ts
observer
called by 14
src/observer.ts
render
called by 8
test/helpers/renderer.ts
createConf
called by 6
rollup.config.dev.js
render
called by 6
examples/develop.tsx
createConf
called by 4
rollup.config.js
bind
called by 3
src/input.ts
useWatch
called by 3
src/hooks.ts

Shape

Function 40
Class 14
Method 4

Languages

TypeScript100%

Modules by API surface

test/observer.test.tsx9 symbols
examples/develop.tsx8 symbols
src/observer.ts7 symbols
src/hooks.ts4 symbols
src/util.ts3 symbols
examples/benchmark-redux.tsx3 symbols
examples/benchmark-mota.tsx3 symbols
examples/benchmark-mota-old.tsx3 symbols
examples/benchmark-mobx.tsx3 symbols
test/input.test.tsx2 symbols
test/hooks.test.tsx2 symbols
test/helpers/renderer.ts2 symbols

For agents

$ claude mcp add mota \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact