Skip to content

Commit

Permalink
Merge pull request #1204 from ParsePlatform/flovilmart.flatteningPush…
Browse files Browse the repository at this point in the history
…AdapterResults

Improve flattening of results from pushAdapter
  • Loading branch information
flovilmart committed Mar 29, 2016
2 parents cd05e3b + aeda714 commit 74b847f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
],
"license": "BSD-3-Clause",
"dependencies": {
"apn": "^1.7.5",
"babel-polyfill": "^6.5.0",
"babel-runtime": "^6.5.0",
"bcrypt-nodejs": "0.0.3",
Expand All @@ -32,7 +31,6 @@
"mime": "^1.3.4",
"mongodb": "~2.1.0",
"multer": "^1.1.0",
"node-gcm": "^0.14.0",
"parse": "^1.8.0",
"parse-server-fs-adapter": "^1.0.0",
"parse-server-gcs-adapter": "^1.0.0",
Expand Down
6 changes: 5 additions & 1 deletion spec/PushController.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
var PushController = require('../src/Controllers/PushController').PushController;

var pushStatusHandler = require('../src/pushStatusHandler');
var Config = require('../src/Config');

const successfulTransmissions = function(body, installations) {
Expand Down Expand Up @@ -357,4 +357,8 @@ describe('PushController', () => {
});
});

it('should flatten', () => {
var res = pushStatusHandler.flatten([1, [2], [[3, 4], 5], [[[6]]]])
expect(res).toEqual([1,2,3,4,5,6]);
})
});
11 changes: 1 addition & 10 deletions src/Controllers/PushController.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,7 @@ export class PushController extends AdaptableController {
}
return this.adapter.send(payload, badgeInstallationsMap[badge]);
});
// Flatten the promises results
return Promise.all(promises).then((results) => {
if (Array.isArray(results)) {
return Promise.resolve(results.reduce((memo, result) => {
return memo.concat(result);
},[]));
} else {
return Promise.resolve(results);
}
})
return Promise.all(promises);
}
return this.adapter.send(body, installations);
}
Expand Down
12 changes: 12 additions & 0 deletions src/pushStatusHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { md5Hash, newObjectId } from './cryptoUtils';

export function flatten(array) {
return array.reduce((memo, element) => {
if (Array.isArray(element)) {
memo = memo.concat(flatten(element));
} else {
memo = memo.concat(element);
}
return memo;
}, []);
}

export default function pushStatusHandler(config) {

let initialPromise;
Expand Down Expand Up @@ -53,6 +64,7 @@ export default function pushStatusHandler(config) {
numFailed: 0,
};
if (Array.isArray(results)) {
results = flatten(results);
results.reduce((memo, result) => {
// Cannot handle that
if (!result.device || !result.device.deviceType) {
Expand Down

0 comments on commit 74b847f

Please sign in to comment.