Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
refactor(auth): move windows functions to core
  • Loading branch information
russellwheatley committed Jun 6, 2024
commit 51726a082ca678c9f8336bc7ccc448dc784cfdaa
43 changes: 8 additions & 35 deletions packages/firebase_auth/firebase_auth_web/lib/src/interop/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

import 'dart:async';
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'package:flutter/foundation.dart';
import 'package:web/web.dart' as web;
import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
import 'package:firebase_core_web/firebase_core_web_interop.dart';
import 'package:http_parser/http_parser.dart';
Expand Down Expand Up @@ -395,32 +392,8 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
// ignore: close_sinks
StreamController<User?>? _changeController;

String get authStateWindowsKey => 'flutterfire-${app.name}_authStateChanges';
String get idTokenStateWindowsKey => 'flutterfire-${app.name}_idTokenChanges';

// No way to unsubscribe from event listeners on hot reload so we set on the windows object
// and clean up on hot restart if it exists.
// See: https://github.com/firebase/flutterfire/issues/7064
void _unsubscribeWindowsListener(String key) {
if (kDebugMode) {
final unsubscribe = web.window.getProperty(key.toJS);
if (unsubscribe != null) {
(unsubscribe as JSFunction).callAsFunction();
}
}
}

void _setWindowsListener(String key, JSFunction unsubscribe) {
if (kDebugMode) {
web.window.setProperty(key.toJS, unsubscribe);
}
}

void _removeWindowsListener(String key) {
if (kDebugMode) {
web.window.delete(key.toJS);
}
}
String get _authStateWindowsKey => 'flutterfire-${app.name}_authStateChanges';
String get _idTokenStateWindowsKey => 'flutterfire-${app.name}_idTokenChanges';

/// Sends events when the users sign-in state changes.
///
Expand All @@ -429,7 +402,7 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
///
/// If the value is `null`, there is no signed-in user.
Stream<User?> get onAuthStateChanged {
_unsubscribeWindowsListener(authStateWindowsKey);
unsubscribeWindowsListener(_authStateWindowsKey);

if (_changeController == null) {
final nextWrapper = (auth_interop.UserJsImpl? user) {
Expand All @@ -443,14 +416,14 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
final unsubscribe =
jsObject.onAuthStateChanged(nextWrapper.toJS, errorWrapper.toJS);
_onAuthUnsubscribe = unsubscribe;
_setWindowsListener(authStateWindowsKey, unsubscribe);
setWindowsListener(_authStateWindowsKey, unsubscribe);
}

void stopListen() {
_onAuthUnsubscribe!.callAsFunction();
_onAuthUnsubscribe = null;
_changeController = null;
_removeWindowsListener(authStateWindowsKey);
removeWindowsListener(_authStateWindowsKey);
}

_changeController = StreamController<User?>.broadcast(
Expand All @@ -476,7 +449,7 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
///
/// If the value is `null`, there is no signed-in user.
Stream<User?> get onIdTokenChanged {
_unsubscribeWindowsListener(idTokenStateWindowsKey);
unsubscribeWindowsListener(_idTokenStateWindowsKey);
if (_idTokenChangedController == null) {
final nextWrapper = (auth_interop.UserJsImpl? user) {
_idTokenChangedController!.add(User.getInstance(user));
Expand All @@ -489,14 +462,14 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
final unsubscribe =
jsObject.onIdTokenChanged(nextWrapper.toJS, errorWrapper.toJS);
_onIdTokenChangedUnsubscribe = unsubscribe;
_setWindowsListener(idTokenStateWindowsKey, unsubscribe);
setWindowsListener(_idTokenStateWindowsKey, unsubscribe);
}

void stopListen() {
_onIdTokenChangedUnsubscribe!.callAsFunction();
_onIdTokenChangedUnsubscribe = null;
_idTokenChangedController = null;
_removeWindowsListener(idTokenStateWindowsKey);
removeWindowsListener(_idTokenStateWindowsKey);
}

_idTokenChangedController = StreamController<User?>.broadcast(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import 'dart:async';
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'package:web/web.dart' as web;
import 'package:flutter/foundation.dart';

import 'func.dart';

Expand Down Expand Up @@ -38,3 +40,27 @@ JSPromise handleFutureWithMapper<T, S>(
});
}.toJS);
}

// No way to unsubscribe from event listeners on hot reload so we set on the windows object
// and clean up on hot restart if it exists.
// See: https://github.com/firebase/flutterfire/issues/7064
void unsubscribeWindowsListener(String key) {
if (kDebugMode) {
final unsubscribe = web.window.getProperty(key.toJS);
if (unsubscribe != null) {
(unsubscribe as JSFunction).callAsFunction();
}
}
}

void setWindowsListener(String key, JSFunction unsubscribe) {
if (kDebugMode) {
web.window.setProperty(key.toJS, unsubscribe);
}
}

void removeWindowsListener(String key) {
if (kDebugMode) {
web.window.delete(key.toJS);
}
}
2 changes: 1 addition & 1 deletion packages/firebase_core/firebase_core_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version: 2.17.1

environment:
sdk: '>=3.2.0 <4.0.0'
flutter: '>=3.3.0'
flutter: '>=3.16.0'

dependencies:
firebase_core_platform_interface: ^5.0.0
Expand Down