Skip to content

Commit 3491f4d

Browse files
Merge pull request #175 from preactjs/option-hooks
2 parents b92ba83 + 9ba751c commit 3491f4d

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"microbundle": "^0.6.0",
101101
"mocha": "^5.2.0",
102102
"npm-merge-driver-install": "^1.1.1",
103-
"preact": "^10.0.4",
103+
"preact": "^10.5.7",
104104
"prettier": "^2.1.2",
105105
"sinon": "^1.17.5",
106106
"sinon-chai": "^2.8.0",

src/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,17 @@ renderToString.render = renderToString;
3939
*/
4040
let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW);
4141

42+
const EMPTY_ARR = [];
43+
function renderToString(vnode, context, opts) {
44+
const res = _renderToString(vnode, context, opts);
45+
// options._commit, we don't schedule any effects in this library right now,
46+
// so we can pass an empty queue to this hook.
47+
if (options.__c) options.__c(vnode, EMPTY_ARR);
48+
return res;
49+
}
50+
4251
/** The default export is an alias of `render()`. */
43-
function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
52+
function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
4453
if (vnode == null || typeof vnode === 'boolean') {
4554
return '';
4655
}
@@ -77,7 +86,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
7786
for (let i = 0; i < children.length; i++) {
7887
rendered +=
7988
(i > 0 && pretty ? '\n' : '') +
80-
renderToString(
89+
_renderToString(
8190
children[i],
8291
context,
8392
opts,
@@ -173,7 +182,8 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
173182
context = assign(assign({}, context), c.getChildContext());
174183
}
175184

176-
return renderToString(
185+
if (options.diffed) options.diffed(vnode);
186+
return _renderToString(
177187
rendered,
178188
context,
179189
opts,
@@ -313,7 +323,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
313323
: nodeName === 'foreignObject'
314324
? false
315325
: isSvgMode,
316-
ret = renderToString(
326+
ret = _renderToString(
317327
child,
318328
context,
319329
opts,

test/render.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ describe('render', () => {
10671067
});
10681068
});
10691069

1070-
it('should invoke options._render and options._diff', () => {
1070+
it('should invoke option hooks', () => {
10711071
const calls = [];
10721072
// _diff
10731073
const oldDiff = options.__b;
@@ -1081,6 +1081,17 @@ describe('render', () => {
10811081
calls.push(['_render', args]);
10821082
if (oldRender) oldRender(...args);
10831083
};
1084+
const oldDiffed = options.diffed;
1085+
options.diffed = (...args) => {
1086+
calls.push(['diffed', args]);
1087+
if (oldDiffed) oldDiffed(...args);
1088+
};
1089+
// _commit
1090+
const oldCommit = options.__c;
1091+
options.__c = (...args) => {
1092+
calls.push(['_commit', args]);
1093+
if (oldCommit) oldCommit(...args);
1094+
};
10841095

10851096
function Component1({ children }) {
10861097
return children;
@@ -1098,11 +1109,14 @@ describe('render', () => {
10981109
expect(calls).to.deep.equal([
10991110
['_diff', [vnode1]],
11001111
['_render', [vnode1]],
1112+
['diffed', [vnode1]],
11011113
['_diff', [vnode2]],
1102-
['_render', [vnode2]]
1114+
['_render', [vnode2]],
1115+
['diffed', [vnode2]],
1116+
['_commit', [vnode1, []]]
11031117
]);
11041118

1105-
expect(calls.length).to.equal(4);
1119+
expect(calls.length).to.equal(7);
11061120

11071121
options.__b = oldDiff;
11081122
options.__r = oldRender;

0 commit comments

Comments
 (0)