Skip to content
This repository was archived by the owner on May 19, 2023. It is now read-only.

Commit 24029b6

Browse files
committed
refactor: additional iterators in render
1 parent 6dd1728 commit 24029b6

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

src/core/render.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DIRECTIVE_PREFIX, UnknownKV } from '../models/generics';
22
import { Directives, ASTNode, ASTNodeType } from '../models/structs';
33
import { rawDirectiveSplitRE } from './utils/patterns';
4-
import timeSlice from './utils/timeSlice';
4+
import concurrent from './utils/concurrent';
55

66
import { renderDirective } from './directive';
77

@@ -14,10 +14,9 @@ const render = (
1414
if (typeof changedProps === 'string') changedProps = [changedProps];
1515
const legalDirectiveNames = Object.keys(directives);
1616

17-
timeSlice(function* () {
18-
for (let i = 0; i < ast.length; i++) {
17+
function* traverse() {
18+
for (const node of ast) {
1919
yield;
20-
const node = ast[i];
2120
if (node.type === ASTNodeType.NULL) continue;
2221

2322
const isStatic = node.type === ASTNodeType.STATIC;
@@ -47,6 +46,7 @@ const render = (
4746
node,
4847
state,
4948
};
49+
yield;
5050
renderDirective(directiveProps, directives);
5151

5252
if (isStaticDirective || isMaskDirective) {
@@ -58,7 +58,9 @@ const render = (
5858
}
5959
}
6060
}
61-
})();
61+
}
62+
63+
concurrent(traverse)();
6264
};
6365

6466
export default render;

src/core/utils/concurrent.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const concurrent = (generatorFunction: () => Generator<undefined, void, unknown>) => {
2+
const generator = generatorFunction();
3+
return function next() {
4+
const start = performance.now();
5+
let task = null;
6+
do {
7+
task = generator.next();
8+
} while (performance.now() - start < 20 && !task.done);
9+
10+
if (task.done) return;
11+
setTimeout(next);
12+
};
13+
};
14+
15+
export default concurrent;

src/core/utils/timeSlice.ts

-15
This file was deleted.

0 commit comments

Comments
 (0)