@@ -37,6 +37,7 @@ export type FrameState =
3737 frameContext : FrameContext ;
3838 specification : SupportedParsingSpecification ;
3939 homeframeUrl : string ;
40+ parseResult : ParseFramesWithReportsResult ;
4041 }
4142 | {
4243 type : "not-initialized" ;
@@ -109,6 +110,7 @@ function createFramesStackReducer(
109110 let specification : SupportedParsingSpecification ;
110111 let frameContext : FrameContext ;
111112 let homeframeUrl : string ;
113+ let parseResult = action . parseResult ;
112114
113115 if ( state . type === "not-initialized" ) {
114116 /**
@@ -125,7 +127,13 @@ function createFramesStackReducer(
125127 resolvedSpecification ) ;
126128 homeframeUrl = action . pendingItem . url ;
127129 } else {
128- ( { signerState, specification, frameContext, homeframeUrl } = state ) ;
130+ ( {
131+ signerState,
132+ specification,
133+ frameContext,
134+ homeframeUrl,
135+ parseResult,
136+ } = state ) ;
129137 }
130138
131139 state . stack [ index ] = {
@@ -143,6 +151,7 @@ function createFramesStackReducer(
143151
144152 return {
145153 ...state ,
154+ parseResult,
146155 signerState,
147156 frameContext,
148157 homeframeUrl,
@@ -167,6 +176,24 @@ function createFramesStackReducer(
167176 stack : state . stack . slice ( ) ,
168177 } ;
169178 }
179+ case "RESET" : {
180+ if ( state . type === "not-initialized" ) {
181+ return state ;
182+ }
183+
184+ const { frameContext, signerState, specification } =
185+ resolveSpecificationRef . current ( { parseResult : state . parseResult } ) ;
186+
187+ return {
188+ ...state ,
189+ stack :
190+ ! ! state . stack [ 0 ] && state . stack . length > 0 ? [ state . stack [ 0 ] ] : [ ] ,
191+ type : "initialized" ,
192+ frameContext,
193+ signerState,
194+ specification,
195+ } ;
196+ }
170197 case "RESET_INITIAL_FRAME" : {
171198 const { frameContext, signerState, specification } =
172199 resolveSpecificationRef . current ( { parseResult : action . parseResult } ) ;
@@ -178,6 +205,7 @@ function createFramesStackReducer(
178205 frameContext,
179206 specification,
180207 homeframeUrl : action . homeframeUrl ,
208+ parseResult : action . parseResult ,
181209 stack : [
182210 {
183211 request : {
@@ -292,7 +320,11 @@ export type FrameStateAPI = {
292320 response : Response ;
293321 responseBody : unknown ;
294322 } ) => void ;
295- reset : ( arg : {
323+ /**
324+ * If arg is omitted it will reset the frame stack to initial frame and resolves the specification again.
325+ * Otherwise it will set the frame state to provided values and resolve the specification.
326+ */
327+ reset : ( arg ?: {
296328 homeframeUrl : string ;
297329 parseResult : ParseFramesWithReportsResult ;
298330 } ) => void ;
@@ -322,6 +354,7 @@ export function useFrameState({
322354 signerState,
323355 specification,
324356 homeframeUrl : frameUrl ,
357+ parseResult,
325358 stack : [
326359 {
327360 response : new Response ( JSON . stringify ( frameResult ) , {
@@ -540,11 +573,15 @@ export function useFrameState({
540573 } ) ;
541574 } ,
542575 reset ( arg ) {
543- dispatch ( {
544- action : "RESET_INITIAL_FRAME" ,
545- homeframeUrl : arg . homeframeUrl ,
546- parseResult : arg . parseResult ,
547- } ) ;
576+ if ( ! arg ) {
577+ dispatch ( { action : "RESET" } ) ;
578+ } else {
579+ dispatch ( {
580+ action : "RESET_INITIAL_FRAME" ,
581+ homeframeUrl : arg . homeframeUrl ,
582+ parseResult : arg . parseResult ,
583+ } ) ;
584+ }
548585 } ,
549586 } ;
550587 } , [ dispatch ] ) ;
0 commit comments