Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from cweinberger/feature/fix-leaking-request
Browse files Browse the repository at this point in the history
Change to a 'weak' reference to request to avoid memory leaks
  • Loading branch information
cweinberger authored Jun 20, 2018
2 parents 8aa2ee6 + 47f0a44 commit 4527dd7
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Sources/Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class Helper {
case warning = "warning"
}

private let request: Request
private weak var request: Request?

public init(request: Request) {
self.request = request
Expand All @@ -28,7 +28,7 @@ public final class Helper {
/// - message: String message
/// - Throws: Error
public func add(_ type: FlashType, _ message: String) throws {
try request.assertSession().data[Helper.flashKey, State.new.rawValue, type.rawValue] = Node(message)
try request?.assertSession().data[Helper.flashKey, State.new.rawValue, type.rawValue] = Node(message)
}

/// Add a message to a custom key
Expand All @@ -38,7 +38,7 @@ public final class Helper {
/// - message: String message
/// - Throws: Error
public func add(_ custom: String, _ message: String) throws {
try request.assertSession().data[Helper.flashKey, State.new.rawValue, custom] = Node(message)
try request?.assertSession().data[Helper.flashKey, State.new.rawValue, custom] = Node(message)
}

/// Refresh session, move current flash messages to "new" again,
Expand All @@ -47,14 +47,14 @@ public final class Helper {
/// - Throws: Error
public func refresh() throws {
// Copy old node to new node
try request.assertSession().data[Helper.flashKey, State.new.rawValue] = try request.assertSession().data[Helper.flashKey, State.old.rawValue] ?? Node([])
try request?.assertSession().data[Helper.flashKey, State.new.rawValue] = try request?.assertSession().data[Helper.flashKey, State.old.rawValue] ?? Node([])
}

/// Clear the session
///
/// - Throws: Error
public func clear() throws {
try request.assertSession().data[Helper.flashKey] = nil
request.storage[Helper.flashKey] = nil
try request?.assertSession().data[Helper.flashKey] = nil
request?.storage[Helper.flashKey] = nil
}
}

0 comments on commit 4527dd7

Please sign in to comment.