-
Notifications
You must be signed in to change notification settings - Fork 15
Add New Action
Vu Le edited this page Jul 11, 2024
·
5 revisions
Follow this instruction to add a new Ensemble action.
- Inside
/lib/action/
, creating a new dart class e.g.update_badge_count_action.dart
.
class UpdateBadgeCountAction extends EnsembleAction {
UpdateBadgeCountAction(this.count);
// dynamic as an input can be its original type (number) or an expression (string)
final dynamic count;
// This is how we create the Action from a data map.
// This will work in both YAML as well as Javascript.
factory UpdateBadgeCount.from({Map? payload}) {
dynamic count = payload?['count'];
if (count == null) {
throw LanguageError(
"${ActionType.updateBadgeCount.name} requires a count");
}
return UpdateBadgeCount(count);
}
// execute this action. We'll show how to invoke this later.
@override
Future<boolean> execute(BuildContext context, ScopeManager scopeManager) {
// we have to evaluate the expression (if any) at the execution time (and not before)
int? evalCount = Utils.optionalInt(scopeManager.dataContext.eval(count));
if (evalCount != null) {
// run your Action logic here
// if it is async return it here
}
return Future.value(true);
}
}
- add an entry to
ActionType
enum e.g. updateBadgeCount. This is the actual action name that can be invoked. - update EnsembleAction's fromActionType() function to wire the action's name to the Action class.
...
else if (actionType == ActionType.updateBadgeCount) {
return UpdateBadgeCount.from(payload: payload);
}
...
- in ActionInvokable's methods(), add your entry to _generateFromActionTypes(). This will call your Action's execute() method automatically.
abstract class ActionInvokable with Invokable {
...
@override
Map<String, Function> methods() {
return _generateFromActionTypes([
... other existing action types,
ActionType.updateBadgeCount,
]);
}
...
}
## Update schema to surface the new Action to the user.
- Go to Studio and update actionSchema.ts.