From a34e6b0792c896ba8a90cd5d4eae4d4ab7c7dfd0 Mon Sep 17 00:00:00 2001 From: Kenzie Davisson <43759233+kenzieschmoll@users.noreply.github.com> Date: Mon, 12 Sep 2022 15:22:28 -0700 Subject: [PATCH] Create enum for service extensions in services library (#111412) --- packages/flutter/lib/services.dart | 1 + .../flutter/lib/src/services/binding.dart | 7 ++--- .../lib/src/services/service_extensions.dart | 26 +++++++++++++++++++ .../foundation/service_extensions_test.dart | 2 +- 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 packages/flutter/lib/src/services/service_extensions.dart diff --git a/packages/flutter/lib/services.dart b/packages/flutter/lib/services.dart index 2fa143e2ad4..9c77b1c0b3f 100644 --- a/packages/flutter/lib/services.dart +++ b/packages/flutter/lib/services.dart @@ -37,6 +37,7 @@ export 'src/services/raw_keyboard_macos.dart'; export 'src/services/raw_keyboard_web.dart'; export 'src/services/raw_keyboard_windows.dart'; export 'src/services/restoration.dart'; +export 'src/services/service_extensions.dart'; export 'src/services/spell_check.dart'; export 'src/services/system_channels.dart'; export 'src/services/system_chrome.dart'; diff --git a/packages/flutter/lib/src/services/binding.dart b/packages/flutter/lib/src/services/binding.dart index 4a7f9e35b6c..918f732a4d0 100644 --- a/packages/flutter/lib/src/services/binding.dart +++ b/packages/flutter/lib/src/services/binding.dart @@ -15,6 +15,7 @@ import 'binary_messenger.dart'; import 'hardware_keyboard.dart'; import 'message_codec.dart'; import 'restoration.dart'; +import 'service_extensions.dart'; import 'system_channels.dart'; import 'text_input.dart'; @@ -213,11 +214,7 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { assert(() { registerStringServiceExtension( - // ext.flutter.evict value=foo.png will cause foo.png to be evicted from - // the rootBundle cache and cause the entire image cache to be cleared. - // This is used by hot reload mode to clear out the cache of resources - // that have changed. - name: 'evict', + name: ServicesServiceExtensions.evict.name, getter: () async => '', setter: (String value) async { evict(value); diff --git a/packages/flutter/lib/src/services/service_extensions.dart b/packages/flutter/lib/src/services/service_extensions.dart new file mode 100644 index 00000000000..9211b75ea2d --- /dev/null +++ b/packages/flutter/lib/src/services/service_extensions.dart @@ -0,0 +1,26 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/// Service extension constants for the services library. +/// +/// These constants will be used when registering service extensions in the +/// framework, and they will also be used by tools and services that call these +/// service extensions. +/// +/// The String value for each of these extension names should be accessed by +/// calling the `.name` property on the enum value. +enum ServicesServiceExtensions { + /// Name of service extension that, when called, will evict an image from the + /// rootBundle cache and cause the image cache to be cleared. + /// + /// This is used by hot reload mode to clear out the cache of resources that + /// have changed. This service extension should be called with a String value + /// of the image path (e.g. foo.png). + /// + /// See also: + /// + /// * [ServicesBinding.initServiceExtensions], where the service extension is + /// registered. + evict, +} diff --git a/packages/flutter/test/foundation/service_extensions_test.dart b/packages/flutter/test/foundation/service_extensions_test.dart index cf6bdc91cbb..7ac88ca8031 100644 --- a/packages/flutter/test/foundation/service_extensions_test.dart +++ b/packages/flutter/test/foundation/service_extensions_test.dart @@ -563,7 +563,7 @@ void main() { }); expect(data, isTrue); expect(completed, isFalse); - result = await binding.testExtension('evict', {'value': 'test'}); + result = await binding.testExtension(ServicesServiceExtensions.evict.name, {'value': 'test'}); expect(result, {'value': ''}); expect(completed, isFalse); data = await rootBundle.loadStructuredData('test', (String value) async {