Skip to content

Commit

Permalink
Deprecate onInitial callback and introduce onInit for improved API co…
Browse files Browse the repository at this point in the history
…nsistency
  • Loading branch information
lamnhan066 committed Mar 2, 2025
1 parent 4c2b00f commit 2674d0a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
9 changes: 9 additions & 0 deletions lib/src/isolate_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class IsolateManager<R, P> {
QueueStrategy<R, P>? queueStrategy,
this.isDebug = false,
}) : isCustomIsolate = false,
// This API will be removed in v6.0.0 when we reach the stable release.
// ignore: deprecated_member_use_from_same_package
initialParams = '',
queueStrategy = queueStrategy ?? QueueStrategyUnlimited(),
_workerName = workerName,
Expand Down Expand Up @@ -70,6 +72,8 @@ class IsolateManager<R, P> {
IsolateManager.createCustom(
IsolateCustomFunction this.isolateFunction, {
String? workerName,
// This API will be removed in v6.0.0 when we reach the stable release.
// ignore: deprecated_consistency
this.initialParams,
this.concurrent = 1,
this.converter,
Expand Down Expand Up @@ -308,6 +312,9 @@ class IsolateManager<R, P> {
final String? _workerName;

/// Initial parameters.
@Deprecated(
'This API will be removed in v6.0.0 when we reach the stable release.',
)
final Object? initialParams;

/// Is using your own isolate function.
Expand Down Expand Up @@ -398,6 +405,8 @@ class IsolateManager<R, P> {
IsolateContactor.createCustom<R, P>(
isolateFunction as IsolateCustomFunction,
workerName: workerName,
// This API will be removed in v6.0.0 when we reach the stable release.
// ignore: deprecated_member_use_from_same_package
initialParams: initialParams,
converter: converter,
workerConverter: workerConverter,
Expand Down
23 changes: 22 additions & 1 deletion lib/src/isolate_manager_function.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ import 'package:isolate_manager/src/isolate_worker/isolate_worker_web.dart'

/// A callback for the [IsolateManagerFunction.customFunction] that will be executed only one time
/// before all events.
@Deprecated(
'Use `IsolateOnInitCallback` instead. This API will be removed in v6.0.0 when we reach the stable release.',
)
typedef IsolateOnInitialCallback<R, P, T> = FutureOr<void> Function(
IsolateManagerController<R, P> controller,
T initialParams,
);

/// A callback for the [IsolateManagerFunction.customFunction] that will be executed only one time
/// before all events.
typedef IsolateOnInitCallback<R, P> = FutureOr<void> Function(
IsolateManagerController<R, P> controller,
);

/// A callback for the [IsolateManagerFunction.customFunction] that will be executed only one time
/// before all events.
typedef IsolateOnDisposeCallback<R, P> = void Function(
Expand Down Expand Up @@ -64,7 +73,11 @@ abstract class IsolateManagerFunction {
/// A default parameter that used by the package.
dynamic params, {
required IsolateOnEventCallback<R, P> onEvent,
@Deprecated(
' Use `onInit` instead. This API will be removed in v6.0.0 when we reach the stable release.',
)
IsolateOnInitialCallback<R, P, Object?>? onInitial,
IsolateOnInitCallback<R, P>? onInit,
IsolateOnDisposeCallback<R, P>? onDispose,
bool autoHandleException = true,
bool autoHandleResult = true,
Expand Down Expand Up @@ -144,9 +157,17 @@ abstract class IsolateManagerFunction {
/// ```
static Future<void> workerFunction<R, P>(
IsolateWorkerFunction<R, P> function, {
@Deprecated(
'Use `onInit` instead. This API will be removed in v6.0.0 when we reach the stable release.',
)
FutureOr<void> Function()? onInitial,
IsolateOnInitCallback<R, P>? onInit,
}) {
return isolateWorkerImpl<R, P>(function, onInitial);
assert(
onInitial == null || onInit == null,
'Provide either `onInit` or `onInitial`, not both.',
);
return isolateWorkerImpl<R, P>(function, onInitial, onInit);
}

/// Create a custom Worker function.
Expand Down
4 changes: 4 additions & 0 deletions lib/src/isolate_worker/isolate_worker_stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import 'package:isolate_manager/src/isolate_manager_function.dart';
/// No-op on `io`.
Future<void> isolateWorkerImpl<R, P>(
IsolateWorkerFunction<R, P> function,
@Deprecated(
'Use `onInit` instead. This API will be removed in v6.0.0 when we reach the stable release.',
)
FutureOr<void> Function()? onInitial,
IsolateOnInitCallback<R, P>? onInit,
) {
throw UnimplementedError('Use only on the Web platform to create a Worker');
}
Expand Down
5 changes: 5 additions & 0 deletions lib/src/isolate_worker/isolate_worker_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ external DedicatedWorkerGlobalScope get _self;
/// Create a worker in your `main`.
Future<void> isolateWorkerImpl<R, P>(
IsolateWorkerFunction<R, P> function,
@Deprecated(
'Use `onInit` instead. This API will be removed in v6.0.0 when we reach the stable release.',
)
FutureOr<void> Function()? onInitial,
IsolateOnInitCallback<R, P>? onInit,
) async {
final controller = IsolateManagerController<R, P>(_self);

await onInitial?.call();
await onInit?.call(controller);

controller.onIsolateMessage.listen((message) async {
try {
Expand Down
10 changes: 2 additions & 8 deletions test/isolate_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -920,10 +920,7 @@ Future<void> isolateFunction(dynamic params) async {
}
return 0;
},
onInitial: (
IsolateManagerController<int, int> controller,
Object? initialParams,
) {},
onInit: (IsolateManagerController<int, int> controller) {},
onDispose: (IsolateManagerController<int, int> controller) {},
autoHandleException: false,
autoHandleResult: false,
Expand All @@ -937,10 +934,7 @@ void isolateFunctionWithAutomaticallyHandlers(dynamic params) {
onEvent: (IsolateManagerController<int, int> controller, int message) {
return fibonacci(message);
},
onInitial: (
IsolateManagerController<int, int> controller,
Object? initialParams,
) {},
onInit: (IsolateManagerController<int, int> controller) {},
onDispose: (IsolateManagerController<int, int> controller) {},
);
}
Expand Down

1 comment on commit 2674d0a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

91.94%

Coverage Report for Changed Files
FileStmtsBranchesFuncsLinesUncovered Lines
lib/src
   isolate_manager_function.dart95%100%100%95%117
   isolate_manager.dart100%100%100%100%
   isolate_manager.dart95.97%100%100%95.97%188, 195, 201, 203, 205
   isolate_manager_function.dart91.67%100%100%91.67%101

Please sign in to comment.