Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored Mar 19, 2024
2 parents 1584d4c + e174713 commit 94bfe9d
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ jobs:
timeout-minutes: 5
steps:
- name: Download locally built preact package
uses: actions/download-artifact@v4
uses: actions/download-artifact@v3
with:
name: npm-package
- run: mv preact.tgz preact-local.tgz
- name: Upload locally built preact package
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v3
with:
name: bench-environment
path: preact-local.tgz
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
npm pack --ignore-scripts
mv preact-*.tgz preact.tgz
- name: Upload npm package
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v3
with:
name: ${{ inputs.artifact_name || 'npm-package' }}
path: preact.tgz
5 changes: 4 additions & 1 deletion compat/test/browser/useSyncExternalStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,10 @@ describe('useSyncExternalStore', () => {
await act(() => {
store.set(1);
});
assertLog([1, 1, 'Reset back to 0', 0, 0]);
// Preact logs differ from React here cuz of how we do rerendering. We
// rerender subtrees and then commit effects so Child2 never sees the
// update to 1 cuz Child1 rerenders and runs its layout effects first.
assertLog([1, /*1,*/ 'Reset back to 0', 0, 0]);
expect(container.textContent).to.equal('00');
});

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.19.6', options, {
window.__PREACT_DEVTOOLS__.attachPreact('10.19.7', options, {
Fragment,
Component
});
Expand Down
12 changes: 8 additions & 4 deletions hooks/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ import { ErrorInfo, PreactContext, Ref as PreactRef } from '../..';

type Inputs = ReadonlyArray<unknown>;

export type StateUpdater<S> = (value: S | ((prevState: S) => S)) => void;
export type Dispatch<A> = (value: A) => void;
export type StateUpdater<S> = S | ((prevState: S) => S);

/**
* Returns a stateful value, and a function to update it.
* @param initialState The initial value (or a function that returns the initial value)
*/
export function useState<S>(initialState: S | (() => S)): [S, StateUpdater<S>];
export function useState<S>(
initialState: S | (() => S)
): [S, Dispatch<StateUpdater<S>>];

export function useState<S = undefined>(): [
S | undefined,
StateUpdater<S | undefined>
Dispatch<StateUpdater<S | undefined>>
];

export type Reducer<S, A> = (prevState: S, action: A) => S;
export type Dispatch<A> = (action: A) => void;

/**
* An alternative to `useState`.
*
Expand Down
4 changes: 2 additions & 2 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function getHookState(index, type) {

/**
* @template {unknown} S
* @param {import('./index').StateUpdater<S>} [initialState]
* @param {import('./index').Dispatch<import('./index').StateUpdater<S>>} [initialState]
* @returns {[S, (state: S) => void]}
*/
export function useState(initialState) {
Expand All @@ -179,7 +179,7 @@ export function useState(initialState) {
* @template {unknown} S
* @template {unknown} A
* @param {import('./index').Reducer<S, A>} reducer
* @param {import('./index').StateUpdater<S>} initialState
* @param {import('./index').Dispatch<import('./index').StateUpdater<S>>} initialState
* @param {(initialState: any) => void} [init]
* @returns {[ S, (state: S) => void ]}
*/
Expand Down
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.19.6",
"version": "10.19.7",
"private": false,
"description": "Fast 3kb React-compatible Virtual DOM library.",
"main": "dist/preact.js",
Expand Down
29 changes: 8 additions & 21 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assign } from './util';
import { diff, commitRoot } from './diff/index';
import options from './options';
import { Fragment } from './create-element';
import { EMPTY_ARR, MODE_HYDRATE } from './constants';
import { MODE_HYDRATE } from './constants';

/**
* Base Component class. Provides `setState()` and `forceUpdate()`, which
Expand Down Expand Up @@ -120,10 +120,12 @@ export function getDomSibling(vnode, childIndex) {
* Trigger in-place re-rendering of a component.
* @param {Component} component The component to rerender
*/
function renderComponent(component, commitQueue, refQueue) {
function renderComponent(component) {
let oldVNode = component._vnode,
oldDom = oldVNode._dom,
parentDom = component._parentDom;
parentDom = component._parentDom,
commitQueue = [],
refQueue = [];

if (parentDom) {
const newVNode = assign({}, oldVNode);
Expand All @@ -145,14 +147,11 @@ function renderComponent(component, commitQueue, refQueue) {

newVNode._original = oldVNode._original;
newVNode._parent._children[newVNode._index] = newVNode;

newVNode._nextDom = undefined;
commitRoot(commitQueue, newVNode, refQueue);

if (newVNode._dom != oldDom) {
updateParentDomPointers(newVNode);
}

return newVNode;
}
}

Expand Down Expand Up @@ -222,33 +221,21 @@ const depthSort = (a, b) => a._vnode._depth - b._vnode._depth;
/** Flush the render queue by rerendering all queued components */
function process() {
let c;
let commitQueue = [];
let refQueue = [];
let root;
rerenderQueue.sort(depthSort);
// Don't update `renderCount` yet. Keep its value non-zero to prevent unnecessary
// process() calls from getting scheduled while `queue` is still being consumed.
while ((c = rerenderQueue.shift())) {
if (c._dirty) {
let renderQueueLength = rerenderQueue.length;
root = renderComponent(c, commitQueue, refQueue) || root;
// If this WAS the last component in the queue, run commit callbacks *before* we exit the tight loop.
// This is required in order for `componentDidMount(){this.setState()}` to be batched into one flush.
// Otherwise, also run commit callbacks if the render queue was mutated.
if (renderQueueLength === 0 || rerenderQueue.length > renderQueueLength) {
commitRoot(commitQueue, root, refQueue);
refQueue.length = commitQueue.length = 0;
root = undefined;
renderComponent(c);
if (rerenderQueue.length > renderQueueLength) {
// When i.e. rerendering a provider additional new items can be injected, we want to
// keep the order from top to bottom with those new items so we can handle them in a
// single pass
rerenderQueue.sort(depthSort);
} else if (root) {
if (options._commit) options._commit(root, EMPTY_ARR);
}
}
}
if (root) commitRoot(commitQueue, root, refQueue);
process._rerenderCount = 0;
}

Expand Down
2 changes: 2 additions & 0 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ export function diff(
* @param {VNode} root
*/
export function commitRoot(commitQueue, root, refQueue) {
root._nextDom = undefined;

for (let i = 0; i < refQueue.length; i++) {
applyRef(refQueue[i], refQueue[++i], refQueue[++i]);
}
Expand Down
9 changes: 5 additions & 4 deletions src/jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1563,10 +1563,10 @@ export namespace JSXInternal {
// Focus Events
onFocus?: FocusEventHandler<Target> | undefined;
onFocusCapture?: FocusEventHandler<Target> | undefined;
onfocusin?: FocusEventHandler<Target> | undefined;
onfocusinCapture?: FocusEventHandler<Target> | undefined;
onfocusout?: FocusEventHandler<Target> | undefined;
onfocusoutCapture?: FocusEventHandler<Target> | undefined;
onFocusIn?: FocusEventHandler<Target> | undefined;
onFocusInCapture?: FocusEventHandler<Target> | undefined;
onFocusOut?: FocusEventHandler<Target> | undefined;
onFocusOutCapture?: FocusEventHandler<Target> | undefined;
onBlur?: FocusEventHandler<Target> | undefined;
onBlurCapture?: FocusEventHandler<Target> | undefined;

Expand Down Expand Up @@ -1718,6 +1718,7 @@ export namespace JSXInternal {

// UI Events
onScroll?: UIEventHandler<Target> | undefined;
onScrollEnd?: UIEventHandler<Target> | undefined;
onScrollCapture?: UIEventHandler<Target> | undefined;

// Wheel Events
Expand Down
2 changes: 1 addition & 1 deletion src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function render(vnode, parentDom, replaceNode) {
refQueue
);

vnode._nextDom = undefined;
// Flush all queued effects
commitRoot(commitQueue, vnode, refQueue);
}

Expand Down

0 comments on commit 94bfe9d

Please sign in to comment.