See #260 - delays Worker initialization.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 3 Nov 2016 18:57:34 +0000 (19:57 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 3 Nov 2016 19:06:21 +0000 (20:06 +0100)
Why:

* There's no point fetching CleanCSS until files are about to be
  optimized.
* It currently uses a fixed version of CleanCSS but we'll pass
  version to initializer once more versions are supported.

docs/js/drag-drop.js
docs/js/optimizer-worker.js
docs/js/optimizer.js

index db157af..1a3cdc0 100644 (file)
@@ -12,6 +12,8 @@
   function fileDraggedIn(event) {
     event.preventDefault()
     event.dataTransfer.dropEffect = 'copy'
+
+    Optimizer.initialize()
   }
 
   function fileDraggedOver(event) {
index 3fc880d..cf51d14 100644 (file)
@@ -1,8 +1,13 @@
-onmessage = function(event) {
-  if (!event.data)
-    return
+var initialized = false
 
+onmessage = function(event) {
   switch (event.data.command) {
+    case 'initialize':
+      if (!initialized) {
+        initialized = true
+        importScripts('//jakubpawlowicz.github.io/clean-css-builds/v3.4.20.js')
+      }
+      break
     case 'optimize':
       new CleanCSS(event.data.options).minify(event.data.input, function (error, output) {
         postMessage({
@@ -16,4 +21,4 @@ onmessage = function(event) {
   }
 }
 
-importScripts('//jakubpawlowicz.github.io/clean-css-builds/v3.4.20.js')
+
index b6460f3..b404570 100644 (file)
@@ -14,6 +14,12 @@ Optimizer = {
     }
   },
 
+  initialize: function() {
+    this.worker.postMessage({
+      command: 'initialize'
+    })
+  },
+
   process: function (id, styles) {
     this.worker.postMessage({
       command: 'optimize',