Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose chunkSize, add various improvements #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jspm_packages
# Optional npm cache directory
.npm

# yarn lockfile
yarn.lock

# Optional REPL history
.node_repl_history

Expand Down
22 changes: 15 additions & 7 deletions src/ExponentClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* exponent-server-sdk
*
* Use this if you are running Node on your server backend when you are working
* with Exponent
* https://getexponent.com
* with Expo
* https://expo.io
*
* @flow
*/
Expand All @@ -13,6 +13,12 @@ import zlib from 'zlib';

const BASE_URL = 'https://exp.host';
const BASE_API_URL = `${BASE_URL}/--/api/v2`;
/**
* The max number of push notifications to be sent at once.
* Since we can't automatically upgrade everyone using this library, we
* should strongly try not to decrease it
*/
const CHUNK_LIMIT = 100;

// TODO: Eventually we'll want to have developers authenticate. Right now it's
// not necessary because push notifications are the only API we have and the
Expand All @@ -28,7 +34,9 @@ export default class ExponentClient {
* Returns `true` if the token is an Exponent push token
*/
static isExponentPushToken(token: ExponentPushToken): boolean {
return (typeof token === 'string') && token.startsWith('ExponentPushToken');
return (typeof token === 'string') &&
token.startsWith('ExponentPushToken[') &&
token[token.length - 1] === ']';
}

/**
Expand Down Expand Up @@ -78,14 +86,11 @@ export default class ExponentClient {
chunkPushNotifications(
messages: ExponentPushMessage[],
): ExponentPushMessage[][] {
// Since we can't automatically upgrade everyone using this library, we
// should strongly try not to decrease it
const chunkLimit = 100;
let chunks = [];
let chunk = [];
for (let message of messages) {
chunk.push(message);
if (chunk.length >= chunkLimit) {
if (chunk.length >= CHUNK_LIMIT) {
chunks.push(chunk);
chunk = [];
}
Expand Down Expand Up @@ -243,6 +248,9 @@ export type ExponentPushMessage = {
badge?: number,
};

// Expose this as prop, so users don't have to hardcode
ExponentClient.chunkSize = CHUNK_LIMIT;

export type ExponentPushReceipt = {
status: 'ok' | 'error',
details?: {
Expand Down