Skip to content

Commit c86f6d6

Browse files
committed
chore: Make use of optional params for nicer API
1 parent f342028 commit c86f6d6

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

example/lib/main.dart

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class _MyAppState extends State<MyApp> {
9898
child: const Text('Track User Action')),
9999
ElevatedButton(
100100
onPressed: () async => {
101-
_contextSdkPlugin.calibrate("my_flow", null, {
101+
_contextSdkPlugin.calibrate("my_flow", customSignals: {
102102
'string': 'string',
103103
'int': 14,
104104
'bool': true,
@@ -117,31 +117,61 @@ class _MyAppState extends State<MyApp> {
117117
child: const Text('Calibrate')),
118118
ElevatedButton(
119119
onPressed: () async => {
120-
_contextSdkPlugin.optimize("my_flow", null, {
121-
'string': 'string',
122-
'int': 14,
123-
'bool': true,
124-
'float': 1.124
125-
}, (value) async {
126-
print("Optimized context: ${await value.validate()}");
127-
value.appendOutcomeMetadata({
120+
_contextSdkPlugin.optimize("my_flow", (context) async {
121+
print("Optimized context: ${await context.validate()}");
122+
context.appendOutcomeMetadata({
128123
'string': 'string',
129124
'int': 14,
130125
'bool': true,
131126
'float': 1.124
132127
});
133-
value.log(Outcome.positive);
128+
await context.log(Outcome.positive);
134129
})
135130
},
136131
child: const Text('Optimize')),
137132
ElevatedButton(
138133
onPressed: () async => {
139-
_contextSdkPlugin.fetchContext("my_flow", 3, {
134+
_contextSdkPlugin.optimize("my_flow", customSignals: {
140135
'string': 'string',
141136
'int': 14,
142137
'bool': true,
143138
'float': 1.124
144-
}).then((value) async {
139+
}, (context) async {
140+
print("Optimized context: ${await context.validate()}");
141+
context.appendOutcomeMetadata({
142+
'string': 'string',
143+
'int': 14,
144+
'bool': true,
145+
'float': 1.124
146+
});
147+
await context.log(Outcome.positive);
148+
})
149+
},
150+
child: const Text('Optimize (Custom Signals)')),
151+
ElevatedButton(
152+
onPressed: () async => {
153+
_contextSdkPlugin.optimize("my_flow", maxDelay: 0,
154+
(context) async {
155+
print("Optimized context: ${await context.validate()}");
156+
context.appendOutcomeMetadata({
157+
'string': 'string',
158+
'int': 14,
159+
'bool': true,
160+
'float': 1.124
161+
});
162+
await context.log(Outcome.positive);
163+
})
164+
},
165+
child: const Text('Optimize (Instant)')),
166+
ElevatedButton(
167+
onPressed: () async => {
168+
_contextSdkPlugin.fetchContext("my_flow", 3,
169+
customSignals: {
170+
'string': 'string',
171+
'int': 14,
172+
'bool': true,
173+
'float': 1.124
174+
}).then((value) async {
145175
print("Fetch context: ${await value.validate()}");
146176
value.appendOutcomeMetadata({
147177
'string': 'string',
@@ -155,8 +185,8 @@ class _MyAppState extends State<MyApp> {
155185
child: const Text('Fetch Context')),
156186
ElevatedButton(
157187
onPressed: () async {
158-
final context = await _contextSdkPlugin.instantContext(
159-
"my_flow", 3, null);
188+
final context =
189+
await _contextSdkPlugin.instantContext("my_flow", 3);
160190
print("Instant context: ${await context.validate()}");
161191
if (await context.shouldUpsell()) {
162192
await context.appendOutcomeMetadata({

lib/context_sdk.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ class ContextSdk {
5050
}
5151

5252
void calibrate(
53-
String flowName,
54-
int? maxDelay,
55-
Map<String, dynamic>? customSignals,
56-
ValueSetter<RealWorldContext> onContextReady) async {
53+
String flowName,
54+
ValueSetter<RealWorldContext> onContextReady, {
55+
int? maxDelay,
56+
Map<String, dynamic>? customSignals,
57+
}) async {
5758
ContextSdkPlatform.instance.calibrate(flowName, maxDelay, customSignals,
5859
(contextId) {
5960
onContextReady(RealWorldContext(ContextIdHolder(contextId)));
@@ -66,10 +67,11 @@ class ContextSdk {
6667
///
6768
/// [onGoodMoment] Will only be called if ContextSDK deems it a good moment, meaning it might never be called.
6869
void optimize(
69-
String flowName,
70-
int? maxDelay,
71-
Map<String, dynamic>? customSignals,
72-
ValueSetter<RealWorldContext> onGoodMoment) {
70+
String flowName,
71+
ValueSetter<RealWorldContext> onGoodMoment, {
72+
int? maxDelay,
73+
Map<String, dynamic>? customSignals,
74+
}) {
7375
ContextSdkPlatform.instance.optimize(flowName, maxDelay, customSignals,
7476
(contextId) {
7577
onGoodMoment(RealWorldContext(ContextIdHolder(contextId)));
@@ -82,7 +84,7 @@ class ContextSdk {
8284
/// - [customSignals] A map of custom signals you want to add to the context
8385
/// Returns: A Future that resolves to a [RealWorldContext] object. If ContextSDK wasn't running this might take upto [duration] seconds to resolve, otherwise this will resolve instantly.
8486
Future<RealWorldContext> fetchContext(String flowName, int duration,
85-
Map<String, dynamic>? customSignals) async {
87+
{Map<String, dynamic>? customSignals}) async {
8688
final contextId = await ContextSdkPlatform.instance
8789
.fetchContext(flowName, duration, customSignals);
8890
return RealWorldContext(ContextIdHolder(contextId));
@@ -95,7 +97,7 @@ class ContextSdk {
9597
/// - [customSignals] A map of custom signals you want to add to the context
9698
/// Returns: A [RealWorldContext] object. This will always resolve instantly, but might lead to an incomplete context object if the duration wasn't reached.
9799
Future<RealWorldContext> instantContext(String flowName, int duration,
98-
Map<String, dynamic>? customSignals) async {
100+
{Map<String, dynamic>? customSignals}) async {
99101
final contextId = await ContextSdkPlatform.instance
100102
.instantContext(flowName, duration, customSignals);
101103
return RealWorldContext(ContextIdHolder(contextId));

0 commit comments

Comments
 (0)