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) - map old VNodes to new ones #236

Merged
merged 2 commits into from
Dec 11, 2020
Merged
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/weak-rules-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@prefresh/core': minor
---

Update oldVnodeTypes in place with newer equivalents
4 changes: 3 additions & 1 deletion packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
HOOK_CLEANUP
} from './constants';
import { computeKey } from './computeKey';
import { vnodesForComponent } from './runtime/vnodesForComponent';
import { vnodesForComponent, mappedVNodes } from './runtime/vnodesForComponent';
import { signaturesForType } from './runtime/signaturesForType';

let typesById = new Map();
Expand Down Expand Up @@ -51,6 +51,8 @@ function replaceComponent(OldType, NewType, resetHookState) {
vnodesForComponent.delete(OldType);
vnodesForComponent.set(NewType, vnodes);

mappedVNodes.set(OldType, NewType);

pendingUpdates = pendingUpdates.filter(p => p[0] !== OldType);

vnodes.forEach(vnode => {
Expand Down
17 changes: 16 additions & 1 deletion packages/core/src/runtime/vnode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { options } from 'preact';
import { vnodesForComponent } from './vnodesForComponent';
import { vnodesForComponent, mappedVNodes } from './vnodesForComponent';
import { VNODE_COMPONENT } from '../constants';

const getMappedVnode = type => {
if (mappedVNodes.has(type)) {
return getMappedVnode(mappedVNodes.get(type));
}

return type;
};

const oldVnode = options.vnode;
options.vnode = vnode => {
Expand All @@ -10,6 +19,12 @@ options.vnode = vnode => {
} else {
vnodes.push(vnode);
}

const foundType = getMappedVnode(vnode.type);
vnode.type = foundType;
if (vnode[VNODE_COMPONENT]) {
vnode[VNODE_COMPONENT].constructor = foundType;
}
}

if (oldVnode) oldVnode(vnode);
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/runtime/vnodesForComponent.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// all vnodes referencing a given constructor
export const vnodesForComponent = new WeakMap();
export const mappedVNodes = new WeakMap();