Skip to content

Commit

Permalink
fix merge closure
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Aug 7, 2024
1 parent 31b1cde commit 66457af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
17 changes: 9 additions & 8 deletions packages/crdt/src/builtins/GSet/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* GSet with support for state and op changes */

export class GSet<T> {
set: Set<T>;

Expand All @@ -7,9 +8,7 @@ export class GSet<T> {
}

add(element: T): void {
let set = this.set;
set.add(element);
this.set = set;
this.set.add(element);
}

lookup(element: T): boolean {
Expand All @@ -26,15 +25,15 @@ export class GSet<T> {
}

/// AssemblyScript functions
function gset_create<T>(set: Set<T> = new Set<T>()): GSet<T> {
export function gset_create<T>(set: Set<T> = new Set<T>()): GSet<T> {
return new GSet<T>(set);
}

export function gset_add<T>(gset: GSet<T>, element: T): void {
gset.add(element);
}

function gset_lookup<T>(gset: GSet<T>, element: T): boolean {
export function gset_lookup<T>(gset: GSet<T>, element: T): boolean {
return gset.lookup(element);
}

Expand All @@ -44,8 +43,10 @@ export function gset_compare<T>(gset: GSet<T>, peerSet: GSet<T>): boolean {
}

export function gset_merge<T>(gset: GSet<T>, peerSet: GSet<T>): void {
let set = gset.set.values();
// @ts-ignore
peerSet.set.values().forEach((value) => {
gset.set.add(value);
});
for (let i = 0, l = peerSet.set.values().length; i < l; ++i) {
// @ts-ignore
gset.add(set[i]);
}
}
6 changes: 3 additions & 3 deletions packages/object/src/chat.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { GSet, gset_add, gset_merge } from "@topology-foundation/crdt/src/builtins/GSet/index";
import { GSet, gset_add, gset_create, gset_merge } from "@topology-foundation/crdt/src/builtins/GSet/index";

class Chat {
// store messages as strings in the format (timestamp, message, peerId)
messages: GSet<string>;

constructor() {
this.messages = new GSet<string>(new Set<string>());
this.messages = gset_create<string>();
}
}

export function createChat(peerId: string): Chat {
export function createChat(): Chat {
return new Chat();
}

Expand Down

0 comments on commit 66457af

Please sign in to comment.