-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Application will save code if there is no edit operation in 1 second. File will save in `appDataDir/files/*.dat`
- Loading branch information
Showing
17 changed files
with
258 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useEffect, useRef } from "react" | ||
|
||
export default function useTimeoutInvoke<T>( | ||
call: (params: T) => void, | ||
timeout: number, | ||
): [(params: T) => void, (params: T) => void, () => void] { | ||
const timer = useRef<NodeJS.Timeout | null>(null) | ||
const param = useRef<T | null>(null) | ||
const caller = useRef<(params: T)=>void>() | ||
|
||
useEffect(()=>{ | ||
caller.current = call | ||
}, [call]) | ||
|
||
function cancel() { | ||
if (timer.current) { | ||
clearTimeout(timer.current) | ||
timer.current = null | ||
} | ||
} | ||
function delay(params: T) { | ||
param.current = params | ||
cancel() | ||
timer.current = setTimeout(() => { | ||
if (timer.current) { | ||
clearTimeout(timer.current) | ||
} | ||
caller.current!(param.current!) | ||
}, timeout) | ||
} | ||
function invokeNow(params: T) { | ||
cancel() | ||
caller.current!(params) | ||
} | ||
|
||
return [delay, invokeNow, cancel] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { path, fs } from "@tauri-apps/api" | ||
import { Source } from "@/store/source" | ||
import * as log from "tauri-plugin-log-api" | ||
|
||
async function getDataDir() { | ||
const filesDir = await path.join(await path.appDataDir(), "files") | ||
if (!(await fs.exists(filesDir))) { | ||
await fs.createDir(filesDir) | ||
} | ||
return filesDir | ||
} | ||
|
||
async function getFilePath(id: number): Promise<string> { | ||
const dir = await getDataDir() | ||
const file = await path.join(dir, `${id}.dat`) | ||
return file | ||
} | ||
|
||
export async function updateCache(id: number, title: string, src: Source) { | ||
const file = await getFilePath(id) | ||
log.info(`update cache ${id} to path ${file}`) | ||
fs.writeTextFile(file, JSON.stringify({ src, title })) | ||
} | ||
|
||
export async function dropCache(id: number) { | ||
const file = await getFilePath(id) | ||
log.info(`drop cache ${id} (${file})`) | ||
if ((await fs.exists(file))) { | ||
await fs.removeFile(file) | ||
} | ||
} | ||
|
||
export async function dropAll() { | ||
const dir = await getDataDir() | ||
const files = await fs.readDir(dir, { recursive: false }) | ||
const tasks = files.map(async (p) => { | ||
if (p.children == null) { | ||
await fs.removeFile(p.path) | ||
} | ||
}) | ||
await Promise.all(tasks) | ||
} | ||
|
||
async function recoverFromFile(file: string): Promise<[string,Source]> { | ||
const content = await fs.readTextFile(file) | ||
const obj = JSON.parse(content) | ||
return [obj.title, obj.src] | ||
} | ||
|
||
export async function recoverAllCache(): Promise<[string, Source][]> { | ||
const dir = await getDataDir() | ||
const files = (await fs.readDir(dir, { recursive: false })) | ||
.filter((p) => p.children == null && p.name != null && p.name.endsWith(".dat")) | ||
.map((e) => e.path!) | ||
|
||
const tasks = files.map(recoverFromFile) | ||
return await Promise.all(tasks) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import MenuEventReceiver from "./menu-event" | ||
import StatusRecover from "./status-recover" | ||
|
||
export default function MainEventRegister() { | ||
return ( | ||
<> | ||
<MenuEventReceiver /> | ||
<StatusRecover /> | ||
</> | ||
) | ||
} |
Oops, something went wrong.