UNPKG

@metamask/snaps-simulation

Version:

A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment

67 lines 2.25 kB
import $reduxjstoolkit from "@reduxjs/toolkit"; const { createSelector, createSlice } = $reduxjstoolkit; import { castDraft } from "immer"; /** * The initial events state. */ const INITIAL_STATE = { events: [], errors: [], traces: [], pendingTraces: [], }; export const trackablesSlice = createSlice({ name: 'trackables', initialState: INITIAL_STATE, reducers: { trackError: (state, action) => { state.errors.push(castDraft(action.payload)); }, trackEvent: (state, action) => { state.events.push(castDraft(action.payload)); }, startTrace: (state, action) => { const trace = castDraft(action.payload); state.pendingTraces.push(trace); }, endTrace: (state, action) => { const endTrace = castDraft(action.payload); const index = state.pendingTraces.findLastIndex((pendingTrace) => pendingTrace.id === endTrace.id && pendingTrace.name === endTrace.name); if (index !== -1) { const pendingTrace = state.pendingTraces[index]; state.pendingTraces.splice(index, 1); state.traces.push(pendingTrace); } }, clearTrackables: (state) => { state.events = []; state.errors = []; state.traces = []; }, }, }); export const { trackError, trackEvent, startTrace, endTrace, clearTrackables } = trackablesSlice.actions; /** * Get the errors from the state. * * @param state - The application state. * @returns An array of errors. */ export const getErrors = createSelector((state) => state.trackables, ({ errors }) => errors); /** * Get the events from the state. * * @param state - The application state. * @returns An array of events. */ export const getEvents = createSelector((state) => state.trackables, ({ events }) => events); /** * Get the traces from the state. This only includes traces that have been * ended, not pending traces. * * @param state - The application state. * @returns An array of traces. */ export const getTraces = createSelector((state) => state.trackables, ({ traces }) => traces); //# sourceMappingURL=trackables.mjs.map