File tree Expand file tree Collapse file tree 2 files changed +54
-1
lines changed Expand file tree Collapse file tree 2 files changed +54
-1
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import {ComponentModule} from "../../src/modules/component.js";
22import "./../../components/material-icon/material-icon.js" ;
33import { icons } from "./icons.js" ;
44import { EventsManager } from "../../src/system/events-manager.js" ;
5+ import { IdleModule } from "./../../src/modules/idle.js" ;
56
67export default class MaterialIconsView extends HTMLElement {
78 static tag = "material-icons-view" ;
@@ -18,7 +19,7 @@ export default class MaterialIconsView extends HTMLElement {
1819 url : import . meta. url ,
1920 } ) ;
2021
21- this . #createIcons( ) ;
22+ IdleModule . perform ( { tasks : [ this . #createIcons. bind ( this ) ] } ) ;
2223 this . #setupSearch( ) ;
2324 }
2425
Original file line number Diff line number Diff line change 1+ import { validateArgs } from "./../validate/validate-args.js" ;
2+
3+ export class IdleModule {
4+ static name = Object . freeze ( "idle" ) ;
5+
6+ static perform ( args ) {
7+ validateArgs ( args , {
8+ tasks : { type : "Array" , required : true }
9+ } , "IdleModule.initialize: " ) ;
10+
11+ return globalThis . idleWorker . perform ( args . tasks ) ;
12+ }
13+ }
14+
15+ /**
16+ * This class is responsible for executing tasks when the browser is idle.
17+ * It maintains a queue of tasks to be executed when the browser is idle.
18+ */
19+ class IdleWorker {
20+ #queue = [ ] ;
21+ #idleCallbackId = null ;
22+
23+ /**
24+ * Perform the first task in the queue.
25+ * @returns
26+ */
27+ #performFirstTask( deadline ) {
28+ while ( ( deadline . timeRemaining ( ) > 0 || deadline . didTimeout ) && this . #queue. length > 0 ) {
29+ const task = this . #queue. shift ( ) ;
30+ task ( ) ;
31+ }
32+
33+ if ( this . #queue. length > 0 ) {
34+ this . #idleCallbackId = requestIdleCallback ( this . #performFirstTask. bind ( this ) ) ;
35+ } else {
36+ this . #idleCallbackId = null ;
37+ }
38+ }
39+
40+ perform ( tasks ) {
41+ this . #queue. push ( ...tasks ) ;
42+ this . #startIdleTaskExecution( ) ;
43+ }
44+
45+ #startIdleTaskExecution( ) {
46+ if ( this . #idleCallbackId === null ) {
47+ this . #idleCallbackId = requestIdleCallback ( this . #performFirstTask. bind ( this ) ) ;
48+ }
49+ }
50+ }
51+
52+ globalThis . idleWorker ||= new IdleWorker ( ) ;
You can’t perform that action at this time.
0 commit comments