-
Notifications
You must be signed in to change notification settings - Fork 0
/
straw-ios.js
368 lines (285 loc) · 8.95 KB
/
straw-ios.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// straw-ios.js
/**
* @class straw
* `straw` is the root object of Straw Framework.
*
* @singleton
*/
var straw = {
/**
* @property {String} version
* The version number
*/
version: 'v0.3.5'
};
straw.core = (function (window) {
'use strict';
var currentId = Math.floor(Math.random() * 10000000);
var genId = function () {
return (currentId++).toString();
};
/**
* @class CallbackPair
*
* CallbackPair class represents the pair of success callback and failure callback.
*
* @private
*/
var CallbackPair = function (id, successCallback, failureCallback) {
if (!successCallback && !failureCallback) {
return null;
}
this.id = id;
this.successCallback = successCallback;
this.failureCallback = failureCallback;
};
var callbackPairPt = CallbackPair.prototype;
/**
* @method
* Call the appropriate callback function according to the given success flag.
*
* @param {Boolean} isSuccess the callback result is success or not
* @param {Object} params the parameters
* @return {void}
*/
callbackPairPt.call = function (isSuccess, params) {
if (isSuccess) {
if (typeof this.successCallback === 'function') {
this.successCallback.call(null, params);
}
} else {
if (typeof this.failureCallback === 'function') {
this.failureCallback.call(null, params);
}
}
};
/**
* @class StrawCore
* StrawCore class provides core bridging functionality of Straw Framework.
*
* @private
*/
var StrawCore = function () {
/**
* @property
* table of callbacks
*/
this.cbTable = {};
/**
* @property
* table of Service call parameters
*/
this.pTable = {};
/**
* @property
* local copy of window.console
*/
this.console = window.console;
};
var strawCorePt = StrawCore.prototype;
/**
* @method
* Execute Straw Service Call through native bridging mechanism.
*
* @param {String} service the service name
* @param {String} method the method name
* @param {Object} params the parameters
* @param {Function} successCallback the callback for success
* @param {Function} failureCallback the callback for failure
* @return {String} the id of the created Straw Service Call
*/
strawCorePt.exec = function (service, method, params, successCallback, failureCallback) {
var id = genId();
var callback = new CallbackPair(id, successCallback, failureCallback);
this.storeCallback(callback);
var requestParams = {callId: id, service: service, method: method, params: params};
this.storeParams(requestParams);
this.invokeNativeBridge(id);
return id;
};
/**
* @method
* Invoke native bridge.
*
* @param {String} id the id to call
* @return {void}
*/
strawCorePt.invokeNativeBridge = function (id) {
var iframe = window.document.createElement('iframe');
// set straw url
iframe.setAttribute('src', this.generateStrawCallUrl(id));
// append it to document element to invoke -webView:shouldStartLoadWithRequest:navigationType: of STWWebViewDelegate
window.document.documentElement.appendChild(iframe);
// remove iframe from main dom tree
iframe.parentNode.removeChild(iframe);
};
/**
* @method
* Generate a custom url for Straw Service Call.
*
* @param {String} id the id of the call
* @return {String} the custom scheme url for Straw Service Call
*/
strawCorePt.generateStrawCallUrl = function (id) {
return 'straw://' + id;
};
/**
* @method
* Store the callback to the callback table
*
* @param {CallbackPair} callback the callback to store
* @return {void}
*/
strawCorePt.storeCallback = function (callback) {
if (callback == null || callback.id == null) {
return;
}
this.cbTable[callback.id] = callback;
};
/**
* @method
* Retrieve callback object from the callback table.
*
* @param {String} id callback id
* @param {Boolean} keepAlive keep callback or not
* @return {CallbackPair} retrieved callback or undefined if not found
*/
strawCorePt.retrieveCallback = function (id, keepAlive) {
var callback = this.cbTable[id];
if (callback == null) {
// this case is not irregular because some services don't require callbacks (for example hud-service)
return;
}
if (!keepAlive) {
delete this.cbTable[id];
}
return callback;
};
/**
* @method
* Store Straw Request parameters.
*
* @param {Object} params the parameters to store
* @return {void}
*/
strawCorePt.storeParams = function (params) {
this.pTable[params.callId] = params;
};
/**
* @method
* Retrieve the parameters for the Straw Request.
*
* @param {String} id the id of Straw Request
* @return {Object} the parameter of the Straw Request
*/
strawCorePt.retrieveParams = function (id) {
var params = this.pTable[id];
if (!params) {
this.console.log('[Warn] param for id=' + id + ' not found');
return;
}
delete this.pTable[id];
return params;
};
/**
* @method
* Perform callback invocation.
*
* This method handles the result of Native Service Method process and invoke the appropriate callback.
*
* @param {String} id the id
* @param {Boolean} isSuccess is success or not
* @param {Object} params parameters for Service callback
* @param {Boolean} keepAlive keep callback or not
* @return {void}
*/
strawCorePt.nativeCallback = function (id, isSuccess, params, keepAlive) {
var callback = this.retrieveCallback(id, keepAlive);
if (callback != null) {
callback.call(isSuccess, params);
}
};
var strawCore = new StrawCore();
/**
* @class straw.core
* `straw.core` is the core API for Straw Service call functionality.
*
* Straw Service developers should use `serviceCall` method of this class to create native Service Call request.
*
* @singleton
*/
var StrawApi = function () {};
var strawApiPt = StrawApi.prototype;
/**
* @method
* Get service call's request parameters.
*
* @param {String} callId the call id
* @return {Object} the request parameter
*/
strawApiPt.getRequestParams = function (callId) {
return strawCore.retrieveParams(callId);
};
/**
* @method
* Call a Straw Service.
*
* @param {String} service the service name
* @param {String} method the method name
* @param {Object} params the parameter
* @param {Function} successCallback the success callback
* @param {Function} failureCallback the failure callback
* @return {String} the id of the created Service call
*/
strawApiPt.serviceCall = function (service, method, params, successCallback, failureCallback) {
return strawCore.exec(service, method, params, successCallback, failureCallback);
};
/**
* @method
* Succeed service call.
*
* @param {String} callId the Service call id
* @param {Object} params the parameter object which contains code and message fields
* @param {Boolean} keepAlive if true then keep the callback, otherwise drop it
* @return {void}
*/
strawApiPt.succeed = function (callId, params, keepAlive) {
strawCore.nativeCallback(callId, true, params, keepAlive);
};
/**
* @method
* Fail service call.
*
* @param {String} callId the Service call id
* @param {Object} params the parameter object which contains code and message fields
* @param {Boolean} keepAlive if true then keep the callback, otherwise drop it
* @return {void}
*/
strawApiPt.fail = function (callId, params, keepAlive) {
strawCore.nativeCallback(callId, false, params, keepAlive);
};
/**
* @method
* Set console object for strawCore
*
* This method is used only for test.
*
* @private
*
* @param {Object} console console object
*/
strawApiPt.setConsole = function (console) {
strawCore.console = console;
};
var exports = new StrawApi();
return exports;
}(window));
/**
* @class
* `straw.service` is the namespace for services of Straw Framework.
*
* Straw Service developers should put their JS interface under this namespace. For example, `straw.service.http`, `straw.service.hud` or `straw.service.locale`.
*
* @singleton
*/
straw.service = {};