
//fix 0.14对此方法的改动，之前refs里面保存的是虚拟DOM
function getDOMNode() {
    return this;
}
function errRef() {
    throw "ref位置错误";
}
export var pendingRefs = [];
export var Refs = {
    newStageFlag: false,
    pendingOwners: [],
    clearOwners: function(){
        for(let i = 0, owner; owner = this.pendingOwners[i++];){
            delete owner.newStageAddRefs;
        }
    },
    clearRefs: function(){
        var refs = pendingRefs.splice(0,pendingRefs.length );
        refs.forEach(function(fn) {
            fn();
        });
    },
    detachRef:function(ref, nextRef, dom) {
        ref = ref || getDOMNode;
        nextRef = nextRef || getDOMNode;   
        if(ref === nextRef) {
            return;
        }    
        if(ref){
            if (ref.string && nextRef.string ? ref.string !== nextRef.string: ref !== getDOMNode ) {
                pendingRefs.push(ref.bind(0,null));
            }
        }
        if(dom && nextRef !== getDOMNode){
            pendingRefs.push(nextRef.bind(0,dom));
        }
    },
    createStringRef: function (owner, ref) {
        var stringRef = owner === null
            ? errRef
            : function (dom) {
                /*  if(Refs.newStageFlag){
                    owner.newStageAddRefs = owner.newStageAddRefs || {};
                    Refs.pendingOwners.push(owner);
                    if(dom){
                        owner.newStageAddRefs[ref] = 1;    
                    }
                }*/
                if (dom) {
                    if (dom.nodeType) {
                        dom.getDOMNode = getDOMNode;
                    }   
                    owner.refs[ref] = dom;
                }else{
                    /* if(Refs.newStageFlag){
                        if(owner.newStageAddRefs[ref]) {
                            delete owner.refs[ref];
                        }
                    }else{*/
                    delete owner.refs[ref];
                    /*   } */
                }
            };
        stringRef.string = ref;
        return stringRef;
    }
};

