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

feat(view): template does not need to be a function #1

Merged
merged 1 commit into from
Jan 29, 2024
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
6 changes: 4 additions & 2 deletions packages/view/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export const withView = (viewFactory) =>
html: createHTML({ $signal }),
});

const record = view(yield);
const viewFn = typeof view === 'function' ? view : () => view;

const record = viewFn(yield);
$root.replaceChildren(record.content);
delete record.content; // free memory

while (true) {
view(yield);
viewFn(yield);
}
};

Expand Down
15 changes: 15 additions & 0 deletions packages/view/test/simple-render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,18 @@ test('renders a nested template', ({ eq }) => {
`<h2>some title</h2><p>hello Robert</p><!--before--><p>you are very happy</p><!--after-->`,
);
});

test('A template is static if it is not a function of any state', ({ eq }) => {
const el = fromView(
({ html }) =>
// prettier-ignore
html`<h2>some title</h2><p>hello there, how are you?</p>`,
);

debug.appendChild(el);
eq(el.innerHTML, `<h2>some title</h2><p>hello there, how are you?</p>`);

el.render();

eq(el.innerHTML, `<h2>some title</h2><p>hello there, how are you?</p>`);
});
Loading