Skip to content

Commit

Permalink
feat(view): template does not need to be a function (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzofox3 authored Jan 29, 2024
1 parent 0af9274 commit 27ea82a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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>`);
});

0 comments on commit 27ea82a

Please sign in to comment.