Giter VIP home page Giter VIP logo

Comments (3)

vovaspace avatar vovaspace commented on May 23, 2024

Hi. Do you mean something like this?

class Some {}

const TOKENS = {
  someAsyncFactory: token<Factory<Promise<Some>>>('AsyncFactory<Some>'),
};

const container = new Container();
container.bind(TOKENS.someAsyncFactory).toFactory(async () => { /* .. */ });

const someAsyncFactory = container.get(TOKENS.someAsyncFactory);
const someInstance = await someAsyncFactory();

Can you give an example of how you would like to use this in the context of your business logic? Thanks!

from brandi.

daniel-white avatar daniel-white commented on May 23, 2024

Yes. I'm using a 3rd party module that has some state it internally refreshes when I call an .refresh() that is an async method. I was trying to avoid calling that in the consuming code.

from brandi.

vovaspace avatar vovaspace commented on May 23, 2024

Hi @daniel-white!

I opened the PR with the addition of async factories. The main changes:

  • The initializer function waits for asynchronous creation of the instance;
  • Factory waits for the resolution of the initialization, if the initializer has returned a Promise.

And it's all typed.

In your case, the logic can be implemented like this:

import { Container, AsyncFactory, token } from 'brandi';

class BusinessLogic { /* ... */ }

const TOKENS = {
  businessLogicFactory: token<AsyncFactory<BusinessLogic>>('AsyncFactory<BusinessLogic>'),
};

const container = new Container();

container
  .bind(TOKENS.businessLogicFactory)
  /*                              ↓ Returns a Promise. */
  .toFactory(BusinessLogic, () => thirdPartyInstance.refresh());

const businessLogicFactory = container.get(TOKENS.businessLogicFactory);

/*                    ↓ Will wait for the initializer to resolve. */
const businessLogic = await businessLogicFactory();

Is this exactly what you need?

from brandi.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.