Skip to content

Commit

Permalink
Simplify ExperimentalCreateKVStore Write class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Braden1996 committed Feb 18, 2023
1 parent 2f5b263 commit 761153f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 103 deletions.
2 changes: 1 addition & 1 deletion packages/example/mobile-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dependencies": {
"@react-native-replicache/example-client-shared": "0.0.0",
"@react-native-replicache/example-shared": "0.0.0",
"@react-native-replicache/react-native-quick-sqlite": "0.0.2",
"@react-native-replicache/react-native-quick-sqlite": "0.0.3",
"expo": "~48.0.1",
"expo-build-properties": "0.5.1",
"expo-crypto": "^12.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-quick-sqlite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-native-replicache/react-native-quick-sqlite",
"version": "0.0.2",
"version": "0.0.3",
"main": "./dist/commonjs/index.js",
"module": "./dist/module/index.js",
"react-native": "./src/index.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class ReplicacheQuickSQLiteReadImpl
{
private _closed = false;

constructor(private readonly _tx: ReplicacheQuickSQLiteTransaction) {}
constructor(protected readonly _tx: ReplicacheQuickSQLiteTransaction) {}

async has(key: string) {
const unsafeValue = await this._tx.get(key);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@
import { ReplicacheQuickSQLiteReadImpl } from "./replicache-quick-sqlite-read-impl";
import { ReplicacheQuickSQLiteTransaction } from "./replicache-quick-sqlite-transaction";
import {
deleteSentinel,
ReplicacheQuickSQLiteWriteImplBase,
} from "./replicache-quick-sqlite-write-impl-base";

export class ReplicacheQuickSQLiteWriteImpl extends ReplicacheQuickSQLiteWriteImplBase {
private readonly _tx: ReplicacheQuickSQLiteTransaction;
private _closed = false;

constructor(tx: ReplicacheQuickSQLiteTransaction) {
super(new ReplicacheQuickSQLiteReadImpl(tx));
this._tx = tx;
}
import { ExperimentalCreateKVStore, ReadonlyJSONValue } from "replicache";

async commit() {
if (this._pending.size === 0) {
return;
}

await Promise.all(
[...this._pending.entries()].map(async ([key, value]) => {
if (value === deleteSentinel) {
await this._tx.delete(key);
} else {
const jsonValueString = JSON.stringify(value);
await this._tx.upsert(key, jsonValueString);
}
})
);
import { ReplicacheQuickSQLiteReadImpl } from "./replicache-quick-sqlite-read-impl";

this._pending.clear();
export class ReplicacheQuickSQLiteWriteImpl
extends ReplicacheQuickSQLiteReadImpl
implements
Awaited<ReturnType<ReturnType<ExperimentalCreateKVStore>["write"]>>
{
async put(key: string, value: ReadonlyJSONValue) {
const jsonValueString = JSON.stringify(value);
await this._tx.upsert(key, jsonValueString);
}

release() {
this._tx.commit();
this._closed = true;
async del(key: string) {
await this._tx.delete(key);
}

get closed() {
return this._closed;
async commit() {
// Do nothing and wait for release.
}
}
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,7 @@ __metadata:
"@braden1996/tsconfig": ^0.0.1
"@react-native-replicache/example-client-shared": 0.0.0
"@react-native-replicache/example-shared": 0.0.0
"@react-native-replicache/react-native-quick-sqlite": 0.0.2
"@react-native-replicache/react-native-quick-sqlite": 0.0.3
"@types/react": ^18.0.28
"@types/react-native": ~0.71.2
"@types/react-native__assets": ^1.0.0
Expand Down Expand Up @@ -2624,7 +2624,7 @@ __metadata:
languageName: unknown
linkType: soft

"@react-native-replicache/react-native-quick-sqlite@0.0.2, @react-native-replicache/react-native-quick-sqlite@workspace:packages/react-native-quick-sqlite":
"@react-native-replicache/react-native-quick-sqlite@0.0.3, @react-native-replicache/react-native-quick-sqlite@workspace:packages/react-native-quick-sqlite":
version: 0.0.0-use.local
resolution: "@react-native-replicache/react-native-quick-sqlite@workspace:packages/react-native-quick-sqlite"
dependencies:
Expand Down

0 comments on commit 761153f

Please sign in to comment.