UNPKG

@metamask/snaps-simulation

Version:

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

1 lines 5.18 kB
{"version":3,"file":"trackables.mjs","sourceRoot":"","sources":["../../src/store/trackables.ts"],"names":[],"mappings":";;AAQA,OAAO,EAAE,SAAS,EAAE,cAAc;AAiClC;;GAEG;AACH,MAAM,aAAa,GAAoB;IACrC,MAAM,EAAE,EAAE;IACV,MAAM,EAAE,EAAE;IACV,MAAM,EAAE,EAAE;IACV,aAAa,EAAE,EAAE;CAClB,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,WAAW,CAAC;IACzC,IAAI,EAAE,YAAY;IAClB,YAAY,EAAE,aAAa;IAC3B,QAAQ,EAAE;QACR,UAAU,EAAE,CAAC,KAAK,EAAE,MAAmC,EAAE,EAAE;YACzD,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;QAC/C,CAAC;QACD,UAAU,EAAE,CAAC,KAAK,EAAE,MAAmC,EAAE,EAAE;YACzD,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;QAC/C,CAAC;QACD,UAAU,EAAE,CAAC,KAAK,EAAE,MAAmC,EAAE,EAAE;YACzD,MAAM,KAAK,GAAG,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YACxC,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAClC,CAAC;QACD,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAqC,EAAE,EAAE;YACzD,MAAM,QAAQ,GAAG,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAC3C,MAAM,KAAK,GAAG,KAAK,CAAC,aAAa,CAAC,aAAa,CAC7C,CAAC,YAAY,EAAE,EAAE,CACf,YAAY,CAAC,EAAE,KAAK,QAAQ,CAAC,EAAE;gBAC/B,YAAY,C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rorParams,\n TrackEventParams,\n} from '@metamask/snaps-sdk';\nimport type { PayloadAction } from '@reduxjs/toolkit';\nimport { createSelector, createSlice } from '@reduxjs/toolkit';\nimport { castDraft } from 'immer';\n\nimport type { ApplicationState } from './store';\n\nexport type TrackedEvent = TrackEventParams['event'];\n\nexport type TrackedError = TrackErrorParams['error'];\n\n/**\n * The trackables state.\n */\nexport type TrackablesState = {\n /**\n * An array of tracked events.\n */\n events: TrackedEvent[];\n\n /**\n * An array of tracked errors.\n */\n errors: TrackedError[];\n\n /**\n * An array of performance traces.\n */\n traces: TraceRequest[];\n\n /**\n * An array of pending traces that have not yet been ended.\n */\n pendingTraces: TraceRequest[];\n};\n\n/**\n * The initial events state.\n */\nconst INITIAL_STATE: TrackablesState = {\n events: [],\n errors: [],\n traces: [],\n pendingTraces: [],\n};\n\nexport const trackablesSlice = createSlice({\n name: 'trackables',\n initialState: INITIAL_STATE,\n reducers: {\n trackError: (state, action: PayloadAction<TrackedError>) => {\n state.errors.push(castDraft(action.payload));\n },\n trackEvent: (state, action: PayloadAction<TrackedEvent>) => {\n state.events.push(castDraft(action.payload));\n },\n startTrace: (state, action: PayloadAction<TraceRequest>) => {\n const trace = castDraft(action.payload);\n state.pendingTraces.push(trace);\n },\n endTrace: (state, action: PayloadAction<EndTraceParams>) => {\n const endTrace = castDraft(action.payload);\n const index = state.pendingTraces.findLastIndex(\n (pendingTrace) =>\n pendingTrace.id === endTrace.id &&\n pendingTrace.name === endTrace.name,\n );\n\n if (index !== -1) {\n const pendingTrace = state.pendingTraces[index];\n state.pendingTraces.splice(index, 1);\n state.traces.push(pendingTrace);\n }\n },\n clearTrackables: (state) => {\n state.events = [];\n state.errors = [];\n state.traces = [];\n },\n },\n});\n\nexport const { trackError, trackEvent, startTrace, endTrace, clearTrackables } =\n trackablesSlice.actions;\n\n/**\n * Get the errors from the state.\n *\n * @param state - The application state.\n * @returns An array of errors.\n */\nexport const getErrors = createSelector(\n (state: ApplicationState) => state.trackables,\n ({ errors }) => errors,\n);\n\n/**\n * Get the events from the state.\n *\n * @param state - The application state.\n * @returns An array of events.\n */\nexport const getEvents = createSelector(\n (state: ApplicationState) => state.trackables,\n ({ events }) => events,\n);\n\n/**\n * Get the traces from the state. This only includes traces that have been\n * ended, not pending traces.\n *\n * @param state - The application state.\n * @returns An array of traces.\n */\nexport const getTraces = createSelector(\n (state: ApplicationState) => state.trackables,\n ({ traces }) => traces,\n);\n"]}