Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(core) - keep types consistent on vnodes #315

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-walls-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@prefresh/core': patch
---

Prevent double HOC's from duplicating components by having the types match up for Preact
1 change: 1 addition & 0 deletions packages/core/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export const VNODE_CHILDREN = '__k';
export const HOOK_VALUE = '__';
export const HOOK_ARGS = '__H';
export const HOOK_CLEANUP = '__c';
export const VNODE_PARENT = '__';
34 changes: 22 additions & 12 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
HOOK_ARGS,
HOOK_VALUE,
HOOK_CLEANUP,
VNODE_PARENT,
} from './constants';
import { computeKey } from './computeKey';
import { vnodesForComponent, mappedVNodes } from './runtime/vnodesForComponent';
Expand Down Expand Up @@ -56,11 +57,9 @@ function replaceComponent(OldType, NewType, resetHookState) {
pendingUpdates = pendingUpdates.filter(p => p[0] !== OldType);

vnodes.forEach(vnode => {
// update the type in-place to reference the new component
vnode.type = NewType;

if (vnode[VNODE_COMPONENT]) {
vnode[VNODE_COMPONENT].constructor = vnode.type;
vnode[VNODE_COMPONENT].constructor = NewType;

try {
if (vnode[VNODE_COMPONENT] instanceof OldType) {
Expand Down Expand Up @@ -99,6 +98,22 @@ function replaceComponent(OldType, NewType, resetHookState) {
vnode[VNODE_COMPONENT].constructor = NewType;
}

if (
vnode[VNODE_PARENT] &&
vnode[VNODE_PARENT][VNODE_CHILDREN] &&
vnode[VNODE_PARENT][VNODE_CHILDREN].length
) {
vnode[VNODE_PARENT][VNODE_CHILDREN].forEach(child => {
if (
child &&
typeof child.type === 'function' &&
child.type === OldType
) {
child.type = NewType;
}
});
}

if (resetHookState) {
if (
vnode[VNODE_COMPONENT][COMPONENT_HOOKS] &&
Expand Down Expand Up @@ -164,16 +179,11 @@ function replaceComponent(OldType, NewType, resetHookState) {
}
);

vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].forEach(
hook => {
if (
hook.__H &&
Array.isArray(hook.__H)
) {
hook.__H = undefined;
}
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].forEach(hook => {
if (hook.__H && Array.isArray(hook.__H)) {
hook.__H = undefined;
}
);
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ exports.binArgs = {
};

exports.goMessage = {
vite: 'running',
vite: 'ready',
snowpack: 'Server started',
webpack: 'successfully',
nollup: 'Compiled',
Expand Down
6 changes: 4 additions & 2 deletions test/fixture/next-webpack5/src/app.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useCounter } from './useCounter'
import { h } from 'preact'
import { setup } from 'goober';
import { Greeting } from './greeting.jsx';
import { StoreProvider } from './context.jsx';
import { Products } from './products.jsx';
import { Effect } from './effect.jsx';
// import { List } from './list.jsx';
import { useCounter } from './useCounter'
import { Style } from './styles';
import { setup } from 'goober';

setup(h);

Expand All @@ -28,6 +29,7 @@ export function App(props) {
<Products />
</StoreProvider>
<Effect />
{/* <List /> */}
</Style>
)
}
3 changes: 3 additions & 0 deletions test/fixture/next-webpack5/src/hoc.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { h } from 'preact';

export const applyHOC = (Component) => (props) => <Component {...props} HOCApplied={true} />
8 changes: 8 additions & 0 deletions test/fixture/next-webpack5/src/list.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { h } from 'preact'
import Item from './listItem.jsx'

const items = [0, 1, 2, 3]

export const List = () => {
return <div id="item-list">{items.map(item => <Item key={`item-${item}`} index={item}/>)}</div>
}
12 changes: 12 additions & 0 deletions test/fixture/next-webpack5/src/listItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { h, Component } from 'preact'
import { applyHOC } from './hoc.jsx'

class ListItem extends Component {
render() {
return <div>item {this.props.index}</div>
}
}

const WrappedListItem = applyHOC(ListItem)

export default WrappedListItem
6 changes: 4 additions & 2 deletions test/fixture/next/src/app.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useCounter } from './useCounter'
import { setup } from 'goober';
import { h } from 'preact'
import { Greeting } from './greeting.jsx';
import { StoreProvider } from './context.jsx';
import { Products } from './products.jsx';
import { Effect } from './effect.jsx';
// import { List } from './list.jsx';
import { Style } from './styles';
import { setup } from 'goober';
import { useCounter } from './useCounter';

setup(h);

Expand All @@ -28,6 +29,7 @@ export function App(props) {
<Products />
</StoreProvider>
<Effect />
{/* <List /> */}
</Style>
)
}
3 changes: 3 additions & 0 deletions test/fixture/next/src/hoc.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { h } from 'preact';

export const applyHOC = (Component) => (props) => <Component {...props} HOCApplied={true} />
8 changes: 8 additions & 0 deletions test/fixture/next/src/list.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { h } from 'preact'
import Item from './listItem.jsx'

const items = [0, 1, 2, 3]

export const List = () => {
return <div id="item-list">{items.map(item => <Item key={`item-${item}`} index={item}/>)}</div>
}
12 changes: 12 additions & 0 deletions test/fixture/next/src/listItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { h, Component } from 'preact'
import { applyHOC } from './hoc.jsx'

class ListItem extends Component {
render() {
return <div>item {this.props.index}</div>
}
}

const WrappedListItem = applyHOC(ListItem)

export default WrappedListItem
6 changes: 4 additions & 2 deletions test/fixture/nollup/src/app.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { h } from 'preact';
import { useCounter } from './useCounter'
import { setup } from 'goober';
import { Greeting } from './greeting.jsx';
import { StoreProvider } from './context.jsx';
import { Products } from './products.jsx';
import { Effect } from './effect.jsx';
// import { List } from './list.jsx';
import { Style } from './styles';
import { setup } from 'goober';
import { useCounter } from './useCounter'

setup(h);

Expand All @@ -28,6 +29,7 @@ export function App(props) {
<Products />
</StoreProvider>
<Effect />
{/* <List /> */}
</Style>
)
}
3 changes: 3 additions & 0 deletions test/fixture/nollup/src/hoc.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { h } from 'preact';

export const applyHOC = (Component) => (props) => <Component {...props} HOCApplied={true} />
8 changes: 8 additions & 0 deletions test/fixture/nollup/src/list.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { h } from 'preact'
import Item from './listItem.jsx'

const items = [0, 1, 2, 3]

export const List = () => {
return <div id="item-list">{items.map(item => <Item key={`item-${item}`} index={item}/>)}</div>
}
12 changes: 12 additions & 0 deletions test/fixture/nollup/src/listItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { h, Component } from 'preact'
import { applyHOC } from './hoc.jsx'

class ListItem extends Component {
render() {
return <div>item {this.props.index}</div>
}
}

const WrappedListItem = applyHOC(ListItem)

export default WrappedListItem
6 changes: 4 additions & 2 deletions test/fixture/snowpack/src/app.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useCounter } from './useCounter'
import { h } from 'preact'
import { setup } from 'goober';
import { StoreProvider } from './context';
import { Products } from './products';
import { Greeting } from './greeting';
import { Effect } from './effect';
import { setup } from 'goober';
import { Style } from './styles';
// import { List } from './list';
import { useCounter } from './useCounter'

setup(h);

Expand All @@ -28,6 +29,7 @@ export function App(props) {
<Products />
</StoreProvider>
<Effect />
{/* <List /> */}
</Style>
)
}
3 changes: 3 additions & 0 deletions test/fixture/snowpack/src/hoc.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { h } from 'preact';

export const applyHOC = (Component) => (props) => <Component {...props} HOCApplied={true} />
8 changes: 8 additions & 0 deletions test/fixture/snowpack/src/list.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { h } from 'preact'
import Item from './listItem.jsx'

const items = [0, 1, 2, 3]

export const List = () => {
return <div id="item-list">{items.map(item => <Item key={`item-${item}`} index={item}/>)}</div>
}
12 changes: 12 additions & 0 deletions test/fixture/snowpack/src/listItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { h, Component } from 'preact'
import { applyHOC } from './hoc.jsx'

class ListItem extends Component {
render() {
return <div>item {this.props.index}</div>
}
}

const WrappedListItem = applyHOC(ListItem)

export default WrappedListItem
6 changes: 4 additions & 2 deletions test/fixture/vite/src/app.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { h } from 'preact';
import { useCounter } from './useCounter';
import { setup } from 'goober';
import { StoreProvider } from './context';
import { Products } from './products';
import { Greeting } from './greeting';
import { Effect } from './effect';
import { setup } from 'goober';
import { Style } from './styles';
import { List } from './list';
import { useCounter } from './useCounter';

setup(h);

Expand All @@ -28,6 +29,7 @@ export function App(props) {
<Products />
</StoreProvider>
<Effect />
<List />
</Style>
)
}
3 changes: 3 additions & 0 deletions test/fixture/vite/src/hoc.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { h } from 'preact';

export const applyHOC = (Component) => (props) => <Component {...props} HOCApplied={true} />
8 changes: 8 additions & 0 deletions test/fixture/vite/src/list.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { h } from 'preact'
import Item from './listItem.jsx'

const items = [0, 1, 2, 3]

export const List = () => {
return <div id="item-list">{items.map(item => <Item key={`item-${item}`} index={item}/>)}</div>
}
12 changes: 12 additions & 0 deletions test/fixture/vite/src/listItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { h, Component } from 'preact'
import { applyHOC } from './hoc.jsx'

class ListItem extends Component {
render() {
return <div>item {this.props.index}</div>
}
}

const WrappedListItem = applyHOC(ListItem)

export default WrappedListItem
4 changes: 3 additions & 1 deletion test/fixture/web-dev-server/src/app.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { h } from 'preact';
import { setup } from 'goober';
import { useCounter } from './useCounter'
import { StoreProvider } from './context';
import { Products } from './products'
import { Greeting } from './greeting';
import { Effect } from './effect';
import { setup } from 'goober';
import { Style } from './styles';
// import { List } from './list';

setup(h);

Expand All @@ -28,6 +29,7 @@ export function App(props) {
<Products />
</StoreProvider>
<Effect />
{/* <List /> */}
</Style>
)
}
3 changes: 3 additions & 0 deletions test/fixture/web-dev-server/src/hoc.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { h } from 'preact';

export const applyHOC = (Component) => (props) => <Component {...props} HOCApplied={true} />
8 changes: 8 additions & 0 deletions test/fixture/web-dev-server/src/list.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { h } from 'preact'
import Item from './listItem.jsx'

const items = [0, 1, 2, 3]

export const List = () => {
return <div id="item-list">{items.map(item => <Item key={`item-${item}`} index={item}/>)}</div>
}
12 changes: 12 additions & 0 deletions test/fixture/web-dev-server/src/listItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { h, Component } from 'preact'
import { applyHOC } from './hoc.jsx'

class ListItem extends Component {
render() {
return <div>item {this.props.index}</div>
}
}

const WrappedListItem = applyHOC(ListItem)

export default WrappedListItem
Loading