From 4f98872e47ea0468c6a894d2ce478a60e587c475 Mon Sep 17 00:00:00 2001 From: Brendan Conron Date: Wed, 31 May 2017 23:39:09 -0700 Subject: [PATCH] changed hook registration functions --- Feathers/Core/Service.swift | 39 +++++++++++++++++++----------- Feathers/Core/ServiceType.swift | 22 +++++++++++------ Feathers/Core/ServiceWrapper.swift | 22 +++++++++++------ FeathersTests/ServiceSpec.swift | 12 ++++----- 4 files changed, 60 insertions(+), 35 deletions(-) diff --git a/Feathers/Core/Service.swift b/Feathers/Core/Service.swift index 1fd84bb..2433f32 100644 --- a/Feathers/Core/Service.swift +++ b/Feathers/Core/Service.swift @@ -63,6 +63,17 @@ open class Service: ServiceType { /// Hooks for `.remove` requests. public let remove: [Hook] + /// True if the object contains any hooks, false otherwise. + public var isEmpty: Bool { + return all.isEmpty + && find.isEmpty + && get.isEmpty + && create.isEmpty + && update.isEmpty + && patch.isEmpty + && remove.isEmpty + } + /// Service hooks initializer. /// /// - Parameters: @@ -155,23 +166,23 @@ open class Service: ServiceType { fatalError("Must be overriden by a subclass") } - final public func hooks(before: Hooks? = nil, after: Hooks? = nil, error: Hooks? = nil) { - if let before = before { - beforeHooks = beforeHooks.add(hooks: before) - } - if let after = after { - afterHooks = afterHooks.add(hooks: after) - } - if let error = error { - errorHooks = errorHooks.add(hooks: error) - } + final public func before(_ hooks: Hooks) { + beforeHooks = beforeHooks.add(hooks: hooks) + } + + final public func after(_ hooks: Hooks) { + afterHooks = afterHooks.add(hooks: hooks) + } + + final public func error(_ hooks: Hooks) { + errorHooks = errorHooks.add(hooks: hooks) } - final public func retrieveHooks(for kind: HookObject.Kind) -> Service.Hooks? { + final public func hooks(for kind: HookObject.Kind) -> Service.Hooks? { switch kind { - case .before: return beforeHooks - case .after: return afterHooks - case .error: return errorHooks + case .before: return beforeHooks.isEmpty ? nil : beforeHooks + case .after: return afterHooks.isEmpty ? nil : afterHooks + case .error: return errorHooks.isEmpty ? nil : errorHooks } } diff --git a/Feathers/Core/ServiceType.swift b/Feathers/Core/ServiceType.swift index 088c1d3..db955e0 100644 --- a/Feathers/Core/ServiceType.swift +++ b/Feathers/Core/ServiceType.swift @@ -32,20 +32,26 @@ public protocol ServiceType { /// - Returns: `SignalProducer` that emits a response or errors. func request(_ method: Service.Method) -> SignalProducer - /// Register hooks with the service. - /// Hooks get added with each successive use, not overridden. + /// Register before hooks with the service. /// - /// - Parameters: - /// - before: Before hooks. - /// - after: After hooks. - /// - error: Error hooks. - func hooks(before: Service.Hooks?, after: Service.Hooks?, error: Service.Hooks?) + /// - Parameter hooks: Before hooks to register. + func before(_ hooks: Service.Hooks) + + /// Register after hooks with the service. + /// + /// - Parameter hooks: After hooks to register. + func after(_ hooks: Service.Hooks) + + /// Register error hooks with the service. + /// + /// - Parameter hooks: Error hooks to register. + func error(_ hooks: Service.Hooks) /// Fetch a service's hooks. /// /// - Parameter kind: The kind of hook. /// - Returns: Hooks registered for `kind`, if any exist. - func retrieveHooks(for kind: HookObject.Kind) -> Service.Hooks? + func hooks(for kind: HookObject.Kind) -> Service.Hooks? /// Register for a real-time event to listen for changing data. /// Signal will continue to emit until disposed. diff --git a/Feathers/Core/ServiceWrapper.swift b/Feathers/Core/ServiceWrapper.swift index 6f290c0..bde1a1f 100644 --- a/Feathers/Core/ServiceWrapper.swift +++ b/Feathers/Core/ServiceWrapper.swift @@ -68,9 +68,9 @@ final public class ServiceWrapper: ServiceType { } let beforeHookObject = HookObject(type: .before, app: application, service: service, method: method) // Get all the hooks - let beforeHooks = service.retrieveHooks(for: .before)?.hooks(for: method) ?? [] - let afterHooks = service.retrieveHooks(for: .after)?.hooks(for: method) ?? [] - let errorHooks = service.retrieveHooks(for: .error)?.hooks(for: method) ?? [] + let beforeHooks = service.hooks(for: .before)?.hooks(for: method) ?? [] + let afterHooks = service.hooks(for: .after)?.hooks(for: method) ?? [] + let errorHooks = service.hooks(for: .error)?.hooks(for: method) ?? [] // Build up the before chains let beforeChain = beforeHooks.reduce(SignalProducer(value: beforeHookObject), reduceHooksClosure) @@ -114,12 +114,20 @@ final public class ServiceWrapper: ServiceType { } } - final public func hooks(before: Service.Hooks? = nil, after: Service.Hooks? = nil, error: Service.Hooks? = nil) { - service.hooks(before: before, after: after, error: error) + final public func before(_ hooks: Service.Hooks) { + service.before(hooks) } - final public func retrieveHooks(for kind: HookObject.Kind) -> Service.Hooks? { - return service.retrieveHooks(for: kind) + final public func after(_ hooks: Service.Hooks) { + service.after(hooks) + } + + final public func error(_ hooks: Service.Hooks) { + service.error(hooks) + } + + final public func hooks(for kind: HookObject.Kind) -> Service.Hooks? { + return service.hooks(for: kind) } final public func on(event: Service.RealTimeEvent) -> Signal<[String: Any], NoError> { diff --git a/FeathersTests/ServiceSpec.swift b/FeathersTests/ServiceSpec.swift index 2fa997e..cc84429 100644 --- a/FeathersTests/ServiceSpec.swift +++ b/FeathersTests/ServiceSpec.swift @@ -52,7 +52,7 @@ class ServiceSpec: QuickSpec { beforeEach { beforeHooks = Service.Hooks(all: [StubHook(data: .object(["name": "Henry"]))]) - service.hooks(before: beforeHooks, after: nil, error: nil) + service.before(beforeHooks) } it("should run the before hook and skip the request") { @@ -81,7 +81,7 @@ class ServiceSpec: QuickSpec { beforeEach { afterHooks = Service.Hooks(all: [PopuplateDataAfterHook(data: ["name": "Susie"])]) - service.hooks(before: nil, after: afterHooks, error: nil) + service.after(afterHooks) } it("should change the response") { @@ -111,7 +111,7 @@ class ServiceSpec: QuickSpec { beforeEach { // Force the hook to error with ErrorHook beforeHooks = Service.Hooks(all: [ErrorHook(error:FeathersNetworkError.unknown)]) - service.hooks(before: beforeHooks, after: nil, error: nil) + service.before(beforeHooks) } context("when a hook rejects with an error") { @@ -140,7 +140,7 @@ class ServiceSpec: QuickSpec { beforeEach { errorHooks = Service.Hooks(all: [ErrorHook(error: FeathersNetworkError.unavailable), ErrorHook(error: FeathersNetworkError.unknown)]) - service.hooks(before: nil, after: nil, error: errorHooks) + service.error(errorHooks) } it("should be able to modify the final error and skip the rest of the chain") { @@ -169,7 +169,7 @@ class ServiceSpec: QuickSpec { beforeEach { errorHooks = Service.Hooks(all: [ModifyErrorHook(error: FeathersNetworkError.unavailable)]) - service.hooks(before: nil, after: nil, error: errorHooks) + service.error(errorHooks) } it("should be able to modify the final error") { @@ -193,7 +193,7 @@ class ServiceSpec: QuickSpec { context("with multiple error hooks that modify the error ") { beforeEach { - service.hooks(before: nil, after: nil, error: Service.Hooks(all: [ModifyErrorHook(error: FeathersNetworkError.unknown)])) + service.error(Service.Hooks(all: [ModifyErrorHook(error: FeathersNetworkError.unknown)])) } it("should pass back the final error") {