@ngxs/devtools-plugin
Version:
redux devtools plugin for @ngxs/store
1 lines • 14 kB
Source Map (JSON)
{"version":3,"file":"ngxs-devtools-plugin.mjs","sources":["../../../packages/devtools-plugin/src/symbols.ts","../../../packages/devtools-plugin/src/devtools.plugin.ts","../../../packages/devtools-plugin/src/devtools.module.ts","../../../packages/devtools-plugin/index.ts","../../../packages/devtools-plugin/ngxs-devtools-plugin.ts"],"sourcesContent":["import { InjectionToken } from '@angular/core';\n\n/**\n * Interface for the redux-devtools-extension API.\n */\nexport interface NgxsDevtoolsExtension {\n init(state: any): void;\n send(action: any, state?: any): void;\n subscribe(fn: (message: NgxsDevtoolsAction) => void): VoidFunction;\n}\n\nexport interface NgxsDevtoolsAction {\n type: string;\n payload: any;\n state: any;\n id: number;\n source: string;\n}\n\nexport interface NgxsDevtoolsOptions {\n /**\n * The name of the extension\n */\n name?: string;\n\n /**\n * Whether the dev tools is enabled or note. Useful for setting during production.\n */\n disabled?: boolean;\n\n /**\n * Max number of entiries to keep.\n */\n maxAge?: number;\n\n /**\n * If more than one action is dispatched in the indicated interval, all new actions will be collected\n * and sent at once. It is the joint between performance and speed. When set to 0, all actions will be\n * sent instantly. Set it to a higher value when experiencing perf issues (also maxAge to a lower value).\n * Default is 500 ms.\n */\n latency?: number;\n\n /**\n * string or array of strings as regex - actions types to be hidden in the monitors (while passed to the reducers).\n * If actionsWhitelist specified, actionsBlacklist is ignored.\n */\n actionsBlacklist?: string | string[];\n\n /**\n * string or array of strings as regex - actions types to be shown in the monitors (while passed to the reducers).\n * If actionsWhitelist specified, actionsBlacklist is ignored.\n */\n actionsWhitelist?: string | string[];\n\n /**\n * called for every action before sending, takes state and action object, and returns true in case it allows\n * sending the current data to the monitor. Use it as a more advanced version of\n * actionsBlacklist/actionsWhitelist parameters\n */\n predicate?: (state: any, action: any) => boolean;\n\n /**\n * Reformat actions before sending to dev tools\n */\n actionSanitizer?: (action: any) => void;\n\n /**\n * Reformat state before sending to devtools\n */\n stateSanitizer?: (state: any) => void;\n\n /**\n * If set to true, will include stack trace for every dispatched action\n */\n trace?: boolean | (() => string);\n\n /**\n * Maximum stack trace frames to be stored (in case trace option was provided as true)\n */\n traceLimit?: number;\n}\n\nexport const NGXS_DEVTOOLS_OPTIONS = new InjectionToken<NgxsDevtoolsOptions>(\n 'NGXS_DEVTOOLS_OPTIONS'\n);\n","import { inject, Injectable, Injector, NgZone, OnDestroy, ɵglobal } from '@angular/core';\nimport { Store } from '@ngxs/store';\nimport {\n InitState,\n getActionTypeFromInstance,\n NgxsNextPluginFn,\n NgxsPlugin\n} from '@ngxs/store/plugins';\nimport { tap, catchError } from 'rxjs/operators';\n\nimport { NGXS_DEVTOOLS_OPTIONS, NgxsDevtoolsAction, NgxsDevtoolsExtension } from './symbols';\n\nconst enum ReduxDevtoolsActionType {\n Dispatch = 'DISPATCH',\n Action = 'ACTION'\n}\n\nconst enum ReduxDevtoolsPayloadType {\n JumpToAction = 'JUMP_TO_ACTION',\n JumpToState = 'JUMP_TO_STATE',\n ToggleAction = 'TOGGLE_ACTION',\n ImportState = 'IMPORT_STATE'\n}\n\n/**\n * Adds support for the Redux Devtools extension:\n * http://extension.remotedev.io/\n */\n@Injectable()\nexport class NgxsReduxDevtoolsPlugin implements OnDestroy, NgxsPlugin {\n private _injector = inject(Injector);\n private _ngZone = inject(NgZone);\n private _options = inject(NGXS_DEVTOOLS_OPTIONS);\n\n private devtoolsExtension: NgxsDevtoolsExtension | null = null;\n private readonly globalDevtools =\n ɵglobal['__REDUX_DEVTOOLS_EXTENSION__'] || ɵglobal['devToolsExtension'];\n\n private unsubscribe: VoidFunction | null = null;\n\n constructor() {\n this.connect();\n }\n\n ngOnDestroy(): void {\n this.unsubscribe?.();\n this.globalDevtools?.disconnect();\n }\n\n /**\n * Lazy get the store for circular dependency issues\n */\n private get store(): Store {\n return this._injector.get<Store>(Store);\n }\n\n /**\n * Middleware handle function\n */\n handle(state: any, action: any, next: NgxsNextPluginFn) {\n if (!this.devtoolsExtension || this._options.disabled) {\n return next(state, action);\n }\n\n return next(state, action).pipe(\n catchError(error => {\n const newState = this.store.snapshot();\n this.sendToDevTools(state, action, newState);\n throw error;\n }),\n tap(newState => {\n this.sendToDevTools(state, action, newState);\n })\n );\n }\n\n private sendToDevTools(state: any, action: any, newState: any) {\n const type = getActionTypeFromInstance(action);\n // if init action, send initial state to dev tools\n const isInitAction = type === InitState.type;\n if (isInitAction) {\n this.devtoolsExtension!.init(state);\n } else {\n this.devtoolsExtension!.send({ ...action, action: null, type }, newState);\n }\n }\n\n /**\n * Handle the action from the dev tools subscription\n */\n dispatched(action: NgxsDevtoolsAction) {\n if (action.type === ReduxDevtoolsActionType.Dispatch) {\n if (\n action.payload.type === ReduxDevtoolsPayloadType.JumpToAction ||\n action.payload.type === ReduxDevtoolsPayloadType.JumpToState\n ) {\n const prevState = JSON.parse(action.state);\n // This makes the DevTools and Router plugins compatible with each other.\n // We check for the existence of the `router` state and ensure it has the\n // `trigger` property, confirming that it is our router state (coming from `@ngxs/router-plugin`).\n // This enables a time-traveling feature, as it not only restores the state but\n // also allows the `RouterState` to navigate back when the action is jumped.\n if (prevState.router && prevState.router.trigger) {\n prevState.router.trigger = 'devtools';\n }\n this.store.reset(prevState);\n } else if (action.payload.type === ReduxDevtoolsPayloadType.ToggleAction) {\n console.warn('Skip is not supported at this time.');\n } else if (action.payload.type === ReduxDevtoolsPayloadType.ImportState) {\n const { actionsById, computedStates, currentStateIndex } =\n action.payload.nextLiftedState;\n this.devtoolsExtension!.init(computedStates[0].state);\n Object.keys(actionsById)\n .filter(actionId => actionId !== '0')\n .forEach(actionId =>\n this.devtoolsExtension!.send(actionsById[actionId], computedStates[actionId].state)\n );\n this.store.reset(computedStates[currentStateIndex].state);\n }\n } else if (action.type === ReduxDevtoolsActionType.Action) {\n const actionPayload = JSON.parse(action.payload);\n this.store.dispatch(actionPayload);\n }\n }\n\n private connect(): void {\n if (!this.globalDevtools || this._options.disabled) {\n return;\n }\n\n // The `connect` method adds a `message` event listener to communicate\n // with an extension through `window.postMessage` and handle message events.\n // Since we only handle two specific events, we aim to avoid unnecessary change\n // detections triggered by events that the extension sends, but we don't need to handle.\n this.devtoolsExtension = this._ngZone.runOutsideAngular(\n () => <NgxsDevtoolsExtension>this.globalDevtools.connect(this._options)\n );\n\n this.unsubscribe = this.devtoolsExtension.subscribe(action => {\n if (\n action.type === ReduxDevtoolsActionType.Dispatch ||\n action.type === ReduxDevtoolsActionType.Action\n ) {\n this.dispatched(action);\n }\n });\n }\n}\n","import {\n NgModule,\n ModuleWithProviders,\n InjectionToken,\n EnvironmentProviders,\n makeEnvironmentProviders\n} from '@angular/core';\nimport { withNgxsPlugin } from '@ngxs/store';\n\nimport { NgxsDevtoolsOptions, NGXS_DEVTOOLS_OPTIONS } from './symbols';\nimport { NgxsReduxDevtoolsPlugin } from './devtools.plugin';\n\nexport function devtoolsOptionsFactory(options: NgxsDevtoolsOptions) {\n return {\n name: 'NGXS',\n ...options\n };\n}\n\nexport const USER_OPTIONS = new InjectionToken('USER_OPTIONS');\n\n@NgModule()\nexport class NgxsReduxDevtoolsPluginModule {\n static forRoot(\n options?: NgxsDevtoolsOptions\n ): ModuleWithProviders<NgxsReduxDevtoolsPluginModule> {\n return {\n ngModule: NgxsReduxDevtoolsPluginModule,\n providers: [\n withNgxsPlugin(NgxsReduxDevtoolsPlugin),\n {\n provide: USER_OPTIONS,\n useValue: options\n },\n {\n provide: NGXS_DEVTOOLS_OPTIONS,\n useFactory: devtoolsOptionsFactory,\n deps: [USER_OPTIONS]\n }\n ]\n };\n }\n}\n\nexport function withNgxsReduxDevtoolsPlugin(\n options?: NgxsDevtoolsOptions\n): EnvironmentProviders {\n return makeEnvironmentProviders([\n withNgxsPlugin(NgxsReduxDevtoolsPlugin),\n {\n provide: USER_OPTIONS,\n useValue: options\n },\n {\n provide: NGXS_DEVTOOLS_OPTIONS,\n useFactory: devtoolsOptionsFactory,\n deps: [USER_OPTIONS]\n }\n ]);\n}\n","/**\n * The public api for consumers of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