-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Create buffering logic for DefaultSearchPlugin
Relates to #1137
- Loading branch information
1 parent
d6aa20f
commit 6a47dcf
Showing
9 changed files
with
154 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/core/src/plugin/default-search-plugin/collection-job-buffer-processor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ID } from '@vendure/common/lib/shared-types'; | ||
import { unique } from '@vendure/common/lib/unique'; | ||
|
||
import { Job, JobBufferProcessor } from '../../job-queue'; | ||
import { ApplyCollectionFiltersJobData } from '../../service/services/collection.service'; | ||
|
||
import { UpdateIndexQueueJobData, UpdateVariantsByIdJobData, UpdateVariantsJobData } from './types'; | ||
|
||
export class CollectionJobBufferProcessor implements JobBufferProcessor<ApplyCollectionFiltersJobData> { | ||
readonly id = 'search-plugin-apply-collection-filters'; | ||
|
||
collect(job: Job): boolean { | ||
return job.queueName === 'apply-collection-filters'; | ||
} | ||
|
||
reduce(collectedJobs: Array<Job<ApplyCollectionFiltersJobData>>): Array<Job<any>> { | ||
const collectionIdsToUpdate = collectedJobs.reduce((result, job) => { | ||
return [...result, ...job.data.collectionIds]; | ||
}, [] as ID[]); | ||
|
||
const referenceJob = collectedJobs[0]; | ||
const batchedCollectionJob = new Job<ApplyCollectionFiltersJobData>({ | ||
...referenceJob, | ||
id: undefined, | ||
data: { | ||
collectionIds: unique(collectionIdsToUpdate), | ||
ctx: referenceJob.data.ctx, | ||
applyToChangedVariantsOnly: referenceJob.data.applyToChangedVariantsOnly, | ||
}, | ||
}); | ||
|
||
return [batchedCollectionJob]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/core/src/plugin/default-search-plugin/search-job-buffer-processor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ID } from '@vendure/common/lib/shared-types'; | ||
import { unique } from '@vendure/common/lib/unique'; | ||
|
||
import { Job, JobBufferProcessor } from '../../job-queue'; | ||
|
||
import { UpdateIndexQueueJobData, UpdateVariantsByIdJobData, UpdateVariantsJobData } from './types'; | ||
|
||
export class SearchJobBufferProcessor implements JobBufferProcessor<UpdateIndexQueueJobData> { | ||
readonly id = 'search-plugin-update-search-index'; | ||
|
||
collect(job: Job<UpdateIndexQueueJobData>): boolean | Promise<boolean> { | ||
return job.queueName === 'update-search-index'; | ||
} | ||
|
||
reduce(collectedJobs: Array<Job<UpdateIndexQueueJobData>>): Array<Job<any>> { | ||
const variantsByIdJobs = this.removeBy<Job<UpdateVariantsByIdJobData | UpdateVariantsJobData>>( | ||
collectedJobs, | ||
item => item.data.type === 'update-variants-by-id' || item.data.type === 'update-variants', | ||
); | ||
|
||
const jobsToAdd = [...collectedJobs]; | ||
|
||
if (variantsByIdJobs.length) { | ||
const variantIdsToUpdate = variantsByIdJobs.reduce((result, job) => { | ||
const ids = job.data.type === 'update-variants-by-id' ? job.data.ids : job.data.variantIds; | ||
return [...result, ...ids]; | ||
}, [] as ID[]); | ||
|
||
const referenceJob = variantsByIdJobs[0]; | ||
const batchedVariantJob = new Job<UpdateVariantsByIdJobData>({ | ||
...referenceJob, | ||
id: undefined, | ||
data: { | ||
type: 'update-variants-by-id', | ||
ids: unique(variantIdsToUpdate), | ||
ctx: referenceJob.data.ctx, | ||
}, | ||
}); | ||
|
||
jobsToAdd.push(batchedVariantJob as any); | ||
} | ||
|
||
return jobsToAdd; | ||
} | ||
|
||
/** | ||
* Removes items from the array based on the filterFn and returns a new array with only the removed | ||
* items. The original input array is mutated. | ||
*/ | ||
private removeBy<R extends T, T = any>(input: T[], filterFn: (item: T) => boolean): R[] { | ||
const removed: R[] = []; | ||
for (let i = input.length - 1; i >= 0; i--) { | ||
const item = input[i]; | ||
if (filterFn(item)) { | ||
removed.push(item as R); | ||
input.splice(i, 1); | ||
} | ||
} | ||
return removed; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters