-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathIEventLocker.ts
More file actions
48 lines (42 loc) · 1.41 KB
/
IEventLocker.ts
File metadata and controls
48 lines (42 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type { IEvent } from './IEvent.ts';
import { isObject } from './isObject.ts';
/**
* Interface for tracking event processing state to prevent concurrent processing
* by multiple processes.
*/
export interface IEventLocker {
/**
* Retrieves the last projected event,
* allowing the projection state to be restored from subsequent events.
*/
getLastEvent(): Promise<IEvent | undefined> | IEvent | undefined;
/**
* Marks an event as projecting to prevent it from being processed
* by another projection instance using the same storage.
*
* @returns `false` if the event is already being processed or has been processed.
*/
tryMarkAsProjecting(event: IEvent): Promise<boolean> | boolean;
/**
* Marks an event as projected.
*/
markAsProjected(event: IEvent): Promise<void> | void;
/**
* Records an event as the last projected event (restore checkpoint).
*/
markAsLastEvent(event: IEvent): Promise<void> | void;
}
export const isEventLocker = (view: unknown): view is IEventLocker =>
(
isObject(view)
&& 'getLastEvent' in view
&& 'tryMarkAsProjecting' in view
&& 'markAsProjected' in view
&& 'markAsLastEvent' in view
) || (
typeof view === 'function'
&& typeof (view as any).getLastEvent === 'function'
&& typeof (view as any).tryMarkAsProjecting === 'function'
&& typeof (view as any).markAsProjected === 'function'
&& typeof (view as any).markAsLastEvent === 'function'
);