Skip to content

Commit dd45c76

Browse files
fixed writing and passing FS sync
1 parent 7502df7 commit dd45c76

File tree

3 files changed

+66
-61
lines changed

3 files changed

+66
-61
lines changed

packages/app/src/app/overmind/effects/vscode/sandboxFsSync/index.ts

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ class SandboxFsSync {
145145
}
146146

147147
public writeFile(fs: SandboxFs, module: Module) {
148-
writeFile(fs, json(module));
148+
const copy = json(module);
149149

150-
this.send('write-file', module);
150+
writeFile(fs, copy);
151+
this.send('write-file', copy);
151152

152153
if (module.title === 'package.json') {
153154
this.syncDependencyTypings();
@@ -163,52 +164,30 @@ class SandboxFsSync {
163164
}
164165

165166
public rmdir(fs: SandboxFs, directory: Directory) {
166-
rmdir(fs, directory);
167-
global.postMessage(
168-
{
169-
$broadcast: true,
170-
$type: 'rmdir',
171-
$data: directory,
172-
},
173-
protocolAndHost()
174-
);
167+
const copy = json(directory);
168+
169+
rmdir(fs, copy);
170+
this.send('rmdir', copy);
175171
}
176172

177173
public unlink(fs: SandboxFs, module: Module) {
178-
unlink(fs, module);
179-
global.postMessage(
180-
{
181-
$broadcast: true,
182-
$type: 'unlink',
183-
$data: module,
184-
},
185-
protocolAndHost()
186-
);
174+
const copy = json(module);
175+
176+
unlink(fs, copy);
177+
this.send('unlink', copy);
187178
}
188179

189180
public mkdir(fs: SandboxFs, directory: Directory) {
190-
mkdir(fs, json(directory));
191-
global.postMessage(
192-
{
193-
$broadcast: true,
194-
$type: 'mkdir',
195-
$data: directory,
196-
},
197-
protocolAndHost()
198-
);
181+
const copy = json(directory);
182+
183+
mkdir(fs, copy);
184+
this.send('mkdir', copy);
199185
}
200186

201187
private syncSandbox() {
202188
// eslint-disable-next-line
203189
console.log('## SYNCING SANDBOX AND TYPINGS WITH WORKERS');
204-
global.postMessage(
205-
{
206-
$broadcast: true,
207-
$type: 'sandbox-fs',
208-
$data: json(this.options.getSandboxFs()),
209-
},
210-
protocolAndHost()
211-
);
190+
this.send('sandbox-fs', json(this.options.getSandboxFs()));
212191
}
213192

214193
private async sync() {
@@ -229,7 +208,7 @@ class SandboxFsSync {
229208
{
230209
$broadcast: true,
231210
$type: type,
232-
$data: json(data),
211+
$data: data,
233212
},
234213
protocolAndHost()
235214
);
@@ -292,14 +271,7 @@ class SandboxFsSync {
292271
}
293272

294273
private sendTypes() {
295-
global.postMessage(
296-
{
297-
$broadcast: true,
298-
$type: 'typings-sync',
299-
$data: this.types,
300-
},
301-
protocolAndHost()
302-
);
274+
this.send('typings-sync', this.types);
303275
}
304276

305277
/**

packages/app/src/app/overmind/namespaces/files/actions.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,14 @@ export const directoryDeleted: AsyncAction<{
217217
1
218218
)[0];
219219

220-
state.editor.currentModuleShortid = state.editor.mainModule.shortid;
221-
222-
// we need to recreate everything, as you might have deleted any number
220+
// We need to recreate everything, as you might have deleted any number
223221
// of nested directories or files
224222
state.editor.modulesByPath = effects.vscode.fs.create(sandbox);
225-
effects.vscode.openModule(state.editor.currentModule);
223+
224+
// We open the main module as we do not really know if you had opened
225+
// any nested file of this directory. It would require complex logic
226+
// to figure that out. This concept is soon removed anyways
227+
effects.vscode.openModule(state.editor.mainModule);
226228
actions.editor.internal.updatePreviewCode();
227229
try {
228230
await effects.api.deleteDirectory(sandbox.id, directoryShortid);

packages/app/src/app/overmind/namespaces/live/liveMessageOperators.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,27 @@ export const onModuleDeleted: Operator<
201201
LiveMessage<{
202202
moduleShortid: string;
203203
}>
204-
> = mutate(({ actions }, { _isOwnMessage, data }) => {
204+
> = mutate(({ state, effects, actions }, { _isOwnMessage, data }) => {
205205
if (_isOwnMessage) {
206206
return;
207207
}
208-
// Do not think this really works? Cause this would fork the sandbox
209-
actions.files.moduleDeleted({
210-
moduleShortid: data.moduleShortid,
211-
});
208+
const removedModule = state.editor.currentSandbox.modules.find(
209+
directory => directory.shortid === data.moduleShortid
210+
);
211+
const moduleIndex = state.editor.currentSandbox.modules.indexOf(
212+
removedModule
213+
);
214+
const wasCurrentModule =
215+
state.editor.currentModuleShortid === data.moduleShortid;
216+
217+
state.editor.currentSandbox.modules.splice(moduleIndex, 1);
218+
effects.vscode.fs.unlink(state.editor.modulesByPath, removedModule);
219+
220+
if (wasCurrentModule) {
221+
actions.editor.internal.setCurrentModule(state.editor.mainModule);
222+
}
223+
224+
actions.editor.internal.updatePreviewCode();
212225
});
213226

214227
export const onDirectoryCreated: Operator<
@@ -248,15 +261,33 @@ export const onDirectoryDeleted: Operator<
248261
LiveMessage<{
249262
directoryShortid: string;
250263
}>
251-
> = mutate(({ state, actions }, { _isOwnMessage, data }) => {
264+
> = mutate(({ state, effects, actions }, { _isOwnMessage, data }) => {
252265
if (_isOwnMessage) {
253266
return;
254267
}
255-
state.editor.currentModuleShortid = state.editor.mainModule.shortid;
256-
// Again, this does not work very well?
257-
actions.files.directoryDeleted({
258-
directoryShortid: data.directoryShortid,
259-
});
268+
const sandbox = state.editor.currentSandbox;
269+
const directory = sandbox.directories.find(
270+
directoryItem => directoryItem.shortid === data.directoryShortid
271+
);
272+
273+
if (!directory) {
274+
return;
275+
}
276+
277+
const removedDirectory = sandbox.directories.splice(
278+
sandbox.directories.indexOf(directory),
279+
1
280+
)[0];
281+
282+
// We need to recreate everything, as you might have deleted any number
283+
// of nested directories or files
284+
state.editor.modulesByPath = effects.vscode.fs.create(sandbox);
285+
286+
// We open the main module as we do not really know if you had opened
287+
// any nested file of this directory. It would require complex logic
288+
// to figure that out. This concept is soon removed anyways
289+
effects.vscode.openModule(state.editor.mainModule);
290+
actions.editor.internal.updatePreviewCode();
260291
});
261292

262293
export const onUserSelection: Operator<

0 commit comments

Comments
 (0)