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

fix: memory leaks from window #1297

Merged
merged 1 commit into from
Apr 2, 2022
Merged
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
6 changes: 1 addition & 5 deletions kraken/lib/src/dom/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ class Window extends EventTarget {
final Screen screen;

Window(BindingContext? context, this.document)
: screen = Screen(context), super(context) {
window.onPlatformBrightnessChanged = () {
dispatchEvent(ColorSchemeChangeEvent(colorScheme));
};
}
: screen = Screen(context), super(context);

@override
EventTarget? get parentEventTarget => null;
Expand Down
21 changes: 21 additions & 0 deletions kraken/lib/src/launcher/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class KrakenViewController
window = Window(
BindingContext(_contextId, windowNativePtrMap[_contextId]!),
document);
_registerPlatformBrightnessChange();
_setEventTarget(WINDOW_ID, window);

// Listeners need to be registered to window in order to dispatch events on demand.
Expand Down Expand Up @@ -252,6 +253,7 @@ class KrakenViewController
debugDOMTreeChanged = null;

_teardownObserver();
_unregisterPlatformBrightnessChange();

// Should clear previous page cached ui commands
clearUICommand(_contextId);
Expand All @@ -265,6 +267,25 @@ class KrakenViewController
_disposed = true;
}

VoidCallback? _originalOnPlatformBrightnessChanged;

void _registerPlatformBrightnessChange() {
_originalOnPlatformBrightnessChanged = ui.window.onPlatformBrightnessChanged;
ui.window.onPlatformBrightnessChanged = _onPlatformBrightnessChanged;
}

void _unregisterPlatformBrightnessChange() {
ui.window.onPlatformBrightnessChanged = _originalOnPlatformBrightnessChanged;
_originalOnPlatformBrightnessChanged = null;
}

void _onPlatformBrightnessChanged() {
if (_originalOnPlatformBrightnessChanged != null) {
_originalOnPlatformBrightnessChanged!();
}
window.dispatchEvent(ColorSchemeChangeEvent(window.colorScheme));
}

Map<int, EventTarget> _eventTargets = <int, EventTarget>{};

T? getEventTargetById<T>(int targetId) {
Expand Down
2 changes: 1 addition & 1 deletion kraken/lib/src/module/navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class NavigatorModule extends BaseModule {
}

static String getLanguages() {
// Strinfiy the list of languages to JSON format.
// Stringify the list of languages to JSON format.
return '[' + PlatformDispatcher.instance.locales.map(((locale) => '"${locale.toLanguageTag()}"')).join(',') + ']';
}

Expand Down