use constants rather than string for ttl

pull/1284/head
Audric Ackermann 5 years ago
parent 467f96acac
commit 48edd431da
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -1,13 +1,18 @@
import { NumberUtils } from './utils';
import { DAYS, HOURS, MINUTES } from './utils/Number';
// tslint:disable: binary-expression-operand-order
// Default TTL
/**
* FIXME The -1 hours is a hack to make the PN not trigger a Notification for those control message.
* Apple devices must show a Notification if a PN is received, and for those
* control message, there is nothing to display (yet).
*/
export const TTL_DEFAULT = {
PAIRING_REQUEST: NumberUtils.timeAsMs(2, 'minutes'),
DEVICE_UNPAIRING: NumberUtils.timeAsMs(4 * 24 - 1, 'hours'),
SESSION_REQUEST: NumberUtils.timeAsMs(4 * 24 - 1, 'hours'),
SESSION_ESTABLISHED: NumberUtils.timeAsMs(2 * 24 - 1, 'hours'),
END_SESSION_MESSAGE: NumberUtils.timeAsMs(4 * 24 - 1, 'hours'),
TYPING_MESSAGE: NumberUtils.timeAsMs(1, 'minute'),
ONLINE_BROADCAST: NumberUtils.timeAsMs(1, 'minute'),
REGULAR_MESSAGE: NumberUtils.timeAsMs(2, 'days'),
PAIRING_REQUEST: 2 * MINUTES,
DEVICE_UNPAIRING: 4 * DAYS - 1 * HOURS,
SESSION_REQUEST: 4 * DAYS - 1 * HOURS,
SESSION_ESTABLISHED: 2 * DAYS - 1 * HOURS,
END_SESSION_MESSAGE: 4 * DAYS - 1 * HOURS,
TYPING_MESSAGE: 1 * MINUTES,
ONLINE_BROADCAST: 1 * MINUTES,
REGULAR_MESSAGE: 2 * DAYS,
};

@ -8,24 +8,10 @@ type TimeUnit =
| 'day'
| 'days';
export const timeAsMs = (value: number, unit: TimeUnit) => {
// Converts a time to milliseconds
// Valid units: second, minute, hour, day
const unitAsSingular = unit.replace(new RegExp('s?$'), '');
switch (unitAsSingular) {
case 'second':
return value * 1000;
case 'minute':
return value * 60 * 1000;
case 'hour':
return value * 60 * 60 * 1000;
case 'day':
return value * 24 * 60 * 60 * 1000;
default:
return value;
}
};
export const MINUTES = 60 * 1000; // in ms
// tslint:disable: binary-expression-operand-order
export const HOURS = 60 * MINUTES; // in ms
export const DAYS = 24 * HOURS; // in ms
export const msAsUnit = (value: number, unit: TimeUnit) => {
// Converts milliseconds to your preferred unit

Loading…
Cancel
Save