Skip to content

Commit

Permalink
chore: Replicache 15.0.1 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesara authored Jul 22, 2024
1 parent 4f9ead9 commit 8dad26e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 40 deletions.
88 changes: 51 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@rocicorp/eslint-config": "^0.5.1",
"@rocicorp/prettier-config": "^0.2.0",
"replicache": "13.0.0-beta.0",
"replicache": "^15.0.1",
"typescript": "5.5.3",
"vitest": "^2.0.3"
},
Expand Down
37 changes: 35 additions & 2 deletions src/replicache-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {compareUTF8} from 'compare-utf8';
import {
DeepReadonly,
filterAsyncIterable,
IndexKey,
isScanIndexOptions,
makeScanResult,
mergeAsyncIterables,
ReadonlyJSONValue,
ScanIndexOptions,
ScanNoIndexOptions,
ScanOptions,
ScanResult,
TransactionLocation,
WriteTransaction,
} from 'replicache';

Expand Down Expand Up @@ -39,20 +44,33 @@ export class ReplicacheTransaction implements WriteTransaction {
}

readonly reason = 'authoritative';

/** @deprecated Use {@link location} instead. */
readonly environment = 'server';
readonly location: TransactionLocation = 'server';

clientID: string;
mutationID = 0;

/**
* @deprecated Use {@link set} instead.
*/
// eslint-disable-next-line require-await
async put(key: string, value: ReadonlyJSONValue): Promise<void> {
this.#cache.set(key, {value, dirty: true});
}

// eslint-disable-next-line require-await
async set(key: string, value: ReadonlyJSONValue): Promise<void> {
this.#cache.set(key, {value, dirty: true});
}

async del(key: string): Promise<boolean> {
const had = await this.has(key);
this.#cache.set(key, {value: undefined, dirty: true});
return had;
}

async get(key: string): Promise<ReadonlyJSONValue | undefined> {
const entry = this.#cache.get(key);
if (entry) {
Expand All @@ -62,6 +80,7 @@ export class ReplicacheTransaction implements WriteTransaction {
this.#cache.set(key, {value, dirty: false});
return value;
}

async has(key: string): Promise<boolean> {
const val = await this.get(key);
return val !== undefined;
Expand All @@ -75,15 +94,29 @@ export class ReplicacheTransaction implements WriteTransaction {
return true;
}

scan(options: ScanOptions = {} as ScanNoIndexOptions) {
scan(options: ScanIndexOptions): ScanResult<IndexKey, ReadonlyJSONValue>;
scan(options?: ScanNoIndexOptions): ScanResult<string, ReadonlyJSONValue>;
scan(options?: ScanOptions): ScanResult<IndexKey | string, ReadonlyJSONValue>;
scan<V extends ReadonlyJSONValue>(
options: ScanIndexOptions,
): ScanResult<IndexKey, DeepReadonly<V>>;
scan<V extends ReadonlyJSONValue>(
options?: ScanNoIndexOptions,
): ScanResult<string, DeepReadonly<V>>;
scan<V extends ReadonlyJSONValue>(
options?: ScanOptions,
): ScanResult<IndexKey | string, DeepReadonly<V>>;
scan(
options: ScanOptions = {},
): ScanResult<IndexKey | string, ReadonlyJSONValue> {
if (isScanIndexOptions(options)) {
throw new Error('not implemented');
}

const storage = this.#storage;
const cache = this.#cache;

return makeScanResult<ScanNoIndexOptions>(options, (fromKey: string) => {
return makeScanResult(options, (fromKey: string) => {
const source = storage.getEntries(fromKey);
const pending = getCacheEntries(cache, fromKey);
const merged = mergeAsyncIterables(source, pending, entryCompare);
Expand Down

0 comments on commit 8dad26e

Please sign in to comment.