update appscript api

change setInterval to setTimeout
main
ULTRACOMBOS-DEV 2 months ago
parent 58d92642f1
commit f13b2dc8e5
  1. 4
      vite/src-tauri/capabilities/default.json
  2. 70
      vite/src/comps/timer.jsx
  3. 37
      vite/src/pages/flow_free.jsx
  4. 2
      vite/src/util/backend.js

@ -23,9 +23,7 @@
{ {
"url": "http://**/" "url": "http://**/"
},{ },{
"url":"https://script.google.com/macros/s/AKfycbxpXbWhMdd4nv0KHhSzeFTKIV0tqsh-HBKCdlaOT34sh2vl1H5aoa36QnimhQg8I2aKRw/exec" "url":"https://script.google.com/macros/s/AKfycbyqLIX2USYfn2dp_1RQ5VdLywVNfUVgpGER9ZNsolXOfdPtJmuPAJCfjfA4aGxKHdBW0A/exec"
},{
"url":"https://script.google.com/macros/s/AKfycbx-Ytlql0yAdVsENNbcaCdXglerX7sNLpNggtusqp4TxebiA3C7V0xjGe4jaI4m1sVjWQ/exec"
} }
] ]
}, },

@ -1,59 +1,77 @@
import { useEffect, useState, useRef, forwardRef, useImperativeHandle, } from "react" import { useEffect, useState, useRef, forwardRef, useImperativeHandle, } from "react"
import { OSC_ADDRESS, sendOscStatus } from "../util/osc"; import { OSC_ADDRESS, sendOscStatus } from "../util/osc";
const Update_Interval = 1000; // 1 second const Update_Interval = 100; // 1 second
export const Countdown=forwardRef(({time, callback, auto,clientId, ...props}, ref)=>{ export const Countdown = forwardRef(({ time, callback, auto, clientId, ...props }, ref) => {
const refTime = useRef(time); const refTime = useRef(time);
const refInterval = useRef(null); const refInterval = useRef(null);
const refDisplay=useRef(); const refDisplay = useRef();
// A simple function to recursively call itself until the timer expires.
function restart(newTime, callback_func) { function restart(newTime, callback_func) {
console.log('restart countdown:', newTime, 'ms'); console.log('restart countdown:', newTime, 'ms');
if(refInterval.current) { // Clear any existing timer.
clearInterval(refInterval.current); if (refInterval.current) {
clearTimeout(refInterval.current);
} }
refTime.current = newTime || time; // Set the end time to ensure accuracy.
refInterval.current=setInterval(() => { const endTime = new Date().getTime() + (newTime || time);
if(refTime.current>0){
refTime.current -= Update_Interval; // The main timer function that gets called every 'Update_Interval'.
// console.log('Countdown:', refTime.current/1000); const tick = () => {
}else{ const now = new Date().getTime();
const timeLeft = endTime - now;
// The timer has expired.
if (timeLeft <= 0) {
refTime.current = 0; refTime.current = 0;
clearInterval(refInterval.current); clearTimeout(refInterval.current);
refInterval.current = null;
if(typeof callback_func === 'function'){ if (typeof callback_func === 'function') {
callback_func(); callback_func();
}else{ } else {
if(callback) callback(); if (callback) callback();
}
} }
if(refDisplay.current) refDisplay.current.innerText = (refTime.current/1000).toFixed(0); // Final display update and OSC message.
sendOscStatus(OSC_ADDRESS.CLIENT_DURATION, `${clientId}#${(refTime.current/1000).toFixed(0)}`); // send remaining time in seconds via OSC if (refDisplay.current) refDisplay.current.innerText = '0';
sendOscStatus(OSC_ADDRESS.CLIENT_DURATION, `${clientId}#0`);
return;
}
}, Update_Interval); // Update the display and send the OSC status.
refTime.current = timeLeft;
if (refDisplay.current) refDisplay.current.innerText = (refTime.current / 1000).toFixed(0);
sendOscStatus(OSC_ADDRESS.CLIENT_DURATION, `${clientId}#${(refTime.current / 1000).toFixed(0)}`);
// Schedule the next tick.
refInterval.current = setTimeout(tick, Update_Interval);
};
// Start the timer.
refInterval.current = setTimeout(tick, Update_Interval);
} }
function stop(){
// The stop function is also updated to use clearTimeout.
function stop() {
console.log('stop countdown'); console.log('stop countdown');
if(refInterval.current) { if (refInterval.current) {
clearInterval(refInterval.current); clearTimeout(refInterval.current);
refInterval.current = null; refInterval.current = null;
} }
// refDisplay.current.innerText = '';
} }
useEffect(()=>{ useEffect(() => {
if(auto) if (auto)
restart(time); restart(time);
// return () => { // return () => {

@ -88,6 +88,8 @@ export function FreeFlow(){
const refFadeOutInterval=useRef(); const refFadeOutInterval=useRef();
const refVolDownInterval=useRef(); const refVolDownInterval=useRef();
const [lastOsc, setLastOsc]=useState();
const { history, status, reset, sendMessage, setStatus, audioOutput, setAudioOutput, stop:stopChat, const { history, status, reset, sendMessage, setStatus, audioOutput, setAudioOutput, stop:stopChat,
audioUrl }=useChat(); audioUrl }=useChat();
@ -120,21 +122,23 @@ export function FreeFlow(){
const message=payload.args[0]; const message=payload.args[0];
const params=message.split('#'); const params=message.split('#');
if(params[0]!='all'){
console.log('set lastOsc', {address, params});
setLastOsc(()=>({address, params}));
return;
}
switch(address){ switch(address){
case OSC_ADDRESS.PLAY_CUE: case OSC_ADDRESS.PLAY_CUE:
if(params[0]=='all' || params[0]==refData.current?.id)
setNextCue(()=>params[1]); setNextCue(()=>params[1]);
break; break;
case OSC_ADDRESS.STOP_CUE: case OSC_ADDRESS.STOP_CUE:
if(params[0]=='all' || params[0]==refData.current?.id)
onStop(); onStop();
break; break;
case OSC_ADDRESS.RESET_CUE: case OSC_ADDRESS.RESET_CUE:
if(params[0]=='all' || params[0]==refData.current?.id){
sendOsc(OSC_ADDRESS.STATUS, 'reset'); sendOsc(OSC_ADDRESS.STATUS, 'reset');
onStop(); onStop();
resetData(); resetData();
}
break; break;
} }
@ -196,7 +200,7 @@ export function FreeFlow(){
clearInterval(refVolDownInterval.current); clearInterval(refVolDownInterval.current);
} }
const dest=0.5; const dest=0.3;
let fadeOutInterval = setInterval(() => { let fadeOutInterval = setInterval(() => {
if (refAudio.current.volume > dest) { if (refAudio.current.volume > dest) {
refAudio.current.volume =Math.max(dest, refAudio.current.volume - 1.0/(AUDIO_FADE_TIME/100)); // Decrease volume gradually refAudio.current.volume =Math.max(dest, refAudio.current.volume - 1.0/(AUDIO_FADE_TIME/100)); // Decrease volume gradually
@ -576,6 +580,29 @@ export function FreeFlow(){
writeSheet(); writeSheet();
} }
useEffect(()=>{
if(!lastOsc) return;
console.log('Process last OSC:', lastOsc);
if(lastOsc.params[0]!=data.id) return;
switch(lastOsc.address){
case OSC_ADDRESS.PLAY_CUE:
setNextCue(()=>lastOsc.params[1]);
break;
case OSC_ADDRESS.STOP_CUE:
onStop();
break;
case OSC_ADDRESS.RESET_CUE:
sendOsc(OSC_ADDRESS.STATUS, 'reset');
onStop();
resetData();
break;
}
},[lastOsc]);
useEffect(()=>{ useEffect(()=>{

@ -36,7 +36,7 @@ export async function updateUser(id, data){
} }
const APPSCRIPT_URL='https://script.google.com/macros/s/AKfycbx-Ytlql0yAdVsENNbcaCdXglerX7sNLpNggtusqp4TxebiA3C7V0xjGe4jaI4m1sVjWQ/exec'; const APPSCRIPT_URL='https://script.google.com/macros/s/AKfycbyqLIX2USYfn2dp_1RQ5VdLywVNfUVgpGER9ZNsolXOfdPtJmuPAJCfjfA4aGxKHdBW0A/exec';
// const APPSCRIPT_URL='https://script.google.com/macros/s/AKfycbxpXbWhMdd4nv0KHhSzeFTKIV0tqsh-HBKCdlaOT34sh2vl1H5aoa36QnimhQg8I2aKRw/exec'; // const APPSCRIPT_URL='https://script.google.com/macros/s/AKfycbxpXbWhMdd4nv0KHhSzeFTKIV0tqsh-HBKCdlaOT34sh2vl1H5aoa36QnimhQg8I2aKRw/exec';
export async function writeToGoogleSheet(date, session, userId, password){ export async function writeToGoogleSheet(date, session, userId, password){

Loading…
Cancel
Save