Skip to content

Commit b9f2209

Browse files
ManuelFernandoMaxPresman
authored andcommitted
Thomasconner add nativescript support (#117)
* Add NativeScript support * Add tests for NativeScript * Replace React-Native with NativeScript in NativeScript tests * Fix support for nativescript * Add tasks for to gulpfile for nativescript * Create dist for nativescript * Update .gitignore for nativescript * Remove NativeScript dist bundle * Use NativeScript http module * Remove some Gulp tasks related to NativeScript * Update compiled modules * Update dist bundles * Replace all uses of uuid with lil-uuid * Fix error detection for NartiveScript network module * Check if abort is a function before calling it Some platforms don't return an object that contains an abort function to cancel network requests. This change checks if an abort function is available before calling it. * Update all the builds * add entry point to app with Native Script * resolve eslint issues * add operation delete to native script * remove multi-spaces
1 parent 96544ec commit b9f2209

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"main": "./lib/node/index.js",
1111
"react-native": "./lib/react_native/index.js",
12+
"nativescript": "./lib/nativescript/index.js",
1213
"browser": "./dist/web/pubnub.min.js",
1314
"repository": {
1415
"type": "git",

src/core/components/subscription_manager.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,9 @@ export default class {
497497

498498
_stopSubscribeLoop() {
499499
if (this._subscribeCall) {
500-
this._subscribeCall.abort();
500+
if (typeof this._subscribeCall.abort === 'function') {
501+
this._subscribeCall.abort();
502+
}
501503
this._subscribeCall = null;
502504
}
503505
}

src/nativescript/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* @flow */
2+
3+
import PubNubCore from '../core/pubnub-common';
4+
import Networking from '../networking';
5+
import Database from '../db/common';
6+
import { del, get, post } from '../networking/modules/nativescript';
7+
import { InternalSetupStruct } from '../core/flow_interfaces';
8+
9+
export default class extends PubNubCore {
10+
constructor(setup: InternalSetupStruct) {
11+
setup.db = new Database();
12+
setup.networking = new Networking({ del, get, post });
13+
setup.sdkFamily = 'NativeScript';
14+
super(setup);
15+
}
16+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* @flow */
2+
3+
import { request as HttpRequest } from 'http';
4+
import { EndpointDefinition, StatusAnnouncement } from '../../core/flow_interfaces';
5+
import { buildUrl } from '../utils';
6+
7+
function log(url, qs, res) {
8+
let _pickLogger = () => {
9+
if (console && console.log) return console; // eslint-disable-line no-console
10+
return console;
11+
};
12+
13+
let start = new Date().getTime();
14+
let timestamp = new Date().toISOString();
15+
let logger = _pickLogger();
16+
logger.log('<<<<<'); // eslint-disable-line no-console
17+
logger.log(`[${timestamp}]`, '\n', url, '\n', qs); // eslint-disable-line no-console
18+
logger.log('-----'); // eslint-disable-line no-console
19+
20+
let now = new Date().getTime();
21+
let elapsed = now - start;
22+
let timestampDone = new Date().toISOString();
23+
24+
logger.log('>>>>>>'); // eslint-disable-line no-console
25+
logger.log(`[${timestampDone} / ${elapsed}]`, '\n', url, '\n', qs, '\n', res); // eslint-disable-line no-console
26+
logger.log('-----'); // eslint-disable-line no-console
27+
}
28+
29+
function xdr(method: string, url: string, params: Object, body: string, endpoint: EndpointDefinition, callback: Function): void {
30+
let status: StatusAnnouncement = {};
31+
status.operation = endpoint.operation;
32+
33+
// $FlowFixMe
34+
return HttpRequest({
35+
method,
36+
url: buildUrl(url, params),
37+
content: body,
38+
timeout: endpoint.timeout
39+
}).then((response) => {
40+
status.error = false;
41+
42+
if (response.statusCode) {
43+
status.statusCode = response.statusCode;
44+
}
45+
46+
return response.content.toJSON();
47+
}).then((response) => {
48+
let resp = response;
49+
50+
if (this._config.logVerbosity) {
51+
log(url, params, resp);
52+
}
53+
54+
callback(status, resp);
55+
}).catch((e) => {
56+
status.error = true;
57+
status.errorData = e;
58+
status.category = this._detectErrorCategory(e);
59+
callback(status, null);
60+
});
61+
}
62+
63+
export function get(params: Object, endpoint: EndpointDefinition, callback: Function) {
64+
let url = this.getStandardOrigin() + endpoint.url;
65+
return xdr.call(this, 'GET', url, params, '', endpoint, callback);
66+
}
67+
68+
export function post(params: Object, body: string, endpoint: EndpointDefinition, callback: Function) {
69+
let url = this.getStandardOrigin() + endpoint.url;
70+
return xdr.call(this, 'POST', url, params, body, endpoint, callback);
71+
}
72+
73+
export function del(params: Object, endpoint: EndpointDefinition, callback: Function) {
74+
let url = this.getStandardOrigin() + endpoint.url;
75+
return xdr.call(this, 'DELETE', url, params, {}, endpoint, callback);
76+
}

0 commit comments

Comments
 (0)