-
-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathcloud-sketchbook-tree-model.ts
196 lines (178 loc) · 6.2 KB
/
cloud-sketchbook-tree-model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import {
inject,
injectable,
postConstruct,
} from '@theia/core/shared/inversify';
import { CompositeTreeNode, TreeNode } from '@theia/core/lib/browser/tree';
import { posixSegments, splitSketchPath } from '../../create/create-paths';
import { CreateApi } from '../../create/create-api';
import { CloudSketchbookTree } from './cloud-sketchbook-tree';
import { AuthenticationClientService } from '../../auth/authentication-client-service';
import { SketchbookTreeModel } from '../sketchbook/sketchbook-tree-model';
import { WorkspaceNode } from '@theia/navigator/lib/browser/navigator-tree';
import { CreateUri } from '../../create/create-uri';
import { FileChangesEvent, FileStat } from '@theia/filesystem/lib/common/files';
import {
LocalCacheFsProvider,
LocalCacheUri,
} from '../../local-cache/local-cache-fs-provider';
import URI from '@theia/core/lib/common/uri';
import { Create } from '../../create/typings';
import { nls } from '@theia/core/lib/common/nls';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { ApplicationConnectionStatusContribution } from '../../theia/core/connection-status-service';
function sketchBaseDir(sketch: Create.Sketch): FileStat {
// extract the sketch path
const [, path] = splitSketchPath(sketch.path);
const dirs = posixSegments(path);
const mtime = Date.parse(sketch.modified_at);
const ctime = Date.parse(sketch.created_at);
const createPath = CreateUri.toUri(dirs[0]);
const baseDir: FileStat = {
name: dirs[0],
isDirectory: true,
isFile: false,
isSymbolicLink: false,
isReadonly: false,
resource: createPath,
mtime,
ctime,
};
return baseDir;
}
function sketchesToFileStats(sketches: Create.Sketch[]): FileStat[] {
const sketchesBaseDirs: Record<string, FileStat> = {};
for (const sketch of sketches) {
const sketchBaseDirFileStat = sketchBaseDir(sketch);
sketchesBaseDirs[sketchBaseDirFileStat.resource.toString()] =
sketchBaseDirFileStat;
}
return Object.keys(sketchesBaseDirs).map(
(dirUri) => sketchesBaseDirs[dirUri]
);
}
@injectable()
export class CloudSketchbookTreeModel extends SketchbookTreeModel {
@inject(CreateApi)
private readonly createApi: CreateApi;
@inject(AuthenticationClientService)
private readonly authenticationService: AuthenticationClientService;
@inject(LocalCacheFsProvider)
private readonly localCacheFsProvider: LocalCacheFsProvider;
@inject(ApplicationConnectionStatusContribution)
private readonly connectionStatus: ApplicationConnectionStatusContribution;
private _localCacheFsProviderReady: Deferred<void> | undefined;
@postConstruct()
protected override init(): void {
super.init();
this.toDispose.pushAll([
this.authenticationService.onSessionDidChange(() => this.updateRoot()),
this.connectionStatus.onOfflineStatusDidChange((offlineStatus) => {
if (!offlineStatus) {
this.updateRoot();
}
}),
]);
}
override *getNodesByUri(uri: URI): IterableIterator<TreeNode> {
if (uri.scheme === LocalCacheUri.scheme) {
const workspace = this.root;
const { session } = this.authenticationService;
if (session && WorkspaceNode.is(workspace)) {
const currentUri = this.localCacheFsProvider.to(uri);
if (currentUri) {
const rootPath = this.localCacheFsProvider
.toUri(session)
.path.toString();
const currentPath = currentUri.path.toString();
if (rootPath === currentPath) {
return workspace;
}
if (currentPath.startsWith(rootPath)) {
const id = currentPath.substring(rootPath.length);
const node = this.getNode(id);
if (node) {
yield node;
}
}
}
}
}
}
protected override isRootAffected(changes: FileChangesEvent): boolean {
return changes.changes
.map(({ resource }) => resource)
.some(
(uri) => uri.parent.toString().startsWith(LocalCacheUri.root.toString()) // all files under the root might affect the tree
);
}
override async refresh(
parent?: Readonly<CompositeTreeNode>
): Promise<CompositeTreeNode | undefined> {
if (parent) {
return super.refresh(parent);
}
await this.updateRoot();
return super.refresh();
}
override async createRoot(): Promise<TreeNode | undefined> {
const { session } = this.authenticationService;
if (!session) {
this.tree.root = undefined;
return;
}
this.createApi.sketchCache.init();
const [sketches] = await Promise.all([
this.createApi.sketches(),
this.ensureLocalFsProviderReady(),
]);
const rootFileStats = sketchesToFileStats(sketches);
if (this.workspaceService.opened) {
const workspaceNode = WorkspaceNode.createRoot(
nls.localize('arduino/cloud/remote', 'Remote')
);
for await (const stat of rootFileStats) {
workspaceNode.children.push(
await this.tree.createWorkspaceRoot(stat, workspaceNode)
);
}
return workspaceNode;
}
}
sketchbookTree(): CloudSketchbookTree {
return this.tree as CloudSketchbookTree;
}
protected override recursivelyFindSketchRoot(
node: TreeNode
): TreeNode | false {
if (node && CloudSketchbookTree.CloudSketchDirNode.is(node)) {
return node;
}
if (node && node.parent) {
return this.recursivelyFindSketchRoot(node.parent);
}
// can't find a root, return false
return false;
}
override async revealFile(uri: URI): Promise<TreeNode | undefined> {
await this.localCacheFsProvider.ready.promise;
// we use remote uris as keys for the tree
// convert local URIs
const remoteUri = this.localCacheFsProvider.from(uri);
if (remoteUri) {
return super.revealFile(remoteUri);
} else {
return super.revealFile(uri);
}
}
private async ensureLocalFsProviderReady(): Promise<void> {
if (this._localCacheFsProviderReady) {
return this._localCacheFsProviderReady.promise;
}
this._localCacheFsProviderReady = new Deferred();
this.fileService
.access(LocalCacheUri.root)
.then(() => this._localCacheFsProviderReady?.resolve());
return this._localCacheFsProviderReady.promise;
}
}