Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Elixxir dApps SDK Swift
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mobile
iOS
Elixxir dApps SDK Swift
Commits
5a729d9f
Commit
5a729d9f
authored
2 years ago
by
Dariusz Rybicki
Browse files
Options
Downloads
Patches
Plain Diff
Add BackupStorage utility
parent
4aa59f6d
No related branches found
No related tags found
2 merge requests
!110
Backup improvements & example
,
!102
Release 1.0.0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Sources/XXMessengerClient/Utils/BackupStorage.swift
+66
-0
66 additions, 0 deletions
Sources/XXMessengerClient/Utils/BackupStorage.swift
Tests/XXMessengerClientTests/Utils/BackupStorageTests.swift
+70
-0
70 additions, 0 deletions
Tests/XXMessengerClientTests/Utils/BackupStorageTests.swift
with
136 additions
and
0 deletions
Sources/XXMessengerClient/Utils/BackupStorage.swift
0 → 100644
+
66
−
0
View file @
5a729d9f
import
Foundation
import
XCTestDynamicOverlay
import
XXClient
public
struct
BackupStorage
{
public
struct
Backup
:
Equatable
{
public
init
(
date
:
Date
,
data
:
Data
)
{
self
.
date
=
date
self
.
data
=
data
}
public
var
date
:
Date
public
var
data
:
Data
}
public
typealias
Observer
=
(
Backup
?)
->
Void
public
var
store
:
(
Data
)
->
Void
public
var
observe
:
(
@escaping
Observer
)
->
Cancellable
public
var
remove
:
()
->
Void
}
extension
BackupStorage
{
public
static
func
live
(
now
:
@escaping
()
->
Date
)
->
BackupStorage
{
var
observers
:
[
UUID
:
Observer
]
=
[:]
var
backup
:
Backup
?
func
notifyObservers
()
{
observers
.
values
.
forEach
{
$0
(
backup
)
}
}
return
BackupStorage
(
store
:
{
data
in
backup
=
Backup
(
date
:
now
(),
data
:
data
)
notifyObservers
()
},
observe
:
{
observer
in
let
id
=
UUID
()
observers
[
id
]
=
observer
defer
{
observers
[
id
]?(
backup
)
}
return
Cancellable
{
observers
[
id
]
=
nil
}
},
remove
:
{
backup
=
nil
notifyObservers
()
}
)
}
}
extension
BackupStorage
{
public
static
let
unimplemented
=
BackupStorage
(
store
:
XCTUnimplemented
(
"
\(
Self
.
self
)
.store"
),
observe
:
XCTUnimplemented
(
"
\(
Self
.
self
)
.observe"
,
placeholder
:
Cancellable
{}),
remove
:
XCTUnimplemented
(
"
\(
Self
.
self
)
.remove"
)
)
}
This diff is collapsed.
Click to expand it.
Tests/XXMessengerClientTests/Utils/BackupStorageTests.swift
0 → 100644
+
70
−
0
View file @
5a729d9f
import
CustomDump
import
XCTest
@testable
import
XXMessengerClient
final
class
BackupStorageTests
:
XCTestCase
{
func
testStorage
()
{
var
now
:
Date
=
.
init
(
0
)
let
storage
:
BackupStorage
=
.
live
(
now
:
{
now
}
)
var
didObserveA
:
[
BackupStorage
.
Backup
?]
=
[]
let
observerA
=
storage
.
observe
{
backup
in
didObserveA
.
append
(
backup
)
}
XCTAssertNoDifference
(
didObserveA
,
[
nil
])
now
=
.
init
(
1
)
didObserveA
=
[]
let
data1
=
"data-1"
.
data
(
using
:
.
utf8
)
!
storage
.
store
(
data1
)
XCTAssertNoDifference
(
didObserveA
,
[
.
init
(
date
:
.
init
(
1
),
data
:
data1
)])
now
=
.
init
(
2
)
didObserveA
=
[]
var
didObserveB
:
[
BackupStorage
.
Backup
?]
=
[]
let
observerB
=
storage
.
observe
{
backup
in
didObserveB
.
append
(
backup
)
}
XCTAssertNoDifference
(
didObserveA
,
[])
XCTAssertNoDifference
(
didObserveB
,
[
.
init
(
date
:
.
init
(
1
),
data
:
data1
)])
now
=
.
init
(
3
)
didObserveA
=
[]
didObserveB
=
[]
let
data2
=
"data-2"
.
data
(
using
:
.
utf8
)
!
storage
.
store
(
data2
)
XCTAssertNoDifference
(
didObserveA
,
[
.
init
(
date
:
.
init
(
3
),
data
:
data2
)])
XCTAssertNoDifference
(
didObserveB
,
[
.
init
(
date
:
.
init
(
3
),
data
:
data2
)])
now
=
.
init
(
4
)
didObserveA
=
[]
didObserveB
=
[]
observerA
.
cancel
()
storage
.
remove
()
XCTAssertNoDifference
(
didObserveA
,
[])
XCTAssertNoDifference
(
didObserveB
,
[
nil
])
now
=
.
init
(
5
)
didObserveA
=
[]
didObserveB
=
[]
observerB
.
cancel
()
let
data3
=
"data-3"
.
data
(
using
:
.
utf8
)
!
storage
.
store
(
data3
)
XCTAssertNoDifference
(
didObserveA
,
[])
XCTAssertNoDifference
(
didObserveB
,
[])
}
}
private
extension
Date
{
init
(
_
timeIntervalSince1970
:
TimeInterval
)
{
self
.
init
(
timeIntervalSince1970
:
timeIntervalSince1970
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment