##// END OF EJS Templates
fixed "singleton" activation type handling in container configuration...
cin -
r65:0c74a0572161 v1.2.13 default
parent child
Show More
@@ -1,4 +1,4
1 define(["./declare", "./log/trace!"], function (declare, trace) {
1 define(["dojo/_base/declare", "./log/trace!"], function (declare, trace) {
2 2 trace.warn("THIS MODULE IS DEPRECATED! use uri-js or similar alternatives.");
3 3
4 4 function parseURI(uri) {
@@ -1,5 +1,5
1 1 import { Uuid } from "../Uuid";
2 import { argumentNotEmptyString, getGlobal } from "../safe";
2 import { argumentNotEmptyString, getGlobal, isNullOrEmptyString } from "../safe";
3 3 import { TraceSource, DebugLevel } from "../log/TraceSource";
4 4 import m = require("module");
5 5
@@ -7,7 +7,7 const sandboxId = Uuid();
7 7 define(sandboxId, ["require"], r => r);
8 8
9 9 // tslint:disable-next-line:no-var-requires
10 const globalRequire = require(sandboxId);
10 const globalRequire = getGlobal().require as Require;
11 11
12 12 const trace = TraceSource.get(m.id);
13 13
@@ -58,15 +58,17 class ModuleResolver {
58 58 }
59 59 }
60 60
61 export function makeResolver(moduleName: string, contextRequire: Require) {
61 export async function makeResolver(moduleName: string, contextRequire: Require) {
62 62 trace.debug(
63 63 "makeResolver moduleName={0}, contextRequire={1}",
64 64 moduleName || "<nil>",
65 65 contextRequire ? typeof (contextRequire) : "<nil>"
66 66 );
67 67
68 const base = moduleName && moduleName.split("/").slice(0, -1).join("/");
68 const nestedRequire = isNullOrEmptyString(moduleName) ? null : await createContextRequire(moduleName);
69 69
70 const resolver = new ModuleResolver(contextRequire, base);
70 // const base = moduleName && moduleName.split("/").slice(0, -1).join("/");
71
72 const resolver = new ModuleResolver(nestedRequire, null);
71 73 return (id: string) => resolver.resolve(id);
72 74 }
@@ -2,7 +2,9 import { TraceSource } from "./TraceSour
2 2 import { Predicate } from "../interfaces";
3 3
4 4 export = {
5 on(filter: any , cb: any) {
5 level: 0,
6
7 on(filter: any, cb: any) {
6 8 if (arguments.length === 1) {
7 9 cb = filter;
8 10 filter = undefined;
@@ -18,11 +20,13 export = {
18 20
19 21 if (test) {
20 22 TraceSource.on(source => {
23 source.level = this.level;
21 24 if (test(source.id))
22 25 source.events.on(cb);
23 26 });
24 27 } else {
25 28 TraceSource.on(source => {
29 source.level = this.level;
26 30 source.events.on(cb);
27 31 });
28 32 }
@@ -1,4 +1,4
1 import { format } from "./StringFormat";
1 import * as format from "./format";
2 2 import { TraceSource, DebugLevel } from "../log/TraceSource";
3 3 import { ITemplateParser, TokenType } from "./TemplateParser";
4 4 import m = require("module");
@@ -98,7 +98,7 export class TemplateCompiler {
98 98 }
99 99
100 100 visitTextFragment(parser: ITemplateParser) {
101 const i = this._data.push(parser.value());
101 const i = this._data.push(parser.value()) - 1;
102 102 this._code.push("$p.push($data[" + i + "]);");
103 103 }
104 104 }
@@ -1,5 +1,9
1 1 import { argumentNotEmptyString } from "../safe";
2 2 import { MapOf } from "../interfaces";
3 import { TraceSource, DebugLevel } from "../log/TraceSource";
4 import m = require("module");
5
6 const trace = TraceSource.get(m.id);
3 7
4 8 const splitRx = /(<%=|\[%=|<%|\[%|%\]|%>)/;
5 9
@@ -45,6 +49,7 export class TemplateParser implements I
45 49 if (this._pos < this._tokens.length) {
46 50 this._value = this._tokens[this._pos];
47 51 this._type = tokenMap[this._value] || TokenType.Text;
52
48 53 return true;
49 54 } else {
50 55 this._type = TokenType.None;
@@ -1,8 +1,9
1 1 import * as module from "module";
2 2 import { TraceSource } from "../log/TraceSource";
3 import { compile } from "./StringFormat";
3 4
4 5 const logger = TraceSource.get(module.id);
5 6
6 7 logger.warn("The module is deprecated, use StringFormat.compile() method directly");
7 8
8 export { compile } from "./StringFormat";
9 export = compile;
@@ -1,8 +1,8
1 1 import { format as dojoFormatNumber } from "dojo/number";
2 2 import { format as dojoFormatDate } from "dojo/date/locale";
3 import { Formatter } from "./StringFormat";
3 import { Formatter, compile as _compile } from "./StringFormat";
4 4
5 import { isNumber } from "../safe";
5 import { isNumber, isNull } from "../safe";
6 6
7 7 interface NumberFormatOptions {
8 8 round?: number;
@@ -42,6 +42,26 function convertDate(value: any, pattern
42 42
43 43 const _formatter = new Formatter([convertNumber, convertDate]);
44 44
45 export = function format(msg: string, ...args: any[]) {
46 return _formatter.format.apply(msg, ...args);
47 };
45 function format(msg: string, ...args: any[]) {
46 return _formatter.format(msg, ...args);
47 }
48
49 function _convert(value: any, pattern: string) {
50 return _formatter.convert(value, pattern);
51 }
52
53 namespace format {
54 export const convert = _convert;
55 export function compile(text: string) {
56 const template = _compile(text);
57
58 return (...data) => {
59 return template((name, pattern) => {
60 const value = data[name];
61 return !isNull(value) ? convert(value, pattern) : "";
62 });
63 };
64 }
65 }
66
67 export = format;
@@ -54,9 +54,9 function setupBrowser() {
54 54
55 55 return _rnds;
56 56 };
57 if ("undefined" !== typeof console && console.warn) {
58 console.warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()");
59 }
57 // if ("undefined" !== typeof console && console.warn) {
58 // console.warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()");
59 // }
60 60 }
61 61 }
62 62
@@ -92,7 +92,7 for (let i = 0; i < 256; i++) {
92 92 }
93 93
94 94 // **`parse()` - Parse a UUID into it's component bytes**
95 export function _parse(s, buf?, offset?): Array<string> {
95 function _parse(s, buf?, offset?): Array<string> {
96 96 const i = (buf && offset) || 0; let ii = 0;
97 97
98 98 buf = buf || [];
@@ -146,7 +146,7 let _clockseq = (_seedBytes[6] << 8 | _s
146 146 let _lastMSecs = 0; let _lastNSecs = 0;
147 147
148 148 // See https://github.com/broofa/node-uuid for API details
149 export function _v1(options?, buf?, offset?): string {
149 function _v1(options?, buf?, offset?): string {
150 150 let i = buf && offset || 0;
151 151 const b = buf || [];
152 152
@@ -231,7 +231,7 export function _v1(options?, buf?, offs
231 231 // **`v4()` - Generate random UUID**
232 232
233 233 // See https://github.com/broofa/node-uuid for API details
234 export function _v4(options?, buf?, offset?): string {
234 function _v4(options?, buf?, offset?): string {
235 235 // Deprecated - 'format' argument, as supported in v1.2
236 236 const i = buf && offset || 0;
237 237
@@ -257,13 +257,16 export function _v4(options?, buf?, offs
257 257 return buf || _unparse(rnds);
258 258 }
259 259
260 export function Uuid() {
260 function _Uuid() {
261 261 return _v4();
262 262 }
263 263
264 export namespace Uuid {
264 namespace _Uuid {
265 265 export const v4 = _v4;
266 266 export const v1 = _v1;
267 267 export const empty = "00000000-0000-0000-0000-000000000000";
268 268 export const parse = _parse;
269 export const Uuid = _v4;
269 270 }
271
272 export = _Uuid;
@@ -28,27 +28,6 import { ICancellation } from "../interf
28 28
29 29 const trace = TraceSource.get("@implab/core/di/Configuration");
30 30
31 declare const define;
32 declare const require;
33 declare const module;
34
35 function hasAmdLoader() {
36 try {
37 // es6 may throw the exception
38 return (typeof define === "function" && define.amd);
39 } catch {
40 return false;
41 }
42 }
43
44 function hasNodeJs() {
45 try {
46 return (typeof module !== "undefined" && module.exports);
47 } catch {
48 return false;
49 }
50 }
51
52 31 async function mapAll(data: object | any[], map?: (v, k) => any): Promise<any> {
53 32 if (data instanceof Array) {
54 33 return Promise.all(map ? data.map(map) : data);
@@ -99,21 +78,21 export class Configuration {
99 78
100 79 this._configName = moduleName;
101 80
102 const r = makeResolver(null, contextRequire);
81 const r = await makeResolver(null, contextRequire);
103 82
104 83 const config = await r(moduleName, ct);
105 84
106 85 await this._applyConfiguration(
107 86 config,
108 makeResolver(moduleName, contextRequire),
87 await makeResolver(moduleName, contextRequire),
109 88 ct
110 89 );
111 90 }
112 91
113 applyConfiguration(data: object, contextRequire?: any, ct = Cancellation.none) {
92 async applyConfiguration(data: object, contextRequire?: any, ct = Cancellation.none) {
114 93 argumentNotNull(data, "data");
115 94
116 return this._applyConfiguration(data, makeResolver(void (0), contextRequire), ct);
95 await this._applyConfiguration(data, await makeResolver(void (0), contextRequire), ct);
117 96 }
118 97
119 98 async _applyConfiguration(data: object, resolver?: ModuleResolver, ct = Cancellation.none) {
@@ -262,7 +241,7 export class Configuration {
262 241 opts.services = this._visitRegistrations(data.services, "services");
263 242
264 243 if (data.inject) {
265 this._path.push("inject");
244 this._enter("inject");
266 245 opts.inject = mapAll(
267 246 data.inject instanceof Array ?
268 247 data.inject :
@@ -353,11 +332,11 export class Configuration {
353 332 }
354 333
355 334 async _visitFactoryRegistration(data: FactoryRegistration, name: _key) {
356 argumentOfType(data.$factory, Function, "data.$type");
335 argumentOfType(data.$factory, Function, "data.$factory");
357 336 this._enter(name);
358 337
359 338 const opts = this._makeServiceParams(data);
360 opts.factory = opts.$factory;
339 opts.factory = data.$factory;
361 340
362 341 const d = new FactoryServiceDescriptor(
363 342 await mapAll(opts)
@@ -14,7 +14,7 export class FactoryServiceDescriptor ex
14 14 argumentNotNull(opts && opts.factory, "opts.factory");
15 15
16 16 // bind to null
17 this._factory = () => opts.factory();
17 this._factory = (...args) => opts.factory.apply(null, args);
18 18
19 19 if (opts.activation === ActivationType.Singleton) {
20 20 this._cacheId = oid(opts.factory);
@@ -1,3 +1,3
1 1 import { ModuleResolver } from "./Configuration";
2 2
3 export declare function makeResolver(moduleName?: string, contextRequire?: any): ModuleResolver; No newline at end of file
3 export declare function makeResolver(moduleName?: string, contextRequire?: any): Promise<ModuleResolver>; No newline at end of file
@@ -14,9 +14,9 function injectMethod(target, method, co
14 14 throw new Error("Method '" + method + "' not found");
15 15
16 16 if (args instanceof Array)
17 return m.apply(target, context.parse(args, "." + method));
17 return m.apply(target, _parse(args, context, "." + method));
18 18 else
19 return m.call(target, context.parse(args, "." + method));
19 return m.call(target, _parse(args, context, "." + method));
20 20 }
21 21
22 22 function makeClenupCallback(target, method: ((instance) => void) | string) {
@@ -90,10 +90,10 export class ServiceDescriptor implement
90 90
91 91 this._owner = opts.owner;
92 92
93 if (opts.activation)
93 if ("activation" in opts)
94 94 this._activationType = opts.activation;
95 95
96 if (opts.params)
96 if ("params" in opts)
97 97 this._params = opts.params;
98 98
99 99 if (opts.inject)
@@ -16,7 +16,7 export interface ServiceMap {
16 16 }
17 17
18 18 export enum ActivationType {
19 Singleton,
19 Singleton = 1,
20 20 Container,
21 21 Hierarchy,
22 22 Context,
@@ -59,11 +59,11 export interface DependencyRegistration
59 59 }
60 60
61 61 export function isTypeRegistration(x): x is TypeRegistration {
62 return (!isPrimitive(x)) && ("$type" in x || "$factory" in x);
62 return (!isPrimitive(x)) && ("$type" in x);
63 63 }
64 64
65 65 export function isFactoryRegistration(x): x is FactoryRegistration {
66 return (!isPrimitive(x)) && ("$type" in x || "$factory" in x);
66 return (!isPrimitive(x)) && ("$factory" in x);
67 67 }
68 68
69 69 export function isValueRegistration(x): x is ValueRegistration {
@@ -1,6 +1,6
1 1 import { Observable } from "../Observable";
2 2 import { Registry } from "./Registry";
3 import { format } from "../text/StringFormat";
3 import { format as _format } from "../text/StringFormat";
4 4
5 5 export const DebugLevel = 400;
6 6
@@ -20,6 +20,12 export interface TraceEvent {
20 20 readonly arg: any;
21 21 }
22 22
23 function format(msg) {
24 if (typeof(msg) !== "string" || arguments.length === 1)
25 return msg;
26 return _format.apply(null, arguments);
27 }
28
23 29 export class TraceSource {
24 30 readonly id: any;
25 31
@@ -3,6 +3,9 const _oid = typeof Symbol === "function
3 3 Symbol("__implab__oid__") :
4 4 "__implab__oid__";
5 5
6 declare const window: any;
7 declare const global: any;
8
6 9 export function oid(instance: object): string {
7 10 if (isNull(instance))
8 11 return null;
@@ -55,7 +58,7 export function isString(val) {
55 58 }
56 59
57 60 export function isPromise(val): val is PromiseLike<any> {
58 return "then" in val && val.then instanceof Function;
61 return val && typeof val.then === "function";
59 62 }
60 63
61 64 export function isNullOrEmptyString(str) {
@@ -68,8 +71,23 export function isNotEmptyArray(arg): ar
68 71 return (arg instanceof Array && arg.length > 0);
69 72 }
70 73
74 function _isStrictMode() {
75 return !this;
76 }
77
78 function _getNonStrictGlobal() {
79 return this;
80 }
81
71 82 export function getGlobal() {
72 return this;
83 // in es3 we can't use indirect call to eval, since it will
84 // be executed in the current call context.
85 if (!_isStrictMode()) {
86 return _getNonStrictGlobal();
87 } else {
88 // tslint:disable-next-line:no-eval
89 return eval.call(null, "this");
90 }
73 91 }
74 92
75 93 export function get(member: string, context?: object) {
@@ -135,6 +153,9 export function mixin<T, S>(dest: T, sou
135 153 argumentNotNull(dest, "to");
136 154 const _res = dest as T & S;
137 155
156 if (isPrimitive(source))
157 return _res;
158
138 159 if (template instanceof Array) {
139 160 for (const p of template) {
140 161 if (p in source)
@@ -268,6 +289,31 export function pmap(items, cb) {
268 289 return next();
269 290 }
270 291
292 export function pfor(items, cb) {
293 argumentNotNull(cb, "cb");
294
295 if (isPromise(items))
296 return items.then(data => {
297 return pmap(data, cb);
298 });
299
300 if (isNull(items) || !items.length)
301 return items;
302
303 let i = 0;
304
305 function next() {
306 while (i < items.length) {
307 const r = cb(items[i], i);
308 i++;
309 if (isPromise(r))
310 return r.then(next);
311 }
312 }
313
314 return next();
315 }
316
271 317 /**
272 318 * Выбирает первый элемент из последовательности, или обещания, если в
273 319 * качестве параметра используется обещание, оно должно вернуть массив.
@@ -124,7 +124,7 function defaultConverter(value: any, pa
124 124 } else if (value instanceof Date) {
125 125 return value.toISOString();
126 126 } else {
127 return pattern ? value.toString(pattern) : value.toString();
127 return value.toString();
128 128 }
129 129 }
130 130
@@ -171,3 +171,7 const _default = new Formatter();
171 171 export function format(msg: string, ...args: any[]) {
172 172 return _default.format(msg, ...args);
173 173 }
174
175 export function convert(value: any, pattern: string) {
176 return _default.format(value, pattern);
177 }
General Comments 0
You need to be logged in to leave comments. Login now