-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3063df1
commit 7c10cab
Showing
17 changed files
with
806 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { merge, keyBy } from 'lodash' | ||
import { DateTime } from 'luxon' | ||
import { encrypt, decrypt } from 'application/helpers/encryption' | ||
|
||
export const mergeData = (local, remote, cryptor) => { | ||
if (!remote) { | ||
local.updatedAt = now() | ||
return local | ||
} | ||
return encrypt( | ||
{ | ||
entries: combine(group(local, cryptor), group(remote, cryptor)), | ||
updatedAt: now() | ||
}, | ||
cryptor | ||
) | ||
} | ||
|
||
const combine = (local, remote) => { | ||
return remote.timestamp > local.timestamp | ||
? mergeInto(local, remote) | ||
: mergeInto(remote, local) | ||
} | ||
|
||
const mergeInto = (base, update) => { | ||
for (let key in base.data) { | ||
if (update.data[key]) { | ||
merge(base.data[key], update.data[key]) | ||
delete update.data[key] | ||
} else { | ||
delete base.data[key] | ||
} | ||
} | ||
|
||
for (let key in update.data) { | ||
base.data[key] = update.data[key] | ||
} | ||
return Object.values(base.data) | ||
} | ||
|
||
const group = (encrypted, cryptor) => { | ||
const data = decrypt(encrypted, cryptor) | ||
return { | ||
data: keyBy(data.entries, 'id'), | ||
timestamp: DateTime.fromISO(data.updatedAt) | ||
} | ||
} | ||
|
||
const now = () => DateTime.local().toISO() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,4 @@ | |
position: relative | ||
top: 2px | ||
path | ||
fill: #fff | ||
fill: #fff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { DateTime as Datetime } from 'luxon' | ||
|
||
const now = Datetime.local() | ||
|
||
export const DateTime = { | ||
local: () => { | ||
return { | ||
toISO: jest.fn().mockReturnValue(now.toISO()), | ||
__minus: (value, units) => { | ||
return now.minus(value, units) | ||
} | ||
} | ||
}, | ||
fromISO: jest.fn(string => { | ||
return Datetime.fromISO(string) | ||
}) | ||
} |
Oops, something went wrong.