anu - component
import { extend, isFn, options, clearArray, noop } from "./util";
import { CurrentOwner } from "./createElement";
/**
*组件的基类
*
* @param {any} props
* @param {any} context
*/
var mountOrder = 1;
export function Component(props, context) {
//防止用户在构造器生成JSX
CurrentOwner.cur = this;
this.__mountOrder = mountOrder++;
this.context = context;
this.props = props;
this.refs = {};
this.state = null;
this.__pendingCallbacks = [];
this.__pendingStates = [];
this.__current = noop;
/*
* this.__hydrating = true 表示组件正在根据虚拟DOM合成真实DOM
* this.__renderInNextCycle = true 表示组件需要在下一周期重新渲染
* this.__forceUpdate = true 表示会无视shouldComponentUpdate的结果
*/
}
Component.prototype = {
constructor: Component,//必须重写constructor,防止别人在子类中使用Object.getPrototypeOf时找不到正确的基类
replaceState() {
console.warn("此方法末实现"); // eslint-disable-line
},
setState(state, cb) {
debounceSetState(this, state, cb);
},
isMounted() {
return !!this.__dom;
},
forceUpdate(cb) {
debounceSetState(this, true, cb);
},
__mergeStates: function (props, context) {
var n = this.__pendingStates.length;
if (n === 0) {
return this.state;
}
var states = clearArray(this.__pendingStates);
var nextState = extend({}, this.state);
for (var i = 0; i < n; i++) {
var partial = states[i];
extend(nextState, isFn(partial)
? partial.call(this, nextState, props, context)
: partial);
}
return nextState;
},
render() { }
};
function debounceSetState(a, b, c) {
if (a.__didUpdate) {//如果用户在componentDidUpdate中使用setState,要防止其卡死
setTimeout(function () {
a.__didUpdate = false;
setStateImpl.call(a, b, c);
}, 300);
return;
}
setStateImpl.call(a, b, c);
}
function setStateImpl(state, cb) {
if (isFn(cb)) {
this
.__pendingCallbacks
.push(cb);
}
let hasDOM = this.__dom;
if (state === true) {//forceUpdate
this.__forceUpdate = true;
} else {//setState
this
.__pendingStates
.push(state);
}
if (!hasDOM) { //组件挂载期
//componentWillUpdate中的setState/forceUpdate应该被忽略
if (this.__hydrating) {
//在挂载过程中,子组件在componentWillReceiveProps里调用父组件的setState,延迟到下一周期更新
this.__renderInNextCycle = true;
}
} else { //组件更新期
if (this.__receiving) {
//componentWillReceiveProps中的setState/forceUpdate应该被忽略
return;
}
this.__renderInNextCycle = true;
if (options.async) {
//在事件句柄中执行setState会进行合并
options.enqueueUpdate(this);
return;
}
if (this.__hydrating) {
// 在componentDidMount里调用自己的setState,延迟到下一周期更新
// 在更新过程中, 子组件在componentWillReceiveProps里调用父组件的setState,延迟到下一周期更新
return;
}
// 不在生命周期钩子内执行setState
options.flushBatchedUpdates([this]);
}
}
anu - component的更多相关文章
- 发布高性能迷你React框架anu
anu, 读作[安努],原意为苏美尔的主神. anu是我继avalon之后又一个新框架(github仓库为https://github.com/RubyLouvre/anu, 欢迎加星与试用) 此框架 ...
- 利用React/anu编写一个弹出层
本文将一步步介绍如何使用React或anu创建 一个弹出层. React时代,代码都是要经过编译的,我们很多时间都耗在babel与webpack上.因此本文也介绍如何玩webpack与babel. 我 ...
- 高性能迷你React框架anu在低版本IE的实践
理想是丰满的,现实是骨感的,react早期的版本虽然号称支持IE8,但是页面总会不自觉切换到奇异模式下,导致报错.因此必须让react连IE6,7都支持,这才是最安全.但React本身并不支持IE6, ...
- anu小程序快速入门
众所周知,微信推出小程序以来,可谓火遍大江南北,就像当前互联网兴起时,大家忙着抢域名与开私人博客一样.小程序之所以这么火,是因为微信拥有庞大的用户量,并且腾讯帮你搞定后台问题及众多功能问题(如分享,支 ...
- openfire的组件(Component)开发
在之前的文章<Openfire阶段实践总结>中提到过一种openfire的扩展模式Compoent.本文将主要探讨对这种模式的应用与开发方法. 内部与外部组件介绍 在openfire中的许 ...
- salesforce 零基础学习(六十一)apex:component简单使用以及图片轮转播放的实现
有的时候,我们项目有可能有类似需求:做一个简单的图像轮转播放功能,不同的VF页面调用可以显示不同的图片以及不同的图片描述.这种情况,如果在每个页面单独处理相关的图像轮转播放则显得代码特别冗余,此种情况 ...
- angular2 service component
[component 需要通过 service 提供的接口 得到一些数据.这是最佳实践.] [由于 有 component 和 service 两个语义,所以出现了下面两种办法] 一,[service ...
- knockoutjs如何动态加载外部的file作为component中的template数据源
玩过knockoutjs的都知道,有一个强大的功能叫做component,而这个component有个牛逼的地方就是拥有自己的viewmodel和template, 比如下面这样: ko.compon ...
- 解读ASP.NET 5 & MVC6系列(14):View Component
在之前的MVC中,我们经常需要类似一种小部件的功能,通常我们都是使用Partial View来实现,因为MVC中没有类似Web Forms中的WebControl的功能.但在MVC6中,这一功能得到了 ...
随机推荐
- 51nod 1445 变色DNA(最短路变形)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1445 题意: 思路: 挺好的一道题目,如果$colormap[i][j] ...
- ZOJ 1456 Minimum Transport Cost(floyd+后继路径记录)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1456 题意:求最短路并且输出字典序最小的答案. 思路:如果用dijkstr ...
- zepto点透解决思路
首先看几个链接, http://blog.youyo.name/archives/zepto-tap-click-through-research.html youyo的分析 http://softw ...
- C++基础-string截取、替换、查找子串函数
1. 截取子串 s.substr(pos, n) 截取s中从pos开始(包括0)的n个字符的子串,并返回 s.substr(pos) 截取s中从从pos开始(包括0)到末尾的所有字 ...
- illumina support
http://support.illumina.com/help/BaseSpace_App_WGS_BWA_help/Content/Vault/Informatics/Sequencing_Ana ...
- go 接口
1. package main import ( "fmt" ) type Phone interface { call() } type NokiaPhone struct { ...
- Ubuntu14.04(server amd 64)编译安装 ceres-solver
文档地址:http://www.ceres-solver.org/installation.html git地址:(需要FQ下载) git clone https://ceres-solver.goo ...
- Qt5.3.2_CentOS6.4_单步调试环境__20160306【勿删,繁琐】
20160306 全程没有f/q ZC:使用的虚拟机环境是:博客园VMwareSkill 的 “CentOS6.4_x86_120g__20160306.rar” 需要调试器 gdb ,从“http: ...
- 音视频学习系列第(七)篇---MediaCodec的使用
音视频系列 什么是MediaCodec MediaCodec是安卓官方提供的一套用于音视频编码和解码的API,该API是在安卓4.1(API 16)引入的,因此只能用于4.1以上的手机 MediaCo ...
- C语言位域解析&符号位扩展规则
从一个例子说起: int main(void){ union{ int i; struct{ ; ; ; }bits; }num; printf("Input an integer for ...