@searchspring/snap-controller 0.51.0 → 0.51.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"SearchController.d.ts","sourceRoot":"","sources":["../../../src/Search/SearchController.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACpE,OAAO,EAAE,YAAY,EAA0B,MAAM,+BAA+B,CAAC;AAErF,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAE3C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,KAAK,EACX,sBAAsB,EAItB,kBAAkB,EAClB,gBAAgB,EAGhB,MAAM,UAAU,CAAC;AAElB,OAAO,KAAK,EAAE,kBAAkB,EAA2E,MAAM,2BAA2B,CAAC;AAqB7I,aAAK,kBAAkB,GAAG;IACzB,OAAO,EAAE;QACR,KAAK,EAAE,CAAC,CAAC,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,KAAK,WAAW,GAAG,SAAS,CAAC;KAC/D,CAAC;CACF,CAAC;AAEF,qBAAa,gBAAiB,SAAQ,kBAAkB;IAChD,IAAI,kBAA0B;IAC7B,KAAK,EAAE,WAAW,CAAC;IACnB,MAAM,EAAE,sBAAsB,CAAC;IACvC,OAAO,EAAE,YAAY,CAAC;IACtB,OAAO,CAAC,eAAe,CAAwC;gBAG9D,MAAM,EAAE,sBAAsB,EAC9B,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,kBAAkB,EAC1F,OAAO,CAAC,EAAE,gBAAgB;IAgK3B,KAAK,EAAE,kBAAkB,CA8CvB;IAEF,IAAI,MAAM,IAAI,kBAAkB,CAgD/B;IAED,MAAM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C,EAAE,gBAAgB;IAgK3B,KAAK,EAAE,kBAAkB,CA8CvB;IAEF,IAAI,MAAM,IAAI,kBAAkB,CAiD/B;IAED,MAAM,QAAa,QAAQ,IAAI,CAAC,CAkN9B;CACF;AAED,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,kBAAkB,GAAG,kBAAkB,CAiBxF;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG,MAAM,GAAG,SAAS,CAgCvG"}
@@ -81,7 +81,6 @@ var AbstractController_1 = require("../Abstract/AbstractController");
81
81
  var snap_store_mobx_1 = require("@searchspring/snap-store-mobx");
82
82
  var getParams_1 = require("../utils/getParams");
83
83
  var types_1 = require("../types");
84
- var API_LIMIT = 500;
85
84
  var defaultConfig = {
86
85
  id: 'search',
87
86
  globals: {},
@@ -148,38 +147,39 @@ var SearchController = /** @class */ (function (_super) {
148
147
  },
149
148
  };
150
149
  _this.search = function () { return __awaiter(_this, void 0, void 0, function () {
151
- var params, err_1, stringyParams, prevStringyParams, searchProfile, meta_1, response_1, preventBackfill, dontBackfill, backfills, pageSize, meta_2, pagesNeeded, _a, backFillPageSize, backFillPages, i, backfillParams, backfillResults_1, backfillResponses, afterSearchProfile, err_2, afterStoreProfile, err_3, err_4;
152
- var _b, _c;
153
- var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
154
- return __generator(this, function (_x) {
155
- switch (_x.label) {
150
+ var params, err_1, stringyParams, prevStringyParams, searchProfile, meta, response, preventBackfill, dontBackfill, backfillRequests, backfillResponses, backfillResults, afterSearchProfile, err_2, afterStoreProfile, err_3, err_4;
151
+ var _a, _b;
152
+ var _this = this;
153
+ var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
154
+ return __generator(this, function (_p) {
155
+ switch (_p.label) {
156
156
  case 0:
157
157
  if (!!this.initialized) return [3 /*break*/, 2];
158
158
  return [4 /*yield*/, this.init()];
159
159
  case 1:
160
- _x.sent();
161
- _x.label = 2;
160
+ _p.sent();
161
+ _p.label = 2;
162
162
  case 2:
163
163
  params = this.params;
164
- if (((_e = (_d = this.params.search) === null || _d === void 0 ? void 0 : _d.query) === null || _e === void 0 ? void 0 : _e.string) && ((_g = (_f = this.params.search) === null || _f === void 0 ? void 0 : _f.query) === null || _g === void 0 ? void 0 : _g.string.length)) {
164
+ if (((_d = (_c = this.params.search) === null || _c === void 0 ? void 0 : _c.query) === null || _d === void 0 ? void 0 : _d.string) && ((_f = (_e = this.params.search) === null || _e === void 0 ? void 0 : _e.query) === null || _f === void 0 ? void 0 : _f.string.length)) {
165
165
  // save it to the history store
166
166
  this.store.history.save(this.params.search.query.string);
167
167
  }
168
- _x.label = 3;
168
+ _p.label = 3;
169
169
  case 3:
170
- _x.trys.push([3, 26, , 27]);
171
- _x.label = 4;
170
+ _p.trys.push([3, 23, , 24]);
171
+ _p.label = 4;
172
172
  case 4:
173
- _x.trys.push([4, 6, , 7]);
173
+ _p.trys.push([4, 6, , 7]);
174
174
  return [4 /*yield*/, this.eventManager.fire('beforeSearch', {
175
175
  controller: this,
176
176
  request: params,
177
177
  })];
178
178
  case 5:
179
- _x.sent();
179
+ _p.sent();
180
180
  return [3 /*break*/, 7];
181
181
  case 6:
182
- err_1 = _x.sent();
182
+ err_1 = _p.sent();
183
183
  if ((err_1 === null || err_1 === void 0 ? void 0 : err_1.message) == 'cancelled') {
184
184
  this.log.warn("'beforeSearch' middleware cancelled");
185
185
  return [2 /*return*/];
@@ -197,106 +197,85 @@ var SearchController = /** @class */ (function (_super) {
197
197
  return [2 /*return*/];
198
198
  }
199
199
  searchProfile = this.profiler.create({ type: 'event', name: 'search', context: params }).start();
200
- if (!(((_h = this.config.settings) === null || _h === void 0 ? void 0 : _h.infinite) && ((_j = params.pagination) === null || _j === void 0 ? void 0 : _j.page) && params.pagination.page > 1)) return [3 /*break*/, 15];
201
- preventBackfill = ((_k = this.config.settings.infinite) === null || _k === void 0 ? void 0 : _k.backfill) && !this.store.results.length && params.pagination.page > this.config.settings.infinite.backfill;
202
- dontBackfill = !((_l = this.config.settings.infinite) === null || _l === void 0 ? void 0 : _l.backfill) && !this.store.results.length;
200
+ meta = {};
201
+ response = {};
202
+ if (!(((_g = this.config.settings) === null || _g === void 0 ? void 0 : _g.infinite) && ((_h = params.pagination) === null || _h === void 0 ? void 0 : _h.page) && params.pagination.page > 1)) return [3 /*break*/, 12];
203
+ preventBackfill = ((_j = this.config.settings.infinite) === null || _j === void 0 ? void 0 : _j.backfill) && !this.store.results.length && params.pagination.page > this.config.settings.infinite.backfill;
204
+ dontBackfill = !((_k = this.config.settings.infinite) === null || _k === void 0 ? void 0 : _k.backfill) && !this.store.results.length;
203
205
  // if the page is higher than the backfill setting redirect back to page 1
204
206
  if (preventBackfill || dontBackfill) {
205
207
  this.storage.set('scrollMap', {});
206
208
  this.urlManager.set('page', 1).go();
207
209
  return [2 /*return*/];
208
210
  }
209
- backfills = [];
210
- pageSize = ((_m = params.pagination) === null || _m === void 0 ? void 0 : _m.pageSize) || this.store.pagination.pageSize || this.store.pagination.defaultPageSize;
211
- if (!(((_o = this.config.settings) === null || _o === void 0 ? void 0 : _o.infinite.backfill) && !this.previousResults.length)) return [3 /*break*/, 10];
212
- if (!!pageSize) return [3 /*break*/, 9];
213
- return [4 /*yield*/, this.client.meta()];
214
- case 8:
215
- meta_2 = _x.sent();
216
- pageSize = (_p = meta_2.pagination) === null || _p === void 0 ? void 0 : _p.defaultPageSize;
217
- _x.label = 9;
218
- case 9:
219
- // restricting pageSize to the limit
220
- pageSize = pageSize > API_LIMIT ? API_LIMIT : pageSize;
221
- pagesNeeded = ((_q = params.pagination) === null || _q === void 0 ? void 0 : _q.page) && ((_r = params.pagination) === null || _r === void 0 ? void 0 : _r.page) > ((_s = this.config.settings) === null || _s === void 0 ? void 0 : _s.infinite.backfill)
222
- ? (_t = this.config.settings) === null || _t === void 0 ? void 0 : _t.infinite.backfill
223
- : (_u = params.pagination) === null || _u === void 0 ? void 0 : _u.page;
224
- _a = backFillSize(pagesNeeded, pageSize), backFillPageSize = _a.size, backFillPages = _a.pages;
225
- for (i = 1; i <= backFillPages; i++) {
226
- backfillParams = (0, deepmerge_1.default)(__assign({}, params), { pagination: { pageSize: backFillPageSize, page: i } });
227
- backfills.push(this.client.search(backfillParams));
228
- }
229
- _x.label = 10;
230
- case 10:
231
- if (!(backfills && backfills.length)) return [3 /*break*/, 12];
232
- backfillResults_1 = [];
233
- return [4 /*yield*/, Promise.all(backfills)];
234
- case 11:
235
- backfillResponses = _x.sent();
236
- backfillResponses.map(function (_a) {
237
- var metaBackfill = _a[0], responseBackfill = _a[1];
238
- if (!meta_1) {
239
- meta_1 = metaBackfill;
240
- }
241
- if (!response_1) {
242
- response_1 = responseBackfill;
243
- }
244
- // push results to array
245
- if (responseBackfill.results) {
246
- backfillResults_1.push.apply(backfillResults_1, responseBackfill.results);
211
+ if (!(((_l = this.config.settings) === null || _l === void 0 ? void 0 : _l.infinite.backfill) && !this.previousResults.length)) return [3 /*break*/, 9];
212
+ backfillRequests = Array(params.pagination.page)
213
+ .fill('backfill')
214
+ .map(function (v, i) {
215
+ var _a, _b, _c, _d;
216
+ var backfillParams = (0, deepmerge_1.default)(__assign({}, params), { pagination: { page: i + 1 }, search: { redirectResponse: 'full' } });
217
+ // don't include page parameter if on page 1
218
+ if (i + 1 == 1) {
219
+ (_a = backfillParams === null || backfillParams === void 0 ? void 0 : backfillParams.pagination) === null || _a === void 0 ? true : delete _a.page;
220
+ if ((_c = (_b = _this.config.settings) === null || _b === void 0 ? void 0 : _b.redirects) === null || _c === void 0 ? void 0 : _c.merchandising) {
221
+ // redirect setting
222
+ // DUPLICATED LOGIC can be found in params getter
223
+ (_d = backfillParams === null || backfillParams === void 0 ? void 0 : backfillParams.search) === null || _d === void 0 ? true : delete _d.redirectResponse;
224
+ }
247
225
  }
226
+ return _this.client.search(backfillParams);
248
227
  });
228
+ return [4 /*yield*/, Promise.all(backfillRequests)];
229
+ case 8:
230
+ backfillResponses = _p.sent();
231
+ // backfillResponses are [meta, searchResponse][]
232
+ // set the meta and response to the first page of backfillResponses
233
+ meta = backfillResponses[0][0];
234
+ response = backfillResponses[0][1];
235
+ backfillResults = backfillResponses.reduce(function (results, response) {
236
+ // response is [meta, searchResponse]
237
+ return results.concat.apply(results, response[1].results);
238
+ }, []);
249
239
  // overwrite pagination params to expected state
250
- response_1.pagination.pageSize = pageSize;
251
- response_1.pagination.totalPages = Math.ceil(response_1.pagination.totalResults / response_1.pagination.pageSize);
252
- response_1.pagination.page = (_v = params.pagination) === null || _v === void 0 ? void 0 : _v.page;
240
+ response.pagination.totalPages = Math.ceil(response.pagination.totalResults / response.pagination.pageSize);
241
+ response.pagination.page = (_m = params.pagination) === null || _m === void 0 ? void 0 : _m.page;
253
242
  // set the response results with results from backfill responses
254
- response_1.results = backfillResults_1;
255
- if (!response_1.meta) {
256
- // @ts-ignore : MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
257
- response_1.meta = meta_1;
258
- }
259
- return [3 /*break*/, 14];
243
+ response.results = backfillResults;
244
+ return [3 /*break*/, 11];
245
+ case 9: return [4 /*yield*/, this.client.search(params)];
246
+ case 10:
247
+ // infinite with no backfills.
248
+ _a = _p.sent(), meta = _a[0], response = _a[1];
249
+ // append new results to previous results
250
+ response.results = __spreadArray(__spreadArray([], this.previousResults, true), (response.results || []), true);
251
+ _p.label = 11;
252
+ case 11: return [3 /*break*/, 14];
260
253
  case 12: return [4 /*yield*/, this.client.search(params)];
261
254
  case 13:
262
- // infinite with no backfills.
263
- _b = _x.sent(), meta_1 = _b[0], response_1 = _b[1];
264
- // @ts-ignore : MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
265
- if (!response_1.meta) {
266
- // @ts-ignore : MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
267
- response_1.meta = meta_1;
255
+ // standard request (not using infinite scroll)
256
+ _b = _p.sent(), meta = _b[0], response = _b[1];
257
+ _p.label = 14;
258
+ case 14:
259
+ // MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
260
+ if (!response.meta) {
261
+ response.meta = meta;
268
262
  }
269
- //append new results to previous results
270
- response_1.results = __spreadArray(__spreadArray([], this.previousResults, true), (response_1.results || []), true);
271
- _x.label = 14;
272
- case 14: return [3 /*break*/, 17];
273
- case 15: return [4 /*yield*/, this.client.search(params)];
274
- case 16:
275
- //standard.
276
- _c = _x.sent(), meta_1 = _c[0], response_1 = _c[1];
277
- // @ts-ignore : MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
278
- if (!response_1.meta) {
279
- // @ts-ignore : MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
280
- response_1.meta = meta_1;
281
- }
282
- _x.label = 17;
283
- case 17:
284
263
  searchProfile.stop();
285
264
  this.log.profile(searchProfile);
286
265
  afterSearchProfile = this.profiler.create({ type: 'event', name: 'afterSearch', context: params }).start();
287
- _x.label = 18;
288
- case 18:
289
- _x.trys.push([18, 20, , 21]);
266
+ _p.label = 15;
267
+ case 15:
268
+ _p.trys.push([15, 17, , 18]);
290
269
  return [4 /*yield*/, this.eventManager.fire('afterSearch', {
291
270
  controller: this,
292
271
  request: params,
293
- response: response_1,
272
+ response: response,
294
273
  })];
295
- case 19:
296
- _x.sent();
297
- return [3 /*break*/, 21];
298
- case 20:
299
- err_2 = _x.sent();
274
+ case 16:
275
+ _p.sent();
276
+ return [3 /*break*/, 18];
277
+ case 17:
278
+ err_2 = _p.sent();
300
279
  if ((err_2 === null || err_2 === void 0 ? void 0 : err_2.message) == 'cancelled') {
301
280
  this.log.warn("'afterSearch' middleware cancelled");
302
281
  afterSearchProfile.stop();
@@ -306,30 +285,30 @@ var SearchController = /** @class */ (function (_super) {
306
285
  this.log.error("error in 'afterSearch' middleware");
307
286
  throw err_2;
308
287
  }
309
- return [3 /*break*/, 21];
310
- case 21:
288
+ return [3 /*break*/, 18];
289
+ case 18:
311
290
  afterSearchProfile.stop();
312
291
  this.log.profile(afterSearchProfile);
313
292
  // store previous results for infinite usage
314
- if ((_w = this.config.settings) === null || _w === void 0 ? void 0 : _w.infinite) {
315
- this.previousResults = JSON.parse(JSON.stringify(response_1.results));
293
+ if ((_o = this.config.settings) === null || _o === void 0 ? void 0 : _o.infinite) {
294
+ this.previousResults = JSON.parse(JSON.stringify(response.results));
316
295
  }
317
296
  // update the store
318
- this.store.update(response_1);
297
+ this.store.update(response);
319
298
  afterStoreProfile = this.profiler.create({ type: 'event', name: 'afterStore', context: params }).start();
320
- _x.label = 22;
321
- case 22:
322
- _x.trys.push([22, 24, , 25]);
299
+ _p.label = 19;
300
+ case 19:
301
+ _p.trys.push([19, 21, , 22]);
323
302
  return [4 /*yield*/, this.eventManager.fire('afterStore', {
324
303
  controller: this,
325
304
  request: params,
326
- response: response_1,
305
+ response: response,
327
306
  })];
328
- case 23:
329
- _x.sent();
330
- return [3 /*break*/, 25];
331
- case 24:
332
- err_3 = _x.sent();
307
+ case 20:
308
+ _p.sent();
309
+ return [3 /*break*/, 22];
310
+ case 21:
311
+ err_3 = _p.sent();
333
312
  if ((err_3 === null || err_3 === void 0 ? void 0 : err_3.message) == 'cancelled') {
334
313
  this.log.warn("'afterStore' middleware cancelled");
335
314
  afterStoreProfile.stop();
@@ -339,13 +318,13 @@ var SearchController = /** @class */ (function (_super) {
339
318
  this.log.error("error in 'afterStore' middleware");
340
319
  throw err_3;
341
320
  }
342
- return [3 /*break*/, 25];
343
- case 25:
321
+ return [3 /*break*/, 22];
322
+ case 22:
344
323
  afterStoreProfile.stop();
345
324
  this.log.profile(afterStoreProfile);
346
- return [3 /*break*/, 27];
347
- case 26:
348
- err_4 = _x.sent();
325
+ return [3 /*break*/, 24];
326
+ case 23:
327
+ err_4 = _p.sent();
349
328
  if (err_4) {
350
329
  if (err_4.err && err_4.fetchDetails) {
351
330
  switch (err_4.fetchDetails.status) {
@@ -386,8 +365,8 @@ var SearchController = /** @class */ (function (_super) {
386
365
  }
387
366
  this.store.loading = false;
388
367
  }
389
- return [3 /*break*/, 27];
390
- case 27: return [2 /*return*/];
368
+ return [3 /*break*/, 24];
369
+ case 24: return [2 /*return*/];
391
370
  }
392
371
  });
393
372
  }); };
@@ -581,6 +560,7 @@ var SearchController = /** @class */ (function (_super) {
581
560
  var _a, _b, _c, _d;
582
561
  var params = (0, deepmerge_1.default)(__assign({}, (0, getParams_1.getSearchParams)(this.urlManager.state)), this.config.globals || {});
583
562
  // redirect setting
563
+ // DUPLICATED LOGIC can be found in infinite backfill (change both if updating)
584
564
  if (!((_b = (_a = this.config.settings) === null || _a === void 0 ? void 0 : _a.redirects) === null || _b === void 0 ? void 0 : _b.merchandising) || this.store.loaded) {
585
565
  params.search = params.search || {};
586
566
  params.search.redirectResponse = 'full';
@@ -670,10 +650,3 @@ function generateHrefSelector(element, href, levels) {
670
650
  return;
671
651
  }
672
652
  exports.generateHrefSelector = generateHrefSelector;
673
- function backFillSize(pages, pageSize) {
674
- var totalResults = pages * pageSize;
675
- var numPages = Math.ceil(totalResults / API_LIMIT);
676
- while (totalResults % numPages)
677
- numPages++;
678
- return { size: totalResults / numPages, pages: numPages };
679
- }
@@ -1 +1 @@
1
- {"version":3,"file":"SearchController.d.ts","sourceRoot":"","sources":["../../../src/Search/SearchController.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACpE,OAAO,EAAE,YAAY,EAA0B,MAAM,+BAA+B,CAAC;AAErF,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAE3C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,KAAK,EACX,sBAAsB,EAItB,kBAAkB,EAClB,gBAAgB,EAGhB,MAAM,UAAU,CAAC;AAElB,OAAO,KAAK,EAAE,kBAAkB,EAA2E,MAAM,2BAA2B,C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B,kBAAkB,EAClB,gBAAgB,EAGhB,MAAM,UAAU,CAAC;AAElB,OAAO,KAAK,EACX,kBAAkB,EAKlB,MAAM,2BAA2B,CAAC;AAmBnC,aAAK,kBAAkB,GAAG;IACzB,OAAO,EAAE;QACR,KAAK,EAAE,CAAC,CAAC,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,KAAK,WAAW,GAAG,SAAS,CAAC;KAC/D,CAAC;CACF,CAAC;AAEF,qBAAa,gBAAiB,SAAQ,kBAAkB;IAChD,IAAI,kBAA0B;IAC7B,KAAK,EAAE,WAAW,CAAC;IACnB,MAAM,EAAE,sBAAsB,CAAC;IACvC,OAAO,EAAE,YAAY,CAAC;IACtB,OAAO,CAAC,eAAe,CAAwC;gBAG9D,MAAM,EAAE,sBAAsB,EAC9B,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,kBAAkB,EAC1F,OAAO,CAAC,EAAE,gBAAgB;IAgK3B,KAAK,EAAE,kBAAkB,CA8CvB;IAEF,IAAI,MAAM,IAAI,kBAAkB,CAiD/B;IAED,MAAM,QAAa,QAAQ,IAAI,CAAC,CAkN9B;CACF;AAED,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,kBAAkB,GAAG,kBAAkB,CAiBxF;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG,MAAM,GAAG,SAAS,CAgCvG"}
@@ -4,7 +4,6 @@ import { AbstractController } from '../Abstract/AbstractController';
4
4
  import { StorageStore, StorageType, ErrorType } from '@searchspring/snap-store-mobx';
5
5
  import { getSearchParams } from '../utils/getParams';
6
6
  import { ControllerTypes } from '../types';
7
- const API_LIMIT = 500;
8
7
  const defaultConfig = {
9
8
  id: 'search',
10
9
  globals: {},
@@ -98,10 +97,9 @@ export class SearchController extends AbstractController {
98
97
  return;
99
98
  }
100
99
  const searchProfile = this.profiler.create({ type: 'event', name: 'search', context: params }).start();
101
- let meta;
102
- let response;
103
- // infinite functionality
104
- // if params.page > 1 and infinite setting exists we should append results
100
+ let meta = {};
101
+ let response = {};
102
+ // infinite scroll functionality (after page 1)
105
103
  if (this.config.settings?.infinite && params.pagination?.page && params.pagination.page > 1) {
106
104
  const preventBackfill = this.config.settings.infinite?.backfill && !this.store.results.length && params.pagination.page > this.config.settings.infinite.backfill;
107
105
  const dontBackfill = !this.config.settings.infinite?.backfill && !this.store.results.length;
@@ -111,75 +109,54 @@ export class SearchController extends AbstractController {
111
109
  this.urlManager.set('page', 1).go();
112
110
  return;
113
111
  }
114
- const backfills = [];
115
- let pageSize = params.pagination?.pageSize || this.store.pagination.pageSize || this.store.pagination.defaultPageSize;
116
- // if no results fetch results...
112
+ // infinite backfill is enabled AND we have not yet fetched any results
117
113
  if (this.config.settings?.infinite.backfill && !this.previousResults.length) {
118
- if (!pageSize) {
119
- // pageSize is unknown - need to fetch meta to know defaultPageSize before we can continue
120
- const meta = await this.client.meta();
121
- pageSize = meta.pagination?.defaultPageSize;
122
- }
123
- // restricting pageSize to the limit
124
- pageSize = pageSize > API_LIMIT ? API_LIMIT : pageSize;
125
- const pagesNeeded = params.pagination?.page && params.pagination?.page > this.config.settings?.infinite.backfill
126
- ? this.config.settings?.infinite.backfill
127
- : params.pagination?.page;
128
- // figure out how many pages of results to backfill and wait on all responses
129
- const { size: backFillPageSize, pages: backFillPages } = backFillSize(pagesNeeded, pageSize);
130
- for (let i = 1; i <= backFillPages; i++) {
131
- const backfillParams = deepmerge({ ...params }, { pagination: { pageSize: backFillPageSize, page: i } });
132
- backfills.push(this.client.search(backfillParams));
133
- }
134
- }
135
- // infinite backfill results
136
- if (backfills && backfills.length) {
137
- // array to hold all results from backfill responses
138
- const backfillResults = [];
139
- const backfillResponses = await Promise.all(backfills);
140
- backfillResponses.map(([metaBackfill, responseBackfill]) => {
141
- if (!meta) {
142
- meta = metaBackfill;
143
- }
144
- if (!response) {
145
- response = responseBackfill;
146
- }
147
- // push results to array
148
- if (responseBackfill.results) {
149
- backfillResults.push(...responseBackfill.results);
114
+ // create requests for all missing pages (using Arrray(page).fill() to populate an array to map)
115
+ const backfillRequests = Array(params.pagination.page)
116
+ .fill('backfill')
117
+ .map((v, i) => {
118
+ const backfillParams = deepmerge({ ...params }, { pagination: { page: i + 1 }, search: { redirectResponse: 'full' } });
119
+ // don't include page parameter if on page 1
120
+ if (i + 1 == 1) {
121
+ delete backfillParams?.pagination?.page;
122
+ if (this.config.settings?.redirects?.merchandising) {
123
+ // redirect setting
124
+ // DUPLICATED LOGIC can be found in params getter
125
+ delete backfillParams?.search?.redirectResponse;
126
+ }
150
127
  }
128
+ return this.client.search(backfillParams);
151
129
  });
130
+ const backfillResponses = await Promise.all(backfillRequests);
131
+ // backfillResponses are [meta, searchResponse][]
132
+ // set the meta and response to the first page of backfillResponses
133
+ meta = backfillResponses[0][0];
134
+ response = backfillResponses[0][1];
135
+ // accumulate results from all backfill responses
136
+ const backfillResults = backfillResponses.reduce((results, response) => {
137
+ // response is [meta, searchResponse]
138
+ return results.concat(...response[1].results);
139
+ }, []);
152
140
  // overwrite pagination params to expected state
153
- response.pagination.pageSize = pageSize;
154
141
  response.pagination.totalPages = Math.ceil(response.pagination.totalResults / response.pagination.pageSize);
155
142
  response.pagination.page = params.pagination?.page;
156
143
  // set the response results with results from backfill responses
157
144
  response.results = backfillResults;
158
- if (!response.meta) {
159
- // @ts-ignore : MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
160
- response.meta = meta;
161
- }
162
145
  }
163
146
  else {
164
147
  // infinite with no backfills.
165
148
  [meta, response] = await this.client.search(params);
166
- // @ts-ignore : MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
167
- if (!response.meta) {
168
- // @ts-ignore : MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
169
- response.meta = meta;
170
- }
171
- //append new results to previous results
149
+ // append new results to previous results
172
150
  response.results = [...this.previousResults, ...(response.results || [])];
173
151
  }
174
152
  }
175
153
  else {
176
- //standard.
154
+ // standard request (not using infinite scroll)
177
155
  [meta, response] = await this.client.search(params);
178
- // @ts-ignore : MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
179
- if (!response.meta) {
180
- // @ts-ignore : MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
181
- response.meta = meta;
182
- }
156
+ }
157
+ // MockClient will overwrite the client search() method and use SearchData to return mock data which already contains meta data
158
+ if (!response.meta) {
159
+ response.meta = meta;
183
160
  }
184
161
  searchProfile.stop();
185
162
  this.log.profile(searchProfile);
@@ -406,6 +383,7 @@ export class SearchController extends AbstractController {
406
383
  get params() {
407
384
  const params = deepmerge({ ...getSearchParams(this.urlManager.state) }, this.config.globals || {});
408
385
  // redirect setting
386
+ // DUPLICATED LOGIC can be found in infinite backfill (change both if updating)
409
387
  if (!this.config.settings?.redirects?.merchandising || this.store.loaded) {
410
388
  params.search = params.search || {};
411
389
  params.search.redirectResponse = 'full';
@@ -486,10 +464,3 @@ export function generateHrefSelector(element, href, levels = 7) {
486
464
  }
487
465
  return;
488
466
  }
489
- function backFillSize(pages, pageSize) {
490
- const totalResults = pages * pageSize;
491
- let numPages = Math.ceil(totalResults / API_LIMIT);
492
- while (totalResults % numPages)
493
- numPages++;
494
- return { size: totalResults / numPages, pages: numPages };
495
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@searchspring/snap-controller",
3
- "version": "0.51.0",
3
+ "version": "0.51.2",
4
4
  "description": "Snap Controllers",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -20,22 +20,22 @@
20
20
  "test:watch": "jest --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@searchspring/snap-toolbox": "^0.51.0",
23
+ "@searchspring/snap-toolbox": "^0.51.2",
24
24
  "css.escape": "1.5.1",
25
25
  "deepmerge": "4.3.1"
26
26
  },
27
27
  "devDependencies": {
28
- "@searchspring/snap-client": "^0.51.0",
29
- "@searchspring/snap-event-manager": "^0.51.0",
30
- "@searchspring/snap-logger": "^0.51.0",
31
- "@searchspring/snap-profiler": "^0.51.0",
32
- "@searchspring/snap-store-mobx": "^0.51.0",
33
- "@searchspring/snap-tracker": "^0.51.0",
34
- "@searchspring/snap-url-manager": "^0.51.0"
28
+ "@searchspring/snap-client": "^0.51.2",
29
+ "@searchspring/snap-event-manager": "^0.51.2",
30
+ "@searchspring/snap-logger": "^0.51.2",
31
+ "@searchspring/snap-profiler": "^0.51.2",
32
+ "@searchspring/snap-store-mobx": "^0.51.2",
33
+ "@searchspring/snap-tracker": "^0.51.2",
34
+ "@searchspring/snap-url-manager": "^0.51.2"
35
35
  },
36
36
  "sideEffects": false,
37
37
  "files": [
38
38
  "dist/**/*"
39
39
  ],
40
- "gitHead": "3b5f7e35a4435b954ad6b0cbeab85e3c383ae4f1"
40
+ "gitHead": "333a7704666bb36a652222cc75a882c8fccd830f"
41
41
  }