moleculerjs / moleculer

:rocket: Progressive microservices framework for Node.js
https://moleculer.services/
MIT License
6.14k stars 582 forks source link

Mixin application order #1288

Closed leiyangyou closed 2 months ago

leiyangyou commented 3 months ago

Current Behavior

mixins started invocation order is in reverse

Expected Behavior

I expect that mixin invocation order is in order of inclusion

Failure Information

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

env SERVICES=exps yarn start

Reproduce code snippet

module.exports = {
  name: 'exps',
  mixins: [
    {
      started () {
        console.log('queue service started')
      },
      stopped () {
        console.log('queue service stopped')
      }
    },
    {
      started () {
        console.log('db service started')
      },
      stopped () {
        console.log('db service stopped')
      }
    }
  ],
  actions: {
    test (ctx) {
      console.log(ctx.logger)
    }
  },
  started () {
    console.log('exps service started', this.settings)
  },
  stopped () {
    console.log('exps service stopped')
  }
}

the above code prints db service started then queue service started

is there any particular reason why mixin inclusions are in reverse order of definition?

the relevant lines are in service.js, why do we reverse()?

                const mixedSchema = Array.from(mixins)
                    .reverse()
                    .reduce((s, mixin) => {
                        if (mixin.mixins) mixin = this.applyMixins(mixin);

                        return s ? this.mergeSchemas(s, mixin) : mixin;
                    }, null);

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.