UNPKG

eye-analysis

Version:

Eye Analysis - Browser-based eye tracking and screen recording library for research and experiments

190 lines (187 loc) 4.74 kB
var __defProp = Object.defineProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true, configurable: true, set: (newValue) => all[name] = () => newValue }); }; var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res); // recorder/state.ts var exports_state = {}; __export(exports_state, { subscribe: () => subscribe, resetState: () => resetState, getSubscriberCount: () => getSubscriberCount, getState: () => getState, dispatch: () => dispatch }); var getInitialState = () => ({ status: "idle", currentSession: null, isRecording: false, recordingDuration: 0, gazeDataCount: 0, eventsCount: 0, videoChunksCount: 0, error: null, lastUpdate: Date.now(), recordingConfig: undefined, startBrowserTime: undefined, recordingStream: null }), ensureGlobalState = () => { if (!globalThis.__eyeAnalysisRecorderState) { globalThis.__eyeAnalysisRecorderState = getInitialState(); } if (!globalThis.__eyeAnalysisStateSubscribers) { globalThis.__eyeAnalysisStateSubscribers = new Set; } }, getCurrentState = () => { ensureGlobalState(); return globalThis.__eyeAnalysisRecorderState; }, getSubscribers = () => { ensureGlobalState(); return globalThis.__eyeAnalysisStateSubscribers; }, stateReducer = (state, action) => { switch (action.type) { case "INITIALIZE": return { ...state, status: "initialized", error: null, lastUpdate: Date.now() }; case "CREATE_SESSION": return { ...state, currentSession: action.payload, error: null, lastUpdate: Date.now() }; case "UPDATE_SESSION": return { ...state, currentSession: action.payload, error: null, lastUpdate: Date.now() }; case "START_RECORDING": return { ...state, status: "recording", isRecording: true, recordingDuration: 0, error: null, lastUpdate: Date.now(), startBrowserTime: performance.now() }; case "STOP_RECORDING": return { ...state, status: "stopped", isRecording: false, error: null, lastUpdate: Date.now(), startBrowserTime: undefined, recordingStream: null }; case "ADD_GAZE_DATA": return { ...state, gazeDataCount: state.gazeDataCount + 1, lastUpdate: Date.now() }; case "ADD_EVENT": return { ...state, eventsCount: state.eventsCount + 1, lastUpdate: Date.now() }; case "UPDATE_DURATION": return { ...state, recordingDuration: action.payload, lastUpdate: Date.now() }; case "SET_ERROR": return { ...state, status: "error", error: action.payload, lastUpdate: Date.now() }; case "CLEAR_ERROR": return { ...state, error: null, lastUpdate: Date.now() }; case "CLEAR_SESSION": return { ...state, status: "initialized", currentSession: null, gazeDataCount: 0, eventsCount: 0, videoChunksCount: 0, recordingDuration: 0, startBrowserTime: undefined, recordingStream: null, lastUpdate: Date.now() }; case "SET_RECORDING_CONFIG": return { ...state, recordingConfig: action.payload, lastUpdate: Date.now() }; case "SET_RECORDING_STREAM": return { ...state, recordingStream: action.payload, lastUpdate: Date.now() }; case "RESET": return { ...getInitialState(), lastUpdate: Date.now() }; default: return state; } }, getState = () => getCurrentState(), dispatch = (action) => { const currentState = getCurrentState(); const newState = stateReducer(currentState, action); if (newState !== currentState) { globalThis.__eyeAnalysisRecorderState = newState; const subscribers = getSubscribers(); subscribers.forEach((subscriber) => { try { subscriber(newState); } catch (error) { console.error("State subscriber error:", error); } }); } }, subscribe = (subscriber) => { const subscribers = getSubscribers(); subscribers.add(subscriber); return () => { subscribers.delete(subscriber); }; }, getSubscriberCount = () => getSubscribers().size, resetState = () => { dispatch({ type: "RESET" }); }; var init_state = __esm(() => { ensureGlobalState(); }); init_state(); export { subscribe, resetState, getSubscriberCount, getState, dispatch };