Skip to content

Commit 1f40c0a

Browse files
committed
Merge pull request #584 from flovilmart/hotfix-pushadapters
hotfix for #549
2 parents 017863f + 48dcfe3 commit 1f40c0a

9 files changed

+95
-82
lines changed

spec/AdapterLoader.spec.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

2-
var AdapterLoader = require("../src/Adapters/AdapterLoader").AdapterLoader;
2+
var loadAdapter = require("../src/Adapters/AdapterLoader").loadAdapter;
33
var FilesAdapter = require("../src/Adapters/Files/FilesAdapter").default;
44

55
describe("AdaptableController", ()=>{
66

77
it("should instantiate an adapter from string in object", (done) => {
88
var adapterPath = require('path').resolve("./spec/MockAdapter");
99

10-
var adapter = AdapterLoader.load({
10+
var adapter = loadAdapter({
1111
adapter: adapterPath,
1212
key: "value",
1313
foo: "bar"
@@ -21,7 +21,7 @@ describe("AdaptableController", ()=>{
2121

2222
it("should instantiate an adapter from string", (done) => {
2323
var adapterPath = require('path').resolve("./spec/MockAdapter");
24-
var adapter = AdapterLoader.load(adapterPath);
24+
var adapter = loadAdapter(adapterPath);
2525

2626
expect(adapter instanceof Object).toBe(true);
2727
expect(adapter.options).toBe(adapterPath);
@@ -30,7 +30,7 @@ describe("AdaptableController", ()=>{
3030

3131
it("should instantiate an adapter from string that is module", (done) => {
3232
var adapterPath = require('path').resolve("./src/Adapters/Files/FilesAdapter");
33-
var adapter = AdapterLoader.load({
33+
var adapter = loadAdapter({
3434
adapter: adapterPath
3535
});
3636

@@ -39,30 +39,30 @@ describe("AdaptableController", ()=>{
3939
});
4040

4141
it("should instantiate an adapter from function/Class", (done) => {
42-
var adapter = AdapterLoader.load({
42+
var adapter = loadAdapter({
4343
adapter: FilesAdapter
4444
});
4545
expect(adapter instanceof FilesAdapter).toBe(true);
4646
done();
4747
});
4848

4949
it("should instantiate the default adapter from Class", (done) => {
50-
var adapter = AdapterLoader.load(null, FilesAdapter);
50+
var adapter = loadAdapter(null, FilesAdapter);
5151
expect(adapter instanceof FilesAdapter).toBe(true);
5252
done();
5353
});
5454

5555
it("should use the default adapter", (done) => {
5656
var defaultAdapter = new FilesAdapter();
57-
var adapter = AdapterLoader.load(null, defaultAdapter);
57+
var adapter = loadAdapter(null, defaultAdapter);
5858
expect(adapter instanceof FilesAdapter).toBe(true);
5959
done();
6060
});
6161

6262
it("should use the provided adapter", (done) => {
6363
var originalAdapter = new FilesAdapter();
64-
var adapter = AdapterLoader.load(originalAdapter);
64+
var adapter = loadAdapter(originalAdapter);
6565
expect(adapter).toBe(originalAdapter);
6666
done();
6767
});
68-
});
68+
});

spec/OneSignalPushAdapter.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
var OneSignalPushAdapter = require('../src/Adapters/Push/OneSignalPushAdapter');
3-
3+
var classifyInstallations = require('../src/Adapters/Push/PushAdapterUtils').classifyInstallations;
44
describe('OneSignalPushAdapter', () => {
55
it('can be initialized', (done) => {
66
// Make mock config
@@ -47,7 +47,7 @@ describe('OneSignalPushAdapter', () => {
4747
}
4848
];
4949

50-
var deviceMap = OneSignalPushAdapter.classifyInstallation(installations, validPushTypes);
50+
var deviceMap = OneSignalPushAdapter.classifyInstallations(installations, validPushTypes);
5151
expect(deviceMap['android']).toEqual([makeDevice('androidToken')]);
5252
expect(deviceMap['ios']).toEqual([makeDevice('iosToken')]);
5353
expect(deviceMap['win']).toBe(undefined);

spec/ParsePushAdapter.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('ParsePushAdapter', () => {
8080
}
8181
];
8282

83-
var deviceMap = ParsePushAdapter.classifyInstallation(installations, validPushTypes);
83+
var deviceMap = ParsePushAdapter.classifyInstallations(installations, validPushTypes);
8484
expect(deviceMap['android']).toEqual([makeDevice('androidToken')]);
8585
expect(deviceMap['ios']).toEqual([makeDevice('iosToken')]);
8686
expect(deviceMap['win']).toBe(undefined);

src/Adapters/AdapterLoader.js

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11

2-
export class AdapterLoader {
3-
static load(options, defaultAdapter) {
4-
let adapter;
2+
export function loadAdapter(options, defaultAdapter) {
3+
let adapter;
54

6-
// We have options and options have adapter key
7-
if (options) {
8-
// Pass an adapter as a module name, a function or an instance
9-
if (typeof options == "string" || typeof options == "function" || options.constructor != Object) {
10-
adapter = options;
11-
}
12-
if (options.adapter) {
13-
adapter = options.adapter;
14-
}
5+
// We have options and options have adapter key
6+
if (options) {
7+
// Pass an adapter as a module name, a function or an instance
8+
if (typeof options == "string" || typeof options == "function" || options.constructor != Object) {
9+
adapter = options;
1510
}
16-
17-
if (!adapter) {
18-
adapter = defaultAdapter;
11+
if (options.adapter) {
12+
adapter = options.adapter;
1913
}
14+
}
15+
16+
if (!adapter) {
17+
adapter = defaultAdapter;
18+
}
2019

21-
// This is a string, require the module
22-
if (typeof adapter === "string") {
23-
adapter = require(adapter);
24-
// If it's define as a module, get the default
25-
if (adapter.default) {
26-
adapter = adapter.default;
27-
}
28-
}
29-
// From there it's either a function or an object
30-
// if it's an function, instanciate and pass the options
31-
if (typeof adapter === "function") {
32-
var Adapter = adapter;
33-
adapter = new Adapter(options);
20+
// This is a string, require the module
21+
if (typeof adapter === "string") {
22+
adapter = require(adapter);
23+
// If it's define as a module, get the default
24+
if (adapter.default) {
25+
adapter = adapter.default;
3426
}
35-
return adapter;
3627
}
28+
// From there it's either a function or an object
29+
// if it's an function, instanciate and pass the options
30+
if (typeof adapter === "function") {
31+
var Adapter = adapter;
32+
adapter = new Adapter(options);
33+
}
34+
return adapter;
3735
}
38-
39-
export default AdapterLoader;

src/Adapters/Push/OneSignalPushAdapter.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// PushAdapter, it uses GCM for android push and APNS
44
// for ios push.
55

6+
import { classifyInstallations } from './PushAdapterUtils';
7+
68
const Parse = require('parse/node').Parse;
79
var deepcopy = require('deepcopy');
810
import PushAdapter from './PushAdapter';
@@ -25,7 +27,7 @@ export class OneSignalPushAdapter extends PushAdapter {
2527

2628
send(data, installations) {
2729
console.log("Sending notification to "+installations.length+" devices.")
28-
let deviceMap = PushAdapter.classifyInstallation(installations, this.validPushTypes);
30+
let deviceMap = classifyInstallations(installations, this.validPushTypes);
2931

3032
let sendPromises = [];
3133
for (let pushType in deviceMap) {
@@ -43,6 +45,14 @@ export class OneSignalPushAdapter extends PushAdapter {
4345
return Parse.Promise.when(sendPromises);
4446
}
4547

48+
static classifyInstallations(installations, validTypes) {
49+
return classifyInstallations(installations, validTypes)
50+
}
51+
52+
getValidPushTypes() {
53+
return this.validPushTypes;
54+
}
55+
4656
sendToAPNS(data,tokens) {
4757

4858
data= deepcopy(data['data']);

src/Adapters/Push/ParsePushAdapter.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Parse = require('parse/node').Parse;
77
const GCM = require('../../GCM');
88
const APNS = require('../../APNS');
99
import PushAdapter from './PushAdapter';
10+
import { classifyInstallations } from './PushAdapterUtils';
1011

1112
export class ParsePushAdapter extends PushAdapter {
1213
constructor(pushConfig = {}) {
@@ -31,8 +32,16 @@ export class ParsePushAdapter extends PushAdapter {
3132
}
3233
}
3334

35+
getValidPushTypes() {
36+
return this.validPushTypes;
37+
}
38+
39+
static classifyInstallations(installations, validTypes) {
40+
return classifyInstallations(installations, validTypes)
41+
}
42+
3443
send(data, installations) {
35-
let deviceMap = PushAdapter.classifyInstallation(installations, this.validPushTypes);
44+
let deviceMap = classifyInstallations(installations, this.validPushTypes);
3645
let sendPromises = [];
3746
for (let pushType in deviceMap) {
3847
let sender = this.senderMap[pushType];

src/Adapters/Push/PushAdapter.js

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,7 @@ export class PushAdapter {
1616
* Get an array of valid push types.
1717
* @returns {Array} An array of valid push types
1818
*/
19-
getValidPushTypes() {
20-
return this.validPushTypes;
21-
}
22-
23-
/**g
24-
* Classify the device token of installations based on its device type.
25-
* @param {Object} installations An array of installations
26-
* @param {Array} validPushTypes An array of valid push types(string)
27-
* @returns {Object} A map whose key is device type and value is an array of device
28-
*/
29-
static classifyInstallation(installations, validPushTypes) {
30-
// Init deviceTokenMap, create a empty array for each valid pushType
31-
let deviceMap = {};
32-
for (let validPushType of validPushTypes) {
33-
deviceMap[validPushType] = [];
34-
}
35-
for (let installation of installations) {
36-
// No deviceToken, ignore
37-
if (!installation.deviceToken) {
38-
continue;
39-
}
40-
let pushType = installation.deviceType;
41-
if (deviceMap[pushType]) {
42-
deviceMap[pushType].push({
43-
deviceToken: installation.deviceToken,
44-
appIdentifier: installation.appIdentifier
45-
});
46-
} else {
47-
console.log('Unknown push type from installation %j', installation);
48-
}
49-
}
50-
return deviceMap;
51-
}
19+
getValidPushTypes() {}
5220
}
5321

5422
export default PushAdapter;

src/Adapters/Push/PushAdapterUtils.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**g
2+
* Classify the device token of installations based on its device type.
3+
* @param {Object} installations An array of installations
4+
* @param {Array} validPushTypes An array of valid push types(string)
5+
* @returns {Object} A map whose key is device type and value is an array of device
6+
*/
7+
export function classifyInstallations(installations, validPushTypes) {
8+
// Init deviceTokenMap, create a empty array for each valid pushType
9+
let deviceMap = {};
10+
for (let validPushType of validPushTypes) {
11+
deviceMap[validPushType] = [];
12+
}
13+
for (let installation of installations) {
14+
// No deviceToken, ignore
15+
if (!installation.deviceToken) {
16+
continue;
17+
}
18+
let pushType = installation.deviceType;
19+
if (deviceMap[pushType]) {
20+
deviceMap[pushType].push({
21+
deviceToken: installation.deviceToken,
22+
appIdentifier: installation.appIdentifier
23+
});
24+
} else {
25+
console.log('Unknown push type from installation %j', installation);
26+
}
27+
}
28+
return deviceMap;
29+
}
30+

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { PushRouter } from './Routers/PushRouter';
3333
import { FilesRouter } from './Routers/FilesRouter';
3434
import { LogsRouter } from './Routers/LogsRouter';
3535

36-
import { AdapterLoader } from './Adapters/AdapterLoader';
36+
import { loadAdapter } from './Adapters/AdapterLoader';
3737
import { FileLoggerAdapter } from './Adapters/Logger/FileLoggerAdapter';
3838
import { LoggerController } from './Controllers/LoggerController';
3939

@@ -107,9 +107,9 @@ function ParseServer({
107107
}
108108

109109

110-
const filesControllerAdapter = AdapterLoader.load(filesAdapter, GridStoreAdapter);
111-
const pushControllerAdapter = AdapterLoader.load(push, ParsePushAdapter);
112-
const loggerControllerAdapter = AdapterLoader.load(loggerAdapter, FileLoggerAdapter);
110+
const filesControllerAdapter = loadAdapter(filesAdapter, GridStoreAdapter);
111+
const pushControllerAdapter = loadAdapter(push, ParsePushAdapter);
112+
const loggerControllerAdapter = loadAdapter(loggerAdapter, FileLoggerAdapter);
113113

114114
// We pass the options and the base class for the adatper,
115115
// Note that passing an instance would work too

0 commit comments

Comments
 (0)