11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst stream = require('stream');
61cb0ef41Sopenharmony_cilet state = 0;
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// What you do
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// const stream = new stream.Transform({
121cb0ef41Sopenharmony_ci//   transform: function transformCallback(chunk, _, next) {
131cb0ef41Sopenharmony_ci//     // part 1
141cb0ef41Sopenharmony_ci//     this.push(chunk);
151cb0ef41Sopenharmony_ci//     //part 2
161cb0ef41Sopenharmony_ci//     next();
171cb0ef41Sopenharmony_ci//   },
181cb0ef41Sopenharmony_ci//   final: function endCallback(done) {
191cb0ef41Sopenharmony_ci//     // part 1
201cb0ef41Sopenharmony_ci//     process.nextTick(function () {
211cb0ef41Sopenharmony_ci//       // part 2
221cb0ef41Sopenharmony_ci//       done();
231cb0ef41Sopenharmony_ci//     });
241cb0ef41Sopenharmony_ci//   },
251cb0ef41Sopenharmony_ci//   flush: function flushCallback(done) {
261cb0ef41Sopenharmony_ci//     // part 1
271cb0ef41Sopenharmony_ci//     process.nextTick(function () {
281cb0ef41Sopenharmony_ci//       // part 2
291cb0ef41Sopenharmony_ci//       done();
301cb0ef41Sopenharmony_ci//     });
311cb0ef41Sopenharmony_ci//   }
321cb0ef41Sopenharmony_ci// });
331cb0ef41Sopenharmony_ci// t.on('data', dataListener);
341cb0ef41Sopenharmony_ci// t.on('end', endListener);
351cb0ef41Sopenharmony_ci// t.on('finish', finishListener);
361cb0ef41Sopenharmony_ci// t.write(1);
371cb0ef41Sopenharmony_ci// t.write(4);
381cb0ef41Sopenharmony_ci// t.end(7, endMethodCallback);
391cb0ef41Sopenharmony_ci//
401cb0ef41Sopenharmony_ci// The order things are called
411cb0ef41Sopenharmony_ci//
421cb0ef41Sopenharmony_ci// 1. transformCallback part 1
431cb0ef41Sopenharmony_ci// 2. dataListener
441cb0ef41Sopenharmony_ci// 3. transformCallback part 2
451cb0ef41Sopenharmony_ci// 4. transformCallback part 1
461cb0ef41Sopenharmony_ci// 5. dataListener
471cb0ef41Sopenharmony_ci// 6. transformCallback part 2
481cb0ef41Sopenharmony_ci// 7. transformCallback part 1
491cb0ef41Sopenharmony_ci// 8. dataListener
501cb0ef41Sopenharmony_ci// 9. transformCallback part 2
511cb0ef41Sopenharmony_ci// 10. finalCallback part 1
521cb0ef41Sopenharmony_ci// 11. finalCallback part 2
531cb0ef41Sopenharmony_ci// 12. flushCallback part 1
541cb0ef41Sopenharmony_ci// 13. finishListener
551cb0ef41Sopenharmony_ci// 14. endMethodCallback
561cb0ef41Sopenharmony_ci// 15. flushCallback part 2
571cb0ef41Sopenharmony_ci// 16. endListener
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ciconst t = new stream.Transform({
601cb0ef41Sopenharmony_ci  objectMode: true,
611cb0ef41Sopenharmony_ci  transform: common.mustCall(function(chunk, _, next) {
621cb0ef41Sopenharmony_ci    // transformCallback part 1
631cb0ef41Sopenharmony_ci    assert.strictEqual(++state, chunk);
641cb0ef41Sopenharmony_ci    this.push(state);
651cb0ef41Sopenharmony_ci    // transformCallback part 2
661cb0ef41Sopenharmony_ci    assert.strictEqual(++state, chunk + 2);
671cb0ef41Sopenharmony_ci    process.nextTick(next);
681cb0ef41Sopenharmony_ci  }, 3),
691cb0ef41Sopenharmony_ci  final: common.mustCall(function(done) {
701cb0ef41Sopenharmony_ci    state++;
711cb0ef41Sopenharmony_ci    // finalCallback part 1
721cb0ef41Sopenharmony_ci    assert.strictEqual(state, 10);
731cb0ef41Sopenharmony_ci    state++;
741cb0ef41Sopenharmony_ci    // finalCallback part 2
751cb0ef41Sopenharmony_ci    assert.strictEqual(state, 11);
761cb0ef41Sopenharmony_ci    done();
771cb0ef41Sopenharmony_ci  }, 1),
781cb0ef41Sopenharmony_ci  flush: common.mustCall(function(done) {
791cb0ef41Sopenharmony_ci    state++;
801cb0ef41Sopenharmony_ci    // fluchCallback part 1
811cb0ef41Sopenharmony_ci    assert.strictEqual(state, 12);
821cb0ef41Sopenharmony_ci    process.nextTick(function() {
831cb0ef41Sopenharmony_ci      state++;
841cb0ef41Sopenharmony_ci      // fluchCallback part 2
851cb0ef41Sopenharmony_ci      assert.strictEqual(state, 13);
861cb0ef41Sopenharmony_ci      done();
871cb0ef41Sopenharmony_ci    });
881cb0ef41Sopenharmony_ci  }, 1)
891cb0ef41Sopenharmony_ci});
901cb0ef41Sopenharmony_cit.on('finish', common.mustCall(function() {
911cb0ef41Sopenharmony_ci  state++;
921cb0ef41Sopenharmony_ci  // finishListener
931cb0ef41Sopenharmony_ci  assert.strictEqual(state, 15);
941cb0ef41Sopenharmony_ci}, 1));
951cb0ef41Sopenharmony_cit.on('end', common.mustCall(function() {
961cb0ef41Sopenharmony_ci  state++;
971cb0ef41Sopenharmony_ci  // endEvent
981cb0ef41Sopenharmony_ci  assert.strictEqual(state, 16);
991cb0ef41Sopenharmony_ci}, 1));
1001cb0ef41Sopenharmony_cit.on('data', common.mustCall(function(d) {
1011cb0ef41Sopenharmony_ci  // dataListener
1021cb0ef41Sopenharmony_ci  assert.strictEqual(++state, d + 1);
1031cb0ef41Sopenharmony_ci}, 3));
1041cb0ef41Sopenharmony_cit.write(1);
1051cb0ef41Sopenharmony_cit.write(4);
1061cb0ef41Sopenharmony_cit.end(7, common.mustCall(function() {
1071cb0ef41Sopenharmony_ci  state++;
1081cb0ef41Sopenharmony_ci  // endMethodCallback
1091cb0ef41Sopenharmony_ci  assert.strictEqual(state, 14);
1101cb0ef41Sopenharmony_ci}, 1));
111