Skip to content

Commit

Permalink
changed hook registration functions
Browse files Browse the repository at this point in the history
  • Loading branch information
thebarndog committed Jun 1, 2017
1 parent 8c2ea2b commit 4f98872
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 35 deletions.
39 changes: 25 additions & 14 deletions Feathers/Core/Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
}

Expand Down
22 changes: 14 additions & 8 deletions Feathers/Core/ServiceType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,26 @@ public protocol ServiceType {
/// - Returns: `SignalProducer` that emits a response or errors.
func request(_ method: Service.Method) -> SignalProducer<Response, AnyFeathersError>

/// 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.
Expand Down
22 changes: 15 additions & 7 deletions Feathers/Core/ServiceWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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> {
Expand Down
12 changes: 6 additions & 6 deletions FeathersTests/ServiceSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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") {
Expand All @@ -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") {
Expand Down

0 comments on commit 4f98872

Please sign in to comment.