Skip to content

Commit

Permalink
Add ability to pass array of chunk schedulers type (closes #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfilatov committed Sep 26, 2021
1 parent f54220f commit 62e0bb0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
14 changes: 14 additions & 0 deletions __tests__/chunkSchedulers/getChunkScheduler.browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getChunkScheduler } from '../../src/chunkSchedulers';
import { timeoutChunkScheduler } from '../../src/chunkSchedulers/timeout';
import { animationFrameChunkScheduler } from '../../src/chunkSchedulers/animationFrame';
import { idleCallbackChunkScheduler } from '../../src/chunkSchedulers/idleCallback';

describe('getChunkScheduler()', () => {
it('should properly detect supported chunk scheduler', () => {
expect(getChunkScheduler('animationFrame')).toBe(animationFrameChunkScheduler!);
expect(getChunkScheduler('immediate')).toBe(timeoutChunkScheduler);
expect(getChunkScheduler(['immediate', 'animationFrame'])).toBe(animationFrameChunkScheduler!);
expect(getChunkScheduler(['immediate'])).toBe(timeoutChunkScheduler);
expect(getChunkScheduler(['auto'])).toBe(idleCallbackChunkScheduler!);
});
});
14 changes: 14 additions & 0 deletions __tests__/chunkSchedulers/getChunkScheduler.nodejs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getChunkScheduler } from '../../src/chunkSchedulers';
import { timeoutChunkScheduler } from '../../src/chunkSchedulers/timeout';
import { immediateChunkScheduler } from '../../src/chunkSchedulers/immediate';

describe('getChunkScheduler()', () => {
it('should properly detect supported chunk scheduler', () => {
expect(getChunkScheduler('animationFrame')).toBe(timeoutChunkScheduler);
expect(getChunkScheduler(['animationFrame', 'idleCallback'])).toBe(timeoutChunkScheduler);
expect(getChunkScheduler('immediate')).toBe(immediateChunkScheduler!);
expect(getChunkScheduler(['animationFrame', 'immediate'])).toBe(immediateChunkScheduler!);
expect(getChunkScheduler(['animationFrame'])).toBe(timeoutChunkScheduler);
expect(getChunkScheduler(['auto'])).toBe(immediateChunkScheduler!);
});
});
30 changes: 25 additions & 5 deletions src/chunkSchedulers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { immediateChunkScheduler } from './immediate';
import { postMessageScheduler } from './postMessage';
import { timeoutChunkScheduler } from './timeout';

const BUILTIN_CHUNK_SHEDULERS: Record<Exclude<ChunkSchedulerType, ChunkScheduler>, ChunkScheduler | null> = {
const BUILTIN_CHUNK_SHEDULERS: Record<Extract<ChunkSchedulerType, string>, ChunkScheduler | null> = {
auto:
idleCallbackChunkScheduler ||
animationFrameChunkScheduler ||
Expand All @@ -18,10 +18,30 @@ const BUILTIN_CHUNK_SHEDULERS: Record<Exclude<ChunkSchedulerType, ChunkScheduler
timeout: timeoutChunkScheduler
};

function getChunkScheduler(type: ChunkSchedulerType): ChunkScheduler {
return typeof type === 'string' ?
BUILTIN_CHUNK_SHEDULERS[type] || timeoutChunkScheduler :
type;
function getChunkScheduler(type: ChunkSchedulerType | ChunkSchedulerType[]): ChunkScheduler {
if(typeof type === 'string') {
return BUILTIN_CHUNK_SHEDULERS[type] || timeoutChunkScheduler;
}

if(Array.isArray(type)) {
for(let i = 0; i < type.length; i++) {
const item = type[i];

if(typeof item === 'string') {
const chunkScheduler = BUILTIN_CHUNK_SHEDULERS[item];

if(chunkScheduler) {
return chunkScheduler;
}
} else {
return item;
}
}

return timeoutChunkScheduler;
}

return type;
}

export {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface Scheduler {
}

interface SchedulerOptions {
chunkScheduler?: ChunkSchedulerType;
chunkScheduler?: ChunkSchedulerType | ChunkSchedulerType[];
chunkBudget?: number;
}

Expand Down

0 comments on commit 62e0bb0

Please sign in to comment.