Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Further fixing: Avoid circular dependency with contribution provider
- Loading branch information
1 parent
f361c07
commit bf32b58
Showing
5 changed files
with
125 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2017 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
*******************************************************************************/ | ||
// from https://github.com/eclipse-theia/theia/blob/9ff0cedff1d591b0eb4be97a05f6d992789d0a24/packages/core/src/common/contribution-provider.ts | ||
|
||
import { interfaces } from 'inversify'; | ||
|
||
export const ContributionProvider = Symbol('ContributionProvider'); | ||
|
||
export interface ContributionProvider<T extends object> { | ||
/** | ||
* @param recursive `true` if the contributions should be collected from the parent containers as well. Otherwise, `false`. | ||
* It is `false` by default. | ||
*/ | ||
getContributions(recursive?: boolean): T[]; | ||
} | ||
|
||
class ContainerBasedContributionProvider<T extends object> implements ContributionProvider<T> { | ||
protected services: T[] | undefined; | ||
|
||
constructor( | ||
protected readonly serviceIdentifier: interfaces.ServiceIdentifier<T>, | ||
protected readonly container: interfaces.Container | ||
) {} | ||
|
||
getContributions(recursive?: boolean): T[] { | ||
if (this.services === undefined) { | ||
const currentServices: T[] = []; | ||
let currentContainer: interfaces.Container | null = this.container; | ||
// eslint-disable-next-line no-null/no-null | ||
while (currentContainer !== null) { | ||
if (currentContainer.isBound(this.serviceIdentifier)) { | ||
try { | ||
currentServices.push(...currentContainer.getAll(this.serviceIdentifier)); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
// eslint-disable-next-line no-null/no-null | ||
currentContainer = recursive === true ? currentContainer.parent : null; | ||
} | ||
this.services = currentServices; | ||
} | ||
return this.services; | ||
} | ||
} | ||
|
||
export type Bindable = interfaces.Bind | interfaces.Container; | ||
export namespace Bindable { | ||
export function isContainer(arg: Bindable): arg is interfaces.Container { | ||
return ( | ||
typeof arg !== 'function' && | ||
// https://github.com/eclipse-theia/theia/issues/3204#issue-371029654 | ||
// In InversifyJS `4.14.0` containers no longer have a property `guid`. | ||
('guid' in arg || 'parent' in arg) | ||
); | ||
} | ||
} | ||
|
||
export function bindContributionProvider(bindable: Bindable, id: symbol): void { | ||
const bindingToSyntax = Bindable.isContainer(bindable) ? bindable.bind(ContributionProvider) : bindable(ContributionProvider); | ||
bindingToSyntax | ||
.toDynamicValue(ctx => new ContainerBasedContributionProvider(id, ctx.container)) | ||
.inSingletonScope() | ||
.whenTargetNamed(id); | ||
} |