Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit ce589b9

Browse files
Matthew Hillvicb
Matthew Hill
authored andcommittedSep 11, 2015
feat(lib/patch/file-reader): zone-binds FileReader#onEventName listeners
fix #137
1 parent 1b804cf commit ce589b9

File tree

5 files changed

+120
-3
lines changed

5 files changed

+120
-3
lines changed
 

‎lib/patch/browser.js

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var webSocketPatch = require('./websocket');
99
var eventTargetPatch = require('./event-target');
1010
var propertyDescriptorPatch = require('./property-descriptor');
1111
var geolocationPatch = require('./geolocation');
12+
var fileReaderPatch = require('./file-reader');
1213

1314
function apply() {
1415
fnPatch.patchSetClearFunction(global, [
@@ -42,6 +43,8 @@ function apply() {
4243
registerElementPatch.apply();
4344

4445
geolocationPatch.apply();
46+
47+
fileReaderPatch.apply();
4548
}
4649

4750
module.exports = {

‎lib/patch/event-target.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ function apply() {
1010
// Note: EventTarget is not available in all browsers,
1111
// if it's not available, we instead patch the APIs in the IDL that inherit from EventTarget
1212
} else {
13-
var apis = [ 'ApplicationCache',
13+
var apis = [
14+
'ApplicationCache',
1415
'EventSource',
1516
'FileReader',
1617
'InputMethodContext',
@@ -33,7 +34,14 @@ function apply() {
3334
];
3435

3536
apis.forEach(function(thing) {
36-
global[thing] && utils.patchEventTargetMethods(global[thing].prototype);
37+
var obj = global[thing] && global[thing].prototype;
38+
39+
// Some browsers e.g. Android 4.3's don't actually implement
40+
// the EventTarget methods for all of these e.g. FileReader.
41+
// In this case, there is nothing to patch.
42+
if (obj && obj.addEventListener) {
43+
utils.patchEventTargetMethods(obj);
44+
}
3745
});
3846
}
3947
}

‎lib/patch/file-reader.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
var utils = require('../utils');
4+
5+
function apply() {
6+
utils.patchClass('FileReader');
7+
}
8+
9+
module.exports = {
10+
apply: apply
11+
};

‎lib/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ function patchProperty(obj, prop) {
7474
};
7575

7676
function patchProperties(obj, properties) {
77-
7877
(properties || (function () {
7978
var props = [];
8079
for (var prop in obj) {
@@ -118,6 +117,7 @@ function patchEventTargetMethods(obj) {
118117
handler[boundFnsKey][eventType] = handler[boundFnsKey][eventType] || zone.bind(fn);
119118
arguments[1] = handler[boundFnsKey][eventType];
120119
}
120+
121121
return global.zone.addEventListener.apply(this, arguments);
122122
};
123123

‎test/patch/FileReader.spec.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
describe('FileReader', ifEnvSupports('FileReader', function () {
4+
var fileReader;
5+
var blob;
6+
var data = 'Hello, World!';
7+
var testZone = zone.fork();
8+
9+
// Android 4.3's native browser doesn't implement add/RemoveEventListener for FileReader
10+
function supportsEventTargetFns () {
11+
return FileReader.prototype.addEventListener && FileReader.prototype.removeEventListener;
12+
}
13+
supportsEventTargetFns.message = 'FileReader#addEventListener and FileReader#removeEventListener';
14+
15+
beforeEach(function () {
16+
fileReader = new FileReader();
17+
18+
try {
19+
blob = new Blob([data]);
20+
} catch (e) {
21+
// For hosts that don't support the Blob ctor (e.g. Android 4.3's native browser)
22+
var blobBuilder = new WebKitBlobBuilder();
23+
blobBuilder.append(data);
24+
25+
blob = blobBuilder.getBlob();
26+
}
27+
});
28+
29+
describe('EventTarget methods', ifEnvSupports(supportsEventTargetFns, function () {
30+
it('should bind addEventListener listeners', function (done) {
31+
testZone.run(function () {
32+
fileReader.addEventListener('load', function () {
33+
expect(zone).toBeDirectChildOf(testZone);
34+
expect(fileReader.result).toEqual(data);
35+
done();
36+
});
37+
});
38+
39+
fileReader.readAsText(blob);
40+
});
41+
42+
it('should remove listeners via removeEventListener', function (done) {
43+
var listenerSpy = jasmine.createSpy();
44+
45+
testZone.run(function () {
46+
fileReader.addEventListener('loadstart', listenerSpy);
47+
fileReader.addEventListener('loadend', function () {
48+
expect(listenerSpy).not.toHaveBeenCalled();
49+
done();
50+
});
51+
});
52+
53+
fileReader.removeEventListener('loadstart', listenerSpy);
54+
fileReader.readAsText(blob);
55+
});
56+
}));
57+
58+
it('should bind onEventType listeners', function (done) {
59+
var listenersCalled = 0;
60+
61+
testZone.run(function () {
62+
fileReader.onloadstart = function () {
63+
listenersCalled++;
64+
expect(zone).toBeDirectChildOf(testZone);
65+
};
66+
67+
fileReader.onload = function () {
68+
listenersCalled++;
69+
expect(zone).toBeDirectChildOf(testZone);
70+
};
71+
72+
fileReader.onloadend = function () {
73+
listenersCalled++;
74+
75+
expect(zone).toBeDirectChildOf(testZone);
76+
expect(fileReader.result).toEqual(data);
77+
expect(listenersCalled).toBe(3);
78+
done();
79+
};
80+
});
81+
82+
fileReader.readAsText(blob);
83+
});
84+
85+
it('should have correct readyState', function (done) {
86+
fileReader.onloadend = function () {
87+
expect(fileReader.readyState).toBe(FileReader.DONE);
88+
done();
89+
};
90+
91+
expect(fileReader.readyState).toBe(FileReader.EMPTY);
92+
93+
fileReader.readAsText(blob);
94+
});
95+
}));

0 commit comments

Comments
 (0)
This repository has been archived.