44 *--------------------------------------------------------------------------------------------*/
55
66import { createContext } from 'react' ;
7- import { getMessageHandler , MessageHandler , vscode } from './message ' ;
7+ import { WebviewHost } from './host ' ;
88import { RemoteInfo } from '../../common/types' ;
99import { CancelCreatePullRequestNew , ChooseBaseRemoteAndBranchResult , ChooseCompareRemoteAndBranchResult , ChooseRemoteAndBranchArgs , CreateParamsNew , CreatePullRequestNew , ScrollPosition , StackCandidate , TitleAndDescriptionArgs , TitleAndDescriptionResult } from '../../common/views' ;
1010import { compareIgnoreCase } from '../../src/common/utils' ;
@@ -39,6 +39,10 @@ const defaultCreateParams: CreateParamsNew = {
3939 usingTemplate : false
4040} ;
4141
42+ function createDefaultParams ( ) : CreateParamsNew {
43+ return { ...defaultCreateParams , labels : [ ] , assignees : [ ] , reviewers : [ ] } ;
44+ }
45+
4246function remoteChanged ( next : RemoteInfo | undefined , current : RemoteInfo | undefined ) : boolean {
4347 return next !== undefined && ( next . owner !== current ?. owner || next . repositoryName !== current ?. repositoryName ) ;
4448}
@@ -53,17 +57,19 @@ function stackCandidateChanged(next: Partial<CreateParamsNew>, current: StackCan
5357
5458export class CreatePRContextNew {
5559 public createParams : CreateParamsNew ;
60+ public onchange : ( ( ctx : CreateParamsNew ) => void ) | null = null ;
5661 private _titleStack : string [ ] = [ ] ;
5762 private _descriptionStack : string [ ] = [ ] ;
63+ private readonly _unsubscribe : ( ) => void ;
5864
59- constructor (
60- public onchange : ( ( ctx : CreateParamsNew ) => void ) | null = null ,
61- private _handler : MessageHandler | null = null ,
62- ) {
63- this . createParams = vscode . getState ( ) ?? defaultCreateParams ;
64- if ( ! _handler ) {
65- this . _handler = getMessageHandler ( this . handleMessage ) ;
66- }
65+ constructor ( private readonly _host : WebviewHost ) {
66+ this . createParams = _host . getState < CreateParamsNew > ( ) ?? createDefaultParams ( ) ;
67+ this . _unsubscribe = _host . onCommand ( this . handleMessage ) ;
68+ }
69+
70+ public dispose ( ) : void {
71+ this . _unsubscribe ( ) ;
72+ this . onchange = null ;
6773 }
6874
6975 get isCreatable ( ) : boolean {
@@ -116,7 +122,7 @@ export class CreatePRContextNew {
116122 // dialog, or the message did not get a response) preserve the user's
117123 // in-progress title/description.
118124 if ( result ?. cancelled === true ) {
119- vscode . setState ( defaultCreateParams ) ;
125+ this . _host . setState ( createDefaultParams ( ) ) ;
120126 }
121127 } ;
122128
@@ -133,8 +139,8 @@ export class CreatePRContextNew {
133139 }
134140
135141 public updateState = ( params : Partial < CreateParamsNew > , reset : boolean = false ) : void => {
136- this . createParams = reset ? { ...defaultCreateParams , ...params } : { ...this . createParams , ...params } ;
137- vscode . setState ( this . createParams ) ;
142+ this . createParams = reset ? { ...createDefaultParams ( ) , ...params } : { ...this . createParams , ...params } ;
143+ this . _host . setState ( this . createParams ) ;
138144 if ( this . onchange ) {
139145 this . onchange ( this . createParams ) ;
140146 }
@@ -324,14 +330,14 @@ export class CreatePRContextNew {
324330 // awaiting would discard the user's title/description if the create fails
325331 // (for example when the branch push fails and needs `--force`), causing
326332 // the description to fall back to the template on the next webview load.
327- vscode . setState ( defaultCreateParams ) ;
333+ this . _host . setState ( createDefaultParams ( ) ) ;
328334 } catch ( e ) {
329335 this . updateState ( { createError : ( typeof e === 'string' ) ? e : ( e . message ? e . message : 'An unknown error occurred.' ) } ) ;
330336 }
331337 } ;
332338
333339 postMessage = async ( message : any ) : Promise < any > => {
334- return this . _handler ? .postMessage ( message ) ;
340+ return this . _host . postMessage ( message ) ;
335341 } ;
336342
337343 handleMessage = async ( message : { command : string , params ?: Partial < CreateParamsNew > , scrollPosition ?: ScrollPosition } ) : Promise < void > => {
@@ -396,7 +402,7 @@ export class CreatePRContextNew {
396402
397403 case 'reset' :
398404 if ( ! message . params ) {
399- this . updateState ( defaultCreateParams , true ) ;
405+ this . updateState ( createDefaultParams ( ) , true ) ;
400406 return ;
401407 }
402408 message . params . creating = message . params . creating ?? false ;
@@ -453,8 +459,8 @@ export class CreatePRContextNew {
453459 }
454460 } ;
455461
456- public static instance = new CreatePRContextNew ( ) ;
457462}
458463
459- const PullRequestContextNew = createContext < CreatePRContextNew > ( CreatePRContextNew . instance ) ;
464+ // Every render entry point must supply a provider.
465+ const PullRequestContextNew = createContext < CreatePRContextNew > ( undefined ! ) ;
460466export default PullRequestContextNew ;
0 commit comments