Skip to content

Commit

Permalink
Merge a17bb18 into 8dea9cc
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Aug 8, 2018
2 parents 8dea9cc + a17bb18 commit 6332c34
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/dom/index.js
@@ -1,4 +1,5 @@
import { IS_NON_DIMENSIONAL } from '../constants';
import { applyRef } from '../util';
import options from '../options';

/**
Expand Down Expand Up @@ -70,8 +71,8 @@ export function setAccessor(node, name, old, value, isSvg) {
// ignore
}
else if (name==='ref') {
if (old) old(null);
if (value) value(node);
applyRef(old, null);
applyRef(value, node);
}
else if (name==='class' && !isSvg) {
node.className = value || '';
Expand Down
6 changes: 6 additions & 0 deletions src/preact.js
Expand Up @@ -5,10 +5,15 @@ import { render } from './render';
import { rerender } from './render-queue';
import options from './options';

function createRef() {
return {};
}

export default {
h,
createElement,
cloneElement,
createRef,
Component,
render,
rerender,
Expand All @@ -19,6 +24,7 @@ export {
h,
createElement,
cloneElement,
createRef,
Component,
render,
rerender,
Expand Down
11 changes: 11 additions & 0 deletions src/util.js
Expand Up @@ -10,6 +10,17 @@ export function extend(obj, props) {
return obj;
}

/** Invoke or update a ref, depending on whether it is a function or object ref.
* @param {object|function} [ref=null]
* @param {any} [value]
*/
export function applyRef(ref, value) {
if (ref!=null) {
if (typeof ref=='function') ref(value);
else ref.current = value;
}
}

/**
* Call a function asynchronously, as soon as possible. Makes
* use of HTML Promise to schedule the callback if available,
Expand Down
6 changes: 3 additions & 3 deletions src/vdom/component.js
@@ -1,6 +1,6 @@
import { SYNC_RENDER, NO_RENDER, FORCE_RENDER, ASYNC_RENDER, ATTR_KEY } from '../constants';
import options from '../options';
import { extend } from '../util';
import { extend, applyRef } from '../util';
import { enqueueRender } from '../render-queue';
import { getNodeProps } from './index';
import { diff, mounts, diffLevel, flushMounts, recollectNodeTree, removeChildren } from './diff';
Expand Down Expand Up @@ -52,7 +52,7 @@ export function setComponentProps(component, props, renderMode, context, mountAl
}
}

if (component.__ref) component.__ref(component);
applyRef(component.__ref, component);
}


Expand Down Expand Up @@ -292,5 +292,5 @@ export function unmountComponent(component) {
removeChildren(base);
}

if (component.__ref) component.__ref(null);
applyRef(component.__ref, null);
}
3 changes: 2 additions & 1 deletion src/vdom/diff.js
Expand Up @@ -4,6 +4,7 @@ import { buildComponentFromVNode } from './component';
import { createNode, setAccessor } from '../dom/index';
import { unmountComponent } from './component';
import options from '../options';
import { applyRef } from '../util';
import { removeNode } from '../dom/index';

/**
Expand Down Expand Up @@ -284,7 +285,7 @@ export function recollectNodeTree(node, unmountOnly) {
else {
// If the node's VNode had a ref function, invoke it with null here.
// (this is part of the React spec, and smart for unsetting references)
if (node[ATTR_KEY]!=null && node[ATTR_KEY].ref) node[ATTR_KEY].ref(null);
if (node[ATTR_KEY]!=null) applyRef(node[ATTR_KEY].ref, null);

if (unmountOnly===false || node[ATTR_KEY]==null) {
removeNode(node);
Expand Down

0 comments on commit 6332c34

Please sign in to comment.