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

Commit 008fd43

Browse files
JiaLiPassionmhevery
authored andcommitted
feat(jsonp): provide a help method to patch jsonp (#997)
1 parent 42b9edb commit 008fd43

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

NON-STANDARD-APIS.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,22 @@ user need to patch `io` themselves just like following code.
198198
please reference the sample repo [zone-socketio](https://github.com/JiaLiPassion/zone-socketio) about
199199
detail usage.
200200

201+
* jsonp
202+
203+
## Usage.
204+
205+
provide a helper method to patch jsonp. Because jsonp has a lot of implementation, so
206+
user need to provide the information to let json `send` and `callback` in zone.
207+
208+
there is a sampel repo [zone-jsonp](https://github.com/JiaLiPassion/test-zone-js-with-jsonp) here,
209+
sample usage is:
210+
211+
```javascript
212+
import 'zone.js/dist/zone-patch-jsonp';
213+
Zone['__zone_symbol__jsonp']({
214+
jsonp: getJSONP,
215+
sendFuncName: 'send',
216+
successFuncName: 'jsonpSuccessCallback',
217+
failedFuncName: 'jsonpFailedCallback'
218+
});
219+
```

gulpfile.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ gulp.task('build/bluebird.min.js', ['compile-esm'], function(cb) {
208208
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.min.js', true, cb);
209209
});
210210

211+
gulp.task('build/zone-patch-jsonp.js', ['compile-esm'], function(cb) {
212+
return generateScript('./lib/extra/jsonp.ts', 'zone-patch-jsonp.js', false, cb);
213+
});
214+
215+
gulp.task('build/zone-patch-jsonp.min.js', ['compile-esm'], function(cb) {
216+
return generateScript('./lib/extra/jsonp.ts', 'zone-patch-jsonp.min.js', true, cb);
217+
});
218+
211219
gulp.task('build/jasmine-patch.js', ['compile-esm'], function(cb) {
212220
return generateScript('./lib/jasmine/jasmine.ts', 'jasmine-patch.js', false, cb);
213221
});
@@ -310,6 +318,8 @@ gulp.task('build', [
310318
'build/zone-mix.js',
311319
'build/bluebird.js',
312320
'build/bluebird.min.js',
321+
'build/zone-patch-jsonp.js',
322+
'build/zone-patch-jsonp.min.js',
313323
'build/jasmine-patch.js',
314324
'build/jasmine-patch.min.js',
315325
'build/mocha-patch.js',

lib/extra/jsonp.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
Zone.__load_patch('jsonp', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
9+
const noop = function() {};
10+
// because jsonp is not a standard api, there are a lot of
11+
// implementations, so zone.js just provide a helper util to
12+
// patch the jsonp send and onSuccess/onError callback
13+
// the options is an object which contains
14+
// - jsonp, the jsonp object which hold the send function
15+
// - sendFuncName, the name of the send function
16+
// - successFuncName, success func name
17+
// - failedFuncName, failed func name
18+
(Zone as any)[Zone.__symbol__('jsonp')] = function patchJsonp(options: any) {
19+
if (!options || !options.jsonp || !options.sendFuncName) {
20+
return;
21+
}
22+
const noop = function() {};
23+
24+
[options.successFuncName, options.failedFuncName].forEach(methodName => {
25+
if (!methodName) {
26+
return;
27+
}
28+
29+
const oriFunc = global[methodName];
30+
if (oriFunc) {
31+
api.patchMethod(global, methodName, (delegate: Function) => (self: any, args: any[]) => {
32+
const task = global[api.symbol('jsonTask')];
33+
if (task) {
34+
task.callback = delegate;
35+
return task.invoke.apply(self, args);
36+
} else {
37+
return delegate.apply(self, args);
38+
}
39+
});
40+
} else {
41+
Object.defineProperty(global, methodName, {
42+
configurable: true,
43+
enumerable: true,
44+
get: function() {
45+
return function() {
46+
const task = global[api.symbol('jsonpTask')];
47+
const target = this ? this : global;
48+
const delegate = global[api.symbol(`jsonp${methodName}callback`)];
49+
50+
if (task) {
51+
if (delegate) {
52+
task.callback = delegate;
53+
}
54+
global[api.symbol('jsonpTask')] = undefined;
55+
return task.invoke.apply(this, arguments);
56+
} else {
57+
if (delegate) {
58+
return delegate.apply(this, arguments);
59+
}
60+
}
61+
return null;
62+
};
63+
},
64+
set: function(callback: Function) {
65+
this[api.symbol(`jsonp${methodName}callback`)] = callback;
66+
}
67+
});
68+
}
69+
});
70+
71+
api.patchMethod(options.jsonp, options.sendFuncName, (delegate: Function) => (self: any, args: any[]) => {
72+
global[api.symbol('jsonpTask')] = Zone.current.scheduleMacroTask('jsonp', noop, {}, (task: Task) => {
73+
return delegate.apply(self, args);
74+
}, noop);
75+
});
76+
};
77+
});

0 commit comments

Comments
 (0)