@@ -5,54 +5,79 @@ import 'package:superuser_interfaces/superuser_interfaces.dart';
55import 'package:superuser_plugin_unix/superuser_plugin_unix.dart' ;
66import 'package:superuser_plugin_windows/superuser_plugin_windows.dart' ;
77
8+ SuperuserInterface ? _instance;
9+
10+ /// Retrive current instance of [SuperuserInterface] .
11+ ///
12+ /// If no instance was created before, it call
13+ /// [bindInstance] automatically that assign
14+ /// platform implemented [SuperuserInterface]
15+ /// as current instance.
16+ @internal
17+ SuperuserInterface get instance {
18+ if (_instance == null ||
19+ _instance is SuperuserPlatform &&
20+ (_instance as SuperuserPlatform ).isClosed) {
21+ SuperuserInstance .bindInstance (null );
22+ }
23+
24+ return _instance! ;
25+ }
26+
827/// Manage instance of [SuperuserInterface] when accessing superuser
928/// properties.
10- @internal
1129abstract final class SuperuserInstance {
12- static SuperuserInterface ? _instance;
13-
1430 const SuperuserInstance ._();
1531
16- /// Retrive current instance of [SuperuserInterface] .
32+ /// Attach specified [suInterface] as data source of superuser
33+ /// property.
34+ ///
35+ /// If [suInterface] is `null` , it binds platform specified
36+ /// [SuperuserInterface] automatically.
1737 ///
18- /// If no instance was created before, it call
19- /// [bindInstance] automatically that assign
20- /// platform implemented [SuperuserInterface]
21- /// as current instance.
22- static SuperuserInterface get instance {
23- if (_instance == null ) {
24- bindInstance (null );
38+ /// When this invoked in neither Windows, macOS or Linux platform,
39+ /// it throws [UnsupportedError] .
40+ ///
41+ /// If [suInterface] is a member of [SuperuserPlatform] ,
42+ /// the provided interface should not be closed. Otherwise,
43+ /// it throws [ArgumentError] .
44+ static void bindInstance (SuperuserInterface ? suInterface) {
45+ if (! Platform .isWindows && ! Platform .isMacOS && ! Platform .isLinux) {
46+ throw UnsupportedError ("Unknown platform" );
2547 }
2648
27- return _instance! ;
28- }
29- }
49+ flushInstance ();
3050
31- /// Attach specified [suInterface] as data source of superuser
32- /// property.
33- ///
34- /// If [suInterface] is `null` , it binds platform specified
35- /// [SuperuserInterface] automatically.
36- ///
37- /// When this invoked in neither Windows, macOS or Linux platform,
38- /// it throws [UnsupportedError] .
39- void bindInstance (SuperuserInterface ? suInterface) {
40- if (! Platform .isWindows && ! Platform .isMacOS && ! Platform .isLinux) {
41- throw UnsupportedError ("Unknown platform" );
42- }
51+ late SuperuserInterface newInst;
4352
44- late SuperuserInterface newInst;
53+ if (suInterface == null ) {
54+ // Denote null interface as uses default implementation.
55+ if (Platform .isWindows) {
56+ newInst = WindowsSuperuser ();
57+ } else if (Platform .isMacOS || Platform .isLinux) {
58+ newInst = UnixSuperuser ();
59+ }
60+ } else {
61+ if (suInterface is SuperuserPlatform && suInterface.isClosed) {
62+ throw ArgumentError (
63+ "The provided interface should not be closed already." ,
64+ "suInterface" );
65+ }
4566
46- if (suInterface == null ) {
47- // Denote null interface as uses default implementation.
48- if (Platform .isWindows) {
49- newInst = WindowsSuperuser ();
50- } else if (Platform .isMacOS || Platform .isLinux) {
51- newInst = UnixSuperuser ();
67+ newInst = suInterface;
5268 }
53- } else {
54- newInst = suInterface ;
69+
70+ _instance = newInst ;
5571 }
5672
57- SuperuserInstance ._instance = newInst;
73+ /// Flush existed [SuperuserInterface] instance.
74+ static void flushInstance () {
75+ if (_instance != null ) {
76+ if (_instance is SuperuserPlatform ) {
77+ (_instance as SuperuserPlatform ).close ();
78+ }
79+
80+ _instance = null ;
81+ }
82+ }
5883}
0 commit comments