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的更多相关文章

  1. 发布高性能迷你React框架anu

    anu, 读作[安努],原意为苏美尔的主神. anu是我继avalon之后又一个新框架(github仓库为https://github.com/RubyLouvre/anu, 欢迎加星与试用) 此框架 ...

  2. 利用React/anu编写一个弹出层

    本文将一步步介绍如何使用React或anu创建 一个弹出层. React时代,代码都是要经过编译的,我们很多时间都耗在babel与webpack上.因此本文也介绍如何玩webpack与babel. 我 ...

  3. 高性能迷你React框架anu在低版本IE的实践

    理想是丰满的,现实是骨感的,react早期的版本虽然号称支持IE8,但是页面总会不自觉切换到奇异模式下,导致报错.因此必须让react连IE6,7都支持,这才是最安全.但React本身并不支持IE6, ...

  4. anu小程序快速入门

    众所周知,微信推出小程序以来,可谓火遍大江南北,就像当前互联网兴起时,大家忙着抢域名与开私人博客一样.小程序之所以这么火,是因为微信拥有庞大的用户量,并且腾讯帮你搞定后台问题及众多功能问题(如分享,支 ...

  5. openfire的组件(Component)开发

    在之前的文章<Openfire阶段实践总结>中提到过一种openfire的扩展模式Compoent.本文将主要探讨对这种模式的应用与开发方法. 内部与外部组件介绍 在openfire中的许 ...

  6. salesforce 零基础学习(六十一)apex:component简单使用以及图片轮转播放的实现

    有的时候,我们项目有可能有类似需求:做一个简单的图像轮转播放功能,不同的VF页面调用可以显示不同的图片以及不同的图片描述.这种情况,如果在每个页面单独处理相关的图像轮转播放则显得代码特别冗余,此种情况 ...

  7. angular2 service component

    [component 需要通过 service 提供的接口 得到一些数据.这是最佳实践.] [由于 有 component 和 service 两个语义,所以出现了下面两种办法] 一,[service ...

  8. knockoutjs如何动态加载外部的file作为component中的template数据源

    玩过knockoutjs的都知道,有一个强大的功能叫做component,而这个component有个牛逼的地方就是拥有自己的viewmodel和template, 比如下面这样: ko.compon ...

  9. 解读ASP.NET 5 & MVC6系列(14):View Component

    在之前的MVC中,我们经常需要类似一种小部件的功能,通常我们都是使用Partial View来实现,因为MVC中没有类似Web Forms中的WebControl的功能.但在MVC6中,这一功能得到了 ...

随机推荐

  1. UVa 821 网页跳跃(Floyd)

    https://vjudge.net/problem/UVA-821 题意:给出一个有向图,任意两点都可相互到达,求任意两点的最短距离的平均值. 思路:求两点的最短距离,用Floyd算法很方便,最后加 ...

  2. MVC ---- DBHelper.ttinclude

    在通过T4模版引擎之基础入门 对T4有了初步印象后,我们开始实战篇.T4模板引擎可以当做一个代码生成器,代码生成器的职责当然是用来生成代码(这不是废话吗).而这其中我们使用的最普遍的是根据数据库生成实 ...

  3. Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路

    E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  4. python 深拷贝

    from copy import deepcopy #create a tuple tuplex = (, [], True) print(tuplex) #make a copy of a tupl ...

  5. 关于怎么解决java.lang.NoClassDefFoundError错误

    五一在部署新的统一登录时,遇到这样一个问题: 很容易把java.lang.NoClassDefFoundError和java.lang.ClassNotfoundException这两个错误搞混,事实 ...

  6. Android Intent参数传递

    Android Intent参数传递 Android参数传递有两种方式, 1.是从一个Activity传递参数到第二个Activity并在第二个Activity获取数据 2.是从第二个Activity ...

  7. R语言-attach、detach、with

    在R语言中,对于串列,数据框中的数据的进行操作时,为了避免重复地键入对象名称,可使用attach或with. 1.attach() 假设data.frame包含列name,age attach(one ...

  8. 微信公众号菜单添加小程序,miniprogram,pagepath参数详解,php开发公众号

    随着微信小程序功能的开发, 已经可以跟公众号打通了, 主要有两种方式: 1) 在公众号文章中插入小程序 2) 在公众号菜单中添加小程序 第一种方式, 子恒老师在前面的课程已经详细介绍过, 今天来讲第二 ...

  9. Loading Xps from MemoryStream

    A common way of loading XpsDocument is to load it from file: XpsDocument document = new XpsDocument( ...

  10. bzoj2242: [SDOI2011]计算器 BSGS+exgcd

    你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值:(快速幂) 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数:(exgcd) 3.给 ...