@jetit/publisher 1.6.2 → 1.7.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetit/publisher",
3
- "version": "1.6.2",
3
+ "version": "1.7.1",
4
4
  "type": "commonjs",
5
5
  "dependencies": {
6
6
  "@jetit/id": "0.0.11",
@@ -61,6 +61,11 @@ class ScheduledProcessor {
61
61
  const streamName = `${eventData.eventName}:${consumerGroup}`;
62
62
  transaction.xadd(streamName, '*', 'data', JSON.stringify(eventData));
63
63
  }
64
+ if (eventData.repeatInterval) {
65
+ const nextEventTime = currentTime + eventData.repeatInterval;
66
+ const nextEventString = JSON.stringify(Object.assign({}, eventData));
67
+ transaction.zadd('se', nextEventTime, nextEventString);
68
+ }
64
69
  transaction.publish(eventData.eventName, '');
65
70
  yield transaction.exec();
66
71
  }
@@ -52,6 +52,8 @@ export declare class Streams {
52
52
  *
53
53
  * @param scheduledTime - The Date object representing the future time when the event should be published.
54
54
  * @param eventData - The event data object, containing the event name and its associated data.
55
+ * @param uniquePerInstance - Have only one scheduled job per type
56
+ * @param repeatInterval - The duration for which to reschedule the job in ms
55
57
  *
56
58
  * @throws Error - Throws an error if the scheduled time is in the past.
57
59
  *
@@ -67,7 +69,7 @@ export declare class Streams {
67
69
  *
68
70
  * await streams.scheduledPublish(futureTime, eventData);
69
71
  */
70
- scheduledPublish<T, const TName extends string = string>(scheduledTime: Date, eventData: EventData<T, TName>, uniquePerInstance?: boolean): Promise<void>;
72
+ scheduledPublish<TData = unknown, TName extends string = string>(scheduledTime: Date, eventData: PublishData<TData, TName>, uniquePerInstance?: boolean, repeatInterval?: number): Promise<void>;
71
73
  /**
72
74
  * Listens for events with the given name and returns an Observable that emits an EventData<T> object
73
75
  * each time a new event is received.
@@ -118,30 +118,13 @@ class Streams {
118
118
  }
119
119
  });
120
120
  }
121
- /**
122
- * Schedules an event to be published at a specified future time. Thee event gets published if the
123
- * differnece between the current time and the scheduled time is less than 500ms.
124
- *
125
- * @param scheduledTime - The Date object representing the future time when the event should be published.
126
- * @param eventData - The event data object, containing the event name and its associated data.
127
- *
128
- * @throws Error - Throws an error if the scheduled time is in the past.
129
- *
130
- * @example
131
- *
132
- * const streams = new Streams('app-service');
133
- *
134
- * const futureTime = new Date(Date.now() + 10000); // 10 seconds from now
135
- * const eventData: EventData<string> = {
136
- * eventName: 'order.created',
137
- * data: 'Order data'
138
- * };
139
- *
140
- * await streams.scheduledPublish(futureTime, eventData);
141
- */
142
- scheduledPublish(scheduledTime, eventData, uniquePerInstance = false) {
121
+ scheduledPublish(scheduledTime, eventData, uniquePerInstance = false, repeatInterval = 0) {
143
122
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
144
123
  const currentTime = new Date();
124
+ delete eventData.repeatInterval;
125
+ if (repeatInterval > 0) {
126
+ eventData.repeatInterval = repeatInterval;
127
+ }
145
128
  if (scheduledTime < currentTime) {
146
129
  throw new Error('PUBLISHER: Cannot schedule an event in the past');
147
130
  }
@@ -4,6 +4,7 @@ export type EventData<TData, TName extends string> = {
4
4
  eventId?: string;
5
5
  createdAt?: number;
6
6
  republishEvent?: string;
7
+ repeatInterval?: number;
7
8
  };
8
9
  export type PublishData<TData, TName extends string> = {
9
10
  data: TData;