Skip to content

Commit

Permalink
Expose and leverage canUseDOM
Browse files Browse the repository at this point in the history
  • Loading branch information
steida committed Oct 22, 2023
1 parent 77bd92f commit 094e25a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .changeset/kind-garlics-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@evolu/react-native": patch
"@evolu/common-web": patch
"@evolu/common": patch
"@evolu/react": patch
---

Expose and leverage canUseDOM
5 changes: 2 additions & 3 deletions packages/evolu-common-web/src/PlatformLive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import {
Mnemonic,
Platform,
SyncLock,
canUseDOM,
} from "@evolu/common";
import { Effect, Function, Layer, Predicate, ReadonlyArray } from "effect";
import { flushSync } from "react-dom";

const hasDoc = typeof document !== "undefined";

const isChromeWithOpfs = (): boolean =>
navigator.userAgentData != null &&
navigator.userAgentData.brands.find(
Expand All @@ -37,7 +36,7 @@ const isSafariWithOpfs = (): boolean => {
return Number(matches[1]) >= 17;
};

const name = hasDoc
const name = canUseDOM
? isChromeWithOpfs() || isFirefoxWithOpfs() || isSafariWithOpfs()
? "web-with-opfs"
: "web-without-opfs"
Expand Down
13 changes: 7 additions & 6 deletions packages/evolu-common-web/src/SqliteLive.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {
Sqlite,
SqliteRow,
canUseDOM,
parseJsonResults,
valuesToSqliteValues,
} from "@evolu/common";
import { Effect, Function, Layer } from "effect";

import sqlite3InitModule from "@sqlite.org/sqlite-wasm";

if (typeof document !== "undefined")
if (canUseDOM)
// @ts-expect-error Missing types.
self.sqlite3ApiConfig = {
debug: Function.constVoid,
Expand All @@ -17,11 +18,11 @@ if (typeof document !== "undefined")
error: Function.constVoid,
};

const sqlitePromise = sqlite3InitModule().then((sqlite3) => {
return typeof document === "undefined"
? new sqlite3.oo1.OpfsDb("/evolu/evolu1.db", "c")
: new sqlite3.oo1.JsStorageDb("local");
});
const sqlitePromise = sqlite3InitModule().then((sqlite3) =>
canUseDOM
? new sqlite3.oo1.JsStorageDb("local")
: new sqlite3.oo1.OpfsDb("/evolu/evolu1.db", "c"),
);

const exec: Sqlite["exec"] = (arg) =>
Effect.gen(function* (_) {
Expand Down
10 changes: 10 additions & 0 deletions packages/evolu-common/src/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,13 @@ export interface AppState {
}

export const AppState = Context.Tag<AppState>("evolu/AppState");

/**
* To detect whether DOM can be used.
* https://github.com/facebook/fbjs/blob/main/packages/fbjs/src/core/ExecutionEnvironment.js
*/
export const canUseDOM = !!(
typeof window !== "undefined" &&
window.document &&
window.document.createElement
);
1 change: 1 addition & 0 deletions packages/evolu-react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
SqliteDate,
String,
String1000,
canUseDOM,
cast,
id,
jsonArrayFrom,
Expand Down
1 change: 1 addition & 0 deletions packages/evolu-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export {
SqliteDate,
String,
String1000,
canUseDOM,
cast,
id,
jsonArrayFrom,
Expand Down

1 comment on commit 094e25a

@vercel
Copy link

@vercel vercel bot commented on 094e25a Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

evolu – ./

evolu-evolu.vercel.app
www.evolu.dev
evolu.dev
evolu-git-main-evolu.vercel.app
evolu.vercel.app

Please sign in to comment.