mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 17:45:22 +00:00
fileStorage/sagas: remove editor init from fileStorage
This is still a bit of a hack but is one step towards being able to remove the 'editor' context.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2022 The Pybricks Authors
|
||||
|
||||
import { monaco } from 'react-monaco-editor';
|
||||
import { eventChannel } from 'redux-saga';
|
||||
import { fork, put, race, select, take, takeEvery } from 'typed-redux-saga/macro';
|
||||
import {
|
||||
fileStorageDidFailToReadFile,
|
||||
fileStorageDidInitialize,
|
||||
fileStorageDidReadFile,
|
||||
fileStorageReadFile,
|
||||
} from '../fileStorage/actions';
|
||||
import { RootState } from '../reducers';
|
||||
|
||||
function* handleDidCreateEditor(editor: monaco.editor.ICodeEditor): Generator {
|
||||
// first, we need to be sure that file storage is ready
|
||||
|
||||
const isFileStorageInitialized = yield* select(
|
||||
(s: RootState) => s.fileStorage.isInitialized,
|
||||
);
|
||||
|
||||
if (!isFileStorageInitialized) {
|
||||
yield* take(fileStorageDidInitialize);
|
||||
}
|
||||
|
||||
// then we can load the most recently used file
|
||||
// REVISIT: should this be here or elsewhere?
|
||||
|
||||
yield* put(fileStorageReadFile('main.py'));
|
||||
|
||||
const { succeeded } = yield* race({
|
||||
succeeded: take(fileStorageDidReadFile.when((a) => a.fileName === 'main.py')),
|
||||
failed: take(
|
||||
fileStorageDidFailToReadFile.when((a) => a.fileName === 'main.py'),
|
||||
),
|
||||
});
|
||||
|
||||
// TODO: what to do in case of failure?
|
||||
|
||||
if (succeeded) {
|
||||
editor.setValue(succeeded.fileContents);
|
||||
}
|
||||
|
||||
// TODO: subscribe to actions that act on the editor
|
||||
}
|
||||
|
||||
function* monitorEditors(): Generator {
|
||||
const ch = eventChannel<monaco.editor.ICodeEditor>((emit) => {
|
||||
const subscription = monaco.editor.onDidCreateEditor(emit);
|
||||
return () => subscription.dispose();
|
||||
});
|
||||
|
||||
yield* takeEvery(ch, handleDidCreateEditor);
|
||||
}
|
||||
|
||||
export default function* (): Generator {
|
||||
yield* fork(monitorEditors);
|
||||
}
|
||||
@@ -6,9 +6,8 @@ import JSZip from 'jszip';
|
||||
import localForage from 'localforage';
|
||||
import { extendPrototype } from 'localforage-observable';
|
||||
import { eventChannel } from 'redux-saga';
|
||||
import { call, fork, getContext, put, takeEvery } from 'typed-redux-saga/macro';
|
||||
import { call, fork, put, takeEvery } from 'typed-redux-saga/macro';
|
||||
import Observable from 'zen-observable';
|
||||
import { EditorType } from '../editor/Editor';
|
||||
import { pythonFileExtension, pythonFileMimeType } from '../pybricksMicropython/lib';
|
||||
import { ensureError, timestamp } from '../utils';
|
||||
import {
|
||||
@@ -269,20 +268,6 @@ function* initialize(): Generator {
|
||||
|
||||
const fileNames = yield* call(() => files.keys());
|
||||
|
||||
// TODO: we should not be loading main.py here
|
||||
// HACK: This assumes that editor is loaded before storage!
|
||||
const editor = yield* getContext<EditorType>('editor');
|
||||
|
||||
if (editor) {
|
||||
const main = yield* call(() => files.getItem<string>('main.py'));
|
||||
|
||||
if (main) {
|
||||
editor.setValue(main);
|
||||
}
|
||||
} else if (process.env.NODE_ENV !== 'test') {
|
||||
console.error('editor was not loaded, so main.py was not loaded');
|
||||
}
|
||||
|
||||
yield* put(fileStorageDidInitialize(fileNames));
|
||||
} catch (err) {
|
||||
yield* put(fileStorageDidFailToInitialize(ensureError(err)));
|
||||
|
||||
@@ -7,6 +7,7 @@ import app from './app/sagas';
|
||||
import blePybricksService from './ble-pybricks-service/sagas';
|
||||
import ble from './ble/sagas';
|
||||
import { EditorType } from './editor/Editor';
|
||||
import editor from './editor/sagas';
|
||||
import errorLog from './error-log/sagas';
|
||||
import explorer from './explorer/sagas';
|
||||
import fileStorage from './fileStorage/sagas';
|
||||
@@ -24,6 +25,7 @@ export default function* (): Generator {
|
||||
app(),
|
||||
blePybricksService(),
|
||||
ble(),
|
||||
editor(),
|
||||
fileStorage(),
|
||||
lwp3BootloaderBle(),
|
||||
lwp3BootloaderProtocol(),
|
||||
|
||||
Reference in New Issue
Block a user