@@ -6,6 +6,7 @@ export class Choice extends Action {
66
77 static setup ( ) {
88 Monogatari . global ( '_CurrentChoice' , null ) ;
9+ Monogatari . history ( 'choice' ) ;
910 return Promise . resolve ( ) ;
1011 }
1112
@@ -52,6 +53,7 @@ export class Choice extends Action {
5253 Monogatari . run ( Monogatari . global ( '_CurrentChoice' ) [ $_ ( this ) . data ( 'choice' ) ] . Do , false ) ;
5354 Monogatari . global ( '_CurrentChoice' , null ) ;
5455 }
56+ Monogatari . history ( 'choice' ) . push ( $_ ( this ) . data ( 'choice' ) ) ;
5557 } else {
5658 Monogatari . run ( $_ ( this ) . data ( 'do' ) , false ) ;
5759 Monogatari . global ( '_CurrentChoice' , null ) ;
@@ -155,8 +157,36 @@ export class Choice extends Action {
155157 // Revert is disabled for choices since we still don't have a way to know what
156158 // a choice did
157159 willRevert ( ) {
160+ if ( Monogatari . history ( 'choice' ) . length > 0 ) {
161+ const choice = Monogatari . history ( 'choice' ) [ Monogatari . history ( 'choice' ) . length - 1 ] ;
162+ if ( this . statement [ choice ] !== 'undefined' ) {
163+
164+ // Check if the choice had an onChosen function with it's matching
165+ // onRevert functionality, or if no onChosen function was provided
166+ // which are the only cases where it can be reverted.
167+ const functionReversible = ( typeof this . statement [ choice ] [ 'onRevert' ] === 'function' && typeof this . statement [ choice ] [ 'onChosen' ] === 'function' ) || typeof this . statement [ choice ] [ 'onChosen' ] !== 'function' ;
168+
169+ if ( functionReversible ) {
170+ return Promise . resolve ( ) ;
171+ }
172+ }
173+ }
158174 return Promise . reject ( ) ;
159175 }
176+
177+ revert ( ) {
178+ const choice = Monogatari . history ( 'choice' ) [ Monogatari . history ( 'choice' ) . length - 1 ] ;
179+ return Monogatari . revert ( this . statement [ choice ] . Do , false ) . then ( ( ) => {
180+ return Monogatari . run ( this . statement ) ;
181+ } ) . catch ( ( ) => {
182+ } ) ;
183+ return Promise . resolve ( ) ;
184+ }
185+
186+ didRevert ( ) {
187+ Monogatari . history ( 'choice' ) . pop ( ) ;
188+ return Promise . resolve ( { advance : false , step : false } ) ;
189+ }
160190}
161191
162192Choice . id = 'Choice' ;
0 commit comments