@@ -10,9 +10,20 @@ var reader = require('../reader');
1010var ok = require ( 'assert' ) . ok ;
1111var equal = require ( 'assert' ) . equal ;
1212
13+ async function initWriter ( writer , lassoContext ) {
14+ if ( ! writer . _initialized && writer . impl . init ) {
15+ await writer . impl . init ( lassoContext ) ;
16+ writer . _initialized = true ;
17+ }
18+ }
19+
1320function Writer ( impl ) {
1421 Writer . $super . call ( this ) ;
1522 this . impl = impl || { } ;
23+
24+ // Lasso writer `init` function should only be called once. We call it the
25+ // first time that either writing a resource or bundle is attempted.
26+ this . _initialized = false ;
1627}
1728
1829Writer . prototype = {
@@ -193,6 +204,7 @@ Writer.prototype = {
193204 const resourceReader = reader . createResourceReader ( path , lassoContext ) ;
194205
195206 try {
207+ await initWriter ( this , lassoContext ) ;
196208 const writeResult = await this . impl . writeResource ( resourceReader , lassoContext ) ;
197209 return done ( writeResult ) ;
198210 } catch ( err ) {
@@ -245,6 +257,7 @@ Writer.prototype = {
245257 async writeBundles ( iteratorFunc , onBundleWrittenCallback , lassoContext ) {
246258 ok ( lassoContext , 'lassoContext is required' ) ;
247259
260+ await initWriter ( this , lassoContext ) ;
248261 let promise = Promise . resolve ( ) ;
249262
250263 iteratorFunc ( ( bundle ) => {
0 commit comments