@@ -212,98 +212,16 @@ public class Lifecycle {
212
212
213
213
extension Lifecycle {
214
214
internal struct Item : LifecycleItem {
215
- public let name : String
216
- private let _start : FunctionWithCallback ?
217
- private let _shutdown : FunctionWithCallback
218
-
219
- init ( name: String , start: FunctionWithCallback ? , shutdown: @escaping FunctionWithCallback ) {
220
- self . name = name
221
- self . _start = start
222
- self . _shutdown = shutdown
223
- }
224
-
225
- init ( name: String , start: FunctionWithCallback ? , shutdown: @escaping ThrowingFunction ) {
226
- self . name = name
227
- self . _start = start
228
- self . _shutdown = Item . callback ( shutdown)
229
- }
230
-
231
- init < Future, Value> ( name: String , start: FunctionWithCallback ? , shutdown: @escaping FutureProvider < Future , Value > ) {
232
- self . name = name
233
- self . _start = start
234
- self . _shutdown = Item . callback ( shutdown)
235
- }
236
-
237
- init ( name: String , start: ThrowingFunction ? , shutdown: @escaping FunctionWithCallback ) {
238
- self . name = name
239
- self . _start = start. map ( Item . callback)
240
- self . _shutdown = shutdown
241
- }
242
-
243
- init ( name: String , start: ThrowingFunction ? , shutdown: @escaping ThrowingFunction ) {
244
- self . name = name
245
- self . _start = start. map ( Item . callback)
246
- self . _shutdown = Item . callback ( shutdown)
247
- }
248
-
249
- init < Future, Value> ( name: String , start: ThrowingFunction ? , shutdown: @escaping FutureProvider < Future , Value > ) {
250
- self . name = name
251
- self . _start = start. map ( Item . callback)
252
- self . _shutdown = Item . callback ( shutdown)
253
- }
254
-
255
- init < Future, Value> ( name: String , start: FutureProvider < Future , Value > ? , shutdown: @escaping FunctionWithCallback ) {
256
- self . name = name
257
- self . _start = start. map ( Item . callback)
258
- self . _shutdown = shutdown
259
- }
260
-
261
- init < Future, Value> ( name: String , start: FutureProvider < Future , Value > ? , shutdown: @escaping ThrowingFunction ) {
262
- self . name = name
263
- self . _start = start. map ( Item . callback)
264
- self . _shutdown = Item . callback ( shutdown)
265
- }
266
-
267
- init < Future, Value> ( name: String , start: FutureProvider < Future , Value > ? , shutdown: @escaping FutureProvider < Future , Value > ) {
268
- self . name = name
269
- self . _start = start. map ( Item . callback)
270
- self . _shutdown = Item . callback ( shutdown)
271
- }
215
+ let name : String
216
+ let start : Handler
217
+ let shutdown : Handler
272
218
273
219
func start( callback: @escaping ( Error ? ) -> Void ) {
274
- if let body = self . _start {
275
- body ( callback)
276
- } else {
277
- callback ( nil )
278
- }
220
+ self . start. run ( callback)
279
221
}
280
222
281
223
func shutdown( callback: @escaping ( Error ? ) -> Void ) {
282
- self . _shutdown ( callback)
283
- }
284
-
285
- private static func callback( _ body: @escaping ThrowingFunction ) -> FunctionWithCallback {
286
- return { callback in
287
- do {
288
- try body ( )
289
- callback ( nil )
290
- } catch {
291
- callback ( error)
292
- }
293
- }
294
- }
295
-
296
- private static func callback< Future, Value> ( _ future: @escaping FutureProvider < Future , Value > ) -> FunctionWithCallback {
297
- return { callback in
298
- future ( ) . whenComplete { result in
299
- switch result {
300
- case . success:
301
- callback ( nil )
302
- case . failure( let error) :
303
- callback ( error)
304
- }
305
- }
306
- }
224
+ self . shutdown. run ( callback)
307
225
}
308
226
}
309
227
}
@@ -334,23 +252,23 @@ public extension Lifecycle {
334
252
///
335
253
/// - parameters:
336
254
/// - items: one or more `LifecycleItem`.
337
- internal func append ( _ items: LifecycleItem ... ) {
255
+ func register ( _ items: [ LifecycleItem ] ) {
338
256
self . stateSemaphore. lock {
339
257
guard case . idle = self . state else {
340
258
preconditionFailure ( " invalid state, \( self . state) " )
341
259
}
342
260
}
343
261
self . itemsSemaphore. lock {
344
- items . forEach { self . items. append ( $0 ) }
262
+ self . items. append ( contentsOf : items )
345
263
}
346
264
}
347
265
348
266
/// Adds a `LifecycleItem` to a `LifecycleItems` collection.
349
267
///
350
268
/// - parameters:
351
269
/// - items: one or more `LifecycleItem`.
352
- func append ( _ items: [ LifecycleItem ] ) {
353
- items . forEach { self . append ( $0 ) }
270
+ internal func register ( _ items: LifecycleItem ... ) {
271
+ self . register ( items )
354
272
}
355
273
356
274
/// Add a `LifecycleItem` to a `LifecycleItems` collection.
@@ -359,128 +277,86 @@ public extension Lifecycle {
359
277
/// - name: name of the item, useful for debugging.
360
278
/// - start: closure to perform the startup.
361
279
/// - shutdown: closure to perform the shutdown.
362
- func append( name: String , start: @escaping FunctionWithCallback , shutdown: @escaping FunctionWithCallback ) {
363
- self . append ( Item ( name: name, start: start, shutdown: shutdown) )
364
- }
365
-
366
- /// Adds a `LifecycleItem` to a `LifecycleItems` collection.
367
- ///
368
- /// - parameters:
369
- /// - name: name of the item, useful for debugging.
370
- /// - shutdown: closure to perform the shutdown.
371
- func append( name: String , shutdown: @escaping FunctionWithCallback ) {
372
- self . append ( Item ( name: name, start: nil as FunctionWithCallback ? , shutdown: shutdown) )
373
- }
374
-
375
- /// Adds a `LifecycleItem` to a `LifecycleItems` collection.
376
- ///
377
- /// - parameters:
378
- /// - name: name of the item, useful for debugging.
379
- /// - start: closure to perform the startup.
380
- /// - shutdown: closure to perform the shutdown.
381
- func append( name: String , start: @escaping FunctionWithCallback , shutdown: @escaping ThrowingFunction ) {
382
- self . append ( Item ( name: name, start: start, shutdown: shutdown) )
383
- }
384
-
385
- /// Adds a `LifecycleItem` to a `LifecycleItems` collection.
386
- ///
387
- /// - parameters:
388
- /// - name: name of the item, useful for debugging.
389
- /// - shutdown: closure to perform the shutdown.
390
- func append( name: String , shutdown: @escaping ThrowingFunction ) {
391
- self . append ( Item ( name: name, start: nil as ThrowingFunction ? , shutdown: shutdown) )
280
+ func register( name: String , start: Handler , shutdown: Handler ) {
281
+ self . register ( Item ( name: name, start: start, shutdown: shutdown) )
392
282
}
393
283
394
284
/// Adds a `LifecycleItem` to a `LifecycleItems` collection.
395
285
///
396
286
/// - parameters:
397
287
/// - name: name of the item, useful for debugging.
398
- /// - start: closure to perform the startup.
399
288
/// - shutdown: closure to perform the shutdown.
400
- func append < Future , Value > ( name: String , start : @escaping FunctionWithCallback , shutdown : @escaping FutureProvider < Future , Value > ) {
401
- self . append ( Item ( name: name, start: start , shutdown: shutdown ) )
289
+ func registerShutdown ( name: String , _ handler : Handler ) {
290
+ self . register ( name: name, start: . none , shutdown: handler )
402
291
}
403
292
404
293
/// Adds a `LifecycleItem` to a `LifecycleItems` collection.
405
294
///
406
295
/// - parameters:
407
296
/// - name: name of the item, useful for debugging.
297
+ /// - start: closure to perform the shutdown.
408
298
/// - shutdown: closure to perform the shutdown.
409
- func append < Future , Value > ( name: String , shutdown : @escaping FutureProvider < Future , Value > ) {
410
- self . append ( Item ( name: name, start: nil as FutureProvider < Future , Value > ? , shutdown: shutdown) )
299
+ func register ( name: String , start : @escaping ( ) throws -> Void , shutdown : @escaping ( ) throws -> Void ) {
300
+ self . register ( name: name, start: . sync ( start ) , shutdown: . sync ( shutdown) )
411
301
}
412
302
413
303
/// Adds a `LifecycleItem` to a `LifecycleItems` collection.
414
304
///
415
305
/// - parameters:
416
306
/// - name: name of the item, useful for debugging.
417
- /// - start: closure to perform the startup.
418
307
/// - shutdown: closure to perform the shutdown.
419
- func append( name: String , start: @escaping ThrowingFunction , shutdown: @escaping FunctionWithCallback ) {
420
- self . append ( Item ( name: name, start: start, shutdown: shutdown) )
421
- }
422
-
423
- /// Adds a `LifecycleItem` to a `LifecycleItems` collection.
424
- ///
425
- /// - parameters:
426
- /// - name: name of the item, useful for debugging.
427
- /// - shutdown: closure to perform the shutdown.
428
- func append( name: String , start: @escaping ThrowingFunction , shutdown: @escaping ThrowingFunction ) {
429
- self . append ( Item ( name: name, start: start, shutdown: shutdown) )
430
- }
431
-
432
- /// Adds a `LifecycleItem` to a `LifecycleItems` collection.
433
- ///
434
- /// - parameters:
435
- /// - name: name of the item, useful for debugging.
436
- /// - start: closure to perform the startup.
437
- /// - shutdown: closure to perform the shutdown.
438
- func append< Future, Value> ( name: String , start: @escaping ThrowingFunction , shutdown: @escaping FutureProvider < Future , Value > ) {
439
- self . append ( Item ( name: name, start: start, shutdown: shutdown) )
440
- }
441
-
442
- /// Adds a `LifecycleItem` to a `LifecycleItems` collection.
443
- ///
444
- /// - parameters:
445
- /// - name: name of the item, useful for debugging.
446
- /// - shutdown: closure to perform the shutdown.
447
- func append< Future, Value> ( name: String , start: @escaping FutureProvider < Future , Value > , shutdown: @escaping FunctionWithCallback ) {
448
- self . append ( Item ( name: name, start: start, shutdown: shutdown) )
449
- }
450
-
451
- /// Add a `LifecycleItem` to a `LifecycleItems` collection.
452
- ///
453
- /// - parameters:
454
- /// - name: name of the item, useful for debugging.
455
- /// - start: closure to perform the startup.
456
- /// - shutdown: closure to perform the shutdown.
457
- func append< Future, Value> ( name: String , start: @escaping FutureProvider < Future , Value > , shutdown: @escaping ThrowingFunction ) {
458
- self . append ( Item ( name: name, start: start, shutdown: shutdown) )
459
- }
460
-
461
- /// Adds a `LifecycleItem` to a `LifecycleItems` collection.
462
- ///
463
- /// - parameters:
464
- /// - name: name of the item, useful for debugging.
465
- /// - start: closure to perform the startup.
466
- /// - shutdown: closure to perform the shutdown.
467
- func append< Future, Value> ( name: String , start: @escaping FutureProvider < Future , Value > , shutdown: @escaping FutureProvider < Future , Value > ) {
468
- self . append ( Item ( name: name, start: start, shutdown: shutdown) )
308
+ func registerShutdown( name: String , _ handler: @escaping ( ) throws -> Void ) {
309
+ self . register ( name: name, start: . none, shutdown: . sync( handler) )
469
310
}
470
311
}
471
312
472
313
/// Supported startup and shutdown method styles
473
-
474
314
public extension Lifecycle {
475
- typealias FunctionWithCallback = ( @escaping ( Error ? ) -> Void ) -> Void
476
- typealias ThrowingFunction = ( ) throws -> Void
477
- typealias FutureProvider < Future: LifecycleFuture , Value> = ( ) -> Future where Future. Value == Value
478
- }
315
+ struct Handler {
316
+ private let body : ( @escaping ( Error ? ) -> Void ) -> Void
317
+
318
+ /// Initialize a `Lifecycle.Handler` based on a completion handler.
319
+ ///
320
+ /// - parameters:
321
+ /// - callback: the underlying completion handler
322
+ public init ( _ callback: @escaping ( @escaping ( Error ? ) -> Void ) -> Void ) {
323
+ self . body = callback
324
+ }
479
325
480
- public protocol LifecycleFuture {
481
- associatedtype Value
326
+ /// Asynchronous `Lifecycle.Handler` based on a completion handler.
327
+ ///
328
+ /// - parameters:
329
+ /// - callback: the underlying completion handler
330
+ public static func async ( _ callback: @escaping ( @escaping ( Error ? ) -> Void ) -> Void ) -> Handler {
331
+ return Handler ( callback)
332
+ }
482
333
483
- func whenComplete( _ callback: @escaping ( Result < Value , Error > ) -> Void )
334
+ /// Asynchronous `Lifecycle.Handler` based on a blocking, throwing function.
335
+ ///
336
+ /// - parameters:
337
+ /// - body: the underlying function
338
+ public static func sync( _ body: @escaping ( ) throws -> Void ) -> Handler {
339
+ return Handler { completionHandler in
340
+ do {
341
+ try body ( )
342
+ completionHandler ( nil )
343
+ } catch {
344
+ completionHandler ( error)
345
+ }
346
+ }
347
+ }
348
+
349
+ /// Noop `Lifecycle.Handler`.
350
+ public static var none : Handler {
351
+ return Handler { callback in
352
+ callback ( nil )
353
+ }
354
+ }
355
+
356
+ internal func run( _ callback: @escaping ( Error ? ) -> Void ) {
357
+ self . body ( callback)
358
+ }
359
+ }
484
360
}
485
361
486
362
/// Represents an item that can be started and shut down
0 commit comments