-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
double way streams returning the objects
- Loading branch information
Showing
8 changed files
with
165 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,50 @@ | ||
import { GCounter } from "@topology-foundation/crdt"; | ||
import { GCounter, IGCounter } from "@topology-foundation/crdt"; | ||
import { TopologyObject } from "@topology-foundation/object"; | ||
|
||
export class Pixel extends TopologyObject { | ||
private _red: GCounter; | ||
private _green: GCounter; | ||
private _blue: GCounter; | ||
export interface IPixel { | ||
red: IGCounter; | ||
green: IGCounter; | ||
blue: IGCounter; | ||
color(): [number, number, number]; | ||
paint(nodeId: string, rgb: [number, number, number]): void; | ||
counters(): [IGCounter, IGCounter, IGCounter]; | ||
merge(peerPixel: IPixel): void; | ||
} | ||
|
||
export class Pixel extends TopologyObject implements IPixel { | ||
red: IGCounter; | ||
green: IGCounter; | ||
blue: IGCounter; | ||
|
||
constructor() { | ||
super(); | ||
this._red = new GCounter({}); | ||
this._green = new GCounter({}); | ||
this._blue = new GCounter({}); | ||
this.red = new GCounter({}); | ||
this.green = new GCounter({}); | ||
this.blue = new GCounter({}); | ||
} | ||
|
||
color(): [number, number, number] { | ||
return [ | ||
this._red.value() % 256, | ||
this._green.value() % 256, | ||
this._blue.value() % 256, | ||
this.red.value() % 256, | ||
this.green.value() % 256, | ||
this.blue.value() % 256, | ||
]; | ||
} | ||
|
||
paint(nodeId: string, rgb: [number, number, number]): void { | ||
this._red.increment(nodeId, rgb[0]); | ||
this._green.increment(nodeId, rgb[1]); | ||
this._blue.increment(nodeId, rgb[2]); | ||
this.red.increment(nodeId, rgb[0]); | ||
this.green.increment(nodeId, rgb[1]); | ||
this.blue.increment(nodeId, rgb[2]); | ||
} | ||
|
||
counters(): [GCounter, GCounter, GCounter] { | ||
return [this._red, this._green, this._blue]; | ||
counters(): [IGCounter, IGCounter, IGCounter] { | ||
return [this.red, this.green, this.blue]; | ||
} | ||
|
||
merge(peerPixel: Pixel): void { | ||
let peerCounters = peerPixel.counters(); | ||
this._red.merge(peerCounters[0]); | ||
this._green.merge(peerCounters[1]); | ||
this._blue.merge(peerCounters[2]); | ||
this.red.merge(peerCounters[0]); | ||
this.green.merge(peerCounters[1]); | ||
this.blue.merge(peerCounters[2]); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,48 +1,50 @@ | ||
export interface IGCounter { | ||
globalCounter: number; | ||
counts: { [nodeKey: string]: number }; | ||
value(): number; | ||
increment(nodeId: string, amount: number): void; | ||
compare(peerCounter: IGCounter): boolean; | ||
merge(peerCounter: IGCounter): void; | ||
} | ||
|
||
/// GCounter with support for state and op changes | ||
export class GCounter { | ||
private _globalCounter: number; | ||
export class GCounter implements IGCounter { | ||
globalCounter: number; | ||
// instead of standard incremental id for replicas | ||
// we map the counter with the node id | ||
private _counts: { [nodeId: string]: number }; | ||
counts: { [nodeId: string]: number }; | ||
|
||
constructor(counts: { [nodeId: string]: number }) { | ||
this._globalCounter = Object.values(counts).reduce((a, b) => a + b, 0); | ||
this._counts = counts; | ||
this.globalCounter = Object.values(counts).reduce((a, b) => a + b, 0); | ||
this.counts = counts; | ||
} | ||
|
||
value(): number { | ||
return this._globalCounter; | ||
return this.globalCounter; | ||
} | ||
|
||
increment(nodeId: string, amount: number): void { | ||
this._globalCounter += amount; | ||
this._counts[nodeId] += amount; | ||
} | ||
|
||
counts(): { [nodeKey: string]: number } { | ||
return this._counts; | ||
this.globalCounter += amount; | ||
this.counts[nodeId] += amount; | ||
} | ||
|
||
compare(peerCounter: GCounter): boolean { | ||
for (let key in Object.keys(this._counts)) { | ||
if (this._counts[key] > peerCounter.counts()[key]) { | ||
compare(peerCounter: IGCounter): boolean { | ||
for (let key in Object.keys(this.counts)) { | ||
if (this.counts[key] > peerCounter.counts[key]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
merge(peerCounter: GCounter): void { | ||
merge(peerCounter: IGCounter): void { | ||
let temp: { [nodeKey: string]: number } = Object.assign( | ||
{}, | ||
this._counts, | ||
peerCounter.counts(), | ||
this.counts, | ||
peerCounter.counts, | ||
); | ||
Object.keys(temp).forEach((key) => { | ||
this._counts[key] = Math.max( | ||
this._counts[key], | ||
peerCounter.counts()[key], | ||
); | ||
this.counts[key] = Math.max(this.counts[key], peerCounter.counts[key]); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// TODO dumb export logic, fix this | ||
export { GCounter } from "./builtins/GCounter"; | ||
export { IGCounter, GCounter } from "./builtins/GCounter"; | ||
export { PNCounter } from "./builtins/PNCounter"; | ||
export { GSet } from "./builtins/GSet"; | ||
export { TwoPSet } from "./builtins/2PSet"; |
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
Oops, something went wrong.