-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication_signals.go
More file actions
111 lines (94 loc) · 3.17 KB
/
Copy pathapplication_signals.go
File metadata and controls
111 lines (94 loc) · 3.17 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2023 The Go-Curses Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cdk
import (
"context"
"github.com/urfave/cli/v2"
"github.com/go-curses/cdk/lib/enums"
"github.com/go-curses/cdk/lib/sync"
)
// TODO: need to go:generate WithArgv* wrappers from signals somehow, maybe
// the fn signature? better yet would be to find the Emit(signal,...)
// and determine both Argv* and WithArgv* funcs
type ApplicationMain func(ctx context.Context, cancel context.CancelFunc, wg *sync.WaitGroup)
type ApplicationRunFn = func(ctx *cli.Context) error
type ApplicationInitFn = func(app Application)
type ApplicationPrepareStartupFn = func(app Application, args []string) enums.EventFlag
type ApplicationStartupFn = func(
app Application,
display Display,
ctx context.Context,
cancel context.CancelFunc,
wg *sync.WaitGroup,
) enums.EventFlag
type ApplicationShutdownFn = func() enums.EventFlag
func WithArgvNoneWithFlagsSignal(fn func() enums.EventFlag) SignalListenerFn {
return func(_ []interface{}, _ ...interface{}) enums.EventFlag {
return fn()
}
}
func WithArgvNoneSignal(fn func(), eventFlag enums.EventFlag) SignalListenerFn {
return func(_ []interface{}, _ ...interface{}) enums.EventFlag {
fn()
return eventFlag
}
}
func WithArgvApplicationSignalPrepareStartup(fn ApplicationPrepareStartupFn) SignalListenerFn {
return func(_ []interface{}, argv ...interface{}) enums.EventFlag {
if app, args, ok := ArgvApplicationSignalPrepareStartup(argv...); ok {
return fn(app, args)
}
return enums.EVENT_STOP
}
}
func ArgvApplicationSignalPrepareStartup(argv ...interface{}) (app Application, args []string, ok bool) {
if ok = len(argv) == 2; ok {
if app, ok = argv[0].(Application); ok {
if args, ok = argv[1].([]string); ok {
return
}
}
app = nil
args = nil
}
return
}
func WithArgvApplicationSignalStartup(fn ApplicationStartupFn) SignalListenerFn {
return func(_ []interface{}, argv ...interface{}) enums.EventFlag {
if app, display, ctx, cancel, wg, ok := ArgvApplicationSignalStartup(argv...); ok {
return fn(app, display, ctx, cancel, wg)
}
return enums.EVENT_STOP
}
}
func ArgvApplicationSignalStartup(argv ...interface{}) (app Application, display Display, ctx context.Context, cancel context.CancelFunc, wg *sync.WaitGroup, ok bool) {
if len(argv) == 5 {
if app, ok = argv[0].(Application); ok {
if display, ok = argv[1].(Display); ok {
if ctx, ok = argv[2].(context.Context); ok {
if cancel, ok = argv[3].(context.CancelFunc); ok {
if wg, ok = argv[4].(*sync.WaitGroup); ok {
return
}
cancel = nil
}
ctx = nil
}
display = nil
}
app = nil
}
}
return
}