Skip to content

Commit

Permalink
Use callback to get fallback snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Lesches committed Dec 13, 2024
1 parent 9413e86 commit df2f722
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ class _DartPadMainPageState extends State<DartPadMainPage>
sampleId: widget.builtinSampleId,
flutterSampleId: widget.flutterSampleId,
channel: widget.initialChannel,
fallbackSnippet:
LocalStorage.instance.getUserCode() ?? Samples.defaultSnippet(),
getFallback: () => LocalStorage.instance.getUserCode()
?? Samples.defaultSnippet(),
);
// Start listening for inject code messages.
handleEmbedMessage(appServices, runOnInject: widget.runOnLoad);
Expand Down
10 changes: 5 additions & 5 deletions pkgs/dartpad_ui/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class AppServices {
String? sampleId,
String? flutterSampleId,
String? channel,
required String fallbackSnippet,
required String Function() getFallback,
}) async {
// Delay a bit for codemirror to initialize.
await Future<void>.delayed(const Duration(milliseconds: 1));
Expand Down Expand Up @@ -239,7 +239,7 @@ class AppServices {

appModel.appendLineToConsole('Error loading sample: $e');

appModel.sourceCodeController.text = fallbackSnippet;
appModel.sourceCodeController.text = getFallback();
appModel.appReady.value = true;
} finally {
loader.dispose();
Expand All @@ -263,7 +263,7 @@ class AppServices {
final source = gist.mainDartSource;
if (source == null) {
appModel.editorStatus.showToast('main.dart not found');
appModel.sourceCodeController.text = fallbackSnippet;
appModel.sourceCodeController.text = getFallback();
} else {
appModel.sourceCodeController.text = source;

Expand All @@ -283,7 +283,7 @@ class AppServices {

appModel.appendLineToConsole('Error loading gist: $e');

appModel.sourceCodeController.text = fallbackSnippet;
appModel.sourceCodeController.text = getFallback();
appModel.appReady.value = true;
} finally {
gistLoader.dispose();
Expand All @@ -293,7 +293,7 @@ class AppServices {
}

// Neither gistId nor flutterSampleId were passed in.
appModel.sourceCodeController.text = fallbackSnippet;
appModel.sourceCodeController.text = getFallback();
appModel.appReady.value = true;
}

Expand Down
12 changes: 7 additions & 5 deletions pkgs/dartpad_ui/test/autosave_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import 'package:dartpad_ui/utils.dart';
import 'package:test/test.dart';

String getFallback() =>
LocalStorage.instance.getUserCode() ?? Samples.defaultSnippet();
LocalStorage.instance.getUserCode() ?? Samples.defaultSnippet();

Never throwingFallback() => throw StateError('DartPad tried to load the fallback');

void main() {
const channel = Channel.stable;
Expand All @@ -32,7 +34,7 @@ void main() {
expect(LocalStorage.instance.getUserCode(), isNull);

await services.performInitialLoad(
fallbackSnippet: getFallback(),
getFallback: getFallback,
);
expect(model.sourceCodeController.text, equals(Samples.defaultSnippet()));
});
Expand All @@ -45,7 +47,7 @@ void main() {
expect(LocalStorage.instance.getUserCode(), equals(sample));

await services.performInitialLoad(
fallbackSnippet: getFallback(),
getFallback: getFallback,
);
expect(model.sourceCodeController.text, equals(sample));
});
Expand All @@ -63,7 +65,7 @@ void main() {
// From gists_tests.dart
const gistId = 'd3bd83918d21b6d5f778bdc69c3d36d6';
await services.performInitialLoad(
fallbackSnippet: getFallback(),
getFallback: throwingFallback,
gistId: gistId,
);
expect(model.sourceCodeController.text, isNot(equals(sample)));
Expand All @@ -77,7 +79,7 @@ void main() {
// From samples_test.dart
const sampleId = 'dart';
await services.performInitialLoad(
fallbackSnippet: getFallback(),
getFallback: throwingFallback,
sampleId: sampleId,
);
expect(model.sourceCodeController.text, isNot(equals(sample)));
Expand Down

0 comments on commit df2f722

Please sign in to comment.