2323 * publish reaches the door: the race the pins are for.
2424 */
2525
26+ import type { MetadataClient } from '@object-ui/data-objectstack' ;
27+
2628type Options = Record < string , unknown > ;
2729
2830interface Row {
@@ -37,6 +39,8 @@ export interface DraftPublishDoor {
3739 reset : ( type : string , name : string , options ?: Options ) => Promise < unknown > ;
3840 getDraft : ( type : string , name : string , options ?: Options ) => Promise < unknown > ;
3941 } ;
42+ /** The same doors, typed as the console's client for callers that take one. */
43+ metadataClient : Pick < MetadataClient , 'save' | 'publish' | 'reset' | 'getDraft' > ;
4044 /** The pending draft body at the address, or `undefined`. */
4145 draftOf : ( type : string , name : string , packageId ?: string | null ) => unknown ;
4246 /** The live body at the address, or `undefined`. */
@@ -71,63 +75,67 @@ export function draftPublishDoor(): DraftPublishDoor {
7175 drafts . set ( k , row ) ;
7276 return row ;
7377 } ;
74- return {
75- client : {
76- async save ( type , name , body , options = { } ) {
77- calls . push ( [ 'save' , type , name , options ] ) ;
78- const k = key ( type , name , options . packageId ) ;
79- if ( options . mode !== 'draft' ) {
80- const row = { body, version : `live-${ ++ seq } ` } ;
81- live . set ( k , row ) ;
82- return { success : true , version : row . version } ;
83- }
84- const current = drafts . get ( k ) ;
85- if ( options . ifNoneMatch === '*' && current ) throw conflict ( current . version ) ;
86- if ( typeof options . ifMatch === 'string' && options . ifMatch !== current ?. version ) {
87- throw conflict ( current ?. version ?? null ) ;
88- }
89- const row = writeDraft ( k , body ) ;
90- return { success : true , version : row . version , state : 'draft' } ;
91- } ,
92- async publish ( type , name , options = { } ) {
93- calls . push ( [ 'publish' , type , name , options ] ) ;
94- if ( pendingInterleave ) {
95- const run = pendingInterleave ;
96- pendingInterleave = null ;
97- run ( ) ;
98- }
99- const k = key ( type , name , options . packageId ) ;
100- const draft = drafts . get ( k ) ;
101- if ( options . draftVersion !== undefined && options . draftVersion !== draft ?. version ) {
102- throw conflict ( draft ?. version ?? null ) ;
103- }
104- const active = live . get ( k ) ;
105- if ( typeof options . ifMatch === 'string' && options . ifMatch !== active ?. version ) throw conflict ( active ?. version ?? null ) ;
106- if ( options . ifNoneMatch === '*' && active ) throw conflict ( active . version ) ;
107- if ( ! draft ) throw Object . assign ( new Error ( 'no draft' ) , { status : 404 , code : 'NO_DRAFT' } ) ;
108- const row = { body : draft . body , version : `live-${ ++ seq } ` } ;
78+ const client : DraftPublishDoor [ 'client' ] = {
79+ async save ( type , name , body , options = { } ) {
80+ calls . push ( [ 'save' , type , name , options ] ) ;
81+ const k = key ( type , name , options . packageId ) ;
82+ if ( options . mode !== 'draft' ) {
83+ const row = { body, version : `live-${ ++ seq } ` } ;
10984 live . set ( k , row ) ;
110- drafts . delete ( k ) ;
11185 return { success : true , version : row . version } ;
112- } ,
113- async reset ( type , name , options = { } ) {
114- calls . push ( [ 'reset' , type , name , options ] ) ;
115- const k = key ( type , name , options . packageId ) ;
116- if ( options . state === 'draft' ) {
117- const draft = drafts . get ( k ) ;
118- if ( typeof options . ifMatch === 'string' && options . ifMatch !== draft ?. version ) throw conflict ( draft ?. version ?? null ) ;
119- drafts . delete ( k ) ;
120- } else {
121- live . delete ( k ) ;
122- }
123- return { success : true } ;
124- } ,
125- async getDraft ( type , name , options = { } ) {
126- calls . push ( [ 'getDraft' , type , name , options ] ) ;
127- const draft = drafts . get ( key ( type , name , options . packageId ) ) ;
128- return draft ? { type, name, item : draft . body , version : draft . version } : null ;
129- } ,
86+ }
87+ const current = drafts . get ( k ) ;
88+ if ( options . ifNoneMatch === '*' && current ) throw conflict ( current . version ) ;
89+ if ( typeof options . ifMatch === 'string' && options . ifMatch !== current ?. version ) {
90+ throw conflict ( current ?. version ?? null ) ;
91+ }
92+ const row = writeDraft ( k , body ) ;
93+ return { success : true , version : row . version , state : 'draft' } ;
94+ } ,
95+ async publish ( type , name , options = { } ) {
96+ calls . push ( [ 'publish' , type , name , options ] ) ;
97+ if ( pendingInterleave ) {
98+ const run = pendingInterleave ;
99+ pendingInterleave = null ;
100+ run ( ) ;
101+ }
102+ const k = key ( type , name , options . packageId ) ;
103+ const draft = drafts . get ( k ) ;
104+ if ( options . draftVersion !== undefined && options . draftVersion !== draft ?. version ) {
105+ throw conflict ( draft ?. version ?? null ) ;
106+ }
107+ const active = live . get ( k ) ;
108+ if ( typeof options . ifMatch === 'string' && options . ifMatch !== active ?. version ) throw conflict ( active ?. version ?? null ) ;
109+ if ( options . ifNoneMatch === '*' && active ) throw conflict ( active . version ) ;
110+ if ( ! draft ) throw Object . assign ( new Error ( 'no draft' ) , { status : 404 , code : 'NO_DRAFT' } ) ;
111+ const row = { body : draft . body , version : `live-${ ++ seq } ` } ;
112+ live . set ( k , row ) ;
113+ drafts . delete ( k ) ;
114+ return { success : true , version : row . version } ;
130115 } ,
116+ async reset ( type , name , options = { } ) {
117+ calls . push ( [ 'reset' , type , name , options ] ) ;
118+ const k = key ( type , name , options . packageId ) ;
119+ if ( options . state === 'draft' ) {
120+ const draft = drafts . get ( k ) ;
121+ if ( typeof options . ifMatch === 'string' && options . ifMatch !== draft ?. version ) throw conflict ( draft ?. version ?? null ) ;
122+ drafts . delete ( k ) ;
123+ } else {
124+ live . delete ( k ) ;
125+ }
126+ return { success : true } ;
127+ } ,
128+ async getDraft ( type , name , options = { } ) {
129+ calls . push ( [ 'getDraft' , type , name , options ] ) ;
130+ const draft = drafts . get ( key ( type , name , options . packageId ) ) ;
131+ return draft ? { type, name, item : draft . body , version : draft . version } : null ;
132+ } ,
133+ } ;
134+ return {
135+ client,
136+ // The doors answer what the console's client answers for these calls; its
137+ // generic signatures are what the cast stands in for.
138+ metadataClient : client as unknown as DraftPublishDoor [ 'metadataClient' ] ,
131139 draftOf : ( type , name , packageId ) => drafts . get ( key ( type , name , packageId ) ) ?. body ,
132140 liveOf : ( type , name , packageId ) => live . get ( key ( type , name , packageId ) ) ?. body ,
133141 seedLive : ( type , name , packageId , body ) => {
0 commit comments