Skip to content

Commit 3e28469

Browse files
authored
feat: add lifecycle hooks
1 parent e283033 commit 3e28469

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

lib/nitric.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ library;
33
export 'src/nitric.dart';
44
export 'src/context/common.dart';
55
export 'src/resources/common.dart';
6+
export 'src/api/lifecycle.dart';

lib/src/api/api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export 'proto.dart';
88
export 'queue.dart';
99
export 'batch.dart';
1010
export 'sql.dart';
11+
export 'lifecycle.dart';
1112

1213
typedef UseClientCallback<GrpcClient extends Client, Resp> = Future<Resp>
1314
Function(GrpcClient);

lib/src/api/lifecycle.dart

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2024, Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import 'dart:io';
16+
17+
/// The environment variable key that will be used to determine the current Nitric lifecycle/executing environment
18+
const String nitricEnvironment = 'NITRIC_ENVIRONMENT';
19+
20+
/// Possible nitric execution environments
21+
enum LifecycleStage {
22+
/// Local development run (using nitric run/start)
23+
local,
24+
25+
/// Local development requirements building/collection (using nitric up)
26+
build,
27+
28+
/// When the code is running in a deployed environment
29+
cloud,
30+
}
31+
32+
/// Get the current lifecycle stage from environment variables
33+
LifecycleStage _getCurrentLifecycle() {
34+
final lifecycle = Platform.environment[nitricEnvironment];
35+
36+
if (lifecycle == null) {
37+
throw Exception(
38+
'Unable to determine the current Nitric lifecycle, please ensure the $nitricEnvironment environment variable is set',
39+
);
40+
}
41+
42+
try {
43+
return LifecycleStage.values.firstWhere(
44+
(stage) => stage.name == lifecycle,
45+
orElse: () => throw Exception('Invalid lifecycle stage: $lifecycle'),
46+
);
47+
} catch (e) {
48+
throw Exception(
49+
'Unable to determine the current Nitric lifecycle, please ensure the $nitricEnvironment environment variable is set to a valid value',
50+
);
51+
}
52+
}
53+
54+
/// Check if the current environment is one of the provided stages
55+
bool _isInLifecycle(List<LifecycleStage> stages) {
56+
final currentStage = _getCurrentLifecycle();
57+
return stages.contains(currentStage);
58+
}
59+
60+
/// If the current environment is one of the provided stages, execute the provided callback
61+
T? _whenInLifecycles<T>(List<LifecycleStage> stages, T Function() callback) {
62+
if (_isInLifecycle(stages)) {
63+
return callback();
64+
}
65+
return null;
66+
}
67+
68+
/// If the current environment is running (local or cloud), execute the provided callback
69+
T? _whenRunning<T>(T Function() callback) => _whenInLifecycles<T>(
70+
[LifecycleStage.local, LifecycleStage.cloud], callback);
71+
72+
/// If the current environment is collecting requirements, execute the provided callback
73+
T? _whenCollecting<T>(T Function() callback) =>
74+
_whenInLifecycles<T>([LifecycleStage.build], callback);
75+
76+
/// Check if the current lifecycle is running (local or cloud)
77+
bool _isRunning() =>
78+
_isInLifecycle([LifecycleStage.local, LifecycleStage.cloud]);
79+
80+
/// Check if the current lifecycle is collecting application requirements
81+
bool _isCollecting() => _isInLifecycle([LifecycleStage.build]);
82+
83+
/// Lifecycle utility class
84+
class Lifecycle {
85+
/// Check if the current environment is one of the provided stages
86+
static bool isInStage(List<LifecycleStage> stages) => _isInLifecycle(stages);
87+
88+
/// Check if the current lifecycle is collecting application requirements
89+
static bool get isCollecting => _isCollecting();
90+
91+
/// Check if the current lifecycle is running the app
92+
static bool get isRunning => _isRunning();
93+
94+
/// If the current environment is one of the provided stages, execute the provided callback
95+
static T? when<T>(List<LifecycleStage> stages, T Function() callback) =>
96+
_whenInLifecycles<T>(stages, callback);
97+
98+
/// If the current environment is collecting application requirements
99+
static T? whenCollecting<T>(T Function() callback) =>
100+
_whenCollecting<T>(callback);
101+
102+
/// If the current environment is a cloud environment, execute the provided callback
103+
static T? whenRunning<T>(T Function() callback) => _whenRunning<T>(callback);
104+
}

0 commit comments

Comments
 (0)