Accessing the original (unmodified) state in a middleware
(currentState: T, originalState?: T, settings?: any, action?: { name: string; params: any[] }) =>
| Promise<T | undefined | void>
| T
| undefined
| void;// app.ts
import { inject, PLATFORM } from 'aurelia-framework';
import { Store, MiddlewarePlacement, StateHistory } from 'aurelia-store';
import { State } from './state'; // Import your State interface
const blacklisterMiddleware = (
currentState: StateHistory<State>,
originalState: StateHistory<State>
) => {
const isBlacklisted = (text: string) => {
const blacklist = ['f**k', 's**t', 'etc']; // Your blacklist
return blacklist.some((word) => text.includes(word));
};
if (isBlacklisted(currentState.present.someTextProperty)) {
console.warn('Blacklisted word detected! Reverting to original state.');
return originalState; // Revert to the original state
}
return currentState; // Otherwise, pass the current state along
};
@inject(Store)
export class App {
constructor(private store: Store<StateHistory<State>>) {
this.store.registerMiddleware(blacklisterMiddleware, PLATFORM.moduleName('before'));
}
}Last updated
Was this helpful?