UNPKG

2.73 kBPlain TextView Raw
1// *****************************************************************************
2// Copyright (C) 2018 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
17/* eslint-disable @typescript-eslint/no-explicit-any */
18
19import { ReactNode } from 'react';
20import { injectable, unmanaged } from 'inversify';
21import { Disposable, DisposableCollection, Emitter, Event, isObject, MaybePromise } from '../../common';
22import { TreeWidget } from '../tree';
23
24export interface TreeElement {
25 /** default: parent id + position among siblings */
26 readonly id?: number | string | undefined
27 /** default: true */
28 readonly visible?: boolean
29 render(host: TreeWidget): ReactNode
30 open?(): MaybePromise<any>
31}
32
33export interface CompositeTreeElement extends TreeElement {
34 /** default: true */
35 readonly hasElements?: boolean
36 getElements(): MaybePromise<IterableIterator<TreeElement>>
37}
38export namespace CompositeTreeElement {
39 export function is(element: unknown): element is CompositeTreeElement {
40 return isObject(element) && 'getElements' in element;
41 }
42 export function hasElements(element: unknown): element is CompositeTreeElement {
43 return is(element) && element.hasElements !== false;
44 }
45}
46
47@injectable()
48export abstract class TreeSource implements Disposable {
49 protected readonly onDidChangeEmitter = new Emitter<void>();
50 readonly onDidChange: Event<void> = this.onDidChangeEmitter.event;
51 protected fireDidChange(): void {
52 this.onDidChangeEmitter.fire(undefined);
53 }
54
55 readonly id: string | undefined;
56 readonly placeholder: string | undefined;
57
58 constructor(@unmanaged() options: TreeSourceOptions = {}) {
59 this.id = options.id;
60 this.placeholder = options.placeholder;
61 }
62
63 protected readonly toDispose = new DisposableCollection(this.onDidChangeEmitter);
64 dispose(): void {
65 this.toDispose.dispose();
66 }
67
68 abstract getElements(): MaybePromise<IterableIterator<TreeElement>>;
69}
70export interface TreeSourceOptions {
71 id?: string
72 placeholder?: string
73}