Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit 79a0311

Browse files
Fixed HttpRepository::flush promise always being resolved even when flush failed.
1 parent f93ebeb commit 79a0311

9 files changed

Lines changed: 76 additions & 44 deletions

File tree

dist/apisearch.js

Lines changed: 17 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Repository/Repository.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ export declare abstract class Repository {
5757
* @param bulkNumber
5858
* @param skipIfLess
5959
*
60-
* @return {Promise<void>}
60+
* @return {Promise<any[]>}
6161
*/
62-
flush(bulkNumber?: number, skipIfLess?: boolean): Promise<void>;
62+
flush(bulkNumber?: number, skipIfLess?: boolean): Promise<any[]>;
6363
/**
6464
* Make chunks of n elements
6565
*

lib/Repository/Repository.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ var Repository = /** @class */ (function () {
6868
* @param bulkNumber
6969
* @param skipIfLess
7070
*
71-
* @return {Promise<void>}
71+
* @return {Promise<any[]>}
7272
*/
7373
Repository.prototype.flush = function (bulkNumber, skipIfLess) {
7474
return tslib_1.__awaiter(this, void 0, void 0, function () {
75+
var promise, resetCachedElements;
7576
var _this = this;
7677
return tslib_1.__generator(this, function (_a) {
7778
if (!bulkNumber) {
@@ -84,20 +85,21 @@ var Repository = /** @class */ (function () {
8485
this.itemsToUpdate.length < bulkNumber) {
8586
return [2 /*return*/];
8687
}
87-
return [2 /*return*/, Promise.all(Repository
88-
.chunkArray(this.itemsToUpdate, bulkNumber)
89-
.map(function (arrayOfItems) {
90-
return _this.flushUpdateItems(arrayOfItems);
91-
})
92-
.concat(Repository
93-
.chunkArray(this.itemsToDelete, bulkNumber)
94-
.map(function (arrayOfItemsUUID) {
95-
return _this.flushDeleteItems(arrayOfItemsUUID);
96-
}))).then(function (_) {
97-
_this.resetCachedElements();
98-
})["catch"](function (_) {
99-
_this.resetCachedElements();
100-
})];
88+
promise = Promise.all(Repository
89+
.chunkArray(this.itemsToUpdate, bulkNumber)
90+
.map(function (arrayOfItems) {
91+
return _this.flushUpdateItems(arrayOfItems);
92+
})
93+
.concat(Repository
94+
.chunkArray(this.itemsToDelete, bulkNumber)
95+
.map(function (arrayOfItemsUUID) {
96+
return _this.flushDeleteItems(arrayOfItemsUUID);
97+
})));
98+
resetCachedElements = function () {
99+
_this.resetCachedElements();
100+
};
101+
promise.then(resetCachedElements, resetCachedElements);
102+
return [2 /*return*/, promise];
101103
});
102104
});
103105
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apisearch",
3-
"version": "0.2.20",
3+
"version": "0.2.21",
44
"description": "Javascript client for Apisearch.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/Repository/Repository.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ export abstract class Repository {
8989
* @param bulkNumber
9090
* @param skipIfLess
9191
*
92-
* @return {Promise<void>}
92+
* @return {Promise<any[]>}
9393
*/
9494
public async flush(
9595
bulkNumber?: number,
9696
skipIfLess?: boolean,
97-
): Promise<void> {
97+
): Promise<any[]> {
9898

9999
if (!bulkNumber) {
100100
bulkNumber = 500;
@@ -111,7 +111,7 @@ export abstract class Repository {
111111
return;
112112
}
113113

114-
return Promise.all(Repository
114+
const promise = Promise.all(Repository
115115
.chunkArray(
116116
this.itemsToUpdate,
117117
bulkNumber,
@@ -128,11 +128,15 @@ export abstract class Repository {
128128
return this.flushDeleteItems(arrayOfItemsUUID);
129129
}),
130130
),
131-
).then((_) => {
132-
this.resetCachedElements();
133-
}).catch((_) => {
131+
);
132+
133+
const resetCachedElements = () => {
134134
this.resetCachedElements();
135-
});
135+
};
136+
137+
promise.then(resetCachedElements, resetCachedElements);
138+
139+
return promise;
136140
}
137141

138142
/**

test/Repository/HttpRepository.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,30 @@ describe('Repository/', () => {
199199
});
200200
});
201201

202+
it("Should fail on flush when client fails", async () => {
203+
const client = new TestClient();
204+
const repository = new HttpRepository(
205+
client,
206+
"aaa",
207+
"bbb",
208+
"error",
209+
transformer,
210+
);
211+
212+
repository.addItems([
213+
Item.create(ItemUUID.createByComposedUUID("1~product")),
214+
]);
215+
216+
expect(client.calls.length).to.be.equal(0);
217+
218+
try {
219+
await repository.flush();
220+
expect.fail("flush() should have failed");
221+
} catch (error) {
222+
//
223+
}
224+
});
225+
202226
it('All other calls', async () => {
203227
let client = new TestClient();
204228
let repository = new HttpRepository(
@@ -222,4 +246,4 @@ describe('Repository/', () => {
222246
expect(counter).to.be.equal(7);
223247
});
224248
});
225-
});
249+
});

0 commit comments

Comments
 (0)