##// END OF EJS Templates
Added childContainer service to container services, ServiceContaier is destroyable, fixed browser Uuid version
Added childContainer service to container services, ServiceContaier is destroyable, fixed browser Uuid version

File last commit:

r142:be7edf08a115 v1.4.0-rc3 default
r146:f3f5c56d3b3e v1.4.0-rc5 default
Show More
safe.ts
502 lines | 15.9 KiB | video/mp2t | TypeScriptLexer
cin
Completely removed IoC annotations...
r135 import { ICancellable, Constructor, IDestroyable, PromiseOrValue } from "./interfaces";
cin
Added safe.delay...
r76 import { Cancellation } from "./Cancellation";
cin
Minor fixes and improvements...
r71
cin
changed the project structure
r49 let _nextOid = 0;
const _oid = typeof Symbol === "function" ?
Symbol("__implab__oid__") :
"__implab__oid__";
cin
Completely removed IoC annotations...
r135 export function oid(instance: null | undefined): undefined;
export function oid(instance: NonNullable<any>): string;
cin
working on IoC configuration
r114 export function oid(instance: any): string | undefined {
cin
changed the project structure
r49 if (isNull(instance))
cin
working on IoC configuration
r114 return undefined;
cin
changed the project structure
r49
if (_oid in instance)
return instance[_oid];
else
return (instance[_oid] = "oid_" + (++_nextOid));
}
cin
working on IoC configuration
r114 export function keys<T>(arg: T): (Extract<keyof T, string>)[] {
return isObject(arg) && arg ? Object.keys(arg) as (Extract<keyof T, string>)[] : [];
}
cin
corrected code to support ts strict mode...
r115 export function isKeyof<T>(k: string, target: T): k is Extract<keyof T, string> {
return target && typeof target === "object" && k in target;
}
cin
added safe.firstWhere
r75 export function argumentNotNull(arg: any, name: string) {
cin
changed the project structure
r49 if (arg === null || arg === undefined)
throw new Error("The argument " + name + " can't be null or undefined");
}
cin
added safe.firstWhere
r75 export function argumentNotEmptyString(arg: any, name: string) {
cin
changed the project structure
r49 if (typeof (arg) !== "string" || !arg.length)
throw new Error("The argument '" + name + "' must be a not empty string");
}
cin
added safe.firstWhere
r75 export function argumentNotEmptyArray(arg: any, name: string) {
cin
changed the project structure
r49 if (!(arg instanceof Array) || !arg.length)
throw new Error("The argument '" + name + "' must be a not empty array");
}
cin
added safe.firstWhere
r75 export function argumentOfType(arg: any, type: Constructor<{}>, name: string) {
cin
changed the project structure
r49 if (!(arg instanceof type))
throw new Error("The argument '" + name + "' type doesn't match");
}
cin
working on IoC configuration
r114 export function isObject(val: any): val is object {
return typeof val === "object";
}
cin
corrected code to support ts strict mode...
r115 export function isNull(val: any): val is null | undefined {
cin
added safe.firstWhere
r75 return (val === null || val === undefined);
cin
changed the project structure
r49 }
cin
corrected code to support ts strict mode...
r115 export type primitive = symbol | string | number | boolean | undefined | null;
export function isPrimitive(val: any): val is primitive {
cin
added safe.firstWhere
r75 return (val === null || val === undefined || typeof (val) === "string" ||
typeof (val) === "number" || typeof (val) === "boolean");
cin
changed the project structure
r49 }
cin
added safe.firstWhere
r75 export function isInteger(val: any): val is number {
return parseInt(val, 10) === val;
cin
changed the project structure
r49 }
cin
added safe.firstWhere
r75 export function isNumber(val: any): val is number {
return parseFloat(val) === val;
cin
changed the project structure
r49 }
cin
added safe.firstWhere
r75 export function isString(val: any): val is string {
cin
changed the project structure
r49 return typeof (val) === "string" || val instanceof String;
}
cin
corrected code to support ts strict mode...
r115 export function isPromise<T = any>(val: any): val is PromiseLike<T> {
cin
fixed "singleton" activation type handling in container configuration...
r65 return val && typeof val.then === "function";
cin
changed the project structure
r49 }
cin
added safe.firstWhere
r75 export function isCancellable(val: any): val is ICancellable {
cin
Minor fixes and improvements...
r71 return val && typeof val.cancel === "function";
}
cin
corrected code to support ts strict mode...
r115 export function isNullOrEmptyString(val: any): val is ("" | null | undefined) {
cin
working on IoC configuration
r114 return (val === null || val === undefined ||
cin
corrected code to support ts strict mode...
r115 ((typeof (val) === "string" || val instanceof String) && val.length === 0));
cin
changed the project structure
r49 }
cin
corrected code to support ts strict mode...
r115 export function isNotEmptyArray<T = any>(arg: any): arg is T[] {
cin
changed the project structure
r49 return (arg instanceof Array && arg.length > 0);
}
cin
working on IoC configuration
r114 function _isStrictMode(this: any) {
cin
fixed "singleton" activation type handling in container configuration...
r65 return !this;
}
cin
working on IoC configuration
r114 function _getNonStrictGlobal(this: any) {
cin
fixed "singleton" activation type handling in container configuration...
r65 return this;
}
cin
changed the project structure
r49 export function getGlobal() {
cin
fixed "singleton" activation type handling in container configuration...
r65 // in es3 we can't use indirect call to eval, since it will
// be executed in the current call context.
if (!_isStrictMode()) {
return _getNonStrictGlobal();
} else {
// tslint:disable-next-line:no-eval
return eval.call(null, "this");
}
cin
changed the project structure
r49 }
export function get(member: string, context?: object) {
argumentNotEmptyString(member, "member");
let that = context || getGlobal();
const parts = member.split(".");
for (const m of parts) {
if (!m)
continue;
if (isNull(that = that[m]))
break;
}
return that;
}
/**
* Выполняет метод для каждого элемента массива, останавливается, когда
* либо достигнут конец массива, либо функция <c>cb</c> вернула
* значение.
*
* @param {Array | Object} obj массив элементов для просмотра
* @param {Function} cb функция, вызываемая для каждого элемента
* @param {Object} thisArg значение, которое будет передано в качестве
* <c>this</c> в <c>cb</c>.
cin
working on fluent configuration, di annotations removed
r134 * @returns {void}
cin
changed the project structure
r49 */
cin
corrected code to support ts strict mode...
r115 export function each<T>(obj: T, cb: <X extends keyof T>(v: NonNullable<T[X]>, k: X) => void): void;
export function each<T>(array: T[], cb: (v: T, i: number) => void): void;
cin
working on IoC configuration
r114 export function each(obj: any, cb: any, thisArg?: any): any;
export function each(obj: any, cb: any, thisArg?: any) {
cin
changed the project structure
r49 argumentNotNull(cb, "cb");
if (obj instanceof Array) {
cin
working on fluent configuration, di annotations removed
r134 let v: any;
cin
changed the project structure
r49 for (let i = 0; i < obj.length; i++) {
cin
working on fluent configuration, di annotations removed
r134 v = obj[i];
if (v !== undefined)
cb.call(thisArg, v, i);
cin
changed the project structure
r49 }
} else {
cin
working on fluent configuration, di annotations removed
r134 Object.keys(obj).forEach(k => obj[k] !== undefined && cb.call(thisArg, obj[k], k));
cin
changed the project structure
r49 }
}
/** Copies property values from a source object to the destination and returns
cin
added provided and configure methods to the fluent container configuration, added applyConfig method to the container
r142 * the destination object.
cin
changed the project structure
r49 *
* @param dest The destination object into which properties from the source
* object will be copied.
* @param source The source of values which will be copied to the destination
* object.
* @param template An optional parameter specifies which properties should be
* copied from the source and how to map them to the destination. If the
* template is an array it contains the list of property names to copy from the
* source to the destination. In case of object the templates contains the map
* where keys are property names in the source and the values are property
* names in the destination object. If the template isn't specified then the
* own properties of the source are entirely copied to the destination.
*
*/
cin
fixed safe.mixin typings
r140 export function mixin<T extends object, S extends object>(dest: T, source: S, template?: (keyof S)[]): T & S;
cin
corrected code to support ts strict mode...
r115 export function mixin<T extends object, S extends object, R extends object = T>(dest: T, source: S, template: { [p in keyof S]?: keyof R; }): T & R;
export function mixin<T extends object, S extends object>(dest: T, source: S, template?: any): any {
argumentNotNull(dest, "dest");
const _res: any = dest as any;
cin
changed the project structure
r49
cin
fixed "singleton" activation type handling in container configuration...
r65 if (isPrimitive(source))
return _res;
cin
changed the project structure
r49 if (template instanceof Array) {
cin
corrected code to support ts strict mode...
r115 template.forEach(p => {
if (isKeyof(p, source))
cin
changed the project structure
r49 _res[p] = source[p];
cin
corrected code to support ts strict mode...
r115 });
cin
changed the project structure
r49 } else if (template) {
cin
corrected code to support ts strict mode...
r115 keys(source).forEach(p => {
if (isKeyof(p, template))
cin
changed the project structure
r49 _res[template[p]] = source[p];
cin
corrected code to support ts strict mode...
r115 });
cin
changed the project structure
r49 } else {
cin
corrected code to support ts strict mode...
r115 keys(source).forEach(p => _res[p] = source[p]);
cin
changed the project structure
r49 }
return _res;
}
/** Wraps the specified function to emulate an asynchronous execution.
* @param{Object} thisArg [Optional] Object which will be passed as 'this' to the function.
* @param{Function|String} fn [Required] Function wich will be wrapped.
*/
cin
corrected code to support ts strict mode...
r115 export function async<T, F extends (...args: any[]) => T | PromiseLike<T>>(
fn: F,
thisArg?: ThisParameterType<F>
): (...args: Parameters<F>) => PromiseLike<T>;
export function async<T, M extends string, O extends { [m in M]?: (...args: any[]) => T | PromiseLike<T> }>(
fn: M,
thisArg: O
): (...args: Parameters<NonNullable<O[M]>>) => PromiseLike<T>;
export function async(_fn: any, thisArg: any): (...args: any[]) => PromiseLike<any> {
cin
changed the project structure
r49 let fn = _fn;
if (arguments.length === 2 && !(fn instanceof Function))
fn = thisArg[fn];
if (fn == null)
throw new Error("The function must be specified");
cin
corrected code to support ts strict mode...
r115 function wrapresult(x: any, e?: any): PromiseLike<any> {
cin
changed the project structure
r49 if (e) {
return {
then(cb, eb) {
try {
return eb ? wrapresult(eb(e)) : this;
} catch (e2) {
return wrapresult(null, e2);
}
}
};
} else {
if (x && x.then)
return x;
return {
then(cb) {
try {
return cb ? wrapresult(cb(x)) : this;
} catch (e2) {
return wrapresult(e2);
}
}
};
}
}
return (...args) => {
try {
return wrapresult(fn.apply(thisArg, args));
} catch (e) {
return wrapresult(null, e);
}
};
}
cin
corrected code to support ts strict mode...
r115 export function delegate<T extends object, F extends (this: T, ...args: any[]) => any>(
target: T,
method: F
): OmitThisParameter<F>;
export function delegate<M extends string, T extends { [m in M]?: (...args: any[]) => any; }>(
target: T,
method: M
): OmitThisParameter<T[M]>;
export function delegate(target: any, _method: any): (...args: any[]) => any {
let method: any;
cin
changed the project structure
r49 if (!(_method instanceof Function)) {
argumentNotNull(target, "target");
method = target[_method];
if (!(method instanceof Function))
throw new Error("'method' argument must be a Function or a method name");
} else {
method = _method;
}
return (...args) => {
return method.apply(target, args);
};
}
cin
Added safe.delay...
r76 export function delay(timeMs: number, ct = Cancellation.none) {
cin
more tests
r77 ct.throwIfRequested();
cin
Added safe.delay...
r76 return new Promise((resolve, reject) => {
cin
more tests
r77 const h = ct.register(e => {
clearTimeout(id);
reject(e);
// we don't nedd to unregister h, since ct is already disposed
});
const id = setTimeout(() => {
h.destroy();
resolve();
}, timeMs);
cin
Added safe.delay...
r76 });
}
cin
corrected code to support ts strict mode...
r115 /** Returns resolved promise, awaiting this method will cause the asynchronous
* completion of the rest of the code.
*/
export function fork() {
return Promise.resolve();
}
/** Always throws Error, can be used as a stub for the methods which should be
* assigned later and are required to be not null.
*/
export function notImplemented(): never {
throw new Error("Not implemeted");
}
cin
changed the project structure
r49 /**
cin
Added ObservableValue...
r80 * Iterates over the specified array of items and calls the callback `cb`, if
* the result of the callback is a promise the next item from the array will be
* proceeded after the promise is resolved.
cin
changed the project structure
r49 *
*/
cin
Added ObservableValue...
r80 export function pmap<T, T2>(
items: ArrayLike<T> | PromiseLike<ArrayLike<T>>,
cb: (item: T, i: number) => T2 | PromiseLike<T2>
): T2[] | PromiseLike<T2[]> {
cin
changed the project structure
r49 argumentNotNull(cb, "cb");
cin
Added ObservableValue...
r80 if (isPromise(items)) {
cin
changed the project structure
r49 return items.then(data => pmap(data, cb));
cin
Added ObservableValue...
r80 } else {
cin
changed the project structure
r49
cin
Added ObservableValue...
r80 if (isNull(items) || !items.length)
return [];
cin
changed the project structure
r49
cin
Added ObservableValue...
r80 let i = 0;
const result = new Array<T2>();
cin
changed the project structure
r49
cin
working on IoC configuration
r114 const next = (): any => {
cin
Added ObservableValue...
r80 while (i < items.length) {
const r = cb(items[i], i);
const ri = i;
i++;
if (isPromise(r)) {
return r.then(x => {
result[ri] = x;
return next();
});
} else {
result[ri] = r;
}
}
return result;
};
cin
changed the project structure
r49
cin
Added ObservableValue...
r80 return next();
cin
changed the project structure
r49 }
}
cin
Added ObservableValue...
r80 export function pfor<T>(
items: ArrayLike<T> | PromiseLike<ArrayLike<T>>,
cb: (item: T, i: number) => any
): void | PromiseLike<void> {
cin
fixed "singleton" activation type handling in container configuration...
r65 argumentNotNull(cb, "cb");
cin
Added ObservableValue...
r80 if (isPromise(items)) {
return items.then(data => pfor(data, cb));
} else {
if (isNull(items) || !items.length)
return;
cin
fixed "singleton" activation type handling in container configuration...
r65
cin
Added ObservableValue...
r80 let i = 0;
cin
fixed "singleton" activation type handling in container configuration...
r65
cin
working on IoC configuration
r114 const next = (): any => {
cin
Added ObservableValue...
r80 while (i < items.length) {
const r = cb(items[i], i);
i++;
if (isPromise(r))
return r.then(next);
}
};
return next();
cin
fixed "singleton" activation type handling in container configuration...
r65 }
}
cin
added safe.firstWhere
r75 export function first<T>(sequence: ArrayLike<T>): T;
export function first<T>(sequence: PromiseLike<ArrayLike<T>>): PromiseLike<T>;
export function first<T>(
sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>,
cin
corrected code to support ts strict mode...
r115 cb?: (x: T) => void,
cin
added safe.firstWhere
r75 err?: (x: Error) => void
): void;
cin
changed the project structure
r49 /**
* Выбирает первый элемент из последовательности, или обещания, если в
* качестве параметра используется обещание, оно должно вернуть массив.
*
* @param {Function} cb обработчик результата, ему будет передан первый
* элемент последовательности в случае успеха
* @param {Function} err обработчик исключения, если массив пустой, либо
* не массив
*
* @remarks Если не указаны ни cb ни err, тогда функция вернет либо
* обещание, либо первый элемент.
* @async
*/
cin
added safe.firstWhere
r75 export function first<T>(
sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>,
cb?: (x: T) => void,
err?: (x: Error) => void
) {
if (isPromise(sequence)) {
cin
corrected code to support ts strict mode...
r115 return sequence.then(res => first(res, cb as any /* force to pass undefined cb */, err));
cin
added safe.firstWhere
r75 } else if (sequence && "length" in sequence) {
if (sequence.length === 0) {
if (err)
return err(new Error("The sequence is empty"));
else
throw new Error("The sequence is empty");
} else if (cb) {
cin
Added ObservableValue...
r80 return cb(sequence[0]);
cin
added safe.firstWhere
r75 } else {
return sequence[0];
cin
changed the project structure
r49 }
cin
added safe.firstWhere
r75 } else {
if (err)
cin
Added ObservableValue...
r80 return err(new Error("The sequence is required"));
cin
added safe.firstWhere
r75 else
throw new Error("The sequence is required");
cin
changed the project structure
r49 }
}
cin
added safe.firstWhere
r75 export function firstWhere<T>(
sequence: ArrayLike<T>,
predicate: (x: T) => boolean
): T;
export function firstWhere<T>(
sequence: PromiseLike<ArrayLike<T>>,
predicate: (x: T) => boolean
): PromiseLike<T>;
export function firstWhere<T>(
sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>,
predicate: (x: T) => boolean,
cb: (x: T) => void,
err?: (x: Error) => void
): void;
export function firstWhere<T>(
sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>,
predicate?: (x: T) => boolean,
cin
Added safe.delay...
r76 cb?: (x: T) => any,
err?: (x: Error) => any
cin
added safe.firstWhere
r75 ) {
if (isPromise(sequence)) {
cin
corrected code to support ts strict mode...
r115 return sequence.then(res => firstWhere(
res,
predicate as any /* force to pass undefined predicate */,
cb as any /* force to pass undefined cb */,
err)
);
cin
added safe.firstWhere
r75 } else if (sequence && "length" in sequence) {
if (sequence.length === 0) {
if (err)
err(new Error("The sequence is empty"));
else
throw new Error("The sequence is empty");
} else {
if (!predicate) {
cin
Added safe.delay...
r76 return cb ? cb(sequence[0]) && void (0) : sequence[0];
cin
added safe.firstWhere
r75 } else {
for (let i = 0; i < sequence.length; i++) {
const v = sequence[i];
if (predicate(v))
return cb ? cb(v) : v;
}
if (err)
err(new Error("The sequence doesn't contain matching items"));
else
throw new Error("The sequence doesn't contain matching items");
}
}
} else {
if (err)
err(new Error("The sequence is required"));
else
throw new Error("The sequence is required");
}
}
cin
working on lifetime management
r129 export function isDestroyable(d: any): d is IDestroyable {
cin
working on fluent configuration, di annotations removed
r134 if (d && "destroy" in d && typeof (destroy) === "function")
cin
working on lifetime management
r129 return true;
return false;
}
cin
added safe.firstWhere
r75 export function destroy(d: any) {
cin
changed the project structure
r49 if (d && "destroy" in d)
d.destroy();
}
cin
added safe.firstWhere
r75
/**
* Used to mark that the async operation isn't awaited intentionally.
* @param p The promise which represents the async operation.
*/
export function nowait(p: Promise<any>) {
}
cin
Added safe.delay...
r76
/** represents already destroyed object.
*/
export const destroyed = {
/** Calling to this method doesn't affect anything, noop.
*/
destroy() {
}
};