-
Notifications
You must be signed in to change notification settings - Fork 41
add the ability to de-register a task #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
resolves #96 |
4e05ee6
to
821c959
Compare
motivation: sometimes a task needs to be de-registered since the task was manually shutdown outside the lifecycle scope changes: * registration APIs now return a registration key which can be used as a cancellation token * add API to de-register a task * refactor state to use a registery instead of array of tasks * add tests
821c959
to
6b88ef1
Compare
Sources/Lifecycle/Lifecycle.swift
Outdated
|
||
func add(_ tasks: [LifecycleTask]) -> [RegistrationKey] { | ||
// FIXME: better id generation scheme (cant use UUID) | ||
let random = UInt64.random(in: UInt64.min ..< UInt64.max).addingReportingOverflow(DispatchTime.now().uptimeNanoseconds).partialValue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better ideas welcome. we cant use UUID here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worth checking for existing keys to make sure there are no conflicts?
Sources/Lifecycle/Lifecycle.swift
Outdated
|
||
func add(_ tasks: [LifecycleTask]) -> [RegistrationKey] { | ||
// FIXME: better id generation scheme (cant use UUID) | ||
let random = UInt64.random(in: UInt64.min ..< UInt64.max).addingReportingOverflow(DispatchTime.now().uptimeNanoseconds).partialValue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worth checking for existing keys to make sure there are no conflicts?
Co-authored-by: Yim Lee <yim_lee@apple.com>
Co-authored-by: Yim Lee <yim_lee@apple.com>
/// - tasks: one or more `LifecycleTask`. | ||
@discardableResult | ||
public func register(_ tasks: LifecycleTask ...) -> [RegistrationKey] { | ||
return self.register(tasks) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return self.register(tasks) | |
self.register(tasks) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still supporting swift 5.0 :D
self.register(tasks) | ||
@discardableResult | ||
public func register(_ tasks: LifecycleTask) -> RegistrationKey { | ||
return self.register(tasks).first! // force the optional on the first in this case is safe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return self.register(tasks).first! // force the optional on the first in this case is safe | |
self.register(tasks).first! // force the optional on the first in this case is safe |
self.register(_LifecycleTask(label: label, shutdownIfNotStarted: shutdownIfNotStarted, start: start, shutdown: shutdown)) | ||
@discardableResult | ||
public func register(label: String, start: LifecycleHandler, shutdown: LifecycleHandler, shutdownIfNotStarted: Bool? = nil) -> RegistrationKey { | ||
return self.register(_LifecycleTask(label: label, shutdownIfNotStarted: shutdownIfNotStarted, start: start, shutdown: shutdown)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return self.register(_LifecycleTask(label: label, shutdownIfNotStarted: shutdownIfNotStarted, start: start, shutdown: shutdown)) | |
self.register(_LifecycleTask(label: label, shutdownIfNotStarted: shutdownIfNotStarted, start: start, shutdown: shutdown)) |
self.register(label: label, start: .none, shutdown: handler) | ||
@discardableResult | ||
public func registerShutdown(label: String, _ handler: LifecycleHandler) -> RegistrationKey { | ||
return self.register(label: label, start: .none, shutdown: handler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return self.register(label: label, start: .none, shutdown: handler) | |
self.register(label: label, start: .none, shutdown: handler) |
self.register(StatefulLifecycleTask(label: label, start: start, shutdown: shutdown)) | ||
@discardableResult | ||
public func registerStateful<State>(label: String, start: LifecycleStartHandler<State>, shutdown: LifecycleShutdownHandler<State>) -> RegistrationKey { | ||
return self.register(StatefulLifecycleTask(label: label, start: start, shutdown: shutdown)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return self.register(StatefulLifecycleTask(label: label, start: start, shutdown: shutdown)) | |
self.register(StatefulLifecycleTask(label: label, start: start, shutdown: shutdown)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thank you!
motivation: sometimes a task needs to be de-registered since the task was manually shutdown outside the lifecycle scope
changes: