UNPKG

3.39 kBPlain TextView Raw
1// *****************************************************************************
2// Copyright (C) 2019 TypeFox and others.
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License v. 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0.
7//
8// This Source Code may also be made available under the following Secondary
9// Licenses when the conditions for such availability set forth in the Eclipse
10// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11// with the GNU Classpath Exception which is available at
12// https://www.gnu.org/software/classpath/license.html.
13//
14// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15// *****************************************************************************
16
17import { interfaces } from 'inversify';
18import {
19 bindContributionProvider, DefaultResourceProvider, MaybePromise, MessageClient,
20 MessageService, ResourceProvider, ResourceResolver
21} from '../common';
22import {
23 bindPreferenceSchemaProvider, PreferenceProvider,
24 PreferenceProviderProvider, PreferenceProxyOptions, PreferenceSchema, PreferenceSchemaProvider, PreferenceScope,
25 PreferenceService, PreferenceServiceImpl, PreferenceValidationService
26} from './preferences';
27import { InjectablePreferenceProxy, PreferenceProxyFactory, PreferenceProxySchema } from './preferences/injectable-preference-proxy';
28
29export function bindMessageService(bind: interfaces.Bind): interfaces.BindingWhenOnSyntax<MessageService> {
30 bind(MessageClient).toSelf().inSingletonScope();
31 return bind(MessageService).toSelf().inSingletonScope();
32}
33
34export function bindPreferenceService(bind: interfaces.Bind): void {
35 bind(PreferenceProvider).toSelf().inSingletonScope().whenTargetNamed(PreferenceScope.User);
36 bind(PreferenceProvider).toSelf().inSingletonScope().whenTargetNamed(PreferenceScope.Workspace);
37 bind(PreferenceProvider).toSelf().inSingletonScope().whenTargetNamed(PreferenceScope.Folder);
38 bind(PreferenceProviderProvider).toFactory(ctx => (scope: PreferenceScope) => {
39 if (scope === PreferenceScope.Default) {
40 return ctx.container.get(PreferenceSchemaProvider);
41 }
42 return ctx.container.getNamed(PreferenceProvider, scope);
43 });
44 bind(PreferenceServiceImpl).toSelf().inSingletonScope();
45 bind(PreferenceService).toService(PreferenceServiceImpl);
46 bindPreferenceSchemaProvider(bind);
47 bind(PreferenceValidationService).toSelf().inSingletonScope();
48 bind(InjectablePreferenceProxy).toSelf();
49 bind(PreferenceProxyFactory).toFactory(({ container }) => (schema: MaybePromise<PreferenceSchema>, options: PreferenceProxyOptions = {}) => {
50 const child = container.createChild();
51 child.bind(PreferenceProxyOptions).toConstantValue(options ?? {});
52 child.bind(PreferenceProxySchema).toConstantValue(schema);
53 const handler = child.get(InjectablePreferenceProxy);
54 return new Proxy(Object.create(null), handler); // eslint-disable-line no-null/no-null
55 });
56}
57
58export function bindResourceProvider(bind: interfaces.Bind): void {
59 bind(DefaultResourceProvider).toSelf().inSingletonScope();
60 bind(ResourceProvider).toProvider(context => uri => context.container.get(DefaultResourceProvider).get(uri));
61 bindContributionProvider(bind, ResourceResolver);
62}