@unchainedshop/core-worker 2.4.2 → 2.5.0

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,6 +1,16 @@
1
1
  import { WorkerEventTypes } from '../director/WorkerEventTypes.js';
2
2
  import { WorkerDirector } from '../director/WorkerDirector.js';
3
3
  import { BaseWorker } from './BaseWorker.js';
4
+ function debounce(func, wait) {
5
+ let timeout;
6
+ return (...args) => {
7
+ clearTimeout(timeout);
8
+ timeout = setTimeout(() => {
9
+ timeout = null;
10
+ func(...args);
11
+ }, wait);
12
+ };
13
+ }
4
14
  export const EventListenerWorker = {
5
15
  ...BaseWorker,
6
16
  key: 'shop.unchained.worker.event-listener',
@@ -8,26 +18,19 @@ export const EventListenerWorker = {
8
18
  version: '1.0.0',
9
19
  type: 'EVENT_LISTENER',
10
20
  actions: ({ workerId }, unchainedAPI) => {
11
- let onAdded;
12
- let onFinished;
13
21
  const baseWorkerActions = BaseWorker.actions({ workerId, worker: EventListenerWorker }, unchainedAPI);
22
+ // Debounce in the event of many work queue events conflicting with each other
23
+ const processWorkQueue = debounce(async () => {
24
+ await baseWorkerActions.process({
25
+ maxWorkItemCount: 1,
26
+ referenceDate: EventListenerWorker.getFloorDate(),
27
+ });
28
+ }, 300);
14
29
  return {
15
30
  ...baseWorkerActions,
16
31
  start() {
17
- onAdded = async () => {
18
- await baseWorkerActions.process({
19
- maxWorkItemCount: 0,
20
- referenceDate: EventListenerWorker.getFloorDate(),
21
- });
22
- };
23
- onFinished = async () => {
24
- await baseWorkerActions.process({
25
- maxWorkItemCount: 0,
26
- referenceDate: EventListenerWorker.getFloorDate(),
27
- });
28
- };
29
- WorkerDirector.events.on(WorkerEventTypes.ADDED, onAdded);
30
- WorkerDirector.events.on(WorkerEventTypes.FINISHED, onFinished);
32
+ WorkerDirector.events.on(WorkerEventTypes.ADDED, processWorkQueue);
33
+ WorkerDirector.events.on(WorkerEventTypes.FINISHED, processWorkQueue);
31
34
  setTimeout(async () => {
32
35
  await baseWorkerActions.autorescheduleTypes({
33
36
  referenceDate: EventListenerWorker.getFloorDate(),
@@ -35,8 +38,8 @@ export const EventListenerWorker = {
35
38
  }, 300);
36
39
  },
37
40
  stop() {
38
- WorkerDirector.events.off(WorkerEventTypes.ADDED, onAdded);
39
- WorkerDirector.events.off(WorkerEventTypes.FINISHED, onFinished);
41
+ WorkerDirector.events.off(WorkerEventTypes.ADDED, processWorkQueue);
42
+ WorkerDirector.events.off(WorkerEventTypes.FINISHED, processWorkQueue);
40
43
  },
41
44
  };
42
45
  },
@@ -1 +1 @@
1
- {"version":3,"file":"EventListenerWorker.js","sourceRoot":"","sources":["../../src/workers/EventListenerWorker.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAM7C,MAAM,CAAC,MAAM,mBAAmB,GAAuC;IACrE,GAAG,UAAU;IAEb,GAAG,EAAE,sCAAsC;IAC3C,KAAK,EAAE,mFAAmF;IAC1F,OAAO,EAAE,OAAO;IAChB,IAAI,EAAE,gBAAgB;IAEtB,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,EAAE,YAAY,EAAE,EAAE;QACtC,IAAI,OAA4B,CAAC;QACjC,IAAI,UAA+B,CAAC;QAEpC,MAAM,iBAAiB,GAAG,UAAU,CAAC,OAAO,CAC1C,EAAE,QAAQ,EAAE,MAAM,EAAE,mBAAmB,EAAE,EACzC,YAAY,CACb,CAAC;QACF,OAAO;YACL,GAAG,iBAAiB;YAEpB,KAAK;gBACH,OAAO,GAAG,KAAK,IAAI,EAAE;oBACnB,MAAM,iBAAiB,CAAC,OAAO,CAAC;wBAC9B,gBAAgB,EAAE,CAAC;wBACnB,aAAa,EAAE,mBAAmB,CAAC,YAAY,EAAE;qBAClD,CAAC,CAAC;g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E,gBAAgB,CAAC,CAAC;gBACpE,cAAc,CAAC,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC;YACzE,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unchainedshop/core-worker",
3
- "version": "2.4.2",
3
+ "version": "2.5.0",
4
4
  "main": "lib/worker-index.js",
5
5
  "exports": {
6
6
  ".": "./lib/worker-index.js",
@@ -32,13 +32,13 @@
32
32
  "homepage": "https://github.com/unchainedshop/unchained#readme",
33
33
  "dependencies": {
34
34
  "@breejs/later": "^4.1.0",
35
- "@unchainedshop/logger": "^2.4.2",
36
- "@unchainedshop/utils": "^2.4.2",
35
+ "@unchainedshop/logger": "^2.5.0",
36
+ "@unchainedshop/utils": "^2.5.0",
37
37
  "simpl-schema": "=3.2.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@types/node": "^18.17.14",
41
- "@unchainedshop/types": "^2.4.2",
40
+ "@types/node": "^18.17.15",
41
+ "@unchainedshop/types": "^2.5.0",
42
42
  "jest": "^29.6.4",
43
43
  "ts-jest": "^29.1.1",
44
44
  "typescript": "^5.2.2"
@@ -3,6 +3,17 @@ import { WorkerEventTypes } from '../director/WorkerEventTypes.js';
3
3
  import { WorkerDirector } from '../director/WorkerDirector.js';
4
4
  import { BaseWorker } from './BaseWorker.js';
5
5
 
6
+ function debounce<T extends (...args: any) => any>(func: T, wait) {
7
+ let timeout;
8
+ return (...args) => {
9
+ clearTimeout(timeout);
10
+ timeout = setTimeout(() => {
11
+ timeout = null;
12
+ func(...args);
13
+ }, wait);
14
+ };
15
+ }
16
+
6
17
  export interface EventListenerWorkerParams {
7
18
  workerId: string;
8
19
  }
@@ -16,32 +27,25 @@ export const EventListenerWorker: IWorker<EventListenerWorkerParams> = {
16
27
  type: 'EVENT_LISTENER',
17
28
 
18
29
  actions: ({ workerId }, unchainedAPI) => {
19
- let onAdded: () => Promise<void>;
20
- let onFinished: () => Promise<void>;
21
-
22
30
  const baseWorkerActions = BaseWorker.actions(
23
31
  { workerId, worker: EventListenerWorker },
24
32
  unchainedAPI,
25
33
  );
34
+
35
+ // Debounce in the event of many work queue events conflicting with each other
36
+ const processWorkQueue = debounce<() => Promise<void>>(async () => {
37
+ await baseWorkerActions.process({
38
+ maxWorkItemCount: 1, // only one work item at a time, else we could end up in a loop
39
+ referenceDate: EventListenerWorker.getFloorDate(),
40
+ });
41
+ }, 300);
42
+
26
43
  return {
27
44
  ...baseWorkerActions,
28
45
 
29
46
  start() {
30
- onAdded = async () => {
31
- await baseWorkerActions.process({
32
- maxWorkItemCount: 0,
33
- referenceDate: EventListenerWorker.getFloorDate(),
34
- });
35
- };
36
- onFinished = async () => {
37
- await baseWorkerActions.process({
38
- maxWorkItemCount: 0,
39
- referenceDate: EventListenerWorker.getFloorDate(),
40
- });
41
- };
42
-
43
- WorkerDirector.events.on(WorkerEventTypes.ADDED, onAdded);
44
- WorkerDirector.events.on(WorkerEventTypes.FINISHED, onFinished);
47
+ WorkerDirector.events.on(WorkerEventTypes.ADDED, processWorkQueue);
48
+ WorkerDirector.events.on(WorkerEventTypes.FINISHED, processWorkQueue);
45
49
 
46
50
  setTimeout(async () => {
47
51
  await baseWorkerActions.autorescheduleTypes({
@@ -51,8 +55,8 @@ export const EventListenerWorker: IWorker<EventListenerWorkerParams> = {
51
55
  },
52
56
 
53
57
  stop() {
54
- WorkerDirector.events.off(WorkerEventTypes.ADDED, onAdded);
55
- WorkerDirector.events.off(WorkerEventTypes.FINISHED, onFinished);
58
+ WorkerDirector.events.off(WorkerEventTypes.ADDED, processWorkQueue);
59
+ WorkerDirector.events.off(WorkerEventTypes.FINISHED, processWorkQueue);
56
60
  },
57
61
  };
58
62
  },