MCPcopy Create free account
hub / github.com/cryptii/cryptii / _triggerEncoderTranslation

Method _triggerEncoderTranslation

src/Pipe.js:613–682  ·  view source on GitHub ↗

* Triggers encoder translation and handles their results. Keeps track of busy * encoders and skips or repeats translations accordingly. * @protected * @param {Encoder} encoder * @param {boolean} isEncode True for encoding, false for decoding.

(encoder, isEncode)

Source from the content-addressed store, hash-verified

611 * @param {boolean} isEncode True for encoding, false for decoding.
612 */
613 async _triggerEncoderTranslation (encoder, isEncode) {
614 // Skip translation if encoder is currently busy
615 // As soon as the encoder responds the translation will be repeated when the
616 // source content differs
617 if (this._getBrickState(encoder, 'busy')) {
618 return this
619 }
620
621 // Mark encoder as busy
622 this._setBrickState(encoder, 'busy', true)
623
624 // Collect translation data
625 const lowerBucket = this.getBucketIndexForBrick(encoder)
626 const source = this.getContent(lowerBucket + (isEncode ? 0 : 1), false)
627 const settingsVersion = this._getBrickState(encoder, 'settingsVersion')
628
629 // Trigger encoder translation and await result
630 let result, error
631 try {
632 result = await encoder.translate(source, isEncode)
633 } catch (e) {
634 error = e
635 }
636
637 // Stop here if encoder is no longer part of the pipe
638 if (!this.containsBrick(encoder)) {
639 return
640 }
641
642 // Mark encoder as no longer busy
643 this._setBrickState(encoder, 'busy', false)
644
645 // Collect post translation data, buckets may have changed in the meantim
646 const postSettingsVersion = this._getBrickState(encoder, 'settingsVersion')
647 const postLowerBucket = this.getBucketIndexForBrick(encoder)
648 const postSourceBucket = postLowerBucket + (isEncode ? 0 : 1)
649 const postSource = this.getContent(postSourceBucket, false)
650 const resultBucket = postLowerBucket + (isEncode ? 1 : 0)
651
652 // Check if the currently selected bucket has been relocated in the opposite
653 // encoding direction in the meantime
654 if ((isEncode && this._selectedBucket >= resultBucket) ||
655 (!isEncode && this._selectedBucket <= resultBucket)) {
656 // Trigger translation in the opposite direction
657 this._triggerEncoderTranslation(encoder, !isEncode)
658 // Result of this translation is no longer relevant
659 // Throw it away and stop here
660 return
661 }
662
663 // If translation succeeded, propagate result
664 if (!error) {
665 this.setContent(result, resultBucket, encoder)
666 }
667
668 // Check if the source or the encoder settings have changed in the meantime
669 if (!postSource.isEqualTo(source) ||
670 settingsVersion !== postSettingsVersion) {

Callers 2

_propagateContentMethod · 0.95
brickSettingDidChangeMethod · 0.95

Calls 9

_getBrickStateMethod · 0.95
_setBrickStateMethod · 0.95
getContentMethod · 0.95
containsBrickMethod · 0.95
setContentMethod · 0.95
brickDidFinishMethod · 0.95
translateMethod · 0.80
isEqualToMethod · 0.80

Tested by

no test coverage detected