gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/lifecycle/options.go (about) 1 package lifecycle 2 3 type Option interface { 4 Apply(lifecycle Lifecycle) 5 } 6 7 type optionFunc func(lifecycle Lifecycle) 8 9 func (f optionFunc) Apply(lifecycle Lifecycle) { 10 f(lifecycle) 11 } 12 13 func WithSelf(self any) Option { 14 return optionFunc(func(lifecycle Lifecycle) { 15 if setter, is := lifecycle.(interface{ setSelf(self any) }); is { 16 setter.setSelf(self) 17 } 18 }) 19 } 20 21 func WithRunner(runner Runner) Option { 22 return optionFunc(func(lifecycle Lifecycle) { 23 if setter, is := lifecycle.(interface{ setRunner(runner Runner) }); is { 24 setter.setRunner(runner) 25 } 26 }) 27 } 28 29 func WithInterruptedRunner(runner InterruptedRunner) Option { 30 return optionFunc(func(lifecycle Lifecycle) { 31 if canInterrupted, is := lifecycle.(canInterrupted); is { 32 canInterrupted.setRunner(newInterrupterRunner(canInterrupted, runner)) 33 } 34 }) 35 } 36 37 func WithStarter(starter Starter) Option { 38 return optionFunc(func(lifecycle Lifecycle) { 39 if setter, is := lifecycle.(interface{ setRunner(runner Runner) }); is { 40 setter.setRunner(newStarterRunner(setter, starter).Runner()) 41 } 42 }) 43 } 44 45 func WithInterruptedStarter(starter InterruptedStarter) Option { 46 return optionFunc(func(lifecycle Lifecycle) { 47 if canInterrupted, is := lifecycle.(canInterrupted); is { 48 canInterrupted.setRunner(newInterruptedStarter(canInterrupted, starter).Runner()) 49 } 50 }) 51 }