@daaku/kombat
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -53,4 +53,4 @@ export declare class Timestamp { | ||
queryLatestMessages(messages: Message[]): Promise<(Message | undefined)[]>; | ||
storeLastSync(timestamp: string): Promise<void>; | ||
queryLastSync(): Promise<string | undefined>; | ||
set(key: string, value: string): Promise<void>; | ||
get(key: string): Promise<string | undefined>; | ||
} | ||
@@ -62,3 +62,5 @@ export declare class SyncDB { | ||
private nextSync?; | ||
constructor(clock: Clock, remote: Remote, local: Local); | ||
private constructor(); | ||
static new(remote: Remote, local: Local): Promise<SyncDB>; | ||
private saveClock; | ||
private apply; | ||
@@ -65,0 +67,0 @@ recv(messages: Message[]): Promise<void>; |
@@ -7,2 +7,4 @@ "use strict"; | ||
const nanoid = nanoid_1.customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz', 16); | ||
const kLastSync = 'last_sync'; | ||
const kClock = 'clock'; | ||
class DuplicateNodeError extends Error { | ||
@@ -234,2 +236,18 @@ constructor(nodeID) { | ||
} | ||
// Create a new instance of a SyncDB. | ||
static async new(remote, local) { | ||
const clockJSON = await local.get(kClock); | ||
let clock; | ||
if (clockJSON) { | ||
clock = Clock.fromJSON(JSON.parse(clockJSON)); | ||
} | ||
else { | ||
clock = new Clock(); | ||
await local.set(kClock, JSON.stringify(clock)); | ||
} | ||
return new SyncDB(clock, remote, local); | ||
} | ||
async saveClock() { | ||
await this.local.set(kClock, JSON.stringify(this.clock)); | ||
} | ||
async apply(messages) { | ||
@@ -267,2 +285,3 @@ // ensure we're always working with sorted messages, ordering is important. | ||
}); | ||
await this.saveClock(); | ||
} | ||
@@ -276,2 +295,3 @@ // Recieve data from a Remote. You may find this useful if in addition to sync | ||
messages.forEach((m) => this.clock.recv(Timestamp.fromJSON(m.timestamp))); | ||
await this.saveClock(); | ||
await this.apply(messages); | ||
@@ -289,5 +309,6 @@ } | ||
const lastSync = this.clock.send().toJSON(); | ||
await this.saveClock(); | ||
// either the given since, or the last sync, or zero | ||
if (!since) { | ||
since = await this.local.queryLastSync(); | ||
since = await this.local.get(kLastSync); | ||
if (!since) { | ||
@@ -303,3 +324,3 @@ since = new Timestamp(0, 0, '0').toJSON(); // the begining of time | ||
await this.recv(syncResponse.messages); | ||
await this.local.storeLastSync(lastSync); | ||
await this.local.set(kLastSync, lastSync); | ||
// if we still have diffferences, we may need another sync. | ||
@@ -316,2 +337,3 @@ const diffTime = syncResponse.merkle.diff(this.clock.merkle); | ||
withTS.forEach((m) => (m.timestamp = this.clock.send().toJSON())); | ||
await this.saveClock(); | ||
await this.apply(withTS); | ||
@@ -318,0 +340,0 @@ this.scheduleSync(); |
{ | ||
"name": "@daaku/kombat", | ||
"author": "Naitik Shah <n@daaku.org>", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Infrastructure for CRDT powered applications.", | ||
@@ -6,0 +6,0 @@ "repository": "git@github.com:daaku/kombat", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41738
599