@@ -8,6 +8,8 @@ class WebsiteThemeConfigPlugin extends Plugin {
8
8
9
9
cache = { } ;
10
10
activeViews = { } ;
11
+ pendingRequests = new Set ( ) ;
12
+ resolves = { } ;
11
13
12
14
resources = {
13
15
builder_actions : {
@@ -26,6 +28,42 @@ class WebsiteThemeConfigPlugin extends Plugin {
26
28
} ,
27
29
} ;
28
30
31
+ loadConfigKey ( keys ) {
32
+ if ( ! Array . isArray ( keys ) ) {
33
+ keys = [ keys ] ;
34
+ }
35
+ for ( const key of keys ) {
36
+ if ( ! ( key in this . cache ) ) {
37
+ this . cache [ key ] = this . _loadBatchKey ( key ) ;
38
+ }
39
+ }
40
+ return Promise . all ( keys . map ( ( key ) => this . cache [ key ] ) ) ;
41
+ }
42
+
43
+ _loadBatchKey ( key ) {
44
+ this . pendingRequests . add ( key ) ;
45
+ return new Promise ( ( resolve ) => {
46
+ this . resolves [ key ] = resolve ;
47
+ setTimeout ( ( ) => {
48
+ if ( this . pendingRequests . size && ! this . isDestroyed ) {
49
+ const keys = [ ...this . pendingRequests ] ;
50
+ this . pendingRequests . clear ( ) ;
51
+ rpc ( "/website/theme_customize_data_get" , {
52
+ keys,
53
+ is_view_data : true ,
54
+ } ) . then ( ( r ) => {
55
+ if ( ! this . isDestroyed ) {
56
+ for ( const key of keys ) {
57
+ this . activeViews [ key ] = r . includes ( key ) ;
58
+ this . resolves [ key ] ( ) ;
59
+ }
60
+ }
61
+ } ) ;
62
+ }
63
+ } , 0 ) ;
64
+ } ) ;
65
+ }
66
+
29
67
async toggleConfig ( action , apply ) {
30
68
if ( this . dependencies . history . getIsPreviewing ( ) ) {
31
69
// ignore previews!
@@ -78,29 +116,6 @@ class WebsiteThemeConfigPlugin extends Plugin {
78
116
this . dispatchTo ( "reload_editor" , { target } ) ;
79
117
}
80
118
81
- async loadConfigKey ( keys ) {
82
- if ( ! Array . isArray ( keys ) ) {
83
- keys = [ keys ] ;
84
- }
85
- for ( const key of keys ) {
86
- if ( ! ( key in this . cache ) ) {
87
- this . cache [ key ] = rpc ( "/website/theme_customize_data_get" , {
88
- keys : [ key ] ,
89
- is_view_data : true ,
90
- } ) . then ( ( r ) => {
91
- this . activeViews [ key ] = r . includes ( key ) ;
92
- } ) ;
93
- }
94
- }
95
- const proms = keys . map ( ( key ) => this . cache [ key ] ) ;
96
- await Promise . all ( proms ) ;
97
- const result = { } ;
98
- for ( const key of keys ) {
99
- result [ key ] = this . activeViews [ key ] ;
100
- }
101
- return result ;
102
- }
103
-
104
119
getConfigKey ( key ) {
105
120
return this . activeViews [ key ] ;
106
121
}
0 commit comments