-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_map.go
42 lines (37 loc) · 1.07 KB
/
new_map.go
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
package promise
import (
"context"
"fmt"
"time"
)
func NewMap[T any](m map[interface{}]interface{}, timeOutLimit time.Duration) FutureMap[T] {
sizeOfMap := len(m)
if sizeOfMap == 0 {
panic(unexpectedCaseError("empty map"))
}
fulfilmentChannel := make(chan Output[T], sizeOfMap)
ctx := context.Background()
var cancelContext context.Context
var cancel context.CancelFunc
if timeOutLimit == 0 {
cancelContext, cancel = context.WithCancel(ctx)
} else {
cancelContext, cancel = context.WithTimeout(ctx, timeOutLimit)
}
fm := make(futureMap[T])
for k, ff := range m {
switch v := ff.(type) {
case Future[T]:
fm[k] = v.setKey(k).setFulfilmentChannel(fulfilmentChannel).setContext(cancelContext, cancel)
case func() (T, error):
fm[k] = New(v).TimeOutLimit(timeOutLimit).setKey(k).setFulfilmentChannel(fulfilmentChannel).setContext(cancelContext, cancel)
case Progress[T]:
cancel()
panic(proprietyError("committed promise can not be mapped"))
default:
cancel()
panic(unexpectedCaseError(fmt.Sprintf("unexpected type %T", v)))
}
}
return &fm
}