-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (140 loc) · 4.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var NativeModules = require('react-native').NativeModules;
import { isEqual } from 'lodash'
import moment from 'moment'
const pluginName = 'AliyunOSS'
const pluginInstanceName = 'AliOssPlugin'
/*
Utility that avoids leaking the arguments object. See
https://www.npmjs.org/package/argsarray
*/
var plugin = {}
console.log('NativeModules[pluginName] ', NativeModules['RCTAliOSSProvider'])
argsArray = function(fun) {
return function() {
var args, i, len;
len = arguments.length;
if (len) {
args = [];
i = -1;
while (++i < len) {
args[i] = arguments[i];
}
return fun.call(this, args);
} else {
return fun.call(this, []);
}
};
};
plugin.exec = function(method, params, success, error) {
if (plugin.AliOssPlugin.DEBUG){
console.log(`${pluginName}.` + method + '(' + JSON.stringify(params) + ')');
}
NativeModules['AliyunOSS'][method](params,success,error);
};
function enablePromiseRuntime(enable){
if (enable){
createPromiseRuntime();
} else {
createCallbackRuntime();
}
};
var AliOssPlugin = function() {
this.instancePlugin = undefined
this.options = {}
}
AliOssPlugin.prototype.setUpClient = function(params:Map,success:Function,error:Function) {
success = success || function(){};
error = error || function(){};
if (!isEqual(this.options,params)) {
plugin.exec('setUpClient',params,success,error)
this.options = Object.assign({},params)
}
else{
const isTimeout = moment(this.options.timeout).isBefore(Date.now())
if (!isTimeout) {
success({status:{code:0,msg:'成功'},para:params})
}
else {
error({status:{code:100,msg:'token失效'},para:params})
}
}
}
AliOssPlugin.prototype.putByParams = function(params:Map,success:Function,error:Function) {
success = success || function(){};
error = error || function(){};
plugin.exec('putByParams',params,success,error)
}
var AliOssFactory = function(){
}
AliOssFactory.prototype.fetchClient = function(config,success,error){
if (typeof this.instancePlugin === 'undefined') {
this.instancePlugin = new AliOssPlugin()
}
this.instancePlugin.setUpClient(config,success,error)
return this.instancePlugin
}
AliOssFactory.prototype.DEBUG = function(debug) {
console.log("Setting debug to:",debug);
plugin.AliOssPlugin.DEBUG = debug;
};
AliOssFactory.prototype.enablePromiseRuntime = enablePromiseRuntime
plugin = {
AliOssFactory : AliOssFactory,
AliOssPlugin : AliOssPlugin
};
plugin.exec = function(method, params, success, error) {
if (plugin.AliOssPlugin.DEBUG){
console.log(`${pluginName}.` + method + '(' + JSON.stringify(params) + ')');
}
NativeModules[pluginName][method](params,success,error);
};
var originalFns = {};
var config = [
[false,"AliOssPlugin","setUpClient",false,false,true],
[false,"AliOssPlugin","putByParams",false,false,true]
];
config.forEach(entry => {
let [returnValueExpected,prototype,fn]= entry;
console.log('plugins ',returnValueExpected,prototype,fn)
let originalFn = plugin[prototype].prototype[fn];
originalFns[prototype + "." + fn] = originalFn;
});
function createCallbackRuntime() {
config.forEach(entry => {
let [returnValueExpected,prototype,fn,argsNeedPadding,reverseCallbacks,rejectOnError]= entry;
plugin[prototype].prototype[fn] = originalFns[prototype + "." + fn];
});
console.log("Callback based runtime ready");
};
function createPromiseRuntime() {
config.forEach(entry => {
let [returnValueExpected,prototype,fn,argsNeedPadding,reverseCallbacks,rejectOnError]= entry;
let originalFn = plugin[prototype].prototype[fn]
plugin[prototype].prototype[fn] = function(...args){
if (argsNeedPadding && args.length == 1){
args.push([]);
}
var promise = new Promise((resolve,reject) => {
let success = function(...args){
if (!returnValueExpected) {
return resolve(args);
}
};
let error = function(err){
console.log('error: ',fn,...args,arguments);
if (rejectOnError) {
reject(err);
}
return false;
};
var retValue = originalFn.call(this,...args,reverseCallbacks ? error : success, reverseCallbacks ? success : error);
if (returnValueExpected){
return resolve(retValue);
}
});
return promise;
}
});
console.log("Promise based runtime ready");
};
export default (new AliOssFactory())