@@ -3,6 +3,7 @@ import * as uuid from 'uuid';
33import type { ActionLessMessage , IoHelper } from './io-helper' ;
44import type { IoMessageMaker } from './message-maker' ;
55import { formatTime } from '../../../util' ;
6+ import type { Duration } from '../payloads/types' ;
67
78export interface SpanEnd {
89 readonly duration : number ;
@@ -55,7 +56,7 @@ type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
5556/**
5657 * Ending the span returns the observed duration
5758 */
58- export interface Duration {
59+ interface ElapsedTime {
5960 readonly asMs : number ;
6061 readonly asSec : number ;
6162}
@@ -64,10 +65,31 @@ export interface Duration {
6465 * A message span that can be ended and read times from
6566 */
6667export interface IMessageSpan < E extends SpanEnd > {
67- time ( ) : Duration ;
68- end ( payload : VoidWhenEmpty < Omit < E , keyof SpanEnd > > ) : Promise < Duration > ;
69- end ( payload : VoidWhenEmpty < Optional < E , keyof SpanEnd > > ) : Promise < Duration > ;
70- end ( message : string , payload : ForceEmpty < Optional < E , keyof SpanEnd > > ) : Promise < Duration > ;
68+ /**
69+ * Get the time elapsed since the start
70+ */
71+ elapsedTime ( ) : Promise < ElapsedTime > ;
72+ /**
73+ * Sends a simple, generic message with the current timing
74+ * For more complex intermediate messages, get the `elapsedTime` and use `notify`
75+ */
76+ timing ( maker : IoMessageMaker < Duration > , message ?: string ) : Promise < ElapsedTime > ;
77+ /**
78+ * Sends an arbitrary intermediate message as part of the span
79+ */
80+ notify ( message : ActionLessMessage < unknown > ) : Promise < void > ;
81+ /**
82+ * End the span with a payload
83+ */
84+ end ( payload : VoidWhenEmpty < Omit < E , keyof SpanEnd > > ) : Promise < ElapsedTime > ;
85+ /**
86+ * End the span with a payload, overwriting
87+ */
88+ end ( payload : VoidWhenEmpty < Optional < E , keyof SpanEnd > > ) : Promise < ElapsedTime > ;
89+ /**
90+ * End the span with a message and payload
91+ */
92+ end ( message : string , payload : ForceEmpty < Optional < E , keyof SpanEnd > > ) : Promise < ElapsedTime > ;
7193}
7294
7395/**
@@ -108,6 +130,7 @@ export class SpanMaker<S extends object, E extends SpanEnd> {
108130 startPayload ,
109131 ) ) ;
110132
133+ const timingMsg = '\n✨ %s time: %ds\n' ;
111134 const time = ( ) => {
112135 const elapsedTime = new Date ( ) . getTime ( ) - startTime ;
113136 return {
@@ -117,22 +140,36 @@ export class SpanMaker<S extends object, E extends SpanEnd> {
117140 } ;
118141
119142 return {
120- time ( ) : Duration {
121- const elapsedTime = new Date ( ) . getTime ( ) - startTime ;
122- return {
123- asMs : elapsedTime ,
124- asSec : formatTime ( elapsedTime ) ,
125- } ;
143+ elapsedTime : async ( msg ?: ActionLessMessage < object > ) : Promise < ElapsedTime > => {
144+ if ( msg ) {
145+ await notify ( {
146+ ...msg ,
147+ data : msg . data ,
148+ } ) ;
149+ }
150+ return time ( ) ;
151+ } ,
152+
153+ notify : async ( msg : ActionLessMessage < unknown > ) : Promise < void > => {
154+ await notify ( msg ) ;
155+ } ,
156+
157+ timing : async ( maker : IoMessageMaker < Duration > , message ?: string ) : Promise < ElapsedTime > => {
158+ const duration = time ( ) ;
159+ const endMsg = message ? message : timingMsg ;
160+ await notify ( maker . msg ( util . format ( endMsg , this . definition . name , duration . asSec ) , {
161+ duration : duration . asMs ,
162+ } ) ) ;
163+ return duration ;
126164 } ,
127165
128- end : async ( a : any , b ?: ForceEmpty < Optional < E , keyof SpanEnd > > ) : Promise < Duration > => {
166+ end : async ( a : any , b ?: ForceEmpty < Optional < E , keyof SpanEnd > > ) : Promise < ElapsedTime > => {
129167 const duration = time ( ) ;
130- const endMsg = b ? a : `\n✨ %s time: ${ duration . asSec } s\n` ;
168+ const endMsg = b ? a : timingMsg ;
131169 const endPayload = b ?? a ;
132170
133171 await notify ( this . definition . end . msg (
134- util . format ( endMsg , this . definition . name ) , {
135- marker : spanId ,
172+ util . format ( endMsg , this . definition . name , duration . asSec ) , {
136173 duration : duration . asMs ,
137174 ...endPayload ,
138175 } as E ) ) ;
0 commit comments