Will Factory.of() be supported in Aurelia 2?

I noticed in the documentation for Aurelia 2 that there is a black X next to the DI Factory resolver. Is this simply an indication that it hasn’t been implemented yet, or is this resolver going away? If the latter, will there be an equivalent?

1 Like

This resolver will be added, there doesn’t seem to be anything special about this resolver that we don’t wanna add in v2. Though can you give an example of how you are using it?

First, that’s a relief!

Second, as to an example, I’m using it all over the place, actually. I use it wherever I have traditional dependencies to supply in the constructor, but also dependencies that I need Aurelia to inject.

Just one example can be found in TsiDocumentHub (note that I’m using the babel-contracts plugin that I set up with Eiffel-style Design-by-Contract terminology):

@inject(SessionService, TaskQueue, Factory.of(TsiDocumentManager))
export class TsiDocumentHub extends TsiMessenger {
  constructor(sessionService, taskQueue, documentManagerFactory) {
    invariant: {
        sessionService, 'Session service exists';
        taskQueue, 'Task queue exists';
        documentManagerFactory, 'Document manager factory exists';
    }

    super();

    ...
    this._documentManagerFactory = documentManagerFactory;
    ...
  }
  ...
}

where TsiDocumentManager has this signature:

@inject(TsiGroupStylesManager)
export class TsiDocumentManager extends TsiMessenger {
  /**
   * @param {string} messageChannel Message channel.
   * @param {string} key Key under which this Document Manager is stored in the Document Hub.
   */
  constructor(groupStylesManager, messageChannel, key) {
    invariant: {
      groupStylesManager, 'Group styles manager exists';
      messageChannel, 'Message channel exists';
      key, 'Key exists';
    }

    super();

    ...
  }
  ...
}

The messageChannel and key dependencies are simply passed in in the traditional way, but Aurelia injects the TsiGroupStylesManager, of course.

1 Like

How will you use _documentManagerFactory later?

  1. Is this like this
const manager = this._documentManagerFactory()
  1. or like this
const manager = this._documentManagerFactory(extraParam1, extraParam2, extraParam3)

if it’s (1), shouldn’t Lazy.of also provide the same effect?

EDIT: Actually nvm, lazy doesn’t guarantee it’s a new instance every call.
Thanks for the example.

To your point, Lazy.of is not the way for me to go here. Also, this is but one example of many throughout my codebase (well, maybe not many, but a good number).

Consider further down in the TsiDocumentHub class:

/**
   * Message handler for requesting that a new document manager be created in the hub,
   * and associated with the given key.
   * @param {object} messageData The message payload.
   * @private
   */
  _onMessageRequestDocumentManager(messageData) {
    require: {
      messageData, 'Message data exists';
      messageData.messageChannel, 'Message channel exists';
      messageData.key, 'Key exists';
      this._documentManagers, 'Document managers exists';
      this._documentManagers instanceof Map, 'Document managers is of type Map';
    }

    this._documentManagers.set(
      messageData.key,
      this._documentManagerFactory(messageData.messageChannel, messageData.key),
    );

    ensure: {
      this._documentManagers.has(messageData.key), 'Document manager is amongst document managers';
    }
  }

The instance of TsiDocumentManager receives from Aurelia the injected dependency of TsiGroupStylesManager referenced in my previous reply.

2 Likes

It’s added in this PR feat(di): @factory resolver by bigopon · Pull Request #1211 · aurelia/aurelia (github.com)

Thanks so much! I will keep an eye on this.

1 Like