This repository has been archived by the owner on Sep 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranchset.ts
211 lines (187 loc) · 7.79 KB
/
branchset.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { SelectPropertyFinder } from './selectpropertyfinder';
import { MemSet } from './memarrayvisitor';
import { Expand, Property, Find, Order, Top, Skip, Filter, Select } from './expressions';
import { DataSet, IDataSet } from './dataset';
import { Utility } from './core';
class BranchContext {
source: IDataSet<any>;
branchName: string;
expressions: Array<any>;
constructor(source: IDataSet<any>, branchName: string, expressions: Array<any>) {
this.source = source;
this.branchName = branchName;
this.expressions = expressions;
}
}
class BrachsetUtility {
/**
* Eğer alınan sonuç array ise tek bir array olarak birleştirilir. Eğer object ise array olarak toplanıp dönülür.
* @param context
* @param response
*/
static getPropertyAndGuaranteeResultIsArray(propName: string, response: any) {
if (response == null) return [];
let results = [];
if (Array.isArray(response)) {
response.forEach((item) => {
let propValue = item[propName];
if (propValue == null) return true;
if (Array.isArray(propValue)) {
results.push.apply(results, propValue);
return true;
}
results.push(propValue);
return true;
});
return results;
}
let propValue = response[propName];
if (Array.isArray(propValue)) return propValue;
return [propValue];
}
static getSelectOrExpandByUsedProperies(expressions: Array<any>) {
let results = [];
expressions.forEach((exp) => {
let visitor = new SelectPropertyFinder();
visitor.visit(exp);
results.push.apply(results, visitor.getAsExpressions());
});
return results;
}
static isWillObject(expressions: Array<any>) {
if (expressions == null) return false;
return expressions.some(a => Utility.instanceof(a,Find));
}
}
export interface IBranchStrategy {
get(context: BranchContext): Promise<any>;
}
/**
* Bütün işlemleri expand üzerinde yapar.
*/
export class DirectStrategy implements IBranchStrategy {
private afterExpressions = [Find];
private escapeAfterExpressions(expressions: Array<any>) {
return expressions.filter(x => !this.afterExpressions.some(e => Utility.instanceof(x,e)));
}
get(context: BranchContext) {
let exps = this.escapeAfterExpressions(context.expressions);
let expsWithUsedExpressions = exps.concat(BrachsetUtility.getSelectOrExpandByUsedProperies(exps));
return context.source.get.apply(context.source, [new Expand([{
property: new Property(context.branchName),
expressions: expsWithUsedExpressions
}])]).then((response) => {
return new MemSet(BrachsetUtility.getPropertyAndGuaranteeResultIsArray(context.branchName, response), expsWithUsedExpressions).get();
})
}
}
/**
* Single Side Collector
* Toplama işleminden sonra order,top,skip,filter işlemlerini yapar.
*/
class SSCollectorStrategy implements IBranchStrategy {
private getExpandAndSelect(expressions: Array<any>) {
let items = [Select, Expand];
return expressions.filter(a => items.some(i => Utility.instanceof(a,i)));
}
get(context: BranchContext): Promise<any> {
// let exps = this.escapeAfterExpressions(context.expressions).concat(this.getDoubleSourceExpressions(context.expressions));
return context.source.get(new Expand([{
property: new Property(context.branchName),
expressions: this.getExpandAndSelect(context.expressions)
}]))
.then((response) => { //
let items = BrachsetUtility.getPropertyAndGuaranteeResultIsArray(context.branchName, response);
// let allExpressions = context.expressions.concat(this.getDoubleSourceExpressions(context.expressions));
return new MemSet(items || [], //hepsi alındıktan sonra filter,order,find,gibi diğer işlemler yapılıyor
context.expressions).then((r) => {
return r;
});
});
}
}
/**
* Double side filter collector.
* Toplama işleminden sonra ve source üzerinde order,top,skip,filter işlemlerini yapar.
*/
class DSFCollectorStrategy implements IBranchStrategy {
private afterExpressions = [Order, Top, Skip, Filter, Find];
private usesDoubleSourceExpressions = [Filter];
/**
*
* @param expressions
*/
private escapeAfterExpressions(expressions: Array<any>) {
return expressions.filter(x => !this.afterExpressions.some(e => Utility.instanceof(x,e)));
}
private getDoubleSourceExpressions(expressions: Array<any>) {
return expressions.filter(x => this.usesDoubleSourceExpressions.some(a => Utility.instanceof(x,a)));
}
get(context: BranchContext): Promise<any> {
let exps = this.escapeAfterExpressions(context.expressions).concat(this.getDoubleSourceExpressions(context.expressions));
return context.source.get(new Expand([{
property: new Property(context.branchName),
expressions: exps
}]))
.then((response) => { //
let items = BrachsetUtility.getPropertyAndGuaranteeResultIsArray(context.branchName, response);
// let allExpressions = context.expressions.concat(this.getDoubleSourceExpressions(context.expressions));
return new MemSet(items, //hepsi alındıktan sonra filter,order,find,gibi diğer işlemler yapılıyor
context.expressions).then((r) => {
return r;
});
});
}
}
class SmartStrategy implements IBranchStrategy {
getStrategy(context: BranchContext) {
if (context.source.getExpressions().some(x => Utility.instanceof(x,Find))) return new DirectStrategy();
return new SSCollectorStrategy();
}
get(context: BranchContext): Promise<any> {
return this.getStrategy(context).get(context);
}
}
/**
* Herhangi bir source üzerindeki objenin expend edilen propertsini tek bir source gibi kullanmak için kullanılır.
*
*/
export class Branchset<T> extends DataSet<T>{
/**
* Double side filter collector.
* Toplama işleminden sonra ve source üzerinde order,top,skip,filter işlemlerini yapar.
*/
static DoubleSideCollector: DSFCollectorStrategy = new DSFCollectorStrategy();
/**
* Single Side Collector
* Toplama işleminden sonra order,top,skip,filter işlemlerini yapar.
*/
static SingleSideCollector: SSCollectorStrategy = new SSCollectorStrategy();
/**
* Bütün işlemleri expand üzerinde yapar.
*/
static Direct: DirectStrategy = new DirectStrategy();
static SmartStrategy: SmartStrategy = new SmartStrategy();
constructor(private source: IDataSet<any>, private branchName: string, expressions: Array<any> = [], private strategy: IBranchStrategy = new SmartStrategy()) {
super(expressions)
}
get(...expressions: any[]): Promise<any> {
return this.getOn(expressions);
}
private getOn(expressions: any[]){
let items = this.expressions.concat.apply(this.expressions, expressions);
return this.strategy.get(new BranchContext(this.source, this.branchName,items));
}
add(element: T): Promise<any> {
return this.source.add(element);
}
delete(element: T): Promise<any> {
return this.source.delete(element);
}
update(element: T): Promise<any> {
return this.source.update(element);
}
query(...expressions: any[]): IDataSet<T> {
return new Branchset(this.source, this.branchName, this.expressions.concat.apply(this.expressions,expressions),this.strategy);
}
}