| @@ -1,16 +1,15 | |||
|
|
1 | 1 | { |
|
|
2 | 2 | "java.configuration.updateBuildConfiguration": "disabled", |
|
|
3 | "tslint.enable": true, | |
|
|
4 | 3 | "search.exclude": { |
|
|
5 | 4 | "**/node_modules": true, |
|
|
6 | 5 | "**/bower_components": true, |
|
|
7 | 6 | "/build": true |
|
|
8 | 7 | }, |
|
|
9 | 8 | "files.watcherExclude": { |
|
|
10 | 9 | "**/.git/objects/**": true, |
|
|
11 | 10 | "**/.git/subtree-cache/**": true, |
|
|
12 | 11 | "**/node_modules/**": true, |
|
|
13 | 12 | "/build": true |
|
|
14 | 13 | }, |
|
|
15 | 14 | "editor.minimap.enabled": false |
|
|
16 | 15 | } No newline at end of file |
| @@ -1,86 +1,93 | |||
|
|
1 |
export |
|
|
|
1 | export interface Constructor<T = {}> { | |
|
|
2 | new (...args: any[]): T; | |
|
|
3 | prototype: T; | |
|
|
4 | } | |
|
|
2 | 5 | |
|
|
3 | 6 | export type Factory<T = {}> = (...args: any[]) => T; |
|
|
4 | 7 | |
|
|
5 | 8 | export type Predicate<T = any> = (x: T) => boolean; |
|
|
6 | 9 | |
|
|
7 | 10 | export interface MapOf<T> { |
|
|
8 | 11 | [key: string]: T; |
|
|
9 | 12 | } |
|
|
10 | 13 | |
|
|
11 | 14 | export interface IDestroyable { |
|
|
12 | destroy(); | |
|
|
15 | destroy(): void; | |
|
|
16 | } | |
|
|
17 | ||
|
|
18 | export interface IRemovable { | |
|
|
19 | remove(): void; | |
|
|
13 | 20 | } |
|
|
14 | 21 | |
|
|
15 | 22 | export interface ICancellation { |
|
|
16 | 23 | throwIfRequested(): void; |
|
|
17 | 24 | isRequested(): boolean; |
|
|
18 | 25 | isSupported(): boolean; |
|
|
19 | 26 | register(cb: (e: any) => void): IDestroyable; |
|
|
20 | 27 | } |
|
|
21 | 28 | |
|
|
22 | 29 | /** |
|
|
23 | 30 | * ΠΠ½ΡΠ΅ΡΡΠ΅ΠΉΡ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°ΡΡΠΈΠΉ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΡ Π°ΠΊΡΠΈΠ²Π°ΡΠΈΡ |
|
|
24 | 31 | */ |
|
|
25 | 32 | export interface IActivatable { |
|
|
26 | 33 | /** |
|
|
27 | 34 | * @returns Boolean indicates the current state |
|
|
28 | 35 | */ |
|
|
29 | 36 | isActive(): boolean; |
|
|
30 | 37 | |
|
|
31 | 38 | /** |
|
|
32 | 39 | * Starts the component activation |
|
|
33 | 40 | * @param ct cancellation token for this operation |
|
|
34 | 41 | */ |
|
|
35 | 42 | activate(ct?: ICancellation): Promise<void>; |
|
|
36 | 43 | |
|
|
37 | 44 | /** |
|
|
38 | 45 | * Starts the component deactivation |
|
|
39 | 46 | * @param ct cancellation token for this operation |
|
|
40 | 47 | */ |
|
|
41 | 48 | deactivate(ct?: ICancellation): Promise<void>; |
|
|
42 | 49 | |
|
|
43 | 50 | /** |
|
|
44 | 51 | * Sets the activation controller for this component |
|
|
45 | 52 | * @param controller The activation controller |
|
|
46 | 53 | * |
|
|
47 | 54 | * Activation controller checks whether this component |
|
|
48 | 55 | * can be activated and manages the active state of the |
|
|
49 | 56 | * component |
|
|
50 | 57 | */ |
|
|
51 | 58 | setActivationController(controller: IActivationController); |
|
|
52 | 59 | |
|
|
53 | 60 | /** |
|
|
54 | 61 | * Gets the current activation controller for this component |
|
|
55 | 62 | */ |
|
|
56 | 63 | getActivationController(): IActivationController; |
|
|
57 | 64 | } |
|
|
58 | 65 | |
|
|
59 | 66 | export interface IActivationController { |
|
|
60 | 67 | activating(component: IActivatable, ct?: ICancellation): Promise<void>; |
|
|
61 | 68 | |
|
|
62 | 69 | activated(component: IActivatable, ct?: ICancellation): Promise<void>; |
|
|
63 | 70 | |
|
|
64 | 71 | deactivating(component: IActivatable, ct?: ICancellation): Promise<void>; |
|
|
65 | 72 | |
|
|
66 | 73 | deactivated(component: IActivatable, ct?: ICancellation): Promise<void>; |
|
|
67 | 74 | |
|
|
68 | 75 | deactivate(ct?: ICancellation): Promise<void>; |
|
|
69 | 76 | |
|
|
70 | 77 | activate(component: IActivatable, ct?: ICancellation): Promise<void>; |
|
|
71 | 78 | |
|
|
72 | 79 | getActive(): IActivatable; |
|
|
73 | 80 | } |
|
|
74 | 81 | |
|
|
75 | 82 | export interface IAsyncComponent { |
|
|
76 | 83 | getCompletion(): Promise<void>; |
|
|
77 | 84 | } |
|
|
78 | 85 | |
|
|
79 | 86 | export interface ICancellable { |
|
|
80 | 87 | cancel(reason?: any): void; |
|
|
81 | 88 | } |
|
|
82 | 89 | |
|
|
83 | 90 | export interface IObservable<T> { |
|
|
84 | 91 | on(next: (x: T) => void, error?: (e: any) => void, complete?: () => void): IDestroyable; |
|
|
85 | 92 | next(ct?: ICancellation): Promise<T>; |
|
|
86 | 93 | } |
| @@ -1,360 +1,428 | |||
|
|
1 | import { ICancellable } from "./interfaces"; | |
|
|
1 | import { ICancellable, Constructor } from "./interfaces"; | |
|
|
2 | 2 | |
|
|
3 | 3 | let _nextOid = 0; |
|
|
4 | 4 | const _oid = typeof Symbol === "function" ? |
|
|
5 | 5 | Symbol("__implab__oid__") : |
|
|
6 | 6 | "__implab__oid__"; |
|
|
7 | 7 | |
|
|
8 | declare const window: any; | |
|
|
9 | declare const global: any; | |
|
|
10 | ||
|
|
11 | 8 | export function oid(instance: object): string { |
|
|
12 | 9 | if (isNull(instance)) |
|
|
13 | 10 | return null; |
|
|
14 | 11 | |
|
|
15 | 12 | if (_oid in instance) |
|
|
16 | 13 | return instance[_oid]; |
|
|
17 | 14 | else |
|
|
18 | 15 | return (instance[_oid] = "oid_" + (++_nextOid)); |
|
|
19 | 16 | } |
|
|
20 | 17 | |
|
|
21 | export function argumentNotNull(arg, name) { | |
|
|
18 | export function argumentNotNull(arg: any, name: string) { | |
|
|
22 | 19 | if (arg === null || arg === undefined) |
|
|
23 | 20 | throw new Error("The argument " + name + " can't be null or undefined"); |
|
|
24 | 21 | } |
|
|
25 | 22 | |
|
|
26 | export function argumentNotEmptyString(arg, name) { | |
|
|
23 | export function argumentNotEmptyString(arg: any, name: string) { | |
|
|
27 | 24 | if (typeof (arg) !== "string" || !arg.length) |
|
|
28 | 25 | throw new Error("The argument '" + name + "' must be a not empty string"); |
|
|
29 | 26 | } |
|
|
30 | 27 | |
|
|
31 | export function argumentNotEmptyArray(arg, name) { | |
|
|
28 | export function argumentNotEmptyArray(arg: any, name: string) { | |
|
|
32 | 29 | if (!(arg instanceof Array) || !arg.length) |
|
|
33 | 30 | throw new Error("The argument '" + name + "' must be a not empty array"); |
|
|
34 | 31 | } |
|
|
35 | 32 | |
|
|
36 | export function argumentOfType(arg, type, name) { | |
|
|
33 | export function argumentOfType(arg: any, type: Constructor<{}>, name: string) { | |
|
|
37 | 34 | if (!(arg instanceof type)) |
|
|
38 | 35 | throw new Error("The argument '" + name + "' type doesn't match"); |
|
|
39 | 36 | } |
|
|
40 | 37 | |
|
|
41 |
export function isNull( |
|
|
|
42 |
return ( |
|
|
|
38 | export function isNull(val: any) { | |
|
|
39 | return (val === null || val === undefined); | |
|
|
43 | 40 | } |
|
|
44 | 41 | |
|
|
45 |
export function isPrimitive( |
|
|
|
46 |
return ( |
|
|
|
47 |
typeof ( |
|
|
|
42 | export function isPrimitive(val: any): val is string | number | boolean | undefined | null { | |
|
|
43 | return (val === null || val === undefined || typeof (val) === "string" || | |
|
|
44 | typeof (val) === "number" || typeof (val) === "boolean"); | |
|
|
48 | 45 | } |
|
|
49 | 46 | |
|
|
50 |
export function isInteger( |
|
|
|
51 |
return parseInt( |
|
|
|
47 | export function isInteger(val: any): val is number { | |
|
|
48 | return parseInt(val, 10) === val; | |
|
|
52 | 49 | } |
|
|
53 | 50 | |
|
|
54 |
export function isNumber( |
|
|
|
55 |
return parseFloat( |
|
|
|
51 | export function isNumber(val: any): val is number { | |
|
|
52 | return parseFloat(val) === val; | |
|
|
56 | 53 | } |
|
|
57 | 54 | |
|
|
58 | export function isString(val): val is string { | |
|
|
55 | export function isString(val: any): val is string { | |
|
|
59 | 56 | return typeof (val) === "string" || val instanceof String; |
|
|
60 | 57 | } |
|
|
61 | 58 | |
|
|
62 | export function isPromise(val): val is PromiseLike<any> { | |
|
|
59 | export function isPromise(val: any): val is PromiseLike<any> { | |
|
|
63 | 60 | return val && typeof val.then === "function"; |
|
|
64 | 61 | } |
|
|
65 | 62 | |
|
|
66 | export function isCancellable(val): val is ICancellable { | |
|
|
63 | export function isCancellable(val: any): val is ICancellable { | |
|
|
67 | 64 | return val && typeof val.cancel === "function"; |
|
|
68 | 65 | } |
|
|
69 | 66 | |
|
|
70 |
export function isNullOrEmptyString( |
|
|
|
71 |
if ( |
|
|
|
72 |
((typeof ( |
|
|
|
67 | export function isNullOrEmptyString(val: any): val is string | null | undefined { | |
|
|
68 | if (val === null || val === undefined || | |
|
|
69 | ((typeof (val) === "string" || val instanceof String) && val.length === 0)) | |
|
|
73 | 70 | return true; |
|
|
74 | 71 | } |
|
|
75 | 72 | |
|
|
76 | export function isNotEmptyArray(arg): arg is Array<any> { | |
|
|
73 | export function isNotEmptyArray(arg: any): arg is Array<any> { | |
|
|
77 | 74 | return (arg instanceof Array && arg.length > 0); |
|
|
78 | 75 | } |
|
|
79 | 76 | |
|
|
80 | 77 | function _isStrictMode() { |
|
|
81 | 78 | return !this; |
|
|
82 | 79 | } |
|
|
83 | 80 | |
|
|
84 | 81 | function _getNonStrictGlobal() { |
|
|
85 | 82 | return this; |
|
|
86 | 83 | } |
|
|
87 | 84 | |
|
|
88 | 85 | export function getGlobal() { |
|
|
89 | 86 | // in es3 we can't use indirect call to eval, since it will |
|
|
90 | 87 | // be executed in the current call context. |
|
|
91 | 88 | if (!_isStrictMode()) { |
|
|
92 | 89 | return _getNonStrictGlobal(); |
|
|
93 | 90 | } else { |
|
|
94 | 91 | // tslint:disable-next-line:no-eval |
|
|
95 | 92 | return eval.call(null, "this"); |
|
|
96 | 93 | } |
|
|
97 | 94 | } |
|
|
98 | 95 | |
|
|
99 | 96 | export function get(member: string, context?: object) { |
|
|
100 | 97 | argumentNotEmptyString(member, "member"); |
|
|
101 | 98 | let that = context || getGlobal(); |
|
|
102 | 99 | const parts = member.split("."); |
|
|
103 | 100 | for (const m of parts) { |
|
|
104 | 101 | if (!m) |
|
|
105 | 102 | continue; |
|
|
106 | 103 | if (isNull(that = that[m])) |
|
|
107 | 104 | break; |
|
|
108 | 105 | } |
|
|
109 | 106 | return that; |
|
|
110 | 107 | } |
|
|
111 | 108 | |
|
|
112 | 109 | /** |
|
|
113 | 110 | * ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΌΠ°ΡΡΠΈΠ²Π°, ΠΎΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΡΡΡ, ΠΊΠΎΠ³Π΄Π° |
|
|
114 | 111 | * Π»ΠΈΠ±ΠΎ Π΄ΠΎΡΡΠΈΠ³Π½ΡΡ ΠΊΠΎΠ½Π΅Ρ ΠΌΠ°ΡΡΠΈΠ²Π°, Π»ΠΈΠ±ΠΎ ΡΡΠ½ΠΊΡΠΈΡ <c>cb</c> Π²Π΅ΡΠ½ΡΠ»Π° |
|
|
115 | 112 | * Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅. |
|
|
116 | 113 | * |
|
|
117 | 114 | * @param {Array | Object} obj ΠΌΠ°ΡΡΠΈΠ² ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² Π΄Π»Ρ ΠΏΡΠΎΡΠΌΠΎΡΡΠ° |
|
|
118 | 115 | * @param {Function} cb ΡΡΠ½ΠΊΡΠΈΡ, Π²ΡΠ·ΡΠ²Π°Π΅ΠΌΠ°Ρ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° |
|
|
119 | 116 | * @param {Object} thisArg Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ΠΎ Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ |
|
|
120 | 117 | * <c>this</c> Π² <c>cb</c>. |
|
|
121 | 118 | * @returns Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠ·ΠΎΠ²Π° ΡΡΠ½ΠΊΡΠΈΠΈ <c>cb</c>, Π»ΠΈΠ±ΠΎ <c>undefined</c> |
|
|
122 | 119 | * Π΅ΡΠ»ΠΈ Π΄ΠΎΡΡΠΈΠ³Π½ΡΡ ΠΊΠΎΠ½Π΅Ρ ΠΌΠ°ΡΡΠΈΠ²Π°. |
|
|
123 | 120 | */ |
|
|
124 | 121 | export function each(obj, cb, thisArg?) { |
|
|
125 | 122 | argumentNotNull(cb, "cb"); |
|
|
126 | 123 | if (obj instanceof Array) { |
|
|
127 | 124 | for (let i = 0; i < obj.length; i++) { |
|
|
128 | 125 | const x = cb.call(thisArg, obj[i], i); |
|
|
129 | 126 | if (x !== undefined) |
|
|
130 | 127 | return x; |
|
|
131 | 128 | } |
|
|
132 | 129 | } else { |
|
|
133 | 130 | const keys = Object.keys(obj); |
|
|
134 | 131 | for (const k of keys) { |
|
|
135 | 132 | const x = cb.call(thisArg, obj[k], k); |
|
|
136 | 133 | if (x !== undefined) |
|
|
137 | 134 | return x; |
|
|
138 | 135 | } |
|
|
139 | 136 | } |
|
|
140 | 137 | } |
|
|
141 | 138 | |
|
|
142 | 139 | /** Copies property values from a source object to the destination and returns |
|
|
143 | 140 | * the destination onject. |
|
|
144 | 141 | * |
|
|
145 | 142 | * @param dest The destination object into which properties from the source |
|
|
146 | 143 | * object will be copied. |
|
|
147 | 144 | * @param source The source of values which will be copied to the destination |
|
|
148 | 145 | * object. |
|
|
149 | 146 | * @param template An optional parameter specifies which properties should be |
|
|
150 | 147 | * copied from the source and how to map them to the destination. If the |
|
|
151 | 148 | * template is an array it contains the list of property names to copy from the |
|
|
152 | 149 | * source to the destination. In case of object the templates contains the map |
|
|
153 | 150 | * where keys are property names in the source and the values are property |
|
|
154 | 151 | * names in the destination object. If the template isn't specified then the |
|
|
155 | 152 | * own properties of the source are entirely copied to the destination. |
|
|
156 | 153 | * |
|
|
157 | 154 | */ |
|
|
158 | 155 | export function mixin<T, S>(dest: T, source: S, template?: string[] | object): T & S { |
|
|
159 | 156 | argumentNotNull(dest, "to"); |
|
|
160 | 157 | const _res = dest as T & S; |
|
|
161 | 158 | |
|
|
162 | 159 | if (isPrimitive(source)) |
|
|
163 | 160 | return _res; |
|
|
164 | 161 | |
|
|
165 | 162 | if (template instanceof Array) { |
|
|
166 | 163 | for (const p of template) { |
|
|
167 | 164 | if (p in source) |
|
|
168 | 165 | _res[p] = source[p]; |
|
|
169 | 166 | } |
|
|
170 | 167 | } else if (template) { |
|
|
171 | 168 | const keys = Object.keys(source); |
|
|
172 | 169 | for (const p of keys) { |
|
|
173 | 170 | if (p in template) |
|
|
174 | 171 | _res[template[p]] = source[p]; |
|
|
175 | 172 | } |
|
|
176 | 173 | } else { |
|
|
177 | 174 | const keys = Object.keys(source); |
|
|
178 | 175 | for (const p of keys) |
|
|
179 | 176 | _res[p] = source[p]; |
|
|
180 | 177 | } |
|
|
181 | 178 | |
|
|
182 | 179 | return _res; |
|
|
183 | 180 | } |
|
|
184 | 181 | |
|
|
185 | 182 | /** Wraps the specified function to emulate an asynchronous execution. |
|
|
186 | 183 | * @param{Object} thisArg [Optional] Object which will be passed as 'this' to the function. |
|
|
187 | 184 | * @param{Function|String} fn [Required] Function wich will be wrapped. |
|
|
188 | 185 | */ |
|
|
189 | 186 | export function async(_fn: (...args: any[]) => any, thisArg): (...args: any[]) => PromiseLike<any> { |
|
|
190 | 187 | let fn = _fn; |
|
|
191 | 188 | |
|
|
192 | 189 | if (arguments.length === 2 && !(fn instanceof Function)) |
|
|
193 | 190 | fn = thisArg[fn]; |
|
|
194 | 191 | |
|
|
195 | 192 | if (fn == null) |
|
|
196 | 193 | throw new Error("The function must be specified"); |
|
|
197 | 194 | |
|
|
198 | 195 | function wrapresult(x, e?): PromiseLike<any> { |
|
|
199 | 196 | if (e) { |
|
|
200 | 197 | return { |
|
|
201 | 198 | then(cb, eb) { |
|
|
202 | 199 | try { |
|
|
203 | 200 | return eb ? wrapresult(eb(e)) : this; |
|
|
204 | 201 | } catch (e2) { |
|
|
205 | 202 | return wrapresult(null, e2); |
|
|
206 | 203 | } |
|
|
207 | 204 | } |
|
|
208 | 205 | }; |
|
|
209 | 206 | } else { |
|
|
210 | 207 | if (x && x.then) |
|
|
211 | 208 | return x; |
|
|
212 | 209 | return { |
|
|
213 | 210 | then(cb) { |
|
|
214 | 211 | try { |
|
|
215 | 212 | return cb ? wrapresult(cb(x)) : this; |
|
|
216 | 213 | } catch (e2) { |
|
|
217 | 214 | return wrapresult(e2); |
|
|
218 | 215 | } |
|
|
219 | 216 | } |
|
|
220 | 217 | }; |
|
|
221 | 218 | } |
|
|
222 | 219 | } |
|
|
223 | 220 | |
|
|
224 | 221 | return (...args) => { |
|
|
225 | 222 | try { |
|
|
226 | 223 | return wrapresult(fn.apply(thisArg, args)); |
|
|
227 | 224 | } catch (e) { |
|
|
228 | 225 | return wrapresult(null, e); |
|
|
229 | 226 | } |
|
|
230 | 227 | }; |
|
|
231 | 228 | } |
|
|
232 | 229 | |
|
|
233 | 230 | type _AnyFn = (...args) => any; |
|
|
234 | 231 | |
|
|
235 | 232 | export function delegate<T, K extends keyof T>(target: T, _method: (K | _AnyFn)) { |
|
|
236 | 233 | let method; |
|
|
237 | 234 | |
|
|
238 | 235 | if (!(_method instanceof Function)) { |
|
|
239 | 236 | argumentNotNull(target, "target"); |
|
|
240 | 237 | method = target[_method]; |
|
|
241 | 238 | if (!(method instanceof Function)) |
|
|
242 | 239 | throw new Error("'method' argument must be a Function or a method name"); |
|
|
243 | 240 | } else { |
|
|
244 | 241 | method = _method; |
|
|
245 | 242 | } |
|
|
246 | 243 | |
|
|
247 | 244 | return (...args) => { |
|
|
248 | 245 | return method.apply(target, args); |
|
|
249 | 246 | }; |
|
|
250 | 247 | } |
|
|
251 | 248 | |
|
|
252 | 249 | /** |
|
|
253 | 250 | * ΠΠ»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΌΠ°ΡΡΠΈΠ²Π° Π²ΡΠ·ΡΠ²Π°Π΅Ρ ΡΠΊΠ°Π·Π°Π½Π½ΡΡ ΡΡΠ½ΠΊΡΠΈΡ ΠΈ ΡΠΎΡ ΡΠ°Π½ΡΠ΅Ρ |
|
|
254 | 251 | * Π²ΠΎΠ·Π²ΡΠ°ΡΠ΅Π½Π½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². |
|
|
255 | 252 | * |
|
|
256 | 253 | * @remarks cb ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠΏΠΎΠ»Π½ΡΡΡΡΡ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΠΎΠ΄Π½ΠΎΠ²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎ Π±ΡΠ΄Π΅Ρ |
|
|
257 | 254 | * ΡΠΎΠ»ΡΠΊΠΎ ΠΎΠ΄Π½Π° ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ. |
|
|
258 | 255 | * |
|
|
259 | 256 | * @async |
|
|
260 | 257 | */ |
|
|
261 | 258 | export function pmap(items, cb) { |
|
|
262 | 259 | argumentNotNull(cb, "cb"); |
|
|
263 | 260 | |
|
|
264 | 261 | if (isPromise(items)) |
|
|
265 | 262 | return items.then(data => pmap(data, cb)); |
|
|
266 | 263 | |
|
|
267 | 264 | if (isNull(items) || !items.length) |
|
|
268 | 265 | return items; |
|
|
269 | 266 | |
|
|
270 | 267 | let i = 0; |
|
|
271 | 268 | const result = []; |
|
|
272 | 269 | |
|
|
273 | 270 | function next() { |
|
|
274 | 271 | let r; |
|
|
275 | 272 | let ri; |
|
|
276 | 273 | |
|
|
277 | 274 | function chain(x) { |
|
|
278 | 275 | result[ri] = x; |
|
|
279 | 276 | return next(); |
|
|
280 | 277 | } |
|
|
281 | 278 | |
|
|
282 | 279 | while (i < items.length) { |
|
|
283 | 280 | r = cb(items[i], i); |
|
|
284 | 281 | ri = i; |
|
|
285 | 282 | i++; |
|
|
286 | 283 | if (isPromise(r)) { |
|
|
287 | 284 | return r.then(chain); |
|
|
288 | 285 | } else { |
|
|
289 | 286 | result[ri] = r; |
|
|
290 | 287 | } |
|
|
291 | 288 | } |
|
|
292 | 289 | return result; |
|
|
293 | 290 | } |
|
|
294 | 291 | |
|
|
295 | 292 | return next(); |
|
|
296 | 293 | } |
|
|
297 | 294 | |
|
|
298 | 295 | export function pfor(items, cb) { |
|
|
299 | 296 | argumentNotNull(cb, "cb"); |
|
|
300 | 297 | |
|
|
301 | 298 | if (isPromise(items)) |
|
|
302 | 299 | return items.then(data => { |
|
|
303 | 300 | return pmap(data, cb); |
|
|
304 | 301 | }); |
|
|
305 | 302 | |
|
|
306 | 303 | if (isNull(items) || !items.length) |
|
|
307 | 304 | return items; |
|
|
308 | 305 | |
|
|
309 | 306 | let i = 0; |
|
|
310 | 307 | |
|
|
311 | 308 | function next() { |
|
|
312 | 309 | while (i < items.length) { |
|
|
313 | 310 | const r = cb(items[i], i); |
|
|
314 | 311 | i++; |
|
|
315 | 312 | if (isPromise(r)) |
|
|
316 | 313 | return r.then(next); |
|
|
317 | 314 | } |
|
|
318 | 315 | } |
|
|
319 | 316 | |
|
|
320 | 317 | return next(); |
|
|
321 | 318 | } |
|
|
322 | 319 | |
|
|
320 | export function first<T>(sequence: ArrayLike<T>): T; | |
|
|
321 | export function first<T>(sequence: PromiseLike<ArrayLike<T>>): PromiseLike<T>; | |
|
|
322 | export function first<T>( | |
|
|
323 | sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>, | |
|
|
324 | cb: (x: T) => void, | |
|
|
325 | err?: (x: Error) => void | |
|
|
326 | ): void; | |
|
|
323 | 327 |
|
|
|
324 | 328 | * ΠΡΠ±ΠΈΡΠ°Π΅Ρ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΈΠ· ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎΡΡΠΈ, ΠΈΠ»ΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, Π΅ΡΠ»ΠΈ Π² |
|
|
325 | 329 | * ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ° ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΎΠ½ΠΎ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π²Π΅ΡΠ½ΡΡΡ ΠΌΠ°ΡΡΠΈΠ². |
|
|
326 | 330 | * |
|
|
327 | 331 | * @param {Function} cb ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, Π΅ΠΌΡ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ ΠΏΠ΅ΡΠ²ΡΠΉ |
|
|
328 | 332 | * ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎΡΡΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ Π° |
|
|
329 | 333 | * @param {Function} err ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΡ, Π΅ΡΠ»ΠΈ ΠΌΠ°ΡΡΠΈΠ² ΠΏΡΡΡΠΎΠΉ, Π»ΠΈΠ±ΠΎ |
|
|
330 | 334 | * Π½Π΅ ΠΌΠ°ΡΡΠΈΠ² |
|
|
331 | 335 | * |
|
|
332 | 336 | * @remarks ΠΡΠ»ΠΈ Π½Π΅ ΡΠΊΠ°Π·Π°Π½Ρ Π½ΠΈ cb Π½ΠΈ err, ΡΠΎΠ³Π΄Π° ΡΡΠ½ΠΊΡΠΈΡ Π²Π΅ΡΠ½Π΅Ρ Π»ΠΈΠ±ΠΎ |
|
|
333 | 337 | * ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, Π»ΠΈΠ±ΠΎ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ. |
|
|
334 | 338 | * @async |
|
|
335 | 339 | */ |
|
|
336 | export function first(sequence, cb: (x) => any, err: (x) => any) { | |
|
|
337 | if (sequence) { | |
|
|
338 | if (isPromise(sequence)) { | |
|
|
339 | return sequence.then(res => first(res, cb, err)); | |
|
|
340 | } else if (sequence && "length" in sequence) { | |
|
|
341 | if (sequence.length === 0) { | |
|
|
342 | if (err) | |
|
|
343 | return err(new Error("The sequence is empty")); | |
|
|
344 | else | |
|
|
345 | throw new Error("The sequence is empty"); | |
|
|
346 | } | |
|
|
347 | return cb ? cb(sequence[0]) : sequence[0]; | |
|
|
340 | export function first<T>( | |
|
|
341 | sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>, | |
|
|
342 | cb?: (x: T) => void, | |
|
|
343 | err?: (x: Error) => void | |
|
|
344 | ) { | |
|
|
345 | if (isPromise(sequence)) { | |
|
|
346 | return sequence.then(res => first(res, cb, err)); | |
|
|
347 | } else if (sequence && "length" in sequence) { | |
|
|
348 | if (sequence.length === 0) { | |
|
|
349 | if (err) | |
|
|
350 | return err(new Error("The sequence is empty")); | |
|
|
351 | else | |
|
|
352 | throw new Error("The sequence is empty"); | |
|
|
353 | } else if (cb) { | |
|
|
354 | cb(sequence[0]); | |
|
|
355 | } else { | |
|
|
356 | return sequence[0]; | |
|
|
348 | 357 | } |
|
|
358 | } else { | |
|
|
359 | if (err) | |
|
|
360 | err(new Error("The sequence is required")); | |
|
|
361 | else | |
|
|
362 | throw new Error("The sequence is required"); | |
|
|
349 | 363 | } |
|
|
350 | ||
|
|
351 | if (err) | |
|
|
352 | return err(new Error("The sequence is required")); | |
|
|
353 | else | |
|
|
354 | throw new Error("The sequence is required"); | |
|
|
355 | 364 | } |
|
|
356 | 365 | |
|
|
357 |
export function |
|
|
|
366 | export function firstWhere<T>( | |
|
|
367 | sequence: ArrayLike<T>, | |
|
|
368 | predicate: (x: T) => boolean | |
|
|
369 | ): T; | |
|
|
370 | export function firstWhere<T>( | |
|
|
371 | sequence: PromiseLike<ArrayLike<T>>, | |
|
|
372 | predicate: (x: T) => boolean | |
|
|
373 | ): PromiseLike<T>; | |
|
|
374 | export function firstWhere<T>( | |
|
|
375 | sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>, | |
|
|
376 | predicate: (x: T) => boolean, | |
|
|
377 | cb: (x: T) => void, | |
|
|
378 | err?: (x: Error) => void | |
|
|
379 | ): void; | |
|
|
380 | ||
|
|
381 | export function firstWhere<T>( | |
|
|
382 | sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>, | |
|
|
383 | predicate?: (x: T) => boolean, | |
|
|
384 | cb?: (x: T) => void, | |
|
|
385 | err?: (x: Error) => void | |
|
|
386 | ) { | |
|
|
387 | if (isPromise(sequence)) { | |
|
|
388 | return sequence.then(res => firstWhere(res, predicate, cb, err)); | |
|
|
389 | } else if (sequence && "length" in sequence) { | |
|
|
390 | if (sequence.length === 0) { | |
|
|
391 | if (err) | |
|
|
392 | err(new Error("The sequence is empty")); | |
|
|
393 | else | |
|
|
394 | throw new Error("The sequence is empty"); | |
|
|
395 | } else { | |
|
|
396 | if (!predicate) { | |
|
|
397 | return cb ? cb(sequence[0]) : sequence[0]; | |
|
|
398 | } else { | |
|
|
399 | for (let i = 0; i < sequence.length; i++) { | |
|
|
400 | const v = sequence[i]; | |
|
|
401 | if (predicate(v)) | |
|
|
402 | return cb ? cb(v) : v; | |
|
|
403 | } | |
|
|
404 | if (err) | |
|
|
405 | err(new Error("The sequence doesn't contain matching items")); | |
|
|
406 | else | |
|
|
407 | throw new Error("The sequence doesn't contain matching items"); | |
|
|
408 | } | |
|
|
409 | } | |
|
|
410 | } else { | |
|
|
411 | if (err) | |
|
|
412 | err(new Error("The sequence is required")); | |
|
|
413 | else | |
|
|
414 | throw new Error("The sequence is required"); | |
|
|
415 | } | |
|
|
416 | } | |
|
|
417 | ||
|
|
418 | export function destroy(d: any) { | |
|
|
358 | 419 | if (d && "destroy" in d) |
|
|
359 | 420 | d.destroy(); |
|
|
360 | 421 | } |
|
|
422 | ||
|
|
423 | /** | |
|
|
424 | * Used to mark that the async operation isn't awaited intentionally. | |
|
|
425 | * @param p The promise which represents the async operation. | |
|
|
426 | */ | |
|
|
427 | export function nowait(p: Promise<any>) { | |
|
|
428 | } | |
| @@ -1,40 +1,42 | |||
|
|
1 | 1 | { |
|
|
2 | 2 | "extends": "tslint:recommended", |
|
|
3 | 3 | "rules": { |
|
|
4 | 4 | "align": [ |
|
|
5 | 5 | true, |
|
|
6 | 6 | "parameters", |
|
|
7 | 7 | "statements" |
|
|
8 | 8 | ], |
|
|
9 | 9 | "interface-name": [false], |
|
|
10 | 10 | "max-line-length": [ true, 185 ], |
|
|
11 | 11 | "member-access": false, |
|
|
12 | 12 | "member-ordering": [ |
|
|
13 | 13 | false, |
|
|
14 | 14 | "variables-before-functions" |
|
|
15 | 15 | ], |
|
|
16 | 16 | "no-bitwise": false, |
|
|
17 | 17 | "no-empty": false, |
|
|
18 | 18 | "no-namespace": false, |
|
|
19 | "no-string-literal": false, | |
|
|
20 | 19 | "ordered-imports": false, |
|
|
20 | "no-return-await": true, | |
|
|
21 | "no-floating-promises": true, | |
|
|
22 | "prefer-for-of": false, | |
|
|
21 | 23 | "one-line": [ |
|
|
22 | 24 | true, |
|
|
23 | 25 | "check-open-brace", |
|
|
24 | 26 | "check-catch", |
|
|
25 | 27 | "check-whitespace" |
|
|
26 | 28 | ], |
|
|
27 | 29 | "object-literal-sort-keys": false, |
|
|
28 | 30 | "trailing-comma": [ |
|
|
29 | 31 | true, |
|
|
30 | 32 | { |
|
|
31 | 33 | "singleline": "never", |
|
|
32 | 34 | "multiline": "never" |
|
|
33 | 35 | } |
|
|
34 | 36 | ], |
|
|
35 | 37 | "variable-name": false, |
|
|
36 | 38 | "curly": false, |
|
|
37 | 39 | "array-type": false, |
|
|
38 | 40 | "arrow-parens": [true, "ban-single-arg-parens"] |
|
|
39 | 41 | } |
|
|
40 | 42 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now
