How to use the @loopback/context.BindingScope.SINGLETON function in @loopback/context

To help you get started, we’ve selected a few @loopback/context examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-next / packages / repository / src / mixins / repository.mixin.ts View on Github external
dataSource(
      dataSource: Class | D,
      name?: string,
    ): Binding {
      // We have an instance of
      if (dataSource instanceof juggler.DataSource) {
        const key = `datasources.${name || dataSource.name}`;
        return this.bind(key)
          .to(dataSource)
          .tag('datasource');
      } else if (typeof dataSource === 'function') {
        const binding = createBindingFromClass(dataSource, {
          name: name || dataSource.dataSourceName,
          namespace: 'datasources',
          type: 'datasource',
          defaultScope: BindingScope.SINGLETON,
        });
        this.add(binding);
        return binding;
      } else {
        throw new Error('not a valid DataSource.');
      }
    }
github strongloop / loopback-next / packages / repository / src / mixins / repository.mixin.ts View on Github external
// We have an instance of
      if (dataSource instanceof juggler.DataSource) {
        // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
        name = name || dataSource.name;
        const key = `datasources.${name}`;
        return this.bind(key)
          .to(dataSource)
          .tag('datasource');
      } else if (typeof dataSource === 'function') {
        // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
        name = name || dataSource.dataSourceName;
        const binding = createBindingFromClass(dataSource, {
          name,
          namespace: 'datasources',
          type: 'datasource',
          defaultScope: BindingScope.SINGLETON,
        });
        this.add(binding);
        return binding;
      } else {
        throw new Error('not a valid DataSource.');
      }
    }
github strongloop / loopback-next / packages / core / src / __tests__ / unit / application.unit.ts View on Github external
it('binds classes with @bind from a component', () => {
      @bind({scope: BindingScope.SINGLETON, tags: ['foo']})
      class MyClass {}

      class MyComponentWithClasses implements Component {
        classes = {'my-class': MyClass};
      }

      app.component(MyComponentWithClasses);
      const binding = app.getBinding('my-class');
      expect(binding.scope).to.eql(BindingScope.SINGLETON);
      expect(binding.tagNames).to.containEql('foo');
    });
github strongloop / loopback-next / packages / core / src / __tests__ / unit / application-lifecycle.unit.ts View on Github external
it('honors @bind', async () => {
      @bind({
        tags: {
          [CoreTags.LIFE_CYCLE_OBSERVER]: CoreTags.LIFE_CYCLE_OBSERVER,
          [CoreTags.LIFE_CYCLE_OBSERVER_GROUP]: 'my-group',
          namespace: CoreBindings.LIFE_CYCLE_OBSERVERS,
        },
        scope: BindingScope.SINGLETON,
      })
      class MyObserverWithBind implements LifeCycleObserver {
        status = 'not-initialized';

        start() {
          this.status = 'started';
        }
        stop() {
          this.status = 'stopped';
        }
      }

      const app = new Application();
      const binding = createBindingFromClass(MyObserverWithBind);
      app.add(binding);
      expect(binding.tagMap[CoreTags.LIFE_CYCLE_OBSERVER_GROUP]).to.eql(
github strongloop / loopback-next / packages / core / src / __tests__ / unit / application-lifecycle.unit.ts View on Github external
it('honors @lifeCycleObserver', async () => {
      const app = new Application();
      const binding = createBindingFromClass(MyObserverWithDecorator);
      app.add(binding);
      expect(binding.tagMap[CoreTags.LIFE_CYCLE_OBSERVER_GROUP]).to.eql(
        'my-group',
      );
      expect(binding.scope).to.eql(BindingScope.SINGLETON);

      const observer = await app.get(binding.key);
      expect(observer.status).to.equal('not-initialized');
      await app.start();
      expect(observer.status).to.equal('started');
      await app.stop();
      expect(observer.status).to.equal('stopped');
    });
github strongloop / loopback-next / packages / core / src / __tests__ / unit / application.unit.ts View on Github external
it('binds a singleton service', () => {
      @bind({scope: BindingScope.SINGLETON})
      class MySingletonService {}

      const binding = app.service(MySingletonService);
      expect(binding.scope).to.equal(BindingScope.SINGLETON);
      expect(findKeysByTag(app, 'service')).to.containEql(binding.key);
    });
github strongloop / loopback-next / packages / core / src / __tests__ / unit / lifecycle-registry.unit.ts View on Github external
async function givenLifeCycleRegistry() {
    context.bind(CoreBindings.LIFE_CYCLE_OBSERVER_OPTIONS).to({
      orderedGroups: DEFAULT_ORDERED_GROUPS,
      parallel: false,
    });
    context
      .bind(CoreBindings.LIFE_CYCLE_OBSERVER_REGISTRY)
      .toClass(TestObserverRegistry)
      .inScope(BindingScope.SINGLETON);
    registry = (await context.get(
      CoreBindings.LIFE_CYCLE_OBSERVER_REGISTRY,
    )) as TestObserverRegistry;
  }
github strongloop / loopback4-example-shopping / packages / shopping / src / services / recommender.service.ts View on Github external
const asRecommenderService: BindingTemplate = binding => {
    extensionFor(RECOMMENDER_SERVICE)(binding);
    binding.tag({protocol}).inScope(BindingScope.SINGLETON);
  };
  return asRecommenderService;
github strongloop / loopback-next / packages / boot / src / mixins / boot.mixin.ts View on Github external
export function _bindBooter(
  ctx: Context,
  booterCls: Constructor,
): Binding {
  const binding = createBindingFromClass(booterCls, {
    namespace: BootBindings.BOOTER_PREFIX,
    defaultScope: BindingScope.SINGLETON,
  }).tag(BootTags.BOOTER);
  ctx.add(binding);
  /**
   * Set up configuration binding as alias to `BootBindings.BOOT_OPTIONS`
   * so that the booter can use `@config`.
   */
  if (binding.tagMap.artifactNamespace) {
    ctx
      .configure(binding.key)
      .toAlias(
        `${BootBindings.BOOT_OPTIONS.key}#${binding.tagMap.artifactNamespace}`,
      );
  }
  return binding;
}
github strongloop / loopback-next / packages / rest / src / router / controller-route.ts View on Github external
* Bind current controller to the request context in `SINGLETON` scope.
     * Within the same request, we always get the same instance of the
     * current controller when `requestContext.get(CoreBindings.CONTROLLER_CURRENT)`
     * is invoked.
     *
     * Please note the controller class itself can be bound to other scopes,
     * such as SINGLETON or TRANSIENT (default) in the application or server
     * context.
     *
     * - SINGLETON: all requests share the same instance of a given controller
     * - TRANSIENT: each request has its own instance of a given controller
     */
    requestContext
      .bind(CoreBindings.CONTROLLER_CURRENT)
      .toDynamicValue(() => this._controllerFactory(requestContext))
      .inScope(BindingScope.SINGLETON);
    requestContext.bind(CoreBindings.CONTROLLER_CLASS).to(this._controllerCtor);
    requestContext
      .bind(CoreBindings.CONTROLLER_METHOD_NAME)
      .to(this._methodName);
    requestContext.bind(RestBindings.OPERATION_SPEC_CURRENT).to(this.spec);
  }