Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions apps/mobile/app/components/paywall/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import { IconButton } from "../ui/icon-button";
import { SvgView } from "../ui/svg";
import Heading from "../ui/typography/heading";
import Paragraph from "../ui/typography/paragraph";
import { db } from "../../common/database";

const Steps = {
select: 1,
Expand Down Expand Up @@ -139,7 +140,7 @@ const PayWall = (props: NavigationProps<"PayWall">) => {
}, [isFocused, step]);

useEffect(() => {
const sub = EV.subscribe(
const sub = db.eventManager.subscribe(
EVENTS.userSubscriptionUpdated,
(sub: User["subscription"]) => {
if (sub.plan === SubscriptionPlan.FREE) return;
Expand Down Expand Up @@ -1006,8 +1007,8 @@ const PricingPlanCard = ({
: "monthly"
}`
: pricingPlans.isGithubRelease
? (WebPlan?.period as string)
: (product?.productId as string)
? (WebPlan?.period as string)
: (product?.productId as string)
);
setStep(Steps.buy);
}}
Expand Down
38 changes: 26 additions & 12 deletions apps/mobile/app/hooks/use-app-events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -724,24 +724,39 @@ export const useAppEvents = () => {

useEffect(() => {
const subscriptions = [
EV.subscribe(EVENTS.syncCheckStatus, onCheckSyncStatus),
EV.subscribe(EVENTS.syncAborted, onSyncAborted),
EV.subscribe(EVENTS.appRefreshRequested, onSyncComplete),
db.eventManager.subscribe(EVENTS.syncCheckStatus, onCheckSyncStatus),
db.eventManager.subscribe(EVENTS.syncAborted, onSyncAborted),
db.eventManager.subscribe(EVENTS.appRefreshRequested, onSyncComplete),
db.eventManager.subscribe(EVENTS.userLoggedOut, onLogout),
db.eventManager.subscribe(EVENTS.userEmailConfirmed, onUserEmailVerified),
EV.subscribe(EVENTS.userSessionExpired, onUserSessionExpired),
db.eventManager.subscribe(
EVENTS.userSessionExpired,
onUserSessionExpired
),
db.eventManager.subscribe(
EVENTS.userSubscriptionUpdated,
onUserSubscriptionStatusChanged
),
EV.subscribe(EVENTS.fileDownload, onDownloadingAttachmentProgress),
EV.subscribe(EVENTS.fileUpload, onUploadingAttachmentProgress),
EV.subscribe(EVENTS.fileDownloaded, onDownloadedAttachmentProgress),
EV.subscribe(EVENTS.fileUploaded, onUploadedAttachmentProgress),
EV.subscribe(EVENTS.downloadCanceled, (data) => {
db.eventManager.subscribe(
EVENTS.fileDownload,
onDownloadingAttachmentProgress
),
db.eventManager.subscribe(
EVENTS.fileUpload,
onUploadingAttachmentProgress
),
db.eventManager.subscribe(
EVENTS.fileDownloaded,
onDownloadedAttachmentProgress
),
db.eventManager.subscribe(
EVENTS.fileUploaded,
onUploadedAttachmentProgress
),
db.eventManager.subscribe(EVENTS.downloadCanceled, (data) => {
useAttachmentStore.getState().setDownloading(data);
}),
EV.subscribe(EVENTS.uploadCanceled, (data) => {
db.eventManager.subscribe(EVENTS.uploadCanceled, (data) => {
useAttachmentStore.getState().setUploading(data);
}),
EV.subscribe(EVENTS.migrationStarted, (name) => {
Expand All @@ -767,7 +782,7 @@ export const useAppEvents = () => {
return;
endProgress();
}),
EV.subscribe(EVENTS.vaultLocked, async () => {
db.eventManager.subscribe(EVENTS.vaultLocked, async () => {
// Lock all notes in all tabs...
for (const tab of useTabStore.getState().tabs) {
const noteId = useTabStore.getState().getTab(tab.id)?.session?.noteId;
Expand Down Expand Up @@ -799,7 +814,6 @@ export const useAppEvents = () => {
return () => {
emitterSubscriptions.forEach((sub) => sub?.remove?.());
subscriptions.forEach((sub) => sub?.unsubscribe?.());
EV.unsubscribeAll();
};
}, [onSyncComplete, onUserUpdated]);

Expand Down
11 changes: 5 additions & 6 deletions apps/mobile/app/hooks/use-sync-progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export type SyncProgressEventType = {

const useSyncProgress = () => {
const [progress, setProgress] = useState<SyncProgressEventType>();
const EV = db.eventManager;

const onProgress = useCallback(
({ type, current, total }: SyncProgressEventType) => {
Expand All @@ -42,13 +41,13 @@ const useSyncProgress = () => {
setProgress(undefined);
};
useEffect(() => {
EV?.subscribe(EVENTS.syncProgress, onProgress);
EV?.subscribe(EVENTS.syncCompleted, onSyncComplete);
db.eventManager.subscribe(EVENTS.syncProgress, onProgress);
db.eventManager.subscribe(EVENTS.syncCompleted, onSyncComplete);
return () => {
EV?.unsubscribe(EVENTS.syncProgress, onProgress);
EV?.unsubscribe(EVENTS.syncCompleted, onSyncComplete);
db.eventManager.unsubscribe(EVENTS.syncProgress, onProgress);
db.eventManager.unsubscribe(EVENTS.syncCompleted, onSyncComplete);
};
}, [EV, onProgress]);
}, [onProgress]);

return {
progress
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/app-effects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
} from "./common";
import { AppEventManager, AppEvents } from "./common/app-events";
import { db } from "./common/db";
import { EV, EVENTS } from "@notesnook/core";
import { EVENTS } from "@notesnook/core";
import { registerKeyMap } from "./common/key-map";
import { updateStatus, removeStatus, getStatus } from "./hooks/use-status";
import { hashNavigate } from "./navigation";
Expand Down Expand Up @@ -113,15 +113,15 @@ export default function AppEffects() {
}
}

const fileDownloadEvents = EV.subscribeMulti(
const fileDownloadEvents = db.eventManager.subscribeMulti(
[EVENTS.fileDownloaded, EVENTS.fileDownload],
({ total, current }: { total: number; current: number }) => {
handleDownloadUploadProgress("download", total, current);
},
null
);

const fileUploadEvents = EV.subscribeMulti(
const fileUploadEvents = db.eventManager.subscribeMulti(
[EVENTS.fileUploaded, EVENTS.fileUpload],
({ total, current }: { total: number; current: number }) => {
handleDownloadUploadProgress("upload", total, current);
Expand Down
1 change: 0 additions & 1 deletion apps/web/src/components/editor/tiptap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ import { showFeatureNotAllowedToast } from "../../common/toasts";
import { UpgradeDialog } from "../../dialogs/buy-dialog/upgrade-dialog";
import { ConfirmDialog } from "../../dialogs/confirm";
import { strings } from "@notesnook/intl";
import { AppEventManager, AppEvents } from "../../common/app-events";

export type OnChangeHandler = (
content: () => string,
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/publish-view/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Loading, Refresh } from "../icons";
import { db } from "../../common/db";
import { writeText } from "clipboard-polyfill";
import { showToast } from "../../utils/toast";
import { EV, EVENTS, hosts, MonographAnalytics } from "@notesnook/core";
import { EVENTS, hosts } from "@notesnook/core";
import { useStore } from "../../stores/monograph-store";
import { Note } from "@notesnook/core";
import { strings } from "@notesnook/intl";
Expand Down Expand Up @@ -67,7 +67,7 @@ function PublishView(props: PublishViewProps) {
}, [monograph?.id, monographAnalytics]);

useEffect(() => {
const fileDownloadedEvent = EV.subscribe(
const fileDownloadedEvent = db.eventManager.subscribe(
EVENTS.fileDownloaded,
({ total, current, groupId }) => {
if (!groupId || !groupId.includes(note.id)) return;
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/hooks/use-vault.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EV, EVENTS } from "@notesnook/core";
import { EVENTS } from "@notesnook/core";
import { useEffect, useState } from "react";
import Vault from "../common/vault";
import { db } from "../common/db";
Expand All @@ -7,8 +7,8 @@ export function useVault() {
const [isLocked, setIsLocked] = useState(!db.vault.unlocked);

useEffect(() => {
EV.subscribe(EVENTS.vaultLocked, () => setIsLocked(true));
EV.subscribe(EVENTS.vaultUnlocked, () => setIsLocked(false));
db.eventManager.subscribe(EVENTS.vaultLocked, () => setIsLocked(true));
db.eventManager.subscribe(EVENTS.vaultUnlocked, () => setIsLocked(false));
}, []);

return {
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/stores/app-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { store as settingStore } from "./setting-store";
import BaseStore from "./index";
import { showToast } from "../utils/toast";
import { Notice } from "../common/notices";
import { EV, EVENTS, SYNC_CHECK_IDS, SyncOptions } from "@notesnook/core";
import { EVENTS, SYNC_CHECK_IDS, SyncOptions } from "@notesnook/core";
import { logger } from "../utils/logger";
import Config from "../utils/config";
import {
Expand Down Expand Up @@ -92,7 +92,7 @@ class AppStore extends BaseStore<AppStore> {
});
this.get().sync({ type: "full" });

EV.subscribe(EVENTS.appRefreshRequested, () => this.refresh());
db.eventManager.subscribe(EVENTS.appRefreshRequested, () => this.refresh());
db.eventManager.subscribe(EVENTS.syncCompleted, () => this.refresh());

db.eventManager.subscribe(EVENTS.syncProgress, ({ type, current }) => {
Expand All @@ -105,7 +105,7 @@ class AppStore extends BaseStore<AppStore> {
});
});

EV.subscribe(EVENTS.syncCheckStatus, async (type) => {
db.eventManager.subscribe(EVENTS.syncCheckStatus, async (type) => {
const { isAutoSyncEnabled, isSyncEnabled } = this.get();
switch (type) {
case SYNC_CHECK_IDS.sync:
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/stores/editor-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { store as appStore } from "./app-store";
import { useStore as useSettingStore } from "./setting-store";
import { db } from "../common/db";
import BaseStore from ".";
import { EV, EVENTS } from "@notesnook/core";
import { EVENTS } from "@notesnook/core";
import { logger } from "../utils/logger";
import Config from "../utils/config";
import { setDocumentTitle } from "../utils/dom";
Expand Down Expand Up @@ -255,7 +255,7 @@ class EditorStore extends BaseStore<EditorStore> {
closeTabs(...tabs.map((s) => s.id));
});

EV.subscribe(EVENTS.vaultLocked, () => {
db.eventManager.subscribe(EVENTS.vaultLocked, () => {
this.set((state) => {
state.sessions = state.sessions.map((session) => {
if (isLockedSession(session)) {
Expand Down
7 changes: 4 additions & 3 deletions apps/web/src/stores/user-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import createStore from "../common/store";
import { db } from "../common/db";
import BaseStore from "./index";
import { EV, EVENTS } from "@notesnook/core";
import { EVENTS } from "@notesnook/core";
import Config from "../utils/config";
import { hashNavigate } from "../navigation";
import { AuthenticatorType, User } from "@notesnook/core";
Expand All @@ -39,7 +39,7 @@ class UserStore extends BaseStore<UserStore> {
counter = 0;

init = () => {
EV.subscribe(EVENTS.userSessionExpired, async () => {
db.eventManager.subscribe(EVENTS.userSessionExpired, async () => {
Config.set("sessionExpired", true);
window.location.replace("/sessionexpired");
});
Expand All @@ -53,7 +53,8 @@ class UserStore extends BaseStore<UserStore> {
user,
isLoggedIn: true
});
if (Config.get("sessionExpired")) EV.publish(EVENTS.userSessionExpired);
if (Config.get("sessionExpired"))
db.eventManager.publish(EVENTS.userSessionExpired);
});

if (Config.get("sessionExpired")) return;
Expand Down
14 changes: 9 additions & 5 deletions apps/web/src/views/checkout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import "../app.css";
import { useEffect, useState } from "react";
import { Box, Button, Flex, Text } from "@theme-ui/components";
import { hardNavigate, hashNavigate, useQueryParams } from "../navigation";
import { hardNavigate, useQueryParams } from "../navigation";
import { Support } from "../components/icons";
import { HeadlessAuth } from "./auth";
import {
Expand All @@ -39,7 +39,8 @@ import { isUserSubscribed } from "../hooks/use-is-user-premium";
import { PLAN_METADATA } from "../dialogs/buy-dialog/plans";
import { planToAvailability } from "@notesnook/common";
import { FeatureCaption } from "../dialogs/buy-dialog/feature-caption";
import { EV, EVENTS } from "@notesnook/core";
import { EVENTS } from "@notesnook/core";
import { db } from "../common/db";

export type Plan = z.infer<typeof PlanSchema>;

Expand Down Expand Up @@ -119,9 +120,12 @@ function Checkout() {

useEffect(() => {
if (currentStep === 2) {
const event = EV.subscribe(EVENTS.userSubscriptionUpdated, () => {
hardNavigate("/notes#/welcome");
});
const event = db.eventManager.subscribe(
EVENTS.userSubscriptionUpdated,
() => {
hardNavigate("/notes#/welcome");
}
);
return () => {
event.unsubscribe();
};
Expand Down
7 changes: 4 additions & 3 deletions packages/common/src/hooks/use-is-feature-available.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
FeatureResult,
isFeatureAvailable
} from "../utils/index.js";
import { EV, EVENTS } from "@notesnook/core";
import { EVENTS } from "@notesnook/core";
import { database } from "../database.js";

export function useIsFeatureAvailable<TId extends FeatureId>(
id: TId | undefined,
Expand All @@ -36,7 +37,7 @@ export function useIsFeatureAvailable<TId extends FeatureId>(
if (!id) return;

isFeatureAvailable(id, value).then((result) => setResult(result));
const userSubscriptionUpdated = EV.subscribe(
const userSubscriptionUpdated = database.eventManager.subscribe(
EVENTS.userSubscriptionUpdated,
() => {
isFeatureAvailable(id, value).then((result) => setResult(result));
Expand All @@ -59,7 +60,7 @@ export function useAreFeaturesAvailable<TIds extends FeatureId[]>(

useEffect(() => {
areFeaturesAvailable(ids, values).then((result) => setResult(result));
const userSubscriptionUpdated = EV.subscribe(
const userSubscriptionUpdated = database.eventManager.subscribe(
EVENTS.userSubscriptionUpdated,
() => {
areFeaturesAvailable(ids, values).then((result) => setResult(result));
Expand Down
21 changes: 14 additions & 7 deletions packages/core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Lookup from "./lookup.js";
import { Content } from "../collections/content.js";
import Backup from "../database/backup.js";
import Hosts from "../utils/constants.js";
import { EV, EVENTS } from "../common.js";
import { EVENTS } from "../common.js";
import { LegacySettings } from "../collections/legacy-settings.js";
import Migrations from "./migrations.js";
import UserManager from "./user-manager.js";
Expand Down Expand Up @@ -126,7 +126,11 @@ class Database {
);
return (
this._fs ||
(this._fs = new FileStorage(this.options.fs, this.tokenManager))
(this._fs = new FileStorage(
this.options.fs,
this.tokenManager,
this.eventManager
))
);
};

Expand Down Expand Up @@ -191,7 +195,7 @@ class Database {
options!: Options;
eventSource?: EventSource | null;

tokenManager = new TokenManager(this.kv);
tokenManager = new TokenManager(this.kv, this.eventManager);
mfa = new MFAManager(this.tokenManager);
subscriptions = new Subscriptions(this);
circle = new Circle(this);
Expand Down Expand Up @@ -293,10 +297,13 @@ class Database {
this.connectSSE,
this
);
EV.subscribe(EVENTS.tokenRefreshed, () => this.connectSSE());
EV.subscribe(EVENTS.attachmentDeleted, async (attachment: Attachment) => {
await this.fs().cancel(attachment.hash);
});
this.eventManager.subscribe(EVENTS.tokenRefreshed, () => this.connectSSE());
this.eventManager.subscribe(
EVENTS.attachmentDeleted,
async (attachment: Attachment) => {
await this.fs().cancel(attachment.hash);
}
);
this.eventManager.subscribe(EVENTS.userLoggedOut, async () => {
await this.monographs.clear();
await this.fs().clear();
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/api/sync/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

import { Cipher } from "@notesnook/crypto";
import Database from "../index.js";
import { CURRENT_DATABASE_VERSION, EV, EVENTS } from "../../common.js";
import { CURRENT_DATABASE_VERSION, EVENTS } from "../../common.js";
import { logger } from "../../logger.js";
import {
SyncItem,
Expand Down Expand Up @@ -48,7 +48,7 @@ class Collector {
): AsyncGenerator<SyncTransferItem, void, unknown> {
const key = await this.db.user.getEncryptionKey();
if (!key || !key.key || !key.salt) {
EV.publish(EVENTS.userSessionExpired);
this.db.eventManager.publish(EVENTS.userSessionExpired);
throw new Error("User encryption key not generated. Please relogin.");
}

Expand Down
Loading
Loading