Skip to content

Commit dd9608a

Browse files
committed
Add crate adapter that coalesces findRecord() requests
1 parent f2f2044 commit dd9608a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

app/adapters/crate.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import ApplicationAdapter from './application';
2+
3+
const BULK_REQUEST_GROUP_SIZE = 10;
4+
5+
export default class CrateAdapter extends ApplicationAdapter {
6+
coalesceFindRequests = true;
7+
8+
groupRecordsForFindMany(store, snapshots) {
9+
let result = [];
10+
for (let i = 0; i < snapshots.length; i += BULK_REQUEST_GROUP_SIZE) {
11+
result.push(snapshots.slice(i, i + BULK_REQUEST_GROUP_SIZE));
12+
}
13+
return result;
14+
}
15+
}

tests/adapters/crate-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { module, test } from 'qunit';
2+
3+
import { setupMirage } from 'ember-cli-mirage/test-support';
4+
5+
import { setupTest } from 'cargo/tests/helpers';
6+
7+
module('Adapter | crate', function (hooks) {
8+
setupTest(hooks);
9+
setupMirage(hooks);
10+
11+
test('findRecord requests are coalesced', async function (assert) {
12+
this.server.create('crate', { name: 'foo' });
13+
this.server.create('version', { crateId: 'foo' });
14+
this.server.create('crate', { name: 'bar' });
15+
this.server.create('version', { crateId: 'bar' });
16+
17+
// if request coalescing works correctly, then this regular API endpoint
18+
// should not be hit in this case
19+
this.server.get('/api/v1/crates/:crate_name', {}, 500);
20+
21+
let store = this.owner.lookup('service:store');
22+
23+
let [foo, bar] = await Promise.all([store.findRecord('crate', 'foo'), store.findRecord('crate', 'bar')]);
24+
assert.equal(foo?.name, 'foo');
25+
assert.equal(bar?.name, 'bar');
26+
});
27+
});

0 commit comments

Comments
 (0)