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

To help you get started, we’ve selected a few @loopback/core 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 / boot / src / __tests__ / fixtures / bindable-classes.artifact.ts View on Github external
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {bind, BindingScope, Provider} from '@loopback/core';

@bind({
  tags: {serviceType: 'local'},
  scope: BindingScope.SINGLETON,
})
export class BindableGreetingService {
  greet(whom = 'world') {
    return Promise.resolve(`Hello ${whom}`);
  }
}

@bind({tags: {serviceType: 'local', name: 'CurrentDate'}})
export class DateProvider implements Provider {
  value(): Promise {
    return Promise.resolve(new Date());
  }
}

export class NotBindableGreetingService {
  greet(whom = 'world') {
github strongloop / loopback-next / examples / greeting-app / src / caching-service.ts View on Github external
import debugFactory from 'debug';
import {Message} from './types';
const debug = debugFactory('greeter-extension');

/**
 * Configuration for CachingService
 */
export interface CachingServiceOptions {
  // The time-to-live setting for a cache entry
  ttl: number;
}

/**
 * Message caching service
 */
@bind({scope: BindingScope.SINGLETON})
export class CachingService {
  private timer: NodeJS.Timer;
  private store: Map = new Map();

  constructor(
    @config.view()
    private optionsView: ContextView,
  ) {
    // Use a view so that we can listen on `refresh` events, which are emitted
    // when the configuration binding is updated in the context.
    optionsView.on('refresh', () => {
      debug('Restarting the service as configuration changes...');
      this.restart().catch(err => {
        console.error('Cannot restart the caching service.', err);
        process.exit(1);
      });
github strongloop / loopback-next / packages / service-proxy / src / __tests__ / unit / mixin / service.mixin.unit.ts View on Github external
it('binds singleton service from app.serviceProvider()', async () => {
    @bind({scope: BindingScope.SINGLETON})
    class SingletonGeocoderServiceProvider extends GeocoderServiceProvider {}
    const myApp = new AppWithServiceMixin();

    const binding = myApp.serviceProvider(SingletonGeocoderServiceProvider);
    expect(binding.scope).to.equal(BindingScope.SINGLETON);
  });
github strongloop / loopback-next / packages / repository / src / __tests__ / unit / mixins / repository.mixin.unit.ts View on Github external
it('binds singleton repository from app.repository()', () => {
    @bind({scope: BindingScope.SINGLETON})
    class SingletonNoteRepo extends NoteRepo {}

    const myApp = new AppWithRepoMixin();

    const binding = myApp.repository(SingletonNoteRepo);
    expect(binding.scope).to.equal(BindingScope.SINGLETON);
  });
github strongloop / loopback-next / packages / boot / src / __tests__ / integration / lifecycle-observer.booter.integration.ts View on Github external
it('boots life cycle observers when app.boot() is called', async () => {
    const expectedBinding = {
      key: `${OBSERVER_PREFIX}.MyLifeCycleObserver`,
      tags: [ContextTags.TYPE, OBSERVER_TAG],
      scope: BindingScope.SINGLETON,
    };

    await app.boot();

    const bindings = app
      .findByTag(OBSERVER_TAG)
      .map(b => ({key: b.key, tags: b.tagNames, scope: b.scope}));
    expect(bindings).to.containEql(expectedBinding);
  });
github strongloop / loopback-next / packages / service-proxy / src / __tests__ / unit / mixin / service.mixin.unit.ts View on Github external
it('binds singleton service from app.serviceProvider()', async () => {
    @bind({scope: BindingScope.SINGLETON})
    class SingletonGeocoderServiceProvider extends GeocoderServiceProvider {}
    const myApp = new AppWithServiceMixin();

    const binding = myApp.serviceProvider(SingletonGeocoderServiceProvider);
    expect(binding.scope).to.equal(BindingScope.SINGLETON);
  });
github strongloop / loopback-next / packages / repository / src / __tests__ / unit / mixins / repository.mixin.unit.ts View on Github external
it('binds singleton repository from app.repository()', () => {
    @bind({scope: BindingScope.SINGLETON})
    class SingletonNoteRepo extends NoteRepo {}

    const myApp = new AppWithRepoMixin();

    const binding = myApp.repository(SingletonNoteRepo);
    expect(binding.scope).to.equal(BindingScope.SINGLETON);
  });
github strongloop / loopback-next / packages / rest / src / __tests__ / unit / router / controller-route.unit.ts View on Github external
it('adds bindings to the request context', async () => {
      expect(requestCtx.contains(CoreBindings.CONTROLLER_CURRENT));
      expect(
        requestCtx.getBinding(CoreBindings.CONTROLLER_CURRENT).scope,
      ).to.equal(BindingScope.SINGLETON);
      expect(await requestCtx.get(CoreBindings.CONTROLLER_CLASS)).to.equal(
        MyController,
      );
      expect(
        await requestCtx.get(CoreBindings.CONTROLLER_METHOD_NAME),
      ).to.equal('greet');
      expect(await requestCtx.get(RestBindings.OPERATION_SPEC_CURRENT)).to.eql({
        'x-controller-name': 'MyController',
        'x-operation-name': 'greet',
        tags: ['MyController'],
        responses: {'200': {description: 'An undocumented response body.'}},
      });
    });