Uses the resolved promise from the adapter

This commit is contained in:
Florent Vilmart
2016-03-12 15:20:52 -05:00
parent 4d401d9daa
commit a392c088d8
5 changed files with 57 additions and 23 deletions

View File

@@ -143,8 +143,8 @@ describe('PushController', () => {
}
})
return Promise.resolve({
body: body,
installations: installations
error: null
payload: body,
})
},
getValidPushTypes: function() {
@@ -195,8 +195,8 @@ describe('PushController', () => {
expect(1).toEqual(installation.badge);
})
return Promise.resolve({
body: body,
installations: installations
payload: body,
error: null
})
},
getValidPushTypes: function() {
@@ -233,9 +233,10 @@ describe('PushController', () => {
send: function(body, installations) {
var badge = body.data.badge;
return Promise.resolve({
body: body,
installations: installations
})
error: null,
response: "OK!",
payload: body
});
},
getValidPushTypes: function() {
return ["ios"];
@@ -271,7 +272,9 @@ describe('PushController', () => {
var pushAdapter = {
send: function(body, installations) {
return Promise.resolve();
return Promise.resolve({
error:null
});
},
getValidPushTypes: function() {
return ["ios"];

View File

@@ -66,6 +66,12 @@ function APNS(args) {
});
conn.on('transmitted', function(notification, device) {
if (device.callback) {
device.callback(null, {
notification: notification,
device: device
});
}
console.log('APNS Connection %d Notification transmitted to %s', conn.index, device.token.toString('hex'));
});
@@ -91,11 +97,14 @@ APNS.prototype.send = function(data, devices) {
let coreData = data.data;
let expirationTime = data['expiration_time'];
let notification = generateNotification(coreData, expirationTime);
for (let device of devices) {
let promises = devices.map((device) => {
let qualifiedConnIndexs = chooseConns(this.conns, device);
// We can not find a valid conn, just ignore this device
if (qualifiedConnIndexs.length == 0) {
continue;
return Promise.resolve({
err: 'No connection available'
});
}
let conn = this.conns[qualifiedConnIndexs[0]];
let apnDevice = new apn.Device(device.deviceToken);
@@ -104,9 +113,19 @@ APNS.prototype.send = function(data, devices) {
if (device.appIdentifier) {
apnDevice.appIdentifier = device.appIdentifier;
}
conn.pushNotification(notification, apnDevice);
}
return Parse.Promise.as();
return new Promise((resolve, reject) => {
apnDevice.callback = (err, res) => {
resolve({
error: err,
response: res,
payload: notification,
deviceType: 'ios'
});
}
conn.pushNotification(notification, apnDevice);
});
});
return Parse.Promise.when(promises);
}
function handleTransmissionError(conns, errCode, notification, apnDevice) {
@@ -133,7 +152,12 @@ function handleTransmissionError(conns, errCode, notification, apnDevice) {
}
// There is no more available conns, we give up in this case
if (newConnIndex < 0 || newConnIndex >= conns.length) {
console.log('APNS can not find vaild connection for %j', apnDevice.token);
if (apnDevice.callback) {
apnDevice.callback({
error: `APNS can not find vaild connection for ${apnDevice.token}`,
code: errCode
});
}
return;
}

View File

@@ -19,7 +19,7 @@ export class ParsePushAdapter extends PushAdapter {
immediatePush: true
};
let pushTypes = Object.keys(pushConfig);
for (let pushType of pushTypes) {
if (this.validPushTypes.indexOf(pushType) < 0) {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
@@ -35,7 +35,7 @@ export class ParsePushAdapter extends PushAdapter {
}
}
}
getValidPushTypes() {
return this.validPushTypes;
}
@@ -43,7 +43,7 @@ export class ParsePushAdapter extends PushAdapter {
static classifyInstallations(installations, validTypes) {
return classifyInstallations(installations, validTypes)
}
send(data, installations) {
let deviceMap = classifyInstallations(installations, this.validPushTypes);
let sendPromises = [];

View File

@@ -92,6 +92,7 @@ export class PushController extends AdaptableController {
}).then(() => {
return rest.find(config, auth, '_Installation', where);
}).then((response) => {
this.updatePushStatus({status: "running"}, {status:"pending", objectId: pushStatus.objectId}, config);
if (body.data && body.data.badge && body.data.badge == "Increment") {
// Collect the badges to reduce the # of calls
let badgeInstallationsMap = response.results.reduce((map, installation) => {
@@ -117,8 +118,9 @@ export class PushController extends AdaptableController {
return Promise.all(promises);
}
return pushAdapter.send(body, response.results, pushStatus);
}).then(() => {
return this.updatePushStatus({status: "running"}, pushStatus, config);
}).then((results) => {
console.log(results);
return Promise.resolve(results);
});
}
@@ -139,8 +141,8 @@ export class PushController extends AdaptableController {
return restWrite.execute();
}
updatePushStatus(update, pushStatus, config) {
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', {objectId: pushStatus.objectId, "status": "pending"}, update);
updatePushStatus(update, where, config) {
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', where, update);
return restWrite.execute();
}

View File

@@ -21,7 +21,7 @@ function GCM(args) {
* @param {Array} devices A array of devices
* @returns {Object} A promise which is resolved after we get results from gcm
*/
GCM.prototype.send = function(data, devices) {
GCM.prototype.send = function(data, devices, callback) {
let pushId = cryptoUtils.newObjectId();
let timeStamp = Date.now();
let expirationTime;
@@ -52,7 +52,12 @@ GCM.prototype.send = function(data, devices) {
request: message,
response: response
});
sendPromise.resolve();
sendPromise.resolve({
error: error,
response: response,
payload: message,
deviceType: 'android'
});
});
sendPromises.push(sendPromise);
}