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

Commit 1c5d00d

Browse files
committed
refactor: concurrent to fiber mode
1 parent e6eb65e commit 1c5d00d

File tree

5 files changed

+20
-21
lines changed

5 files changed

+20
-21
lines changed

src/core/render.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { CONCURRENT_MODE_THRESHOLD, DIRECTIVE_PREFIX, UnknownKV } from '../models/generics';
1+
import { DIRECTIVE_PREFIX, UnknownKV } from '../models/generics';
22
import { ASTNode, ASTNodeType, Directives } from '../models/structs';
33
import { renderDirective } from './directive';
4-
import concurrent from './utils/concurrent';
4+
import fiber from './utils/fiber';
55
import { rawDirectiveSplitRE } from './utils/patterns';
66

77
const render = (
@@ -12,7 +12,7 @@ const render = (
1212
): void => {
1313
const legalDirectiveNames = Object.keys(directives);
1414

15-
concurrent(CONCURRENT_MODE_THRESHOLD, function* () {
15+
const renderFiber = fiber(function* () {
1616
for (const node of ast) {
1717
if (node.type === ASTNodeType.NULL) continue;
1818
yield;
@@ -62,7 +62,9 @@ const render = (
6262
node.el.dispatchEvent(effectEvent);
6363
}
6464
}
65-
})();
65+
});
66+
67+
window.requestIdleCallback(renderFiber);
6668
};
6769

6870
export default render;

src/core/utils/__test__/concurrent.spec.ts

-7
This file was deleted.

src/core/utils/__test__/fiber.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import fiber from '../fiber';
2+
3+
describe('.fiber', () => {
4+
it('should be a function', () => {
5+
expect(typeof fiber).toEqual('function');
6+
});
7+
});
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
/* istanbul ignore file */
22

3-
// Concurrent allows us to delay render calls if the main thread is blocked
3+
// Fiber allows us to delay render calls if the main thread is blocked
44
// This is kind of like time slicing in React but less advanced
55

6-
export const concurrent = (
7-
threshold: number,
6+
export const fiber = (
87
generatorFunction: () => Generator<undefined, void, unknown>
98
// eslint-disable-next-line @typescript-eslint/ban-types
10-
): Function => {
9+
): IdleRequestCallback => {
1110
const generator = generatorFunction();
12-
return function next() {
13-
const start = performance.now();
11+
return function next(deadline: IdleDeadline) {
1412
let task = null;
1513
do {
1614
task = generator.next();
17-
} while (performance.now() - start < threshold && !task.done);
15+
} while (!task.done && deadline.timeRemaining() > 0);
1816

1917
if (task.done) return;
2018
/* istanbul ignore next */
21-
setTimeout(next);
19+
requestIdleCallback(next);
2220
};
2321
};
2422

25-
export default concurrent;
23+
export default fiber;

src/models/generics.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export const DIRECTIVE_PREFIX = 'l-';
22
export const COMPONENT_FLAG = 'component';
33
export const FOR_TEMPLATE_FLAG = '__for_template';
44
export const MODEL_REGISTERED_FLAG = '__model_registered';
5-
export const CONCURRENT_MODE_THRESHOLD = 25;
65
export enum DIRECTIVE_SHORTHANDS {
76
'@' = 'on',
87
':' = 'bind',

0 commit comments

Comments
 (0)