Clean things up for worker based approach.

This commit is contained in:
Jean-Philip Desjardins 2021-10-25 13:26:21 -04:00
parent 3d4904d744
commit 3aea626dd2
8 changed files with 31 additions and 66 deletions

View file

@ -3462,14 +3462,6 @@
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz",
"integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ=="
},
"async-mutex": {
"version": "0.3.2",
"resolved": "https://registry.npmjs.org/async-mutex/-/async-mutex-0.3.2.tgz",
"integrity": "sha512-HuTK7E7MT7jZEh1P9GtRW9+aTWiDWWi9InbZ5hjxrnRa39KS4BW04+xLBhYNS2aXhHUIKZSw3gj4Pn1pj+qGAA==",
"requires": {
"tslib": "^2.3.1"
}
},
"asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",

View file

@ -14,7 +14,6 @@
"@types/node": "^12.20.21",
"@types/react": "^17.0.19",
"@types/react-dom": "^17.0.9",
"async-mutex": "^0.3.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",

View file

@ -1,42 +1,23 @@
import { configureStore, createAsyncThunk, createReducer } from "@reduxjs/toolkit";
import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
import { PsfPlayerModule, initPsfPlayerModule, getPsfArchiveFileList, loadPsfFromArchive, getCurrentPsfTags, tickPsf } from "./PsfPlayerModule";
import { Mutex } from 'async-mutex';
import { PsfPlayerModule, initPsfPlayerModule, resumePsf, pausePsf, getPsfArchiveFileList, loadPsfFromArchive, getCurrentPsfTags } from "./PsfPlayerModule";
const invalidPlayingIndex = -1;
let archiveFilePath = "archive.zip";
let tickMutex = new Mutex();
const updateDelay : number = 30;
let updateTimer : any;
let updateFct = async function() {
let releaseLock = await tickMutex.acquire();
// await tickPsf();
updateTimer = setTimeout(updateFct, updateDelay);
releaseLock();
};
const loadPsfFromPath = async function(psfFilePath : string) {
let releaseLock = await tickMutex.acquire();
clearTimeout(updateTimer);
await loadPsfFromArchive(archiveFilePath, psfFilePath);
let tags = getCurrentPsfTags();
updateTimer = setTimeout(updateFct, updateDelay);
releaseLock();
return tags;
}
const playPsf = async function() {
let releaseLock = await tickMutex.acquire();
updateTimer = setTimeout(updateFct, updateDelay);
releaseLock();
resumePsf();
}
const stopPsf = async function() {
let releaseLock = await tickMutex.acquire();
clearTimeout(updateTimer);
releaseLock();
pausePsf();
}
export type AudioState = {
@ -160,13 +141,11 @@ const reducer = createReducer(initialState, (builder) => (
return state;
})
.addCase(play.fulfilled, (state) => {
console.log("playing");
state.value = "playing";
state.playing = true;
return state;
})
.addCase(pause.fulfilled, (state) => {
console.log("paused");
state.value = "paused";
state.playing = false;
return state;

View file

@ -40,20 +40,24 @@ export let initPsfPlayerModule = async function() {
PsfPlayerModule.ccall("initVm", "", [], []);
};
export let resumePsf = async function () {
PsfPlayerModule.resumePsf();
}
export let pausePsf = async function () {
PsfPlayerModule.pausePsf();
}
export let getPsfArchiveFileList = function(archivePath : string) {
let fileList = PsfPlayerModule.getPsfArchiveFileList(archivePath);
return convertStringVectorToArray(fileList);
}
export let loadPsfFromArchive = async function(archivePath : string, psfPath : string) {
await PsfPlayerModule.ccall("loadPsf", "", ['string', 'string'], [archivePath, psfPath], { async: true });
PsfPlayerModule.loadPsf(archivePath, psfPath);
}
export let getCurrentPsfTags = function() {
let tags = convertStringMapToDictionary(PsfPlayerModule.getCurrentPsfTags());
return tags;
}
export let tickPsf = async function() {
await PsfPlayerModule.ccall("step", "", [], [], { async: true });
}