-
Notifications
You must be signed in to change notification settings - Fork 22
/
intent.ts
96 lines (82 loc) · 2.29 KB
/
intent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// Copyright 2023 DXOS.org
//
import type { MaybePromise } from '@dxos/util';
import type { Plugin } from '../PluginHost';
export type IntentData<T extends Record<string, any> = Record<string, any>> = T & {
/**
* The data from the result of the previous intent.
*/
result?: any;
};
/**
* An intent is an abstract description of an operation to be performed.
* Intents allow actions to be performed across plugins.
*/
export type Intent = {
/**
* Plugin ID.
* If specified, the intent will be sent explicitly to the plugin.
* Otherwise, the intent will be sent to all plugins, in order and the first to resolve a non-null value will be used.
*/
plugin?: string;
/**
* The action to perform.
*/
action: string;
/**
* Whether or not the intent is being undone.
*/
undo?: boolean;
/**
* Any data needed to perform the desired action.
*/
// TODO(burdon): Typed intents.
data?: IntentData;
};
export type IntentResult = {
/**
* The output of the action that was performed.
*
* If the intent is apart of a chain of intents, the data will be passed to the next intent.
*/
data?: any;
/**
* If provided, the action will be undoable.
*/
undoable?: {
/**
* Message to display to the user when indicating that the action can be undone.
*/
message: string;
/**
* Will be merged with the original intent data when firing the undo intent.
*/
data?: IntentData;
};
/**
* An error that occurred while performing the action.
*
* If the intent is apart of a chain of intents and an error occurs, the chain will be aborted.
*/
error?: Error;
/**
* Other intent chains to be triggered.
*/
intents?: Intent[][];
};
/**
* Trigger one or more intents to be sent.
* If multiple intents are specified, the result of each will be merged with the data to the next.
*
* @returns The result of the last intent.
*/
// TODO(burdon): Generic/typed intents.
export type IntentDispatcher = (intent: Intent | Intent[]) => Promise<IntentResult | void>;
/**
* Resolves an intent that was dispatched.
* If the intent is not handled, nothing should be returned.
*
* @returns The result of the intent.
*/
export type IntentResolver = (intent: Intent, plugins: Plugin[]) => MaybePromise<IntentResult | void>;