Skip to content

Commit

Permalink
make extract time elapsed update every frame :)
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabwhy committed Mar 4, 2024
1 parent 2955738 commit b123b3b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ const mainStore = useStore();
<span>{{ mainStore.extractProgress }} / {{ mainStore.extractTotal }} {{ mainStore.extractErrors.length ? `(${mainStore.extractErrors.length} failed)` : '' }}</span>
</div>
<template v-if="mainStore.extracting">
<p class="timer">Elapsed {{ ((Date.now() - mainStore.extractStartTime) / 1000).toFixed(3) }}s</p>
<p class="timer">Elapsed {{ (mainStore.extractElapsedTime / 1000).toFixed(3) }}s</p>
<button @click="mainStore.cancelExtract">Cancel</button>
</template>
<template v-else>
<p class="timer">Took {{ ((mainStore.extractFinishTime - mainStore.extractStartTime) / 1000).toFixed(3) }}s</p>
<p class="timer">Took {{ (mainStore.extractElapsedTime / 1000).toFixed(3) }}s</p>
<button @click="mainStore.showExtract = false">Close</button>
</template>
</div>
Expand Down
59 changes: 49 additions & 10 deletions src/stores/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const useStore = defineStore('main', () => {
const extracting = ref(false);
const extractCancelled = ref(false);
const extractStartTime = ref(0);
const extractFinishTime = ref(0);
const extractElapsedTime = ref(0);
const extractProgress = ref(0);
const extractTotal = ref(0);
const extractErrors = ref<string[]>([]);
Expand Down Expand Up @@ -90,13 +90,21 @@ export const useStore = defineStore('main', () => {
async function extractAll() {
let res = await open({ title: 'Select a output directory', directory: true, multiple: false })
if (res !== null && typeof res == 'string') {
extractElapsedTime.value = 0;
let timerTickInterval;
const timerTick = () => {
extractElapsedTime.value = performance.now() - extractStartTime.value;
timerTickInterval = requestAnimationFrame(timerTick);
}
timerTickInterval = requestAnimationFrame(timerTick);

extractProgress.value = 0;
extractTotal.value = numFiles.value;
extractErrors.value = [];
showExtract.value = true;
extracting.value = true;
extractCancelled.value = false;
extractStartTime.value = Date.now();
extractStartTime.value = performance.now();
let unlistenExtracted = await listen('extracted', () => {
extractProgress.value++;
});
Expand All @@ -107,22 +115,32 @@ export const useStore = defineStore('main', () => {
});
await invoke('extract_all', { outDir: res });
extracting.value = false;
extractFinishTime.value = Date.now();
unlistenExtracted();
unlistenExtractFail();
cancelAnimationFrame(timerTickInterval);
extractElapsedTime.value = performance.now() - extractStartTime.value;
}
}

async function extractDir(dir: string) {
let res = await open({ title: 'Select a output directory', directory: true, multiple: false })
if (res !== null && typeof res == 'string') {
extractElapsedTime.value = 0;
let timerTickInterval;
const timerTick = () => {
console.log("tick");
extractElapsedTime.value = performance.now() - extractStartTime.value;
timerTickInterval = requestAnimationFrame(timerTick);
}
timerTickInterval = requestAnimationFrame(timerTick);

extractProgress.value = 0;
extractTotal.value = await invoke('count_dir', { dir }) as number;
extractErrors.value = [];
showExtract.value = true;
extracting.value = true;
extractCancelled.value = false;
extractStartTime.value = Date.now();
extractStartTime.value = performance.now();
let unlistenExtracted = await listen('extracted', () => {
extractProgress.value++;
});
Expand All @@ -133,9 +151,10 @@ export const useStore = defineStore('main', () => {
});
await invoke('extract_dir', { dir, outDir: res });
extracting.value = false;
extractFinishTime.value = Date.now();
unlistenExtracted();
unlistenExtractFail();
cancelAnimationFrame(timerTickInterval);
extractElapsedTime.value = performance.now() - extractStartTime.value;
}
}

Expand All @@ -146,13 +165,22 @@ export const useStore = defineStore('main', () => {
filePath = ' /' + filePath;
}

extractElapsedTime.value = 0;
let timerTickInterval;
const timerTick = () => {
console.log("tick");
extractElapsedTime.value = performance.now() - extractStartTime.value;
timerTickInterval = requestAnimationFrame(timerTick);
}
timerTickInterval = requestAnimationFrame(timerTick);

extractProgress.value = 0;
extractTotal.value = 1;
extractErrors.value = [];
showExtract.value = true;
extracting.value = true;
extractCancelled.value = false;
extractStartTime.value = Date.now();
extractStartTime.value = performance.now();
let unlistenExtracted = await listen('extracted', () => {
extractProgress.value++;
});
Expand All @@ -167,22 +195,32 @@ export const useStore = defineStore('main', () => {
console.error(e);
}
extracting.value = false;
extractFinishTime.value = Date.now();
unlistenExtracted();
unlistenExtractFail();
cancelAnimationFrame(timerTickInterval);
extractElapsedTime.value = performance.now() - extractStartTime.value;
}
}

async function extractFiles(files: string[]) {
let res = await open({ title: 'Select a output directory', directory: true, multiple: false })
if (res !== null && typeof res == 'string') {
extractElapsedTime.value = 0;
let timerTickInterval;
const timerTick = () => {
console.log("tick");
extractElapsedTime.value = performance.now() - extractStartTime.value;
timerTickInterval = requestAnimationFrame(timerTick);
}
timerTickInterval = requestAnimationFrame(timerTick);

extractProgress.value = 0;
extractTotal.value = files.length;
extractErrors.value = [];
showExtract.value = true;
extracting.value = true;
extractCancelled.value = false;
extractStartTime.value = Date.now();
extractStartTime.value = performance.now();
let unlistenExtracted = await listen('extracted', () => {
extractProgress.value++;
});
Expand All @@ -193,9 +231,10 @@ export const useStore = defineStore('main', () => {
});
await invoke('extract_files', { files, outDir: res });
extracting.value = false;
extractFinishTime.value = Date.now();
unlistenExtracted();
unlistenExtractFail();
cancelAnimationFrame(timerTickInterval);
extractElapsedTime.value = performance.now() - extractStartTime.value;
}
}

Expand All @@ -208,6 +247,6 @@ export const useStore = defineStore('main', () => {
vpkPath, loading, loaded, hasError, error, fileTree, fileList,
loadFromPath, openVPK, memoryMap, showExtract, extracting, extractTotal, extractCancelled,
numFiles, extractProgress, extractErrors, extractAll, extractDir, extractFile, extractFiles,
extractStartTime, extractFinishTime, cancelExtract,
extractStartTime, extractElapsedTime, cancelExtract,
};
})

0 comments on commit b123b3b

Please sign in to comment.