Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented autosave on a 5-second timer #3037

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkgs/dartpad_ui/lib/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,17 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
@override
void initState() {
super.initState();

_autosaveTimer = Timer.periodic(const Duration(seconds: 5), _autosave);
widget.appModel.appReady.addListener(_updateEditableStatus);
}

Timer? _autosaveTimer;
void _autosave([Timer? timer]) {
final content = widget.appModel.sourceCodeController.text;
if (content.isEmpty) return;
web.window.localStorage.setItem('user_input', content);
}

void _platformViewCreated(int id, {required bool darkMode}) {
codeMirror = codeMirrorInstance;

Expand Down Expand Up @@ -305,6 +312,8 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
@override
void dispose() {
listener?.cancel();
_autosaveTimer?.cancel();
_autosave();

widget.appServices.registerEditorService(null);

Expand Down
2 changes: 1 addition & 1 deletion pkgs/dartpad_ui/lib/execution/frame.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function dartPrint(message) {
'sender': 'frame',
'type': 'stdout',
'message': message.toString()
}, '*');
}, '*');
}
''');

Expand Down
4 changes: 3 additions & 1 deletion pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:provider/provider.dart';
import 'package:split_view/split_view.dart';
import 'package:url_launcher/url_launcher.dart' as url_launcher;
import 'package:vtable/vtable.dart';
import 'package:web/web.dart' as web;

import 'console.dart';
import 'docs.dart';
Expand Down Expand Up @@ -277,7 +278,8 @@ class _DartPadMainPageState extends State<DartPadMainPage>
sampleId: widget.builtinSampleId,
flutterSampleId: widget.flutterSampleId,
channel: widget.initialChannel,
fallbackSnippet: Samples.getDefault(type: 'dart'))
fallbackSnippet: web.window.localStorage.getItem('user_input') ??
Samples.getDefault(type: 'dart'))
Comment on lines +281 to +282
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's worth it to convert this fallback to a callback, so we don't unnecessarily retrieve the old snippet?

Copy link
Author

@Levi-Lesches Levi-Lesches Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ?? is short circuiting, so it should be fine? EDIT: Just checked, it is

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, if you meant not retrieving the old snippet or user input in case a gist id or otherwise is provided.

I tried making it a callback, and got some very strange errors on the web side. Each time I restarted, it was that JS can't read some property, but it was a different property each time. Once it didn't even let the flutter tool connect because the bootstrapper got stuck. Not sure what's up with that but I'll leave the PR as-is for now

.then((value) {
// Start listening for inject code messages.
handleEmbedMessage(appServices, runOnInject: widget.runOnLoad);
Expand Down