@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
1 lines • 3.05 kB
Source Map (JSON)
{"version":3,"file":"state.mjs","sourceRoot":"","sources":["../../src/store/state.ts"],"names":[],"mappings":";;AAkBA;;GAEG;AACH,MAAM,aAAa,GAAU;IAC3B,SAAS,EAAE,IAAI;IACf,WAAW,EAAE,IAAI;CAClB,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,WAAW,CAAC;IACpC,IAAI,EAAE,OAAO;IACb,YAAY,EAAE,aAAa;IAC3B,QAAQ,EAAE;QACR,QAAQ,EAAE,CACR,KAAK,EACL,MAAmE,EACnE,EAAE;YACF,IAAI,MAAM,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;gBAC7B,KAAK,CAAC,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC;gBACvC,OAAO,KAAK,CAAC;YACf,CAAC;YAED,KAAK,CAAC,WAAW,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC;YACzC,OAAO,KAAK,CAAC;QACf,CAAC;QACD,UAAU,EAAE,CAAC,KAAK,EAAE,MAA6C,EAAE,EAAE;YACnE,IAAI,MAAM,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;gBAC7B,KAAK,CAAC,SAAS,GAAG,IAAI,CAAC;gBACvB,OAAO,KAAK,CAAC;YACf,CAAC;YAED,KAAK,CAAC,WAAW,GAAG,IAAI,CAAC;YACzB,OAAO,KAAK,CAAC;QACf,CAAC;KACF;CACF,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,GAAG,UAAU,CAAC,OAAO,CAAC;AAE3D;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,SAAkB;IACzC,OAAO,cAAc,CACnB,CAAC,KAAuB,EAAE,EAAE,CAAC,KAAK,EAClC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE;QACZ,IAAI,SAAS,EAAE,CAAC;YACd,OAAO,KAAK,CAAC,SAAS,CAAC;QACzB,CAAC;QAED,OAAO,KAAK,CAAC,WAAW,CAAC;IAC3B,CAAC,CACF,CAAC;AACJ,CAAC","sourcesContent":["import type { PayloadAction } from '@reduxjs/toolkit';\nimport { createSelector, createSlice } from '@reduxjs/toolkit';\n\nimport type { ApplicationState } from './store';\n\n/**\n * The Snap state object.\n *\n * @property encrypted - The encrypted state. Can be null if the Snap does not\n * have an encrypted state.\n * @property unencrypted - The unencrypted state. Can be null if the Snap does\n * not have an unencrypted state.\n */\nexport type State = {\n encrypted: string | null;\n unencrypted: string | null;\n};\n\n/**\n * The initial state.\n */\nconst INITIAL_STATE: State = {\n encrypted: null,\n unencrypted: null,\n};\n\n/**\n * The state slice, which stores the state of the Snap.\n */\nexport const stateSlice = createSlice({\n name: 'state',\n initialState: INITIAL_STATE,\n reducers: {\n setState: (\n state,\n action: PayloadAction<{ state: string | null; encrypted: boolean }>,\n ) => {\n if (action.payload.encrypted) {\n state.encrypted = action.payload.state;\n return state;\n }\n\n state.unencrypted = action.payload.state;\n return state;\n },\n clearState: (state, action: PayloadAction<{ encrypted: boolean }>) => {\n if (action.payload.encrypted) {\n state.encrypted = null;\n return state;\n }\n\n state.unencrypted = null;\n return state;\n },\n },\n});\n\nexport const { setState, clearState } = stateSlice.actions;\n\n/**\n * Get the state from the store.\n *\n * @param encrypted - Whether to get the encrypted or unencrypted state.\n * @returns A selector that returns the state.\n */\nexport function getState(encrypted: boolean) {\n return createSelector(\n (state: ApplicationState) => state,\n ({ state }) => {\n if (encrypted) {\n return state.encrypted;\n }\n\n return state.unencrypted;\n },\n );\n}\n"]}