Code Monkey home page Code Monkey logo

leaf-observable's Introduction

leaf-observable

Build Status Coverage Status

Observe data & make data Reactive.

Intro

现代前端科技解析 —— 数据响应式系统 (Data Reactivity System)

Install

npm install leaf-observable --save-dev

or

yarn add leaf-observable --dev

Feature

  • 支持数组监测
  • 支持自动依赖收集
  • 支持异步事件队列
  • 支持通过 set/del 添加删除属性
  • 支持 deep watch
  • 支持 immediate 同步执行回调
  • 可以设置 expFn 的 context,允许 'return this.a + this.b' 写法
  • 支持 unwatch

Usage

import {observify, watch, set, del} from 'leaf-observable'

let o = {
  a: 1,
  b: {
    c: [1, 2, 3, 4],
    d: 2,
    e: 3
  }
}

observify(o)

watch(() => {
  return o.a + o.b.e
}, (newVal, oldVal) => {
  console.log('newValue:', newVal, ', oldValue:', oldVal)
})

o.a = 2   // console: newVal: 5  oldVal: 4

API

observify(obj)

Observify a object.

  • obj(object): the object to be observified.

watch(expFn, callback, options)

Add a new watcher.

  • expFn(function): function which return needed value which rely on reactive data.
  • callback(function): function to call after expFn's result has been changed.
  • options(object):
    • deep(boolean): whether watch nested objects.
    • immediate(boolean): whether run callback function syncly.
    • context(object): the context when expFn runs, will be 'this' in expFn.

return a watcher insatce. use instance.unwatch can cancel watcher.

set(obj, key, value)

Dynamic add attribute to a reactive object.

  • obj(object): target object
  • key(string): key
  • value(any): value

del(obj, key)

Dynamic delete attribute in a reactive object.

  • obj(object): target object
  • key(string): key

LICENSE

MIT

leaf-observable's People

Contributors

jin5354 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.