1
- import { IStorage } from "../storage" ;
2
- import { DurablePromise } from "../promise" ;
1
+ import { IPromiseStorage , IScheduleStorage } from "../storage" ;
2
+ import { DurablePromise , searchStates } from "../promise" ;
3
+ import { Schedule } from "../schedule" ;
3
4
4
- export class MemoryStorage implements IStorage {
5
+ export class MemoryPromiseStorage implements IPromiseStorage {
5
6
private promises : Record < string , DurablePromise > = { } ;
6
-
7
7
constructor ( ) { }
8
8
9
- async rmw < P extends DurablePromise | undefined > (
10
- id : string ,
11
- f : ( promise : DurablePromise | undefined ) => P ,
12
- ) : Promise < P > {
13
- const promise = f ( this . promises [ id ] ) ;
14
- if ( promise ) {
15
- this . promises [ id ] = promise ;
9
+ async rmw < P extends DurablePromise | undefined > ( id : string , f : ( item : DurablePromise | undefined ) => P ) : Promise < P > {
10
+ const item = f ( this . promises [ id ] ) ;
11
+ if ( item ) {
12
+ this . promises [ id ] = item ;
16
13
}
17
14
18
- return promise ;
15
+ return item ;
19
16
}
20
17
21
18
async * search (
@@ -24,8 +21,53 @@ export class MemoryStorage implements IStorage {
24
21
tags : Record < string , string > | undefined ,
25
22
limit : number | undefined ,
26
23
) : AsyncGenerator < DurablePromise [ ] , void > {
27
- // for now WithTimeout will implement
28
- // search logic
29
- yield Object . values ( this . promises ) ;
24
+ // for the memory storage, we will ignore limit and return all
25
+ // promises that match the search criteria
26
+ const regex = new RegExp ( id . replaceAll ( "*" , ".*" ) ) ;
27
+ const states = searchStates ( state ) ;
28
+ const tagEntries = Object . entries ( tags ?? { } ) ;
29
+
30
+ yield Object . values ( this . promises )
31
+ . filter ( ( promise ) => states . includes ( promise . state ) )
32
+ . filter ( ( promise ) => regex . test ( promise . id ) )
33
+ . filter ( ( promise ) => tagEntries . every ( ( [ k , v ] ) => promise . tags ?. [ k ] == v ) ) ;
34
+ }
35
+ }
36
+
37
+ export class MemoryScheduleStorage implements IScheduleStorage {
38
+ private schedules : Record < string , Schedule > = { } ;
39
+
40
+ constructor ( ) { }
41
+
42
+ async rmw < S extends Schedule | undefined > ( id : string , f : ( item : Schedule | undefined ) => S ) : Promise < S > {
43
+ const item = f ( this . schedules [ id ] ) ;
44
+ if ( item ) {
45
+ this . schedules [ id ] = item ;
46
+ }
47
+
48
+ return item ;
49
+ }
50
+
51
+ async * search (
52
+ id : string ,
53
+ tags : Record < string , string > | undefined ,
54
+ limit : number | undefined ,
55
+ ) : AsyncGenerator < Schedule [ ] , void > {
56
+ // for the memory storage, we will ignore limit and return all
57
+ // schedules that match the search criteria
58
+ const regex = new RegExp ( id . replaceAll ( "*" , ".*" ) ) ;
59
+ const tagEntries = Object . entries ( tags ?? { } ) ;
60
+
61
+ yield Object . values ( this . schedules )
62
+ . filter ( ( schedule ) => regex . test ( schedule . id ) )
63
+ . filter ( ( schedule ) => tagEntries . every ( ( [ k , v ] ) => schedule . tags ?. [ k ] == v ) ) ;
64
+ }
65
+
66
+ async delete ( id : string ) : Promise < boolean > {
67
+ if ( this . schedules [ id ] ) {
68
+ delete this . schedules [ id ] ;
69
+ return true ;
70
+ }
71
+ return false ;
30
72
}
31
73
}
0 commit comments