瀏覽代碼

Added Promises support in MXL reading

Andrea Condoluci 9 年之前
父節點
當前提交
00c29bba8c
共有 4 個文件被更改,包括 30 次插入24 次删除
  1. 1 0
      package.json
  2. 15 7
      src/Common/FileIO/Mxl.ts
  3. 9 15
      test/Common/FileIO/Mxl.ts
  4. 5 2
      typings.json

+ 1 - 0
package.json

@@ -26,6 +26,7 @@
     "dependencies": {
         "jszip": "",
         "vexflow": "",
+        "es6-promise": "",
 
         "chai": "^3.4.1",
         "mocha": "^2.3.4",

+ 15 - 7
src/Common/FileIO/Mxl.ts

@@ -1,29 +1,37 @@
 import { IXmlElement } from "./Xml";
+import { Promise } from "es6-promise";
 import JSZip = require("jszip");
 
-export function extractSheetFromMxl(data: string, onFullfilled: any, onRejected: any): void {
+export function extractSheetFromMxl(data: string): Promise<any> {
   "use strict";
+  // _zip_ must be of type 'any' for now, since typings for JSZip are not up-to-date
   let zip: any = new JSZip();
-  zip.loadAsync(data).then(
+  // asynchronously load zip file and process it - with Promises
+  return zip.loadAsync(data).then(
     (_: any) => {
       return zip.file("META-INF/container.xml").async("string");
     },
-    onRejected
+    (err: any) => {
+      throw err;
+    }
   ).then(
     (content: string) => {
       let parser: DOMParser = new DOMParser();
       let doc: Document = parser.parseFromString(content, "text/xml");
-      // doc.Root.Element("rootfiles").Element("rootfile").Attribute("full-path").Value;
       let rootFile: string = doc.getElementsByTagName("rootfile")[0].getAttribute("full-path");
       return zip.file(rootFile).async("string");
     },
-    onRejected
+    (err: any) => {
+      throw err;
+    }
   ).then(
     (content: string) => {
       let parser: DOMParser = new DOMParser();
       let doc: Document = parser.parseFromString(content, "text/xml");
-      onFullfilled(new IXmlElement(doc.documentElement));
+      return Promise.resolve(new IXmlElement(doc.documentElement));
     },
-    onRejected
+    (err: any) => {
+      throw err;
+    }
   );
 }

+ 9 - 15
test/Common/FileIO/Mxl.ts

@@ -15,24 +15,18 @@ describe("MXL Tests", () => {
       let mxl: string = getSheet("test/data/" + scoreName + ".mxl");
       chai.expect(mxl).to.not.be.undefined;
       // Extract XML from MXL
-      // Warning, the sheet is loaded asynchronously,
-      // (with Promises), thus we need a little hack to
-      // make Mocha work asynch. with "done()"
-      extractSheetFromMxl(
-        mxl,
+      // Warning: the sheet is loaded asynchronously,
+      // (with Promises), thus we need a little fix
+      // in the end with 'then(null, done)' to
+      // make Mocha work asynchronously
+      extractSheetFromMxl(mxl).then(
         (score: IXmlElement) => {
-          try {
-            chai.expect(score).to.not.be.undefined;
-            chai.expect(score.name).to.equal("score-partwise");
-          } catch (e) {
-            return done(e);
-          }
+          chai.expect(score).to.not.be.undefined;
+          chai.expect(score.name).to.equal("score-partwise");
           done();
         },
-        (reason: any) => {
-          done(reason.message);
-        }
-      )
+        (exc: any) => { throw exc; }
+      ).then(undefined, done);
     });
   }
 

+ 5 - 2
typings.json

@@ -1,7 +1,10 @@
 {
   "ambientDependencies": {
     "chai": "registry:dt/chai#3.4.0+20160317120654",
-    "mocha": "registry:dt/mocha#2.2.5+20160317120654",
-    "jszip": "registry:dt/jszip"
+    "jszip": "registry:dt/jszip",
+    "mocha": "registry:dt/mocha#2.2.5+20160317120654"
+  },
+  "dependencies": {
+    "es6-promise": "registry:npm/es6-promise#3.0.0+20160211003958"
   }
 }