@@ -44,6 +44,8 @@ import {
4444 UISchemaTester ,
4545 ValidationMode ,
4646 updateI18n ,
47+ Middleware ,
48+ defaultMiddleware ,
4749} from '@jsonforms/core' ;
4850import { BehaviorSubject , Observable } from 'rxjs' ;
4951import type { JsonFormsBaseRenderer } from './base.renderer' ;
@@ -56,6 +58,7 @@ export const USE_STATE_VALUE = Symbol('Marker to use state value');
5658export class JsonFormsAngularService {
5759 private _state : JsonFormsSubStates ;
5860 private state : BehaviorSubject < JsonFormsState > ;
61+ private middleware : Middleware ;
5962
6063 init (
6164 initialState : JsonFormsSubStates = {
@@ -66,8 +69,10 @@ export class JsonFormsAngularService {
6669 validationMode : 'ValidateAndShow' ,
6770 additionalErrors : undefined ,
6871 } ,
69- }
72+ } ,
73+ middleware : Middleware = defaultMiddleware
7074 ) {
75+ this . middleware = middleware ;
7176 this . _state = initialState ;
7277 this . _state . config = configReducer (
7378 undefined ,
@@ -143,9 +148,10 @@ export class JsonFormsAngularService {
143148 }
144149
145150 updateValidationMode ( validationMode : ValidationMode ) : void {
146- const coreState = coreReducer (
151+ const coreState = this . middleware (
147152 this . _state . core ,
148- Actions . setValidationMode ( validationMode )
153+ Actions . setValidationMode ( validationMode ) ,
154+ coreReducer
149155 ) ;
150156 this . _state . core = coreState ;
151157 this . updateSubject ( ) ;
@@ -161,7 +167,11 @@ export class JsonFormsAngularService {
161167 }
162168
163169 updateCore < T extends CoreActions > ( coreAction : T ) : T {
164- const coreState = coreReducer ( this . _state . core , coreAction ) ;
170+ const coreState = this . middleware (
171+ this . _state . core ,
172+ coreAction ,
173+ coreReducer
174+ ) ;
165175 if ( coreState !== this . _state . core ) {
166176 this . _state . core = coreState ;
167177 this . updateSubject ( ) ;
@@ -199,13 +209,14 @@ export class JsonFormsAngularService {
199209 setUiSchema ( uischema : UISchemaElement | undefined ) : void {
200210 const newUiSchema =
201211 uischema ?? generateDefaultUISchema ( this . _state . core . schema ) ;
202- const coreState = coreReducer (
212+ const coreState = this . middleware (
203213 this . _state . core ,
204214 Actions . updateCore (
205215 this . _state . core . data ,
206216 this . _state . core . schema ,
207217 newUiSchema
208- )
218+ ) ,
219+ coreReducer
209220 ) ;
210221 if ( coreState !== this . _state . core ) {
211222 this . _state . core = coreState ;
@@ -214,13 +225,14 @@ export class JsonFormsAngularService {
214225 }
215226
216227 setSchema ( schema : JsonSchema | undefined ) : void {
217- const coreState = coreReducer (
228+ const coreState = this . middleware (
218229 this . _state . core ,
219230 Actions . updateCore (
220231 this . _state . core . data ,
221232 schema ?? generateJsonSchema ( this . _state . core . data ) ,
222233 this . _state . core . uischema
223- )
234+ ) ,
235+ coreReducer
224236 ) ;
225237 if ( coreState !== this . _state . core ) {
226238 this . _state . core = coreState ;
@@ -229,13 +241,14 @@ export class JsonFormsAngularService {
229241 }
230242
231243 setData ( data : any ) : void {
232- const coreState = coreReducer (
244+ const coreState = this . middleware (
233245 this . _state . core ,
234246 Actions . updateCore (
235247 data ,
236248 this . _state . core . schema ,
237249 this . _state . core . uischema
238- )
250+ ) ,
251+ coreReducer
239252 ) ;
240253 if ( coreState !== this . _state . core ) {
241254 this . _state . core = coreState ;
@@ -257,6 +270,11 @@ export class JsonFormsAngularService {
257270 this . updateSubject ( ) ;
258271 }
259272
273+ setMiddleware ( middleware : Middleware ) : void {
274+ this . _state . middleware = middleware ;
275+ this . updateSubject ( ) ;
276+ }
277+
260278 getState ( ) : JsonFormsState {
261279 return cloneDeep ( { jsonforms : this . _state } ) ;
262280 }
0 commit comments