From 935cc8b109391ce9e07d19c17f800aa03bd7fa67 Mon Sep 17 00:00:00 2001 From: artahmetaj Date: Fri, 25 Oct 2024 09:51:01 +0200 Subject: [PATCH 1/5] Add cached factory to excluded types on assert and save lastParams --- lib/get_it_impl.dart | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/get_it_impl.dart b/lib/get_it_impl.dart index a19b830..cc045ec 100644 --- a/lib/get_it_impl.dart +++ b/lib/get_it_impl.dart @@ -171,7 +171,8 @@ class _ServiceFactory { /// returns an instance depending on the type of the registration if [async==false] T getObject(dynamic param1, dynamic param2) { assert( - !(factoryType != _ServiceFactoryType.alwaysNew && + !(![_ServiceFactoryType.alwaysNew, _ServiceFactoryType.cachedFactory] + .contains(factoryType) && (param1 != null || param2 != null)), 'You can only pass parameters to factories!', ); @@ -201,6 +202,8 @@ class _ServiceFactory { param2 == lastParam2) { return weakReferenceInstance!.target!; } else { + lastParam1 = param1 as P1; + lastParam2 = param2 as P2; T newInstance; if (creationFunctionParam != null) { newInstance = creationFunctionParam!(param1 as P1, param2 as P2); @@ -1546,6 +1549,8 @@ class _GetItImplementation implements GetIt { FactoryFuncAsync? factoryFuncAsync, FactoryFuncParamAsync? factoryFuncParamAsync, T? instance, + P1? param1, + P2? param2, required String? instanceName, required bool isAsync, Iterable? dependsOn, From 9cc5cba94dc6eaba1f6dc88b3e14822e6c2117cf Mon Sep 17 00:00:00 2001 From: artahmetaj Date: Fri, 25 Oct 2024 10:32:47 +0200 Subject: [PATCH 2/5] add tests for the cached factories --- lib/get_it_impl.dart | 4 +-- test/get_it_test.dart | 84 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/lib/get_it_impl.dart b/lib/get_it_impl.dart index cc045ec..be62059 100644 --- a/lib/get_it_impl.dart +++ b/lib/get_it_impl.dart @@ -202,8 +202,8 @@ class _ServiceFactory { param2 == lastParam2) { return weakReferenceInstance!.target!; } else { - lastParam1 = param1 as P1; - lastParam2 = param2 as P2; + lastParam1 = param1 as P1?; + lastParam2 = param2 as P2?; T newInstance; if (creationFunctionParam != null) { newInstance = creationFunctionParam!(param1 as P1, param2 as P2); diff --git a/test/get_it_test.dart b/test/get_it_test.dart index fcadac5..ef0c6a5 100644 --- a/test/get_it_test.dart +++ b/test/get_it_test.dart @@ -39,11 +39,17 @@ class TestClassDisposable extends TestBaseClass with Disposable { class TestClass2 {} -class TestClassParam { +class TestClassParam extends TestBaseClass { final String? param1; final int? param2; - TestClassParam({this.param1, this.param2}); + TestClassParam({this.param1, this.param2}) { + constructorCounter++; + } + + void dispose() { + disposeCounter++; + } } class TestClassDisposableWithDependency with Disposable { @@ -216,6 +222,80 @@ void main() { GetIt.I.reset(); }); + test('register cached factory', () { + final getIt = GetIt.instance; + constructorCounter = 0; + getIt.registerCachedFactory(() => TestClass()); + final TestBaseClass instance1 = getIt(); + expect(instance1 is TestClass, true); + final instance2 = getIt.get(); + expect(getIt.isRegistered(), true); + expect(instance1, instance2); + expect(constructorCounter, 1); + }); + + test('register cached factory with one param ', () { + final getIt = GetIt.instance; + constructorCounter = 0; + getIt.registerCachedFactoryParam( + (s, _) => TestClassParam(param1: s), + ); + final instance1 = getIt(param1: 'abc'); + final instance2 = getIt(param1: 'abc'); + expect(instance1 is TestClassParam, true); + expect(instance1.param1, 'abc'); + expect(instance1, instance2); + expect(constructorCounter, 1); + }); + + test('register cached factory with different params', () { + final getIt = GetIt.instance; + constructorCounter = 0; + getIt.registerCachedFactoryParam( + (s, _) => TestClassParam(param1: s), + ); + final instance1 = getIt(param1: 'abc'); + final instance2 = getIt(param1: '123'); + expect(instance1 is TestClassParam, true); + expect(instance1.param1, 'abc'); + expect(instance2.param1, '123'); + expect(instance2 is TestClassParam, true); + expect(instance1, isNot(instance2)); + expect(constructorCounter, 2); + }); + + test('register cached factory with two equal params', () { + final getIt = GetIt.instance; + constructorCounter = 0; + getIt.registerCachedFactoryParam( + (f,s) => TestClassParam(param1: f,param2:s), + ); + final instance1 = getIt(param1: 'abc', param2: 1); + final instance2 = getIt(param1: 'abc', param2: 1); + expect(instance1 is TestClassParam, true); + expect(instance1.param1, 'abc'); + expect(instance1, instance2); + expect(constructorCounter, 1); + }); + + test('register cached factory with one different param out of two', () { + final getIt = GetIt.instance; + constructorCounter = 0; + getIt.registerCachedFactoryParam( + (f,s) => TestClassParam(param1: f,param2: s), + ); + final instance1 = getIt(param1: 'abc', param2: 1); + final instance2 = getIt(param1: 'abc', param2: 2); + expect(instance1 is TestClassParam, true); + expect(instance1.param1, 'abc'); + expect(instance1.param2, 1); + expect(instance2.param1, 'abc'); + expect(instance2.param2, 2); + expect(instance2 is TestClassParam, true); + expect(instance1, isNot(instance2)); + expect(constructorCounter, 2); + }); + test('register constant', () { final getIt = GetIt.instance; constructorCounter = 0; From 37ac31d493ee13b440157da43cf505db9d2fabb4 Mon Sep 17 00:00:00 2001 From: artahmetaj Date: Fri, 25 Oct 2024 10:37:13 +0200 Subject: [PATCH 3/5] Add same fixes on async equivalent function --- lib/get_it_impl.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/get_it_impl.dart b/lib/get_it_impl.dart index be62059..ea358ee 100644 --- a/lib/get_it_impl.dart +++ b/lib/get_it_impl.dart @@ -257,7 +257,8 @@ class _ServiceFactory { /// if [dependsOn.isNotEmpty]. Future getObjectAsync(dynamic param1, dynamic param2) async { assert( - !(factoryType != _ServiceFactoryType.alwaysNew && + !(![_ServiceFactoryType.alwaysNew, _ServiceFactoryType.cachedFactory] + .contains(factoryType) && (param1 != null || param2 != null)), 'You can only pass parameters to factories!', ); @@ -294,6 +295,8 @@ class _ServiceFactory { return Future.value(weakReferenceInstance!.target! as R); } else { if (creationFunctionParam != null) { + lastParam1 = param1 as P1?; + lastParam2 = param2 as P2?; return asyncCreationFunctionParam!(param1 as P1, param2 as P2) .then((value) { weakReferenceInstance = WeakReference(value); From 06525e8c675d33cc66ecb26036f087413844c3fc Mon Sep 17 00:00:00 2001 From: artahmetaj Date: Mon, 28 Oct 2024 09:10:29 +0100 Subject: [PATCH 4/5] remove p1 and p2 on register scenario --- lib/get_it_impl.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/get_it_impl.dart b/lib/get_it_impl.dart index ea358ee..45fa185 100644 --- a/lib/get_it_impl.dart +++ b/lib/get_it_impl.dart @@ -1552,8 +1552,6 @@ class _GetItImplementation implements GetIt { FactoryFuncAsync? factoryFuncAsync, FactoryFuncParamAsync? factoryFuncParamAsync, T? instance, - P1? param1, - P2? param2, required String? instanceName, required bool isAsync, Iterable? dependsOn, From d118a07497264f10751bc7a8efdbe2c93c346653 Mon Sep 17 00:00:00 2001 From: artahmetaj Date: Mon, 28 Oct 2024 12:10:17 +0100 Subject: [PATCH 5/5] Remove dispose method on testclass param --- test/get_it_test.dart | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/test/get_it_test.dart b/test/get_it_test.dart index ef0c6a5..113f50e 100644 --- a/test/get_it_test.dart +++ b/test/get_it_test.dart @@ -46,10 +46,6 @@ class TestClassParam extends TestBaseClass { TestClassParam({this.param1, this.param2}) { constructorCounter++; } - - void dispose() { - disposeCounter++; - } } class TestClassDisposableWithDependency with Disposable { @@ -268,7 +264,7 @@ void main() { final getIt = GetIt.instance; constructorCounter = 0; getIt.registerCachedFactoryParam( - (f,s) => TestClassParam(param1: f,param2:s), + (f, s) => TestClassParam(param1: f, param2: s), ); final instance1 = getIt(param1: 'abc', param2: 1); final instance2 = getIt(param1: 'abc', param2: 1); @@ -282,7 +278,7 @@ void main() { final getIt = GetIt.instance; constructorCounter = 0; getIt.registerCachedFactoryParam( - (f,s) => TestClassParam(param1: f,param2: s), + (f, s) => TestClassParam(param1: f, param2: s), ); final instance1 = getIt(param1: 'abc', param2: 1); final instance2 = getIt(param1: 'abc', param2: 2);