Skip to content

Support global request batch size #1053

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

Merged
merged 1 commit into from
Jan 8, 2020
Merged
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
1 change: 1 addition & 0 deletions src/CoreManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ const config: Config & { [key: string]: mixed } = {
!!process.versions.node &&
!process.versions.electron),
REQUEST_ATTEMPT_LIMIT: 5,
REQUEST_BATCH_SIZE: 20,
REQUEST_HEADERS: {},
SERVER_URL: 'https://api.parse.com/1',
SERVER_AUTH_TYPE: null,
Expand Down
6 changes: 2 additions & 4 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ type SaveOptions = FullOptions & {
cascadeSave?: boolean
}

const DEFAULT_BATCH_SIZE = 20;

// Mapping of class names to constructors, so we can populate objects from the
// server with appropriate subclasses of ParseObject
const classMap = {};
Expand Down Expand Up @@ -2141,7 +2139,7 @@ const DefaultController = {
},

async destroy(target: ParseObject | Array<ParseObject>, options: RequestOptions): Promise<Array<void> | ParseObject> {
const batchSize = (options && options.batchSize) ? options.batchSize : DEFAULT_BATCH_SIZE;
const batchSize = (options && options.batchSize) ? options.batchSize : CoreManager.get('REQUEST_BATCH_SIZE');
const localDatastore = CoreManager.getLocalDatastore();

const RESTController = CoreManager.getRESTController();
Expand Down Expand Up @@ -2216,7 +2214,7 @@ const DefaultController = {
},

save(target: ParseObject | Array<ParseObject | ParseFile>, options: RequestOptions) {
const batchSize = (options && options.batchSize) ? options.batchSize : DEFAULT_BATCH_SIZE;
const batchSize = (options && options.batchSize) ? options.batchSize : CoreManager.get('REQUEST_BATCH_SIZE');
const localDatastore = CoreManager.getLocalDatastore();
const mapIdForPin = {};

Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/Parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,11 @@ describe('Parse module', () => {
expect(CoreManager.get('ENCRYPTED_KEY')).toBe('My Super secret key');
expect(Parse.secret).toBe('My Super secret key');
});

it('can set and get request batch size', () => {
expect(CoreManager.get('REQUEST_BATCH_SIZE')).toBe(20);
CoreManager.set('REQUEST_BATCH_SIZE', 4);
expect(CoreManager.get('REQUEST_BATCH_SIZE')).toBe(4);
CoreManager.set('REQUEST_BATCH_SIZE', 20);
});
});
63 changes: 63 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,69 @@ describe('ParseObject', () => {
jest.runAllTicks();
});

it('can saveAll with global batchSize', async (done) => {
const xhrs = [];
for (let i = 0; i < 2; i++) {
xhrs[i] = {
setRequestHeader: jest.fn(),
open: jest.fn(),
send: jest.fn(),
status: 200,
readyState: 4
};
}
let current = 0;
RESTController._setXHR(function() { return xhrs[current++]; });
const objects = [];
for (let i = 0; i < 22; i++) {
objects[i] = new ParseObject('Person');
}
ParseObject.saveAll(objects).then(() => {
expect(xhrs[0].open.mock.calls[0]).toEqual(
['POST', 'https://api.parse.com/1/batch', true]
);
expect(xhrs[1].open.mock.calls[0]).toEqual(
['POST', 'https://api.parse.com/1/batch', true]
);
done();
});
jest.runAllTicks();
await flushPromises();

xhrs[0].responseText = JSON.stringify([
{ success: { objectId: 'pid0' } },
{ success: { objectId: 'pid1' } },
{ success: { objectId: 'pid2' } },
{ success: { objectId: 'pid3' } },
{ success: { objectId: 'pid4' } },
{ success: { objectId: 'pid5' } },
{ success: { objectId: 'pid6' } },
{ success: { objectId: 'pid7' } },
{ success: { objectId: 'pid8' } },
{ success: { objectId: 'pid9' } },
{ success: { objectId: 'pid10' } },
{ success: { objectId: 'pid11' } },
{ success: { objectId: 'pid12' } },
{ success: { objectId: 'pid13' } },
{ success: { objectId: 'pid14' } },
{ success: { objectId: 'pid15' } },
{ success: { objectId: 'pid16' } },
{ success: { objectId: 'pid17' } },
{ success: { objectId: 'pid18' } },
{ success: { objectId: 'pid19' } },
]);
xhrs[0].onreadystatechange();
jest.runAllTicks();
await flushPromises();

xhrs[1].responseText = JSON.stringify([
{ success: { objectId: 'pid20' } },
{ success: { objectId: 'pid21' } },
]);
xhrs[1].onreadystatechange();
jest.runAllTicks();
});

it('returns the first error when saving an array of objects', async (done) => {
const xhrs = [];
for (let i = 0; i < 2; i++) {
Expand Down