From 889281247962bdc1a442ebf2755595daded36bc2 Mon Sep 17 00:00:00 2001 From: Tobias Ollmann Date: Wed, 18 Sep 2024 12:17:51 +0200 Subject: [PATCH 01/14] fix: add support for non-root base-href if a web-app is deployed using `--base-href` during build, the URLs starting with `./assets` won't work anymore. Use `assetManager` to properly resolve them --- wakelock_plus/lib/src/web_impl/import_js_library.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wakelock_plus/lib/src/web_impl/import_js_library.dart b/wakelock_plus/lib/src/web_impl/import_js_library.dart index b635c65..29ba421 100644 --- a/wakelock_plus/lib/src/web_impl/import_js_library.dart +++ b/wakelock_plus/lib/src/web_impl/import_js_library.dart @@ -1,4 +1,5 @@ import 'dart:js_interop'; +import 'dart:ui_web'; import 'package:web/web.dart'; @@ -20,10 +21,10 @@ Future importJsLibrary( String _libraryUrl(String url, String pluginName) { if (url.startsWith('./')) { url = url.replaceFirst('./', ''); - return './assets/packages/$pluginName/$url'; + return assetManager.getAssetUrl("packages/$pluginName/$url"); } if (url.startsWith('assets/')) { - return './assets/packages/$pluginName/$url'; + return assetManager.getAssetUrl("packages/$pluginName/$url"); } else { return url; } From 31526ae27a2cde4f688ec63b53abf3621b272995 Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Mon, 2 Dec 2024 21:18:28 -0500 Subject: [PATCH 02/14] Moved no_sleep.js to lib/assets. --- wakelock_plus/{ => lib}/assets/no_sleep.js | 0 wakelock_plus/pubspec.yaml | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename wakelock_plus/{ => lib}/assets/no_sleep.js (100%) diff --git a/wakelock_plus/assets/no_sleep.js b/wakelock_plus/lib/assets/no_sleep.js similarity index 100% rename from wakelock_plus/assets/no_sleep.js rename to wakelock_plus/lib/assets/no_sleep.js diff --git a/wakelock_plus/pubspec.yaml b/wakelock_plus/pubspec.yaml index b2f991b..29337d6 100644 --- a/wakelock_plus/pubspec.yaml +++ b/wakelock_plus/pubspec.yaml @@ -99,4 +99,4 @@ flutter: # https://flutter.dev/custom-fonts/#from-packages assets: - - assets/no_sleep.js + - packages/wakelock_plus/assets/no_sleep.js From 606ade6c308fe89232140225dc979dd563bdea4d Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Mon, 2 Dec 2024 21:21:07 -0500 Subject: [PATCH 03/14] Added web alias to all package:web calls in import_js_library.dart. --- .../lib/src/web_impl/import_js_library.dart | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/wakelock_plus/lib/src/web_impl/import_js_library.dart b/wakelock_plus/lib/src/web_impl/import_js_library.dart index 29ba421..28b7741 100644 --- a/wakelock_plus/lib/src/web_impl/import_js_library.dart +++ b/wakelock_plus/lib/src/web_impl/import_js_library.dart @@ -1,7 +1,7 @@ import 'dart:js_interop'; import 'dart:ui_web'; -import 'package:web/web.dart'; +import 'package:web/web.dart' as web; /// This is an implementation of the `import_js_library` plugin that is used /// until that plugin is migrated to null safety. @@ -30,8 +30,8 @@ String _libraryUrl(String url, String pluginName) { } } -HTMLScriptElement _createScriptTag(String library) { - final script = document.createElement('script') as HTMLScriptElement +web.HTMLScriptElement _createScriptTag(String library) { + final script = web.document.createElement('script') as web.HTMLScriptElement ..type = 'text/javascript' ..charset = 'utf-8' ..async = true @@ -43,13 +43,25 @@ HTMLScriptElement _createScriptTag(String library) { /// Future that resolves when all load. Future _importJSLibraries(List libraries) { final loading = >[]; - final head = document.head; + final head = web.document.head; for (final library in libraries) { if (!_isImported(library)) { final scriptTag = _createScriptTag(library); head!.appendChild(scriptTag); loading.add(scriptTag.onLoad.first); + scriptTag.onError.listen((event) { + final scriptElement = event.srcElement is web.HTMLScriptElement + ? event.srcElement as web.HTMLScriptElement + : null; + if (scriptElement != null) { + loading.add( + Future.error( + Exception('Error loading: ${scriptElement.src}'), + ), + ); + } + }); } } @@ -57,18 +69,18 @@ Future _importJSLibraries(List libraries) { } bool _isImported(String url) { - final head = document.head!; + final head = web.document.head!; return _isLoaded(head, url); } -bool _isLoaded(HTMLHeadElement head, String url) { +bool _isLoaded(web.HTMLHeadElement head, String url) { if (url.startsWith('./')) { url = url.replaceFirst('./', ''); } for (int i = 0; i < head.children.length; i++) { final element = head.children.item(i)!; if (element.instanceOfString('HTMLScriptElement')) { - if ((element as HTMLScriptElement).src.endsWith(url)) { + if ((element as web.HTMLScriptElement).src.endsWith(url)) { return true; } } From b4114a93ba6389805cb7ee62f1bcce73a9e66ee6 Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Mon, 2 Dec 2024 21:29:42 -0500 Subject: [PATCH 04/14] Added more simplified library URL method in import_js_library.dart. --- .../lib/src/web_impl/import_js_library.dart | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/wakelock_plus/lib/src/web_impl/import_js_library.dart b/wakelock_plus/lib/src/web_impl/import_js_library.dart index 28b7741..886b21e 100644 --- a/wakelock_plus/lib/src/web_impl/import_js_library.dart +++ b/wakelock_plus/lib/src/web_impl/import_js_library.dart @@ -1,5 +1,5 @@ import 'dart:js_interop'; -import 'dart:ui_web'; +import 'dart:ui_web' as ui_web; import 'package:web/web.dart' as web; @@ -19,15 +19,19 @@ Future importJsLibrary( } String _libraryUrl(String url, String pluginName) { + // Added suggested changes as per + // https://github.com/fluttercommunity/wakelock_plus/issues/19#issuecomment-2301963609 if (url.startsWith('./')) { url = url.replaceFirst('./', ''); - return assetManager.getAssetUrl("packages/$pluginName/$url"); } + if (url.startsWith('assets/')) { - return assetManager.getAssetUrl("packages/$pluginName/$url"); - } else { - return url; + return ui_web.assetManager.getAssetUrl( + 'packages/$pluginName/$url', + ); } + + return url; } web.HTMLScriptElement _createScriptTag(String library) { From e94805481660870186f7dd310455505316a33dd4 Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Mon, 2 Dec 2024 21:31:53 -0500 Subject: [PATCH 05/14] WakelockPlusWebPlugin now lazily loads no_sleep.js on demand as opposed to when its added to the registrar. --- .../lib/src/wakelock_plus_web_plugin.dart | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/wakelock_plus/lib/src/wakelock_plus_web_plugin.dart b/wakelock_plus/lib/src/wakelock_plus_web_plugin.dart index 73cf3f3..3664edc 100644 --- a/wakelock_plus/lib/src/wakelock_plus_web_plugin.dart +++ b/wakelock_plus/lib/src/wakelock_plus_web_plugin.dart @@ -2,10 +2,10 @@ import 'dart:async'; import 'dart:js_interop'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; -import 'package:wakelock_plus_platform_interface/wakelock_plus_platform_interface.dart'; import 'package:wakelock_plus/src/web_impl/import_js_library.dart'; import 'package:wakelock_plus/src/web_impl/js_wakelock.dart' as wakelock_plus_web; +import 'package:wakelock_plus_platform_interface/wakelock_plus_platform_interface.dart'; /// The web implementation of the [WakelockPlatformInterface]. /// @@ -14,21 +14,32 @@ class WakelockPlusWebPlugin extends WakelockPlusPlatformInterface { /// Registers [WakelockPlusWebPlugin] as the default instance of the /// [WakelockPlatformInterface]. static void registerWith(Registrar registrar) { - // Import a version of `NoSleep.js` that was adjusted for the wakelock - // plugin. - _jsLoaded = importJsLibrary( - url: 'assets/no_sleep.js', flutterPluginName: 'wakelock_plus'); - WakelockPlusPlatformInterface.instance = WakelockPlusWebPlugin(); } - // The future that resolves when the JS library is loaded. - static late Future _jsLoaded; + // The future that signals when the JS is loaded. + // This needs to be `await`ed before accessing any methods of the + // JS-interop layer. + late Future _jsLoaded; + bool _jsLibraryLoaded = false; + + // + // Lazily imports the JS library once, then awaits to ensure that + // it's loaded into the DOM. + // + Future _ensureJsLoaded() async { + if (!_jsLibraryLoaded) { + _jsLoaded = importJsLibrary( + url: 'assets/no_sleep.js', flutterPluginName: 'wakelock_plus'); + _jsLibraryLoaded = true; + } + await _jsLoaded; + } @override Future toggle({required bool enable}) async { // Make sure the JS library is loaded before calling it. - await _jsLoaded; + await _ensureJsLoaded(); wakelock_plus_web.toggle(enable); } @@ -36,8 +47,7 @@ class WakelockPlusWebPlugin extends WakelockPlusPlatformInterface { @override Future get enabled async { // Make sure the JS library is loaded before calling it. - await _jsLoaded; - + await _ensureJsLoaded(); final completer = Completer(); wakelock_plus_web.enabled().toDart.then( From 9e1fd5b9ffcc1744aeb8da477259522155755519 Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Mon, 2 Dec 2024 21:35:22 -0500 Subject: [PATCH 06/14] Got the WakelockPlusWebPlugin tests passing. --- .../test/wakelock_plus_web_plugin_test.dart | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/wakelock_plus/test/wakelock_plus_web_plugin_test.dart b/wakelock_plus/test/wakelock_plus_web_plugin_test.dart index f5d9ebe..241b7a4 100644 --- a/wakelock_plus/test/wakelock_plus_web_plugin_test.dart +++ b/wakelock_plus/test/wakelock_plus_web_plugin_test.dart @@ -6,10 +6,14 @@ import 'package:wakelock_plus/src/wakelock_plus_web_plugin.dart'; import 'package:wakelock_plus/wakelock_plus.dart'; import 'package:wakelock_plus_platform_interface/wakelock_plus_platform_interface.dart'; +/// +/// Run these tests with: +/// flutter run -d chrome test/wakelock_plus_web_plugin_test.dart +/// void main() { group('$WakelockPlusWebPlugin', () { setUpAll(() async { - // todo: the web tests do not work as the JS library import does not work. + // todo: the web tests do not work as the JS library import does not work when using flutter run test --platform chrome. WakelockPlusPlatformInterface.instance = WakelockPlusWebPlugin(); }); @@ -24,16 +28,23 @@ void main() { test('enable', () async { await WakelockPlus.enable(); + // Wait a bit for web to enable the wakelock + await Future.delayed(const Duration(milliseconds: 50)); expect(WakelockPlus.enabled, completion(isTrue)); }); test('disable', () async { + await WakelockPlus.enable(); + // Wait a bit for web to enable the wakelock + await Future.delayed(const Duration(milliseconds: 50)); await WakelockPlus.disable(); expect(WakelockPlus.enabled, completion(isFalse)); }); test('toggle', () async { await WakelockPlus.toggle(enable: true); + // Wait a bit for web to enable the wakelock + await Future.delayed(const Duration(milliseconds: 50)); expect(WakelockPlus.enabled, completion(isTrue)); await WakelockPlus.toggle(enable: false); From 39f6598f2d5f709a9534f6b9d243dd689bd1e0d3 Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Mon, 2 Dec 2024 21:35:42 -0500 Subject: [PATCH 07/14] Fixed example app web-related depreactions. --- wakelock_plus/example/.gitignore | 3 +-- wakelock_plus/example/web/index.html | 25 ++----------------------- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/wakelock_plus/example/.gitignore b/wakelock_plus/example/.gitignore index d0a10c6..fe41d4a 100644 --- a/wakelock_plus/example/.gitignore +++ b/wakelock_plus/example/.gitignore @@ -32,11 +32,10 @@ /build/ # Web related -lib/generated_plugin_registrant.dart # Symbolication related app.*.symbols # Obfuscation related app.*.map.json -!/ios/Podfile \ No newline at end of file +!/ios/Podfile diff --git a/wakelock_plus/example/web/index.html b/wakelock_plus/example/web/index.html index be820e8..887760a 100644 --- a/wakelock_plus/example/web/index.html +++ b/wakelock_plus/example/web/index.html @@ -29,31 +29,10 @@ - example + flutter_scratchpad - - - - - + From f2542bd8a91db5bb38222fc983e43de2ff68a0eb Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Mon, 2 Dec 2024 21:39:49 -0500 Subject: [PATCH 08/14] Added straggler from previous commit. --- wakelock_plus/example/web/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wakelock_plus/example/web/index.html b/wakelock_plus/example/web/index.html index 887760a..14967bb 100644 --- a/wakelock_plus/example/web/index.html +++ b/wakelock_plus/example/web/index.html @@ -29,7 +29,7 @@ - flutter_scratchpad + example From b8d0b58d9c1cc808cc760cca7aaf172b69d57787 Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Tue, 3 Dec 2024 10:33:40 -0500 Subject: [PATCH 09/14] Added correct script tag error handling by checking if the target is a script element. --- wakelock_plus/lib/src/web_impl/import_js_library.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wakelock_plus/lib/src/web_impl/import_js_library.dart b/wakelock_plus/lib/src/web_impl/import_js_library.dart index 886b21e..a388c43 100644 --- a/wakelock_plus/lib/src/web_impl/import_js_library.dart +++ b/wakelock_plus/lib/src/web_impl/import_js_library.dart @@ -55,8 +55,8 @@ Future _importJSLibraries(List libraries) { head!.appendChild(scriptTag); loading.add(scriptTag.onLoad.first); scriptTag.onError.listen((event) { - final scriptElement = event.srcElement is web.HTMLScriptElement - ? event.srcElement as web.HTMLScriptElement + final scriptElement = event.target.isA() + ? event.target as web.HTMLScriptElement : null; if (scriptElement != null) { loading.add( From bd3e7a75eba871fe0dc653aacb99644cd06c3e13 Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Tue, 3 Dec 2024 10:42:27 -0500 Subject: [PATCH 10/14] Upgraded flutter_lints to latest version. --- wakelock_plus/lib/src/web_impl/js_wakelock.dart | 2 +- wakelock_plus/pubspec.yaml | 2 +- wakelock_plus/test/wakelock_plus_web_plugin_test.dart | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/wakelock_plus/lib/src/web_impl/js_wakelock.dart b/wakelock_plus/lib/src/web_impl/js_wakelock.dart index ccbca5b..38b7822 100644 --- a/wakelock_plus/lib/src/web_impl/js_wakelock.dart +++ b/wakelock_plus/lib/src/web_impl/js_wakelock.dart @@ -1,5 +1,5 @@ @JS('Wakelock') -library wakelock.js; +library; import 'dart:js_interop'; diff --git a/wakelock_plus/pubspec.yaml b/wakelock_plus/pubspec.yaml index 29337d6..5beb3eb 100644 --- a/wakelock_plus/pubspec.yaml +++ b/wakelock_plus/pubspec.yaml @@ -31,7 +31,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^3.0.2 + flutter_lints: ^5.0.0 pigeon: ^21.2.0 # dart run pigeon --input "pigeons/messages.dart" # For information on the generic Dart part of this file, see the diff --git a/wakelock_plus/test/wakelock_plus_web_plugin_test.dart b/wakelock_plus/test/wakelock_plus_web_plugin_test.dart index 241b7a4..2e5c8be 100644 --- a/wakelock_plus/test/wakelock_plus_web_plugin_test.dart +++ b/wakelock_plus/test/wakelock_plus_web_plugin_test.dart @@ -1,5 +1,5 @@ @TestOn('browser') -library wakelock_plus_library_plugin_test; +library; import 'package:flutter_test/flutter_test.dart'; import 'package:wakelock_plus/src/wakelock_plus_web_plugin.dart'; From 4900aaf562ddf809d9881cf1eb801a74c0fa48c6 Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Tue, 3 Dec 2024 12:41:05 -0500 Subject: [PATCH 11/14] Revamped the no_sleep.js script to use a Promise-based Completer so that it awaits asynchronous calls from the various wakelock related callbacks. As a result, the Dart layer no longer needs to manually await before calling WakelockPlus.enabled. --- wakelock_plus/lib/assets/no_sleep.js | 129 ++++++++++++------ .../lib/src/wakelock_plus_web_plugin.dart | 14 +- .../lib/src/web_impl/js_wakelock.dart | 2 +- .../test/wakelock_plus_web_plugin_test.dart | 25 +++- 4 files changed, 123 insertions(+), 47 deletions(-) diff --git a/wakelock_plus/lib/assets/no_sleep.js b/wakelock_plus/lib/assets/no_sleep.js index ccfab74..22a2ac4 100644 --- a/wakelock_plus/lib/assets/no_sleep.js +++ b/wakelock_plus/lib/assets/no_sleep.js @@ -1,3 +1,30 @@ +class PromiseCompleter { + _promise; + _resolve; + _reject; + constructor() { + this._promise = new Promise((resolve, reject) => { + this._resolve = resolve; + this._reject = reject; + }); + } + + isCompleted = false; + + get future() { + return this._promise; + } + + complete(value) { + this.isCompleted = true; + this._resolve(value); + } + + completeError(error) { + this._reject(error); + } +} + var webm = 'data:video/webm;base64,GkXfo0AgQoaBAUL3gQFC8oEEQvOBCEKCQAR3ZWJtQoeBAkKFgQIYU4BnQI0VSalmQCgq17FAAw9CQE2AQAZ3aGFtbXlXQUAGd2hhbW15RIlACECPQAAAAAAAFlSua0AxrkAu14EBY8WBAZyBACK1nEADdW5khkAFVl9WUDglhohAA1ZQOIOBAeBABrCBCLqBCB9DtnVAIueBAKNAHIEAAIAwAQCdASoIAAgAAUAmJaQAA3AA/vz0AAA=' var mp4 = @@ -46,21 +73,19 @@ var oldIOS = var nativeWakeLock = 'wakeLock' in navigator var NoSleep = (function () { - var _releasedNative = true - var _nativeRequestInProgress = false + var _nativeEnabledCompleter; + var _playVideoCompleter; function NoSleep() { var _this = this _classCallCheck(this, NoSleep) + this.nativeEnabled = false if (nativeWakeLock) { this._wakeLock = null var handleVisibilityChange = function handleVisibilityChange() { - if ( - _this._wakeLock !== null && - document.visibilityState === 'visible' - ) { + if (_this._wakeLock !== null && document.visibilityState === 'visible') { _this.enable() } } @@ -106,27 +131,36 @@ var NoSleep = (function () { }, { key: 'enable', - value: function enable() { + value: async function enable() { var _this2 = this if (nativeWakeLock) { - _nativeRequestInProgress = true + await this.disable() + if (_nativeEnabledCompleter == null) { + _nativeEnabledCompleter = new PromiseCompleter() + } navigator.wakeLock .request('screen') .then(function (wakeLock) { - _releasedNative = false - _nativeRequestInProgress = false - _this2._wakeLock = wakeLock + _this2.nativeEnabled = true + _nativeEnabledCompleter.complete() + _nativeEnabledCompleter = null + console.log("Wake Lock active."); _this2._wakeLock.addEventListener('release', function () { - _releasedNative = true + _this2.nativeEnabled = false _this2._wakeLock = null + console.log("Wake Lock released."); }) }) .catch(function (err) { - _nativeRequestInProgress = false - console.error(err.name + ', ' + err.message) + _this2.nativeEnabled = false + var errorMessage = err.name + ', ' + err.message + console.error(errorMessage) + _nativeEnabledCompleter.completeError(errorMessage) + _nativeEnabledCompleter = null }) + return _nativeEnabledCompleter.future } else if (oldIOS) { this.disable() console.warn( @@ -138,17 +172,34 @@ var NoSleep = (function () { window.setTimeout(window.stop, 0) } }, 15000) + return Promise.resolve() } else { - this.noSleepVideo.play() + if (_playVideoCompleter == null) { + _playVideoCompleter = new PromiseCompleter() + } + var playPromise = this.noSleepVideo.play() + playPromise.then(function (res) { + _playVideoCompleter.complete() + _playVideoCompleter = null + }).catch(function (err) { + var errorMessage = err.name + ', ' + err.message + console.error(errorMessage) + _playVideoCompleter.completeError(errorMessage) + _playVideoCompleter = null + }); + return _playVideoCompleter.future } }, }, { key: 'disable', - value: function disable() { + value: async function disable() { if (nativeWakeLock) { + if (_nativeEnabledCompleter != null) { + await _nativeEnabledCompleter.future + } if (this._wakeLock != null) { - _releasedNative = true + this.nativeEnabled = false this._wakeLock.release() } @@ -162,34 +213,29 @@ var NoSleep = (function () { this.noSleepTimer = null } } else { + if (_playVideoCompleter != null) { + await _playVideoCompleter.future + } this.noSleepVideo.pause() } + return Promise.resolve(); }, }, { - key: 'enabled', - value: async function enabled() { + key: 'isEnabled', + value: async function isEnabled() { if (nativeWakeLock) { - if (_nativeRequestInProgress == true) { - // Wait until the request is done. - while (true) { - // Wait for 42 milliseconds. - await new Promise((resolve, reject) => setTimeout(resolve, 42)) - if (_nativeRequestInProgress == false) { - break - } - } - } - - // todo: use WakeLockSentinel.released when that is available (https://developer.mozilla.org/en-US/docs/Web/API/WakeLockSentinel/released) - if (_releasedNative != false) { - return false + if (_nativeEnabledCompleter != null) { + await _nativeEnabledCompleter.future } - return true + return this.nativeEnabled } else if (oldIOS) { return this.noSleepTimer != null } else { + if (_playVideoCompleter != null) { + await _playVideoCompleter.future + } if (this.noSleepVideo == undefined) { return false } @@ -208,17 +254,22 @@ var noSleep = new NoSleep() var Wakelock = { enabled: async function () { try { - return noSleep.enabled() + return noSleep.isEnabled() } catch (e) { return false } }, toggle: async function (enable) { - if (enable) { - noSleep.enable() - } else { - noSleep.disable() + try { + if (enable) { + await noSleep.enable() + } else { + await noSleep.disable() + } + } catch (e) { + throw e } + return Promise.resolve() }, } diff --git a/wakelock_plus/lib/src/wakelock_plus_web_plugin.dart b/wakelock_plus/lib/src/wakelock_plus_web_plugin.dart index 3664edc..4ac9b19 100644 --- a/wakelock_plus/lib/src/wakelock_plus_web_plugin.dart +++ b/wakelock_plus/lib/src/wakelock_plus_web_plugin.dart @@ -40,8 +40,20 @@ class WakelockPlusWebPlugin extends WakelockPlusPlatformInterface { Future toggle({required bool enable}) async { // Make sure the JS library is loaded before calling it. await _ensureJsLoaded(); + final completer = Completer(); - wakelock_plus_web.toggle(enable); + wakelock_plus_web.toggle(enable).toDart.then( + // onResolve + (value) { + completer.complete(); + }, + // onReject + onError: (error) { + completer.completeError(error); + }, + ); + + return completer.future; } @override diff --git a/wakelock_plus/lib/src/web_impl/js_wakelock.dart b/wakelock_plus/lib/src/web_impl/js_wakelock.dart index 38b7822..bbf2351 100644 --- a/wakelock_plus/lib/src/web_impl/js_wakelock.dart +++ b/wakelock_plus/lib/src/web_impl/js_wakelock.dart @@ -5,7 +5,7 @@ import 'dart:js_interop'; /// Toggles the JS wakelock. @JS() -external void toggle(bool enable); +external JSPromise toggle(bool enable); /// Returns a JS promise of whether the wakelock is enabled or not. @JS() diff --git a/wakelock_plus/test/wakelock_plus_web_plugin_test.dart b/wakelock_plus/test/wakelock_plus_web_plugin_test.dart index 2e5c8be..f205f1d 100644 --- a/wakelock_plus/test/wakelock_plus_web_plugin_test.dart +++ b/wakelock_plus/test/wakelock_plus_web_plugin_test.dart @@ -17,6 +17,10 @@ void main() { WakelockPlusPlatformInterface.instance = WakelockPlusWebPlugin(); }); + tearDown(() async { + await WakelockPlus.disable(); + }); + test('$WakelockPlusWebPlugin set as default instance', () { expect( WakelockPlusPlatformInterface.instance, isA()); @@ -28,23 +32,32 @@ void main() { test('enable', () async { await WakelockPlus.enable(); - // Wait a bit for web to enable the wakelock - await Future.delayed(const Duration(milliseconds: 50)); + expect(WakelockPlus.enabled, completion(isTrue)); + }); + + test('enable more than once', () async { + await WakelockPlus.enable(); + await WakelockPlus.enable(); + await WakelockPlus.enable(); expect(WakelockPlus.enabled, completion(isTrue)); }); test('disable', () async { await WakelockPlus.enable(); - // Wait a bit for web to enable the wakelock - await Future.delayed(const Duration(milliseconds: 50)); + await WakelockPlus.disable(); + expect(WakelockPlus.enabled, completion(isFalse)); + }); + + test('disable more than once', () async { + await WakelockPlus.enable(); + await WakelockPlus.disable(); + await WakelockPlus.disable(); await WakelockPlus.disable(); expect(WakelockPlus.enabled, completion(isFalse)); }); test('toggle', () async { await WakelockPlus.toggle(enable: true); - // Wait a bit for web to enable the wakelock - await Future.delayed(const Duration(milliseconds: 50)); expect(WakelockPlus.enabled, completion(isTrue)); await WakelockPlus.toggle(enable: false); From ac4e06924f7eb8b3fb4635bf061cf3110c21fad0 Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Tue, 3 Dec 2024 12:55:46 -0500 Subject: [PATCH 12/14] Fixed a dart lint issue due to HTML in the documentation of _importJSLibraries. --- wakelock_plus/lib/src/web_impl/import_js_library.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wakelock_plus/lib/src/web_impl/import_js_library.dart b/wakelock_plus/lib/src/web_impl/import_js_library.dart index a388c43..562d77b 100644 --- a/wakelock_plus/lib/src/web_impl/import_js_library.dart +++ b/wakelock_plus/lib/src/web_impl/import_js_library.dart @@ -43,7 +43,7 @@ web.HTMLScriptElement _createScriptTag(String library) { return script; } -/// Injects a bunch of libraries in the and returns a +/// Injects a bunch of libraries in the `` and returns a /// Future that resolves when all load. Future _importJSLibraries(List libraries) { final loading = >[]; From a7ddb22859ada5649b8d95bf2e3b8a2168744681 Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Tue, 3 Dec 2024 13:12:55 -0500 Subject: [PATCH 13/14] Added minor comments to no_sleep.js. --- wakelock_plus/lib/assets/no_sleep.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wakelock_plus/lib/assets/no_sleep.js b/wakelock_plus/lib/assets/no_sleep.js index 22a2ac4..d74345f 100644 --- a/wakelock_plus/lib/assets/no_sleep.js +++ b/wakelock_plus/lib/assets/no_sleep.js @@ -135,6 +135,7 @@ var NoSleep = (function () { var _this2 = this if (nativeWakeLock) { + // Disalbe any previously held wakelocks. await this.disable() if (_nativeEnabledCompleter == null) { _nativeEnabledCompleter = new PromiseCompleter() @@ -146,6 +147,7 @@ var NoSleep = (function () { _this2.nativeEnabled = true _nativeEnabledCompleter.complete() _nativeEnabledCompleter = null + // We now have a wakelock. Notify all of the existing callers. console.log("Wake Lock active."); _this2._wakeLock.addEventListener('release', function () { _this2.nativeEnabled = false @@ -160,6 +162,7 @@ var NoSleep = (function () { _nativeEnabledCompleter.completeError(errorMessage) _nativeEnabledCompleter = null }) + // We then wait for screen to be made available. return _nativeEnabledCompleter.future } else if (oldIOS) { this.disable() @@ -195,6 +198,7 @@ var NoSleep = (function () { key: 'disable', value: async function disable() { if (nativeWakeLock) { + // If we're still trying to enable the wakelock, wait for it to be enabled if (_nativeEnabledCompleter != null) { await _nativeEnabledCompleter.future } @@ -225,6 +229,7 @@ var NoSleep = (function () { key: 'isEnabled', value: async function isEnabled() { if (nativeWakeLock) { + // If we're still trying to enable the wakelock, wait for it to be enabled if (_nativeEnabledCompleter != null) { await _nativeEnabledCompleter.future } From f5d5bc420155f55964171075545103dfa4b244f9 Mon Sep 17 00:00:00 2001 From: Diego Tori Date: Fri, 20 Dec 2024 22:42:32 -0500 Subject: [PATCH 14/14] Fixed a pubspec straggler from the previous commit. --- wakelock_plus/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wakelock_plus/pubspec.yaml b/wakelock_plus/pubspec.yaml index 1815519..6a2e1fb 100644 --- a/wakelock_plus/pubspec.yaml +++ b/wakelock_plus/pubspec.yaml @@ -99,4 +99,4 @@ flutter: # https://flutter.dev/custom-fonts/#from-packages assets: - - assets/no_sleep.js + - packages/wakelock_plus/assets/no_sleep.js