github.com/metacurrency/holochain@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/action_bundleclose.go (about)

     1  package holochain
     2  
     3  //------------------------------------------------------------
     4  // CloseBundle
     5  
     6  type APIFnCloseBundle struct {
     7  	commit bool
     8  }
     9  
    10  func (a *APIFnCloseBundle) Name() string {
    11  	return "bundleClose"
    12  }
    13  
    14  func (a *APIFnCloseBundle) Args() []Arg {
    15  	return []Arg{{Name: "commit", Type: BoolArg}}
    16  }
    17  
    18  func (a *APIFnCloseBundle) Call(h *Holochain) (response interface{}, err error) {
    19  
    20  	bundle := h.Chain().BundleStarted()
    21  	if bundle == nil {
    22  		err = ErrBundleNotStarted
    23  		return
    24  	}
    25  
    26  	isCancel := !a.commit
    27  	// if this is a cancel call all the bundleCancel routines
    28  	if isCancel {
    29  		for _, zome := range h.nucleus.dna.Zomes {
    30  			var r Ribosome
    31  			r, _, err = h.MakeRibosome(zome.Name)
    32  			if err != nil {
    33  				continue
    34  			}
    35  			var result string
    36  			result, err = r.BundleCanceled(BundleCancelReasonUserCancel)
    37  			if err != nil {
    38  				Debugf("error in %s.bundleCanceled():%v", zome.Name, err)
    39  				continue
    40  			}
    41  			if result == BundleCancelResponseCommit {
    42  				Debugf("%s.bundleCanceled() overrode cancel", zome.Name)
    43  				err = nil
    44  				return
    45  			}
    46  		}
    47  	}
    48  	err = h.Chain().CloseBundle(a.commit)
    49  	if err == nil {
    50  		// if there wasn't an error closing the bundle share all the commits
    51  		for _, a := range bundle.sharing {
    52  			_, def, err := h.GetEntryDef(a.GetHeader().Type)
    53  			if err != nil {
    54  				h.dht.dlog.Logf("Error getting entry def in close bundle:%v", err)
    55  				err = nil
    56  			} else {
    57  				err = a.Share(h, def)
    58  			}
    59  		}
    60  	}
    61  	return
    62  }