Skip to content

Commit

Permalink
thrw on write
Browse files Browse the repository at this point in the history
  • Loading branch information
kepta committed Dec 5, 2023
1 parent edd2594 commit 5bfa6a6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/app-window/main-content/pages/ws/_name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { useStore, useTrack } from '@nalanda/react';
import DeleteIcon from '@spectrum-icons/workflow/Delete';
import FolderAddIcon from '@spectrum-icons/workflow/FolderAdd';
import React, { useEffect, useMemo } from 'react';
import { useParams } from 'wouter';

import { slicePage } from '@bangle.io/slice-page';
import { APP_DIALOG_NAME, sliceUI } from '@bangle.io/slice-ui';
import { sliceWorkspace } from '@bangle.io/slice-workspace';
import { FilesTable, MainContentWrapper } from '@bangle.io/ui';
Expand Down Expand Up @@ -42,10 +42,10 @@ function FileActions({ disabledKeys, onAction }: FileActionsProps) {
}

export default function PageWsName() {
const params = useParams();
const store = useStore();

const wsName = params.wsName;
const { wsName } = useTrack(slicePage);

const { allFiles, workspace } = useTrack(sliceWorkspace);
const { widescreen } = useTrack(sliceUI);

Expand Down
10 changes: 10 additions & 0 deletions packages/lib-window/slice-workspace/slice-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Workspace } from '@bangle.io/workspace';
// WARNING: If changing also make changes to slice-workspace-naukar
const key = createKey('slice-workspace', [slicePage]);

const refreshAllFilesCounterField = key.field(0);
const rawWorkspaceField = key.field<Workspace | undefined>(undefined);

const rawAllFilesField = key.field<
Expand Down Expand Up @@ -42,6 +43,10 @@ const currentAllFilesField = key.derive((state) => {
return rawAllFiles.files;
});

function refreshAllFiles() {
return refreshAllFilesCounterField.update((c) => c + 1);
}

key.effect(async function workspaceInit(store) {
const { wsName } = slicePage.track(store);

Expand All @@ -54,6 +59,9 @@ key.effect(async function workspaceInit(store) {
const workspace = await Workspace.create({
wsName,
database: eternalVars.appDatabase,
onChange(event) {
store.dispatch(refreshAllFiles());
},
});

store.dispatch(rawWorkspaceField.update(workspace));
Expand All @@ -65,6 +73,8 @@ key.effect(async function workspaceInit(store) {

key.effect(async function allFilesInit(store) {
const workspace = currentWorkspaceField.track(store);
// to refresh the allFiles list
const refreshAllFilesCounter = refreshAllFilesCounterField.track(store);

if (!workspace) {
const hasData = currentAllFilesField.get(store.state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class FileStorageIndexedDB implements BaseFileStorageProvider {
}

async createFile(wsPath: string, file: File): Promise<void> {
await this.writeFile(wsPath, file);
const path = toFSPath(wsPath);
await this._idb.writeFile(path, file);

this.options.onChange({
type: 'create',
Expand Down Expand Up @@ -129,6 +130,10 @@ export class FileStorageIndexedDB implements BaseFileStorageProvider {
}

async writeFile(wsPath: string, file: File): Promise<void> {
if (!(await this.fileExists(wsPath))) {
throw new Error(`Cannot write! File ${wsPath} does not exist`);
}

const path = toFSPath(wsPath);
await this._idb.writeFile(path, file);
this.options.onChange({
Expand Down
18 changes: 15 additions & 3 deletions packages/lib/workspace/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { readFileAsText } from '@bangle.io/baby-fs';
import { WorkspaceType } from '@bangle.io/constants';
import { FileStorageIndexedDB } from '@bangle.io/file-storage-indexeddb';
import { AppDatabase, BaseFileStorageProvider } from '@bangle.io/shared-types';
import {
AppDatabase,
BaseFileStorageProvider,
FileStorageChangeEvent,
} from '@bangle.io/shared-types';
import {
getExtension,
isValidFileWsPath,
Expand All @@ -16,16 +20,19 @@ import { logger } from './logger';
export type Config = {
readonly wsName: string;
readonly database: AppDatabase;
readonly onChange?: (event: FileStorageChangeEvent) => void;
};

export class Workspace {
private provider!: BaseFileStorageProvider;
destroyed = false;

get wsName() {
return this.config.wsName;
}

destroy() {
this.destroyed = true;
this.provider.destroy();
}

Expand All @@ -50,7 +57,7 @@ export class Workspace {
if (info?.type === WorkspaceType.Browser) {
this.provider = new FileStorageIndexedDB();
} else {
throw new Error(`Workspace type ${info?.type} not supported`);
throw new Error(`Workspace type ${info?.type} not implemented`);
}

if (!this.provider.isSupported()) {
Expand All @@ -61,7 +68,12 @@ export class Workspace {
wsName: this.wsName,

onChange: (event) => {
// TODO cross tab sync
if (this.destroyed) {
return;
}

this.config.onChange?.(event);

switch (event.type) {
case 'create': {
break;
Expand Down

0 comments on commit 5bfa6a6

Please sign in to comment.