This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
/
DataSourcePlugin.ts
137 lines (109 loc) · 3.4 KB
/
DataSourcePlugin.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { ToastActions } from '../../components/Toast';
export interface IDataSourceOptions {
dependencies: (string | Object)[];
/** This would be variable storing the results */
dependables: (string | Object)[];
}
export interface ICalculated {
[key: string]: (state: Object, dependencies: IDictionary, prevState: Object) => any;
}
export interface IOptions<T> {
params: T;
}
export interface IDataSourcePlugin {
type: string;
defaultProperty: string;
connectionType: string;
_props: {
id: string,
dependencies: { [key: string]: string },
dependables: string[],
actions: string[],
params: IDictionary,
calculated: ICalculated
};
bind (actionClass: any): void;
updateDependencies (dependencies: IDictionary, args: IDictionary, callback: () => void): void;
getDependencies(): { [ key: string]: string };
getDependables(): string[];
getActions(): string[];
getParamKeys(): string[];
getParams(): IDictionary;
getCalculated(): ICalculated;
getConnection(): IStringDictionary;
}
export abstract class DataSourcePlugin<T> implements IDataSourcePlugin {
abstract type: string;
abstract defaultProperty: string;
connectionType: string = null;
_props = {
id: '',
dependencies: {} as any,
dependables: [],
actions: [ 'updateDependencies', 'failure', 'updateSelectedValues' ],
params: <T> {},
calculated: {}
};
/**
* @param {DataSourcePlugin} options - Options object
*/
constructor(options: IDictionary, protected connections: IConnections = {}) {
var props = this._props;
props.id = options.id;
props.dependencies = options.dependencies || [];
props.dependables = options.dependables || [];
props.actions.push.apply(props.actions, options.actions || []);
props.params = <T> (options.params || {});
props.calculated = options.calculated || {};
this.updateDependencies = this.updateDependencies.bind(this);
this.updateSelectedValues = this.updateSelectedValues.bind(this);
this.getCalculated = this.getCalculated.bind(this);
}
abstract updateDependencies (dependencies: IDictionary, args: IDictionary, callback: (result: any) => void): void;
abstract updateSelectedValues (dependencies: IDictionary, selectedValues: any, callback: (result: any) => void): void;
bind (actionClass: any) {
actionClass.type = this.type;
actionClass._props = this._props;
}
updateConnections(connections: IConnections) {
this.connections = connections;
}
getConnection(): IConnection {
return (this.connections && this.connections[this.connectionType]) || {};
}
/**
* @returns {string[]} Array of dependencies
*/
getDependencies() {
return this._props.dependencies;
}
getDependables() {
return this._props.dependables;
}
getActions() {
return this._props.actions;
}
getParamKeys() {
return Object.keys(this._props.params);
}
getParams(): T {
return this._props.params;
}
getCalculated() {
return this._props.calculated;
}
failure(error: any): void {
ToastActions.addToast({ text: this.errorToMessage(error) });
return error;
}
private errorToMessage(error: any): string {
if (!(error instanceof Error)) {
return error;
}
const message = (error as Error).message;
if (message === '[object ProgressEvent]') {
return 'There is a problem connecting to the internet.';
}
return `Error: ${message}`;
}
}