|
| 1 | +# Mixins |
| 2 | + |
| 3 | +This directory contains source files for the mixins exported by this extension. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Sometimes it's helpful to write partial classes and then combining them together to build more powerful classes. This pattern is called Mixins (mixing in partial classes) and is supported by LoopBack 4. |
| 8 | + |
| 9 | +LoopBack 4 supports mixins at an `Application` level. Your partial class can then be mixed into the `Application` class. A mixin class can modify or override existing methods of the class or add new ones! It is also possible to mixin multiple classes together as needed. |
| 10 | + |
| 11 | +### High level example |
| 12 | +```ts |
| 13 | +class MyApplication extends MyMixinClass(Application) { |
| 14 | + // Your code |
| 15 | +}; |
| 16 | + |
| 17 | +// Multiple Classes mixed together |
| 18 | +class MyApp extends MyMixinClass(MyMixinClass2(Application)) { |
| 19 | + // Your code |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +## Getting Started |
| 24 | + |
| 25 | +For hello-extensions we write a simple Mixin that allows the `Application` class to bind a `Logger` class from ApplicationOptions, Components, or `.logger()` method that is mixed in. `Logger` instances are bound to the key `loggers.${Logger.name}`. Once a Logger has been bound, the user can retrieve it by using [Dependency Injection](http://loopback.io/doc/en/lb4/Dependency-injection.html) and the key for the `Logger`. |
| 26 | + |
| 27 | +### What is a Logger? |
| 28 | +> A Logger class is provides a mechanism for logging messages of varying priority by providing an implementation for `Logger.info()` & `Logger.error()`. An example of a Logger is `console` which has `console.log()` and `console.error()`. |
| 29 | +
|
| 30 | +#### An example Logger |
| 31 | +```ts |
| 32 | +class ColorLogger implements Logger { |
| 33 | + log(...args: LogArgs) { |
| 34 | + console.log('log :', ...args); |
| 35 | + } |
| 36 | + |
| 37 | + error(...args: LogArgs) { |
| 38 | + const data = args.join(' '); |
| 39 | + // log in red color |
| 40 | + console.log('\x1b[31m error: ' + data + '\x1b[0m'); |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## LoggerMixin |
| 46 | +A complete & functional implementation can be found in `logger.mixin.ts`. *Here are some key things to keep in mind when writing your own Mixin*. |
| 47 | + |
| 48 | +### constructor() |
| 49 | +A Mixin constructor must take an array of any type as it's argument. This would represent `ApplicationOptions` for our base class `Application` as well as any properties we would like for our Mixin. |
| 50 | + |
| 51 | +It is also important for the constructor to call `super(args)` so `Application` continues to work as expected. |
| 52 | +```ts |
| 53 | +constructor(...args: any[]) { |
| 54 | + super(args); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Binding via `ApplicationOptions` |
| 59 | +As mentioned earlier, since our `args` represents `ApplicationOptions`, we can make it possible for users to pass in their `Logger` implementations in a `loggers` array on `ApplicationOptions`. We can then read the array and automatically bind these for the user. |
| 60 | + |
| 61 | +#### Example user experience |
| 62 | +```ts |
| 63 | +class MyApp extends LoggerMixin(Application){ |
| 64 | + constructor(...args: any[]) { |
| 65 | + super(...args); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +const app = new MyApp({ |
| 70 | + loggers: [ColorLogger] |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +#### Example Implementation |
| 75 | +To implement this, we would check `this.options` to see if it has a `loggers` array and if so, bind it by calling the `.logger()` method. (More on that below). |
| 76 | +```ts |
| 77 | +if (this.options.loggers) { |
| 78 | + for (const logger of this.options.loggers) { |
| 79 | + this.logger(logger); |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### Binding via `.logger()` |
| 85 | +As mentioned earlier, we can add a new function to our `Application` class called `.logger()` into which a user would pass in their `Logger` implementation so we can bind it to the `loggers.*` key for them. We just add this new method on our partial Mixin class. |
| 86 | +```ts |
| 87 | +logger(logClass: Logger) { |
| 88 | + const loggerKey = `loggers.${logClass.name}`; |
| 89 | + this.bind(loggerKey).toClass(logClass); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Binding a `Logger` from a `Component` |
| 94 | +Our base class of `Application` already has a method that binds components. We can modify this method to continue binding a `Component` as usual but also binding any `Logger` instances provided by that `Component`. When modifying behavior of an existing method, we can ensure existing behavior by calling the `super.method()`. In our case the method is `.component()`. |
| 95 | +```ts |
| 96 | +component(component: Constructor<any>) { |
| 97 | + super.component(component); // ensures existing behavior from Application |
| 98 | + this.mountComponentLoggers(component); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +We have now modified `.component()` to do it's thing and then call our method `mountComponentLoggers()`. In this method is where we check for `Logger` implementations declared by the component in a `loggers` array by retrieving the instance of the `Component`. Then if `loggers` array exists, we bind the `Logger` instances as normal (by leveraging our `.logger()` method). |
| 103 | + |
| 104 | +```ts |
| 105 | +mountComponentLoggers(component: Constructor<any>) { |
| 106 | + const componentKey = `components.${component.name}`; |
| 107 | + const compInstance = this.getSync(componentKey); |
| 108 | + |
| 109 | + if (compInstance.loggers) { |
| 110 | + for (const logger of compInstance.loggers) { |
| 111 | + this.logger(logger); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | + |
| 118 | +## Examples for using LoggerMixin |
| 119 | +``` |
| 120 | +// Using the app's .logger() function. |
| 121 | +class LoggingApplication extends LoggerMixin(Application) { |
| 122 | + constructor(...args: any[]) { |
| 123 | + super(...args); |
| 124 | + this.logger(ColorLogger); |
| 125 | + } |
| 126 | +} |
| 127 | +
|
| 128 | +// Binding a Logger provided by a component |
| 129 | +class LoggingComponent implements Component{ |
| 130 | + loggers: [ColorLogger]; |
| 131 | +} |
| 132 | +
|
| 133 | +const app = new LoggingApplication({ |
| 134 | + components: [LoggingComponent] // Logger from MyComponent will be bound to loggers.ColorLogger |
| 135 | +}); |
| 136 | +``` |
0 commit comments