Skip to content

Commit

Permalink
Merge branch 'master' into controlled-inputs-compat
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins authored Apr 6, 2023
2 parents 9c83d5d + a9d9c64 commit 49bab24
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 18 deletions.
18 changes: 17 additions & 1 deletion benches/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export { afterFrame };

export const measureName = 'duration';

const majorTask = () =>
new Promise(resolve => {
window.addEventListener('message', resolve, { once: true });
window.postMessage('major task delay', '*');
});

let promise = null;
export function afterFrameAsync() {
if (promise === null) {
Expand All @@ -19,10 +25,20 @@ export function afterFrameAsync() {
return promise;
}

export function measureMemory() {
export async function measureMemory() {
if ('gc' in window && 'memory' in performance) {
// Report results in MBs
performance.mark('gc-start');
window.gc();
performance.measure('gc', 'gc-start');

// window.gc synchronously triggers one Major GC. However that MajorGC
// asynchronously triggers additional MajorGCs until the
// usedJSHeapSizeBefore and usedJSHeapSizeAfter are the same. Here, we'll
// wait a moment for some (hopefully all) additional GCs to finish before
// measuring the memory.
await majorTask();
performance.mark('measure-memory');
window.usedJSHeapSize = performance.memory.usedJSHeapSize / 1e6;
} else {
window.usedJSHeapSize = 0;
Expand Down
2 changes: 1 addition & 1 deletion devtools/src/devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { options, Fragment, Component } from 'preact';

export function initDevTools() {
if (typeof window != 'undefined' && window.__PREACT_DEVTOOLS__) {
window.__PREACT_DEVTOOLS__.attachPreact('10.13.1', options, {
window.__PREACT_DEVTOOLS__.attachPreact('10.13.2', options, {
Fragment,
Component
});
Expand Down
23 changes: 22 additions & 1 deletion hooks/test/browser/useImperativeHandle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('useImperativeHandle', () => {
expect(() => render(<Comp />, scratch)).to.not.throw();
});

it('should reset ref to null when the component get unmounted', () => {
it('should reset ref object to null when the component get unmounted', () => {
let ref,
createHandleSpy = sinon.spy(() => ({ test: () => 'test' }));

Expand All @@ -198,4 +198,25 @@ describe('useImperativeHandle', () => {
expect(createHandleSpy).to.have.been.calledOnce;
expect(ref.current).to.equal(null);
});

it('should reset ref callback to null when the component get unmounted', () => {
const ref = sinon.spy();
const handle = { test: () => 'test' };
const createHandleSpy = sinon.spy(() => handle);

function Comp() {
useImperativeHandle(ref, createHandleSpy, [1]);
return <p>Test</p>;
}

render(<Comp />, scratch);
expect(createHandleSpy).to.have.been.calledOnce;
expect(ref).to.have.been.calledWith(handle);

ref.resetHistory();

render(<div />, scratch);
expect(createHandleSpy).to.have.been.calledOnce;
expect(ref).to.have.been.calledWith(null);
});
});
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "preact",
"amdName": "preact",
"version": "10.13.1",
"version": "10.13.2",
"private": false,
"description": "Fast 3kb React-compatible Virtual DOM library.",
"main": "dist/preact.js",
Expand Down
5 changes: 3 additions & 2 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { diff, unmount, applyRef } from './index';
import { createVNode, Fragment } from '../create-element';
import { EMPTY_OBJ, EMPTY_ARR } from '../constants';
import { getDomSibling } from '../component';
import { isArray } from '../util';

/**
* Diff the children of a virtual node
Expand Down Expand Up @@ -70,7 +71,7 @@ export function diffChildren(
null,
childVNode
);
} else if (Array.isArray(childVNode)) {
} else if (isArray(childVNode)) {
childVNode = newParentVNode._children[i] = createVNode(
Fragment,
{ children: childVNode },
Expand Down Expand Up @@ -265,7 +266,7 @@ function reorderChildren(childVNode, oldDom, parentDom) {
export function toChildArray(children, out) {
out = out || [];
if (children == null || typeof children == 'boolean') {
} else if (Array.isArray(children)) {
} else if (isArray(children)) {
children.some(child => {
toChildArray(child, out);
});
Expand Down
6 changes: 3 additions & 3 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component, getDomSibling } from '../component';
import { Fragment } from '../create-element';
import { diffChildren } from './children';
import { diffProps, setProperty } from './props';
import { assign, removeNode, slice } from '../util';
import { assign, isArray, removeNode, slice } from '../util';
import options from '../options';

/**
Expand Down Expand Up @@ -232,7 +232,7 @@ export function diff(

diffChildren(
parentDom,
Array.isArray(renderResult) ? renderResult : [renderResult],
isArray(renderResult) ? renderResult : [renderResult],
newVNode,
oldVNode,
globalContext,
Expand Down Expand Up @@ -438,7 +438,7 @@ function diffElementNodes(
i = newVNode.props.children;
diffChildren(
dom,
Array.isArray(i) ? i : [i],
isArray(i) ? i : [i],
newVNode,
oldVNode,
globalContext,
Expand Down
14 changes: 7 additions & 7 deletions src/diff/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ export function setProperty(dom, name, value, oldValue, isSvg) {
} catch (e) {}
}

// ARIA-attributes have a different notion of boolean values.
// The value `false` is different from the attribute not
// existing on the DOM, so we can't remove it. For non-boolean
// ARIA-attributes we could treat false as a removal, but the
// amount of exceptions would cost us too many bytes. On top of
// that other VDOM frameworks also always stringify `false`.
// aria- and data- attributes have no boolean representation.
// A `false` value is different from the attribute not being
// present, so we can't remove it. For non-boolean aria
// attributes we could treat false as a removal, but the
// amount of exceptions would cost too many bytes. On top of
// that other frameworks generally stringify `false`.

if (typeof value === 'function') {
// never serialize functions as attribute values
} else if (value != null && (value !== false || name.indexOf('-') != -1)) {
} else if (value != null && (value !== false || name[4] === '-')) {
dom.setAttribute(name, value);
} else {
dom.removeAttribute(name);
Expand Down
2 changes: 2 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { EMPTY_ARR } from './constants';

export const isArray = Array.isArray;

/**
* Assign properties from `props` to `obj`
* @template O, P The obj and props types
Expand Down

0 comments on commit 49bab24

Please sign in to comment.