Compare commits

...

6 Commits

Author SHA1 Message Date
Ammar Ahmed
8006914cd8 Merge branch 'master' into fix-db-init-background
Signed-off-by: Ammar Ahmed <40239442+ammarahm-ed@users.noreply.github.com>
2023-04-15 23:22:20 +05:00
ammarahm-ed
cc2706a39e mobile: cleanup 2023-04-11 00:38:17 +05:00
ammarahm-ed
ea3c480edb mobile: improve db init in app/share extension 2023-04-11 00:37:50 +05:00
ammarahm-ed
abbcc1263a mobile: don't call db.init on logout 2023-04-11 00:18:50 +05:00
ammarahm-ed
c77e29d7aa mobile: fix db init in background 2023-04-10 15:21:26 +05:00
ammarahm-ed
ba68adbeb1 core: init collections separately 2023-04-10 15:20:27 +05:00
9 changed files with 53 additions and 57 deletions

View File

@@ -68,14 +68,3 @@ db.host(
ISSUES_HOST: "https://issues.streetwriters.co" ISSUES_HOST: "https://issues.streetwriters.co"
} }
); );
export async function loadDatabase() {
// if (!DB) {
// let module = await import(/* webpackChunkName: "notes-core" */ 'notes-core/api/index');
// DB = module.default;
// }
// db = new DB(Storage, Platform.OS === 'ios' ? EventSource : AndroidEventSource, filesystem);
// if (DOMParser) {
// await DOMParser.prepare();
// }
}

View File

@@ -17,12 +17,14 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import notifee from "@notifee/react-native";
import React, { useCallback, useEffect, useRef } from "react"; import React, { useCallback, useEffect, useRef } from "react";
import { Platform, View } from "react-native"; import { Platform, View } from "react-native";
import RNBootSplash from "react-native-bootsplash"; import RNBootSplash from "react-native-bootsplash";
import { checkVersion } from "react-native-check-version"; import { checkVersion } from "react-native-check-version";
import Config from "react-native-config";
import { enabled } from "react-native-privacy-snapshot"; import { enabled } from "react-native-privacy-snapshot";
import { DatabaseLogger, db, loadDatabase } from "../../common/database"; import { DatabaseLogger, db } from "../../common/database";
import { useAppState } from "../../hooks/use-app-state"; import { useAppState } from "../../hooks/use-app-state";
import BiometricService from "../../services/biometrics"; import BiometricService from "../../services/biometrics";
import { eSendEvent, presentSheet } from "../../services/event-manager"; import { eSendEvent, presentSheet } from "../../services/event-manager";
@@ -36,6 +38,7 @@ import { useSettingStore } from "../../stores/use-setting-store";
import { useThemeStore } from "../../stores/use-theme-store"; import { useThemeStore } from "../../stores/use-theme-store";
import { useUserStore } from "../../stores/use-user-store"; import { useUserStore } from "../../stores/use-user-store";
import { eOpenAnnouncementDialog } from "../../utils/events"; import { eOpenAnnouncementDialog } from "../../utils/events";
import { getGithubVersion } from "../../utils/github-version";
import { SIZE } from "../../utils/size"; import { SIZE } from "../../utils/size";
import { sleep } from "../../utils/time"; import { sleep } from "../../utils/time";
import { SVG } from "../auth/background"; import { SVG } from "../auth/background";
@@ -50,9 +53,6 @@ import { SvgView } from "../ui/svg";
import Heading from "../ui/typography/heading"; import Heading from "../ui/typography/heading";
import Paragraph from "../ui/typography/paragraph"; import Paragraph from "../ui/typography/paragraph";
import { Walkthrough } from "../walkthroughs"; import { Walkthrough } from "../walkthroughs";
import Config from "react-native-config";
import { getGithubVersion } from "../../utils/github-version";
import notifee from "@notifee/react-native";
const Launcher = React.memo( const Launcher = React.memo(
function Launcher() { function Launcher() {
@@ -69,7 +69,7 @@ const Launcher = React.memo(
const introCompleted = useSettingStore( const introCompleted = useSettingStore(
(state) => state.settings.introCompleted (state) => state.settings.introCompleted
); );
const dbInitCompleted = useRef(false);
const loadNotes = useCallback(async () => { const loadNotes = useCallback(async () => {
if (verifyUser) { if (verifyUser) {
return; return;
@@ -80,28 +80,25 @@ const Launcher = React.memo(
initialize(); initialize();
setImmediate(() => { setImmediate(() => {
setLoading(false); setLoading(false);
if (!dbInitCompleted.current) {
setImmediate(() => doAppLoadActions());
}
}); });
}); });
}); });
}, [doAppLoadActions, setLoading, verifyUser]); }, [setLoading, verifyUser]);
const init = useCallback(async () => { const init = useCallback(async () => {
if (!dbInitCompleted.current) { if (!db.isInitialized) {
await RNBootSplash.hide({ fade: true }); await RNBootSplash.hide({ fade: true });
await loadDatabase();
DatabaseLogger.info("Initializing database"); DatabaseLogger.info("Initializing database");
await db.init(); await db.init();
dbInitCompleted.current = true;
} }
if (db.migrations.required() && !verifyUser) { if (db.migrations.required() && !verifyUser) {
presentSheet({ presentSheet({
component: <Migrate />, component: <Migrate />,
onClose: async () => { onClose: async () => {
await db.init(); if (!db.isInitialized) {
await db.init();
}
loadNotes(); loadNotes();
}, },
disableClosing: true disableClosing: true
@@ -120,9 +117,6 @@ const Launcher = React.memo(
if (!loading) { if (!loading) {
doAppLoadActions(); doAppLoadActions();
} }
return () => {
dbInitCompleted.current = false;
};
}, [doAppLoadActions, loading]); }, [doAppLoadActions, loading]);
const doAppLoadActions = useCallback(async () => { const doAppLoadActions = useCallback(async () => {

View File

@@ -558,7 +558,7 @@ export const useAppEvents = () => {
let shareExtensionOpened = MMKV.getString("shareExtensionOpened"); let shareExtensionOpened = MMKV.getString("shareExtensionOpened");
if (notesAddedFromIntent) { if (notesAddedFromIntent) {
if (Platform.OS === "ios") { if (Platform.OS === "ios") {
await db.init(); await db.initCollections();
await db.notes.init(); await db.notes.init();
} }
useNoteStore.getState().setNotes(); useNoteStore.getState().setNotes();

View File

@@ -317,7 +317,6 @@ export const settingsGroups: SettingSection[] = [
await PremiumService.setPremiumStatus(); await PremiumService.setPremiumStatus();
await BiometicService.resetCredentials(); await BiometicService.resetCredentials();
MMKV.clearStore(); MMKV.clearStore();
await db.init();
await clearAllStores(); await clearAllStores();
SettingsService.init(); SettingsService.init();
setTimeout(() => { setTimeout(() => {

View File

@@ -85,13 +85,20 @@ async function getNextMonthlyReminderDate(
return await getNextMonthlyReminderDate(reminder, dayjs().year() + 1); return await getNextMonthlyReminderDate(reminder, dayjs().year() + 1);
} }
async function initDatabase(notes = true) {
if (db.isInitialized) return;
await db.initCollections();
if (notes) {
await db.notes?.init();
}
}
const onEvent = async ({ type, detail }: Event) => { const onEvent = async ({ type, detail }: Event) => {
const { notification, pressAction, input } = detail; const { notification, pressAction, input } = detail;
if (type === EventType.DELIVERED && Platform.OS === "android") { if (type === EventType.DELIVERED && Platform.OS === "android") {
const reminder = db.reminders?.reminder(notification?.id?.split("_")[0]); const reminder = db.reminders?.reminder(notification?.id?.split("_")[0]);
if (reminder && reminder.recurringMode === "month") { if (reminder && reminder.recurringMode === "month") {
await db.init(); await initDatabase();
await db.notes?.init();
await scheduleNotification(reminder); await scheduleNotification(reminder);
} }
return; return;
@@ -100,8 +107,7 @@ const onEvent = async ({ type, detail }: Event) => {
notifee.decrementBadgeCount(); notifee.decrementBadgeCount();
if (notification?.data?.type === "quickNote") return; if (notification?.data?.type === "quickNote") return;
MMKV.removeItem("appState"); MMKV.removeItem("appState");
await db.init(); await initDatabase();
await db.notes?.init();
if (notification?.data?.type === "reminder") { if (notification?.data?.type === "reminder") {
const reminder = db.reminders?.reminder(notification.id?.split("_")[0]); const reminder = db.reminders?.reminder(notification.id?.split("_")[0]);
await sleep(1000); await sleep(1000);
@@ -131,8 +137,7 @@ const onEvent = async ({ type, detail }: Event) => {
notifee.decrementBadgeCount(); notifee.decrementBadgeCount();
switch (pressAction?.id) { switch (pressAction?.id) {
case "REMINDER_SNOOZE": { case "REMINDER_SNOOZE": {
await db.init(); await initDatabase();
await db.notes?.init();
const reminder = db.reminders?.reminder( const reminder = db.reminders?.reminder(
notification?.id?.split("_")[0] notification?.id?.split("_")[0]
); );
@@ -151,8 +156,7 @@ const onEvent = async ({ type, detail }: Event) => {
break; break;
} }
case "REMINDER_DISABLE": { case "REMINDER_DISABLE": {
await db.init(); await initDatabase();
await db.notes?.init();
const reminder = db.reminders?.reminder( const reminder = db.reminders?.reminder(
notification?.id?.split("_")[0] notification?.id?.split("_")[0]
); );
@@ -168,8 +172,7 @@ const onEvent = async ({ type, detail }: Event) => {
break; break;
} }
case "UNPIN": { case "UNPIN": {
await db.init(); await initDatabase();
await db.notes?.init();
remove(notification?.id as string); remove(notification?.id as string);
const reminder = db.reminders?.reminder( const reminder = db.reminders?.reminder(
notification?.id?.split("_")[0] notification?.id?.split("_")[0]
@@ -198,7 +201,7 @@ const onEvent = async ({ type, detail }: Event) => {
reply_button_text: "Take note", reply_button_text: "Take note",
reply_placeholder_text: "Write something..." reply_placeholder_text: "Write something..."
}); });
await db.init(); await initDatabase(false);
await db.notes?.add({ await db.notes?.add({
content: { content: {
type: "tiptap", type: "tiptap",

View File

@@ -32,7 +32,7 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
import Icon from "react-native-vector-icons/MaterialCommunityIcons"; import Icon from "react-native-vector-icons/MaterialCommunityIcons";
import { db } from "../app/common/database"; import { db } from "../app/common/database";
import { getElevation } from "../app/utils"; import { getElevation } from "../app/utils";
import { useShareStore } from "./store"; import { initDatabase, useShareStore } from "./store";
const ListItem = ({ item, mode, close }) => { const ListItem = ({ item, mode, close }) => {
const colors = useShareStore((state) => state.colors); const colors = useShareStore((state) => state.colors);
@@ -226,8 +226,7 @@ export const Search = ({ close, getKeyboardHeight, quicknote, mode }) => {
const onSearch = async () => { const onSearch = async () => {
if (!searchableItems.current) { if (!searchableItems.current) {
await db.init(); await initDatabase();
await db.notes.init();
searchableItems.current = get(); searchableItems.current = get();
} }
if (timer.current) { if (timer.current) {
@@ -248,8 +247,7 @@ export const Search = ({ close, getKeyboardHeight, quicknote, mode }) => {
useEffect(() => { useEffect(() => {
(async () => { (async () => {
await db.init(); await initDatabase();
await db.notes.init();
searchableItems.current = get(); searchableItems.current = get();
setSearchResults(searchableItems.current); setSearchResults(searchableItems.current);
})(); })();

View File

@@ -47,7 +47,7 @@ import { getElevation } from "../app/utils";
import { eOnLoadNote } from "../app/utils/events"; import { eOnLoadNote } from "../app/utils/events";
import { sleep } from "../app/utils/time"; import { sleep } from "../app/utils/time";
import { Search } from "./search"; import { Search } from "./search";
import { useShareStore } from "./store"; import { initDatabase, useShareStore } from "./store";
import { Editor } from "./editor"; import { Editor } from "./editor";
const getLinkPreview = (url) => { const getLinkPreview = (url) => {
return getPreviewData(url, 5000); return getPreviewData(url, 5000);
@@ -314,8 +314,7 @@ const ShareView = ({ quicknote = false }) => {
const onPress = async () => { const onPress = async () => {
setLoading(true); setLoading(true);
await db.init(); await initDatabase();
await db.notes.init();
await sleep(1500); await sleep(1500);
if (!noteContent.current) return; if (!noteContent.current) return;
if (appendNote && !db.notes.note(appendNote.id)) { if (appendNote && !db.notes.note(appendNote.id)) {

View File

@@ -25,6 +25,15 @@ import {
COLOR_SCHEME_LIGHT COLOR_SCHEME_LIGHT
} from "../app/utils/color-scheme"; } from "../app/utils/color-scheme";
import { MMKV } from "../app/common/database/mmkv"; import { MMKV } from "../app/common/database/mmkv";
import { db } from "../app/common/database";
export async function initDatabase() {
if (!db.isInitialized) {
// Only load collections in database.
await db.initCollections();
await db.notes.init();
}
}
const StorageKeys = { const StorageKeys = {
appendNote: "shareMenuAppendNote", appendNote: "shareMenuAppendNote",

View File

@@ -58,6 +58,7 @@ var NNEventSource;
// const DIFFERENCE_THRESHOLD = 20 * 1000; // const DIFFERENCE_THRESHOLD = 20 * 1000;
// const MAX_TIME_ERROR_FAILURES = 5; // const MAX_TIME_ERROR_FAILURES = 5;
class Database { class Database {
isInitialized = false;
/** /**
* *
* @param {any} storage * @param {any} storage
@@ -131,6 +132,19 @@ class Database {
this.pricing = new Pricing(); this.pricing = new Pricing();
this.subscriptions = new Subscriptions(this.user.tokenManager); this.subscriptions = new Subscriptions(this.user.tokenManager);
await this.initCollections();
await this.settings.init();
await this.outbox.init();
await this.user.init();
await this.migrations.init();
this.isInitialized = true;
if (this.migrations.required()) {
logger.warn("Database migration is required.");
}
}
async initCollections() {
// collections // collections
/** @type {Notes} */ /** @type {Notes} */
this.notes = await Notes.new(this, "notes", true, true); this.notes = await Notes.new(this, "notes", true, true);
@@ -154,15 +168,6 @@ class Database {
this.relations = await Relations.new(this, "relations"); this.relations = await Relations.new(this, "relations");
this.trash = new Trash(this); this.trash = new Trash(this);
await this.settings.init();
await this.outbox.init();
await this.user.init();
await this.migrations.init();
if (this.migrations.required()) {
logger.warn("Database migration is required.");
}
} }
disconnectSSE() { disconnectSSE() {