code cosmetics: simplify importFromString a bit
This commit is contained in:
		
							parent
							
								
									a2faa9398a
								
							
						
					
					
						commit
						346245a847
					
				|  | @ -51,7 +51,8 @@ function importFromString(jsonString) { | ||||||
|     onBackgroundReady().then(() => importFromString(jsonString)); |     onBackgroundReady().then(() => importFromString(jsonString)); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   const json = BG.tryJSONparse(jsonString) || []; // create object in background context
 |   // create objects in background context
 | ||||||
|  |   const json = BG.tryJSONparse(jsonString) || []; | ||||||
|   if (typeof json.slice != 'function') { |   if (typeof json.slice != 'function') { | ||||||
|     json.length = 0; |     json.length = 0; | ||||||
|   } |   } | ||||||
|  | @ -71,15 +72,29 @@ function importFromString(jsonString) { | ||||||
|   const renderQueue = []; |   const renderQueue = []; | ||||||
|   const RENDER_NAP_TIME_MAX = 1000; // ms
 |   const RENDER_NAP_TIME_MAX = 1000; // ms
 | ||||||
|   const RENDER_QUEUE_MAX = 50; // number of styles
 |   const RENDER_QUEUE_MAX = 50; // number of styles
 | ||||||
|  |   const SAVE_OPTIONS = {reason: 'import', notify: false}; | ||||||
|   return new Promise(proceed); |   return new Promise(proceed); | ||||||
| 
 | 
 | ||||||
|   function proceed(resolve) { |   function proceed(resolve) { | ||||||
|     while (index < json.length) { |     while (index < json.length) { | ||||||
|       const item = json[index++]; |       const item = json[index++]; | ||||||
|  |       const info = analyze(item); | ||||||
|  |       if (info) { | ||||||
|  |         // using saveStyle directly since json was parsed in background page context
 | ||||||
|  |         return BG.saveStyle(Object.assign(item, SAVE_OPTIONS)) | ||||||
|  |           .then(style => account({style, info, resolve})); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     renderQueue.forEach(style => handleUpdate(style, {reason: 'import'})); | ||||||
|  |     renderQueue.length = 0; | ||||||
|  |     done(resolve); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   function analyze(item) { | ||||||
|     if (!item || !item.name || !item.name.trim() || typeof item != 'object' |     if (!item || !item.name || !item.name.trim() || typeof item != 'object' | ||||||
|     || (item.sections && typeof item.sections.slice != 'function')) { |     || (item.sections && typeof item.sections.slice != 'function')) { | ||||||
|       stats.invalid.names.push(`#${index}: ${limitString(item && item.name || '')}`); |       stats.invalid.names.push(`#${index}: ${limitString(item && item.name || '')}`); | ||||||
|         continue; |       return; | ||||||
|     } |     } | ||||||
|     item.name = item.name.trim(); |     item.name = item.name.trim(); | ||||||
|     const byId = BG.cachedStyles.byId.get(item.id); |     const byId = BG.cachedStyles.byId.get(item.id); | ||||||
|  | @ -96,13 +111,12 @@ function importFromString(jsonString) { | ||||||
|     if (metaEqual && codeEqual) { |     if (metaEqual && codeEqual) { | ||||||
|       stats.unchanged.names.push(oldStyle.name); |       stats.unchanged.names.push(oldStyle.name); | ||||||
|       stats.unchanged.ids.push(oldStyle.id); |       stats.unchanged.ids.push(oldStyle.id); | ||||||
|         continue; |       return; | ||||||
|     } |     } | ||||||
|       // using saveStyle directly since json was parsed in background page context
 |     return {oldStyle, metaEqual, codeEqual}; | ||||||
|       BG.saveStyle(Object.assign(item, { |   } | ||||||
|         reason: 'import', | 
 | ||||||
|         notify: false, |   function account({style, info, resolve}) { | ||||||
|       })).then(style => { |  | ||||||
|     renderQueue.push(style); |     renderQueue.push(style); | ||||||
|     if (performance.now() - lastRenderTime > RENDER_NAP_TIME_MAX |     if (performance.now() - lastRenderTime > RENDER_NAP_TIME_MAX | ||||||
|     || renderQueue.length > RENDER_QUEUE_MAX) { |     || renderQueue.length > RENDER_QUEUE_MAX) { | ||||||
|  | @ -112,6 +126,7 @@ function importFromString(jsonString) { | ||||||
|       lastRenderTime = performance.now(); |       lastRenderTime = performance.now(); | ||||||
|     } |     } | ||||||
|     setTimeout(proceed, 0, resolve); |     setTimeout(proceed, 0, resolve); | ||||||
|  |     const {oldStyle, metaEqual, codeEqual} = info; | ||||||
|     if (!oldStyle) { |     if (!oldStyle) { | ||||||
|       stats.added.names.push(style.name); |       stats.added.names.push(style.name); | ||||||
|       stats.added.ids.push(style.id); |       stats.added.ids.push(style.id); | ||||||
|  | @ -129,12 +144,6 @@ function importFromString(jsonString) { | ||||||
|     } |     } | ||||||
|     stats.metaOnly.names.push(reportNameChange(oldStyle, style)); |     stats.metaOnly.names.push(reportNameChange(oldStyle, style)); | ||||||
|     stats.metaOnly.ids.push(style.id); |     stats.metaOnly.ids.push(style.id); | ||||||
|       }); |  | ||||||
|       return; |  | ||||||
|     } |  | ||||||
|     renderQueue.forEach(style => handleUpdate(style, {reason: 'import'})); |  | ||||||
|     renderQueue.length = 0; |  | ||||||
|     done(resolve); |  | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function done(resolve) { |   function done(resolve) { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user