-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (46 loc) · 1.14 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"context"
"fmt"
"github.com/firasdarwish/ore"
)
func main() {
ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) Counter {
fmt.Println("NEWLY INITIALIZED FROM FUNC")
return &counter{}
}, "firas")
ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) Counter {
fmt.Println("NEWLY INITIALIZED FROM FUNC")
return &counter{}
}, "darwish")
ore.RegisterLazyCreator[Counter](ore.Singleton, &counter{})
var cc Counter
cc = &counter{}
ore.RegisterEagerSingleton[Counter](cc)
ctx := context.Background()
fmt.Println("STARTED ...")
c, ctx := ore.Get[Counter](ctx, "firas")
c.AddOne()
c.AddOne()
c, ctx = ore.Get[Counter](ctx, "darwish")
c.AddOne()
c.AddOne()
fmt.Printf("Total Count: %v", c.Total())
ore.RegisterLazyCreator[GenericCounter[uint]](ore.Scoped, &genCounter[uint]{})
gc, ctx := ore.Get[GenericCounter[uint]](ctx)
gc.Add(1)
gc.Add(1)
gc.Add(1)
gc.Add(1)
gc, ctx = ore.Get[GenericCounter[uint]](ctx)
gc.Add(1)
fmt.Println(gc.Total())
}
type Counter interface {
AddOne()
Total() int
}
type GenericCounter[T numeric] interface {
Add(num T)
Total() T
}