Skip to content

Commit

Permalink
rebuild reloadProject workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmatthewnguyen105 committed Dec 21, 2023
1 parent 10f0178 commit 6f42a12
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 185 deletions.
4 changes: 2 additions & 2 deletions src/_node/file/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,12 @@ const rename = ({
}
})();

const action: TFileAction = {
/* const action: TFileAction = {
type: "rename",
param1: { currentFileUid: renameUid, parentUid: parentUid },
param2: { orgName: _orgName, newName: _newName },
};
dispatch(setFileAction(action));
dispatch(setFileAction(action)); */

// update redux
dispatch(setCurrentFileUid(newUid));
Expand Down
286 changes: 158 additions & 128 deletions src/_node/file/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import {
TFileNodeData,
TFileNodeTreeData,
TFileParserResponse,
TIDBProjectLoaderBaseResponse,
TLocalProjectLoaderBaseResponse,
TNodeUid,
TProjectLoaderResponse,
} from "../";
import { getInitialFileUidToOpen, sortFilesByASC } from "./helpers";
import { TFileHandlerInfo, TFileHandlerInfoObj, TZipFileInfo } from "./types";
Expand Down Expand Up @@ -101,8 +102,8 @@ export const loadIDBProject = async (
projectPath: string,
isReload: boolean = false,
fileTree?: TFileNodeTreeData,
): Promise<TProjectLoaderResponse> => {
return new Promise<TProjectLoaderResponse>(async (resolve, reject) => {
): Promise<TIDBProjectLoaderBaseResponse> => {
return new Promise<TIDBProjectLoaderBaseResponse>(async (resolve, reject) => {
try {
const deletedUidsObj: { [uid: TNodeUid]: true } = {};
if (isReload) {
Expand Down Expand Up @@ -238,174 +239,203 @@ export const loadLocalProject = async (
osType: TOsType,
isReload: boolean = false,
fileTree?: TFileNodeTreeData,
): Promise<TProjectLoaderResponse> => {
return new Promise<TProjectLoaderResponse>(async (resolve, reject) => {
try {
// verify project-handler permission
if (!(await verifyFileHandlerPermission(projectHandle)))
throw "project handler permission error";
): Promise<TLocalProjectLoaderBaseResponse> => {
return new Promise<TLocalProjectLoaderBaseResponse>(
async (resolve, reject) => {
try {
// verify project-handler permission
if (!(await verifyFileHandlerPermission(projectHandle)))
throw "project handler permission error";

const deletedUidsObj: { [uid: TNodeUid]: true } = {};
if (isReload) {
getSubNodeUidsByBfs(
RootNodeUid,
fileTree as TFileNodeTreeData,
false,
).map((uid) => {
deletedUidsObj[uid] = true;
});
}

const deletedUidsObj: { [uid: TNodeUid]: true } = {};
if (isReload) {
getSubNodeUidsByBfs(
RootNodeUid,
fileTree as TFileNodeTreeData,
false,
).map((uid) => {
deletedUidsObj[uid] = true;
});
}
// build project root-handler
const rootHandler: TFileHandlerInfo = {
uid: RootNodeUid,
parentUid: null,
children: [],

path: `/${projectHandle.name}`,
kind: "directory",
name: projectHandle.name,

handler: projectHandle,
};
const handlerArr: TFileHandlerInfo[] = [rootHandler];
const handlerObj: TFileHandlerInfoObj = { [RootNodeUid]: rootHandler };

// loop through the project
const dirHandlers: TFileHandlerInfo[] = [rootHandler];
while (dirHandlers.length) {
const {
uid: p_uid,
path: p_path,
handler: p_handler,
} = dirHandlers.shift() as TFileHandlerInfo;

for await (const entry of (
p_handler as FileSystemDirectoryHandle
).values()) {
// skip system directories & hidden files
if (SystemDirectories[osType][entry.name] || entry.name[0] === ".")
continue;

// build project root-handler
const rootHandler: TFileHandlerInfo = {
uid: RootNodeUid,
parentUid: null,
children: [],
// build c_handler
const c_uid = _path.join(p_uid, entry.name) as string;
const c_path = _path.join(p_path, entry.name) as string;

path: `/${projectHandle.name}`,
kind: "directory",
name: projectHandle.name,
const c_kind = entry.kind;

handler: projectHandle,
};
const handlerArr: TFileHandlerInfo[] = [rootHandler];
const handlerObj: TFileHandlerInfoObj = { [RootNodeUid]: rootHandler };
const nameArr = entry.name.split(".");
const c_ext = nameArr.length > 1 ? nameArr.pop() : undefined;
const c_name = nameArr.join(".");

// loop through the project
const dirHandlers: TFileHandlerInfo[] = [rootHandler];
while (dirHandlers.length) {
const {
uid: p_uid,
path: p_path,
handler: p_handler,
} = dirHandlers.shift() as TFileHandlerInfo;

for await (const entry of (
p_handler as FileSystemDirectoryHandle
).values()) {
// skip system directories & hidden files
if (SystemDirectories[osType][entry.name] || entry.name[0] === ".")
continue;

// build c_handler
const c_uid = _path.join(p_uid, entry.name) as string;
const c_path = _path.join(p_path, entry.name) as string;

const c_kind = entry.kind;

const nameArr = entry.name.split(".");
const c_ext = nameArr.length > 1 ? nameArr.pop() : undefined;
const c_name = nameArr.join(".");

let c_content: Uint8Array | undefined = undefined;
if (c_kind === "file") {
const fileEntry = await (entry as FileSystemFileHandle).getFile();
c_content = Buffer.from(await fileEntry.arrayBuffer());
}
let c_content: Uint8Array | undefined = undefined;
if (c_kind === "file") {
const fileEntry = await (entry as FileSystemFileHandle).getFile();
c_content = Buffer.from(await fileEntry.arrayBuffer());
}

const c_handlerInfo: TFileHandlerInfo = {
uid: c_uid,
parentUid: p_uid,
children: [],
const c_handlerInfo: TFileHandlerInfo = {
uid: c_uid,
parentUid: p_uid,
children: [],

path: c_path,
kind: c_kind,
name: c_kind === "directory" ? entry.name : c_name,
path: c_path,
kind: c_kind,
name: c_kind === "directory" ? entry.name : c_name,

ext: c_ext,
content: c_content,
ext: c_ext,
content: c_content,

handler: entry,
};
handler: entry,
};

// update handler-arr, handler-obj
handlerObj[p_uid].children.push(c_uid);
handlerObj[c_uid] = c_handlerInfo;
handlerArr.push(c_handlerInfo);
c_kind === "directory" && dirHandlers.push(c_handlerInfo);
// update handler-arr, handler-obj
handlerObj[p_uid].children.push(c_uid);
handlerObj[c_uid] = c_handlerInfo;
handlerArr.push(c_handlerInfo);
c_kind === "directory" && dirHandlers.push(c_handlerInfo);

// remove c_uid from deletedUids array
delete deletedUidsObj[c_uid];
// remove c_uid from deletedUids array
delete deletedUidsObj[c_uid];
}
}
}

// sort by ASC directory/file
sortFilesByASC(handlerObj);
// define the initialFileUidToOpen
let _initialFileUidToOpen: TNodeUid = getInitialFileUidToOpen(handlerObj);
// sort by ASC directory/file
sortFilesByASC(handlerObj);
// define the initialFileUidToOpen
let _initialFileUidToOpen = getInitialFileUidToOpen(handlerObj);

// build fileTree and fileHandlers
const _fileTree: TFileNodeTreeData = {};
const _fileHandlers: TFileHandlerCollection = {};
Object.keys(handlerObj).map((uid) => {
const { parentUid, children, path, kind, name, ext, content, handler } =
handlerObj[uid];
// build fileTree and fileHandlers
const _fileTree: TFileNodeTreeData = {};
const _fileHandlers: TFileHandlerCollection = {};
Object.keys(handlerObj).map((uid) => {
const {
parentUid,
children,
path,
kind,
name,
ext,
content,
handler,
} = handlerObj[uid];

const parsable = kind === "file" && ParsableFileTypes[ext as string];
const fileContent = parsable ? content?.toString() : "";
const parsable = kind === "file" && ParsableFileTypes[ext as string];
const fileContent = parsable ? content?.toString() : "";

_fileTree[uid] = {
uid,
parentUid: parentUid,
_fileTree[uid] = {
uid,
parentUid: parentUid,

displayName: name,
displayName: name,

isEntity: kind === "file",
children: [...children],
isEntity: kind === "file",
children: [...children],

data: {
valid: true,
data: {
valid: true,

path: path,
path: path,

kind: kind,
name: name,
ext: ext ?? "",
kind: kind,
name: name,
ext: ext ?? "",

orgContent: fileContent,
content: fileContent,
contentInApp: "",
orgContent: fileContent,
content: fileContent,
contentInApp: "",

changed: false,
} as TFileNodeData,
} as TFileNode;
changed: false,
} as TFileNodeData,
} as TFileNode;

_fileHandlers[uid] = handler as FileSystemHandle;
});
_fileHandlers[uid] = handler as FileSystemHandle;
});

resolve({
handlerArr,
_fileHandlers,
_fileTree,
_initialFileUidToOpen,
deletedUidsObj,
deletedUids: Object.keys(deletedUidsObj),
});
} catch (err) {
LogAllow && console.log("error in loadLocalProject API", err);
reject(err);
}
});
resolve({
handlerArr,
_fileHandlers,
_fileTree,
_initialFileUidToOpen,
deletedUidsObj,
deletedUids: Object.keys(deletedUidsObj),
});
} catch (err) {
LogAllow && console.log("error in loadLocalProject API", err);
reject(err);
}
},
);
};
export const buildNohostIDB = async (
handlerArr: TFileHandlerInfo[],
deletedPaths: TNodeUid[] = [],
): Promise<void> => {
return new Promise<void>(async (resolve, reject) => {
try {
await Promise.all(
deletedPaths.map(async (path) => {
try {
await _removeIDBDirectoryOrFile(path);
} catch (err) {
LogAllow &&
console.error("error while deleting in buildNohostIDB API", err);
}
}),
);
await Promise.all(
handlerArr.map(async (_handler) => {
const { kind, path, content } = _handler;
if (kind === "directory") {
try {
await _createIDBDirectory(path);
} catch (err) {
console.error("error in buildNohostIDB API", err);
LogAllow &&
console.error(
"error whil creating a directory in buildNohostIDB API",
err,
);
}
} else {
try {
await _writeIDBFile(path, content as Uint8Array);
} catch (err) {
console.error("error in buildNohostIDB API", err);
LogAllow &&
console.error(
"error while creating a file in buildNohostIDB API",
err,
);
}
}
}),
Expand Down
13 changes: 10 additions & 3 deletions src/_node/file/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ export type TFileHandlerInfo = {
};
export type TFileHandlerInfoObj = { [uid: TNodeUid]: TFileHandlerInfo };

export type TProjectLoaderResponse = {
handlerArr?: TFileHandlerInfo[];
_fileHandlers?: TFileHandlerCollection;
export type TIDBProjectLoaderBaseResponse = {
_fileTree: TFileNodeTreeData;
_initialFileUidToOpen: TNodeUid;

deletedUids: TNodeUid[];
deletedUidsObj: { [uid: TNodeUid]: true };
};
export type TLocalProjectLoaderBaseResponse = {
handlerArr: TFileHandlerInfo[];
_fileHandlers: TFileHandlerCollection;

_fileTree: TFileNodeTreeData;
_initialFileUidToOpen: TNodeUid;
Expand Down
1 change: 1 addition & 0 deletions src/_redux/main/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const MainContext: Context<TMainContext> = createContext<TMainContext>({

importProject: () => {},
reloadCurrentProject: () => {},

onUndo: () => {},
onRedo: () => {},
});
Loading

0 comments on commit 6f42a12

Please sign in to comment.