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