Skip to content

Commit

Permalink
Whoops, clientID needs to be changeable because in dd31 a (#9)
Browse files Browse the repository at this point in the history
single push can contain mutations from multiple transactions.
  • Loading branch information
aboodman authored May 1, 2023
1 parent c45972d commit f7d6528
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
16 changes: 9 additions & 7 deletions src/replicache-transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ class MemStorage implements Storage {

test('ReplicacheTransaction', async () => {
const s = new MemStorage();
const t1 = new ReplicacheTransaction(s, 'c1', 42);
const t1 = new ReplicacheTransaction(s, 'c1');

expect(t1.clientID).equal('c1');
expect(t1.mutationID).equal(42);
expect(t1.environment).equal('server');
expect(t1.reason).equal('authoritative');
expect(await t1.has('foo')).false;
Expand All @@ -69,26 +68,29 @@ test('ReplicacheTransaction', async () => {

expect(await s.getEntry('foo')).equal('bar');

const t2 = new ReplicacheTransaction(s, 'c1', 1);
const t2 = new ReplicacheTransaction(s);
await t2.del('foo');
await t2.flush();

expect(await s.getEntry('foo')).equal(undefined);
expect(s.getAllEntries()).deep.equal([]);

t1.clientID = 'c2';
expect(t1.clientID).equals('c2');
});

test('ReplicacheTransaction overlap', async () => {
const s = new MemStorage();
const t1 = new ReplicacheTransaction(s, 'c1', 1);
const t1 = new ReplicacheTransaction(s);
await t1.put('foo', 'bar');

const t2 = new ReplicacheTransaction(s, 'c1', 1);
const t2 = new ReplicacheTransaction(s);
expect(await t2.has('foo')).false;

await t1.flush();
expect(await t2.has('foo')).false;

const t3 = new ReplicacheTransaction(s, 'c1', 1);
const t3 = new ReplicacheTransaction(s);
expect(await t3.has('foo')).true;
});

Expand All @@ -110,7 +112,7 @@ test('ReplicacheTransaction scan', async () => {
s.clear();
await putEntries(sources);

const t = new ReplicacheTransaction(s, 'c1', 1);
const t = new ReplicacheTransaction(s);
for (const change of changes) {
await t.put(change, change);
}
Expand Down
13 changes: 4 additions & 9 deletions src/replicache-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,19 @@ export interface Storage {
* transaction.
*/
export class ReplicacheTransaction implements WriteTransaction {
private _clientID: string;
private _storage: Storage;
private _cache: CacheMap = new Map();

constructor(storage: Storage, clientID: string, mutationID = 0) {
constructor(storage: Storage, clientID = '') {
this._storage = storage;
this._clientID = clientID;
this.mutationID = mutationID;
this.clientID = clientID;
}

readonly reason = 'authoritative';
readonly environment = 'server';

mutationID: number;

get clientID(): string {
return this._clientID;
}
clientID: string;
mutationID = 0;

// eslint-disable-next-line require-await
async put(key: string, value: ReadonlyJSONValue): Promise<void> {
Expand Down

0 comments on commit f7d6528

Please sign in to comment.