| @@ -0,0 +1,17 | |||||
|
|
1 | <?xml version="1.0" encoding="UTF-8"?> | |||
|
|
2 | <projectDescription> | |||
|
|
3 | <name>core</name> | |||
|
|
4 | <comment>Project core created by Buildship.</comment> | |||
|
|
5 | <projects> | |||
|
|
6 | </projects> | |||
|
|
7 | <buildSpec> | |||
|
|
8 | <buildCommand> | |||
|
|
9 | <name>org.eclipse.buildship.core.gradleprojectbuilder</name> | |||
|
|
10 | <arguments> | |||
|
|
11 | </arguments> | |||
|
|
12 | </buildCommand> | |||
|
|
13 | </buildSpec> | |||
|
|
14 | <natures> | |||
|
|
15 | <nature>org.eclipse.buildship.core.gradleprojectnature</nature> | |||
|
|
16 | </natures> | |||
|
|
17 | </projectDescription> | |||
| @@ -0,0 +1,4 | |||||
|
|
1 | { | |||
|
|
2 | "java.configuration.updateBuildConfiguration": "disabled", | |||
|
|
3 | "tslint.enable": true | |||
|
|
4 | } No newline at end of file | |||
| @@ -0,0 +1,126 | |||||
|
|
1 | import { TraceSource } from "./log/TraceSource"; | |||
|
|
2 | import { argumentNotNull, argumentNotEmptyString, isPrimitive, each, isNull } from "./safe"; | |||
|
|
3 | import { Uuid } from './Uuid'; | |||
|
|
4 | import {ActivationContext} from "./di/ActivationContext"; | |||
|
|
5 | ||||
|
|
6 | let trace = TraceSource.get("di"); | |||
|
|
7 | ||||
|
|
8 | export interface Descriptor { | |||
|
|
9 | activate(context: ActivationContext, name: string): any | |||
|
|
10 | } | |||
|
|
11 | ||||
|
|
12 | export function isDescriptor(value: any): value is Descriptor { | |||
|
|
13 | return ("activate" in value); | |||
|
|
14 | } | |||
|
|
15 | ||||
|
|
16 | export interface ServiceMap { | |||
|
|
17 | [s: string] : Descriptor | |||
|
|
18 | } | |||
|
|
19 | ||||
|
|
20 | export class ActivationContextInfo { | |||
|
|
21 | name: string | |||
|
|
22 | ||||
|
|
23 | service: string | |||
|
|
24 | ||||
|
|
25 | scope: ServiceMap | |||
|
|
26 | } | |||
|
|
27 | ||||
|
|
28 | export class ActivationError { | |||
|
|
29 | activationStack: ActivationContextInfo[] | |||
|
|
30 | ||||
|
|
31 | service: string | |||
|
|
32 | ||||
|
|
33 | innerException: any | |||
|
|
34 | ||||
|
|
35 | message: string | |||
|
|
36 | ||||
|
|
37 | constructor(service: string, activationStack: ActivationContextInfo[], innerException) { | |||
|
|
38 | this.message = "Failed to activate the service"; | |||
|
|
39 | this.activationStack = activationStack; | |||
|
|
40 | this.service = service; | |||
|
|
41 | this.innerException = innerException; | |||
|
|
42 | } | |||
|
|
43 | ||||
|
|
44 | toString() { | |||
|
|
45 | var parts = [this.message]; | |||
|
|
46 | if (this.service) | |||
|
|
47 | parts.push("when activating: " + this.service.toString()); | |||
|
|
48 | ||||
|
|
49 | if (this.innerException) | |||
|
|
50 | parts.push("caused by: " + this.innerException.toString()); | |||
|
|
51 | ||||
|
|
52 | if (this.activationStack) { | |||
|
|
53 | parts.push("at"); | |||
|
|
54 | this.activationStack.forEach(function (x) { | |||
|
|
55 | parts.push(" " + x.name + " " + | |||
|
|
56 | (x.service ? x.service.toString() : "")); | |||
|
|
57 | }); | |||
|
|
58 | } | |||
|
|
59 | ||||
|
|
60 | return parts.join("\n"); | |||
|
|
61 | } | |||
|
|
62 | } | |||
|
|
63 | ||||
|
|
64 | ||||
|
|
65 | export enum ActivationType { | |||
|
|
66 | SINGLETON, | |||
|
|
67 | CONTAINER, | |||
|
|
68 | HIERARCHY, | |||
|
|
69 | CONTEXT, | |||
|
|
70 | CALL | |||
|
|
71 | } | |||
|
|
72 | ||||
|
|
73 | ||||
|
|
74 | ||||
|
|
75 | interface ServiceDescriptorParams { | |||
|
|
76 | ||||
|
|
77 | } | |||
|
|
78 | ||||
|
|
79 | class ServiceDescriptor extends Descriptor { | |||
|
|
80 | constructor(opts: ServiceDescriptorParams) { | |||
|
|
81 | super(); | |||
|
|
82 | } | |||
|
|
83 | ||||
|
|
84 | activate(context: ActivationContext, name: string) { | |||
|
|
85 | throw new Error("Method not implemented."); | |||
|
|
86 | } | |||
|
|
87 | isInstanceCreated(): boolean { | |||
|
|
88 | throw new Error("Method not implemented."); | |||
|
|
89 | } | |||
|
|
90 | getInstance() { | |||
|
|
91 | throw new Error("Method not implemented."); | |||
|
|
92 | } | |||
|
|
93 | } | |||
|
|
94 | ||||
|
|
95 | ||||
|
|
96 | ||||
|
|
97 | class AggregateDescriptor<T> extends Descriptor { | |||
|
|
98 | constructor(value: T) { | |||
|
|
99 | super(); | |||
|
|
100 | } | |||
|
|
101 | ||||
|
|
102 | activate(context: ActivationContext, name: string) { | |||
|
|
103 | throw new Error("Method not implemented."); | |||
|
|
104 | } | |||
|
|
105 | isInstanceCreated(): boolean { | |||
|
|
106 | throw new Error("Method not implemented."); | |||
|
|
107 | } | |||
|
|
108 | getInstance(): T { | |||
|
|
109 | throw new Error("Method not implemented."); | |||
|
|
110 | } | |||
|
|
111 | } | |||
|
|
112 | ||||
|
|
113 | class ValueDescriptor<T> implements Descriptor { | |||
|
|
114 | activate(context: ActivationContext, name: string) { | |||
|
|
115 | throw new Error("Method not implemented."); | |||
|
|
116 | } | |||
|
|
117 | isInstanceCreated(): boolean { | |||
|
|
118 | throw new Error("Method not implemented."); | |||
|
|
119 | } | |||
|
|
120 | getInstance(): T { | |||
|
|
121 | throw new Error("Method not implemented."); | |||
|
|
122 | } | |||
|
|
123 | constructor(value: T) { | |||
|
|
124 | ||||
|
|
125 | } | |||
|
|
126 | } No newline at end of file | |||
| @@ -0,0 +1,132 | |||||
|
|
1 | import { TraceSource } from "../log/TraceSource"; | |||
|
|
2 | import { argumentNotNull, argumentNotEmptyString, isPrimitive, each, isNull } from "../safe"; | |||
|
|
3 | import { Uuid } from '../Uuid'; | |||
|
|
4 | import { Container, ActivationContextInfo, ServiceMap, Descriptor, isDescriptor } from "../di"; | |||
|
|
5 | ||||
|
|
6 | let trace = TraceSource.get("di"); | |||
|
|
7 | ||||
|
|
8 | ||||
|
|
9 | export class ActivationContext { | |||
|
|
10 | _cache: object | |||
|
|
11 | ||||
|
|
12 | _services: ServiceMap | |||
|
|
13 | ||||
|
|
14 | _stack: ActivationContextInfo[] | |||
|
|
15 | ||||
|
|
16 | _visited: any | |||
|
|
17 | ||||
|
|
18 | container: any | |||
|
|
19 | ||||
|
|
20 | ||||
|
|
21 | constructor(container: Container, services: ServiceMap, cache?: object, visited?) { | |||
|
|
22 | argumentNotNull(container, "container"); | |||
|
|
23 | argumentNotNull(services, "services"); | |||
|
|
24 | ||||
|
|
25 | this._visited = visited || {}; | |||
|
|
26 | this._stack = []; | |||
|
|
27 | this._cache = cache || {}; | |||
|
|
28 | this._services = services; | |||
|
|
29 | this.container = container; | |||
|
|
30 | } | |||
|
|
31 | ||||
|
|
32 | getService(name, def?): any { | |||
|
|
33 | let d = this._services[name]; | |||
|
|
34 | ||||
|
|
35 | if (!d) | |||
|
|
36 | if (arguments.length > 1) | |||
|
|
37 | return def; | |||
|
|
38 | else | |||
|
|
39 | throw new Error("Service '" + name + "' not found"); | |||
|
|
40 | ||||
|
|
41 | return d.activate(this, name); | |||
|
|
42 | } | |||
|
|
43 | ||||
|
|
44 | /** | |||
|
|
45 | * registers services local to the the activation context | |||
|
|
46 | * | |||
|
|
47 | * @name{string} the name of the service | |||
|
|
48 | * @service{string} the service descriptor to register | |||
|
|
49 | */ | |||
|
|
50 | register(name: string, service: Descriptor) { | |||
|
|
51 | argumentNotEmptyString(name, "name"); | |||
|
|
52 | ||||
|
|
53 | this._services[name] = service; | |||
|
|
54 | } | |||
|
|
55 | ||||
|
|
56 | clone() { | |||
|
|
57 | return new ActivationContext( | |||
|
|
58 | this.container, | |||
|
|
59 | Object.create(this._services), | |||
|
|
60 | this._cache, | |||
|
|
61 | this._visited | |||
|
|
62 | ); | |||
|
|
63 | ||||
|
|
64 | } | |||
|
|
65 | ||||
|
|
66 | has(id) { | |||
|
|
67 | return id in this._cache; | |||
|
|
68 | } | |||
|
|
69 | ||||
|
|
70 | get(id) { | |||
|
|
71 | return this._cache[id]; | |||
|
|
72 | } | |||
|
|
73 | ||||
|
|
74 | store(id, value) { | |||
|
|
75 | return (this._cache[id] = value); | |||
|
|
76 | } | |||
|
|
77 | ||||
|
|
78 | parse(data: any, name) { | |||
|
|
79 | var me = this; | |||
|
|
80 | if (isPrimitive(data)) | |||
|
|
81 | return data; | |||
|
|
82 | ||||
|
|
83 | if (isDescriptor(data)) { | |||
|
|
84 | return data.activate(this, name); | |||
|
|
85 | } else if (data instanceof Array) { | |||
|
|
86 | me.enter(name); | |||
|
|
87 | var v = data.map(function (x, i) { | |||
|
|
88 | return me.parse(x, "." + i); | |||
|
|
89 | }); | |||
|
|
90 | me.leave(); | |||
|
|
91 | return v; | |||
|
|
92 | } else { | |||
|
|
93 | me.enter(name); | |||
|
|
94 | var result = {}; | |||
|
|
95 | for (var p in data) | |||
|
|
96 | result[p] = me.parse(data[p], "." + p); | |||
|
|
97 | me.leave(); | |||
|
|
98 | return result; | |||
|
|
99 | } | |||
|
|
100 | } | |||
|
|
101 | ||||
|
|
102 | visit(id) { | |||
|
|
103 | var count = this._visited[id] || 0; | |||
|
|
104 | this._visited[id] = count + 1; | |||
|
|
105 | return count; | |||
|
|
106 | } | |||
|
|
107 | ||||
|
|
108 | getStack() { | |||
|
|
109 | return this._stack.slice().reverse(); | |||
|
|
110 | } | |||
|
|
111 | ||||
|
|
112 | enter(name, d?, localize?) { | |||
|
|
113 | if (trace.isLogEnabled()) | |||
|
|
114 | trace.log("enter " + name + " " + (d || "") + | |||
|
|
115 | (localize ? " localize" : "")); | |||
|
|
116 | this._stack.push({ | |||
|
|
117 | name: name, | |||
|
|
118 | service: d, | |||
|
|
119 | scope: this._services | |||
|
|
120 | }); | |||
|
|
121 | if (localize) | |||
|
|
122 | this._services = Object.create(this._services); | |||
|
|
123 | } | |||
|
|
124 | ||||
|
|
125 | leave() { | |||
|
|
126 | var ctx = this._stack.pop(); | |||
|
|
127 | this._services = ctx.scope; | |||
|
|
128 | ||||
|
|
129 | if (trace.isLogEnabled()) | |||
|
|
130 | trace.log("leave " + ctx.name + " " + (ctx.service || "")); | |||
|
|
131 | } | |||
|
|
132 | } No newline at end of file | |||
| @@ -0,0 +1,282 | |||||
|
|
1 | import { Uuid } from "../Uuid"; | |||
|
|
2 | import { ActivationContext } from "./ActivationContext"; | |||
|
|
3 | import { ActivationError } from "../di"; | |||
|
|
4 | ||||
|
|
5 | ||||
|
|
6 | export class Container { | |||
|
|
7 | _services | |||
|
|
8 | ||||
|
|
9 | _cache | |||
|
|
10 | ||||
|
|
11 | _cleanup: any[] | |||
|
|
12 | ||||
|
|
13 | _root: Container | |||
|
|
14 | ||||
|
|
15 | _parent: Container | |||
|
|
16 | ||||
|
|
17 | constructor(parent?: Container) { | |||
|
|
18 | this._parent = parent; | |||
|
|
19 | this._services = parent ? Object.create(parent._services) : {}; | |||
|
|
20 | this._cache = {}; | |||
|
|
21 | this._cleanup = []; | |||
|
|
22 | this._root = parent ? parent.getRootContainer() : this; | |||
|
|
23 | this._services.container = new ValueDescriptor(this); | |||
|
|
24 | } | |||
|
|
25 | ||||
|
|
26 | getRootContainer() { | |||
|
|
27 | return this._root; | |||
|
|
28 | } | |||
|
|
29 | ||||
|
|
30 | getParent() { | |||
|
|
31 | return this._parent; | |||
|
|
32 | } | |||
|
|
33 | ||||
|
|
34 | getService<T = any>(name: string, def?: T) { | |||
|
|
35 | let d = this._services[name]; | |||
|
|
36 | if (!d) | |||
|
|
37 | if (arguments.length > 1) | |||
|
|
38 | return def; | |||
|
|
39 | else | |||
|
|
40 | throw new Error("Service '" + name + "' isn't found"); | |||
|
|
41 | ||||
|
|
42 | if (d.isInstanceCreated()) | |||
|
|
43 | return d.getInstance(); | |||
|
|
44 | ||||
|
|
45 | var context = new ActivationContext(this, this._services); | |||
|
|
46 | ||||
|
|
47 | try { | |||
|
|
48 | return d.activate(context, name); | |||
|
|
49 | } catch (error) { | |||
|
|
50 | throw new ActivationError(name, context.getStack(), error); | |||
|
|
51 | } | |||
|
|
52 | } | |||
|
|
53 | ||||
|
|
54 | register(nameOrCollection, service?) { | |||
|
|
55 | if (arguments.length == 1) { | |||
|
|
56 | var data = nameOrCollection; | |||
|
|
57 | for (let name in data) | |||
|
|
58 | this.register(name, data[name]); | |||
|
|
59 | } else { | |||
|
|
60 | if (!(service instanceof Descriptor)) | |||
|
|
61 | service = new ValueDescriptor(service); | |||
|
|
62 | this._services[nameOrCollection] = service; | |||
|
|
63 | } | |||
|
|
64 | return this; | |||
|
|
65 | } | |||
|
|
66 | ||||
|
|
67 | onDispose(callback) { | |||
|
|
68 | if (!(callback instanceof Function)) | |||
|
|
69 | throw new Error("The callback must be a function"); | |||
|
|
70 | this._cleanup.push(callback); | |||
|
|
71 | } | |||
|
|
72 | ||||
|
|
73 | dispose() { | |||
|
|
74 | if (this._cleanup) { | |||
|
|
75 | for (var i = 0; i < this._cleanup.length; i++) | |||
|
|
76 | this._cleanup[i].call(null); | |||
|
|
77 | this._cleanup = null; | |||
|
|
78 | } | |||
|
|
79 | } | |||
|
|
80 | ||||
|
|
81 | /** | |||
|
|
82 | * @param{String|Object} config | |||
|
|
83 | * The configuration of the contaier. Can be either a string or an object, | |||
|
|
84 | * if the configuration is an object it's treated as a collection of | |||
|
|
85 | * services which will be registed in the contaier. | |||
|
|
86 | * | |||
|
|
87 | * @param{Function} opts.contextRequire | |||
|
|
88 | * The function which will be used to load a configuration or types for services. | |||
|
|
89 | * | |||
|
|
90 | */ | |||
|
|
91 | async configure(config, opts) { | |||
|
|
92 | var me = this, | |||
|
|
93 | contextRequire = (opts && opts.contextRequire); | |||
|
|
94 | ||||
|
|
95 | if (typeof (config) === "string") { | |||
|
|
96 | let args; | |||
|
|
97 | if (!contextRequire) { | |||
|
|
98 | var shim = [config, Uuid()].join(config.indexOf("/") != -1 ? "-" : "/"); | |||
|
|
99 | args = await new Promise((resolve, reject) => { | |||
|
|
100 | define(shim, ["require", config], function (ctx, data) { | |||
|
|
101 | resolve([data, { | |||
|
|
102 | contextRequire: ctx | |||
|
|
103 | }]); | |||
|
|
104 | }) | |||
|
|
105 | }); | |||
|
|
106 | require([shim]); | |||
|
|
107 | } else { | |||
|
|
108 | // TODO how to get correct contextRequire for the relative config module? | |||
|
|
109 | args = await new Promise((resolve, reject) => { | |||
|
|
110 | contextRequire([config], function (data) { | |||
|
|
111 | resolve([data, { | |||
|
|
112 | contextRequire: contextRequire | |||
|
|
113 | }]); | |||
|
|
114 | }); | |||
|
|
115 | }); | |||
|
|
116 | } | |||
|
|
117 | ||||
|
|
118 | return me._configure.apply(me, args); | |||
|
|
119 | } else { | |||
|
|
120 | return me._configure(config, opts); | |||
|
|
121 | } | |||
|
|
122 | } | |||
|
|
123 | ||||
|
|
124 | createChildContainer() { | |||
|
|
125 | return new Container(this); | |||
|
|
126 | } | |||
|
|
127 | ||||
|
|
128 | has(id) { | |||
|
|
129 | return id in this._cache; | |||
|
|
130 | } | |||
|
|
131 | ||||
|
|
132 | get(id) { | |||
|
|
133 | return this._cache[id]; | |||
|
|
134 | } | |||
|
|
135 | ||||
|
|
136 | store(id, value) { | |||
|
|
137 | return (this._cache[id] = value); | |||
|
|
138 | } | |||
|
|
139 | ||||
|
|
140 | async _configure(data, opts) { | |||
|
|
141 | var typemap = {}, | |||
|
|
142 | me = this, | |||
|
|
143 | p, | |||
|
|
144 | contextRequire = (opts && opts.contextRequire) || require; | |||
|
|
145 | ||||
|
|
146 | var services = {}; | |||
|
|
147 | ||||
|
|
148 | for (p in data) { | |||
|
|
149 | var service = me._parse(data[p], typemap); | |||
|
|
150 | if (!(service instanceof Descriptor)) | |||
|
|
151 | service = new AggregateDescriptor(service); | |||
|
|
152 | services[p] = service; | |||
|
|
153 | } | |||
|
|
154 | ||||
|
|
155 | me.register(services); | |||
|
|
156 | ||||
|
|
157 | var names = []; | |||
|
|
158 | ||||
|
|
159 | for (p in typemap) | |||
|
|
160 | names.push(p); | |||
|
|
161 | ||||
|
|
162 | return new Promise((resolve, reject) => { | |||
|
|
163 | if (names.length) { | |||
|
|
164 | contextRequire(names, function () { | |||
|
|
165 | for (var i = 0; i < names.length; i++) | |||
|
|
166 | typemap[names[i]] = arguments[i]; | |||
|
|
167 | resolve(me); | |||
|
|
168 | }); | |||
|
|
169 | } else { | |||
|
|
170 | resolve(me); | |||
|
|
171 | } | |||
|
|
172 | }); | |||
|
|
173 | ||||
|
|
174 | } | |||
|
|
175 | ||||
|
|
176 | _parse(data, typemap) { | |||
|
|
177 | if (isPrimitive(data) || data instanceof Descriptor) | |||
|
|
178 | return data; | |||
|
|
179 | if (data.$dependency) { | |||
|
|
180 | return new ReferenceDescriptor( | |||
|
|
181 | data.$dependency, | |||
|
|
182 | data.lazy, | |||
|
|
183 | data.optional, | |||
|
|
184 | data["default"], | |||
|
|
185 | data.services && this._parseObject(data.services, typemap)); | |||
|
|
186 | } else if (data.$value) { | |||
|
|
187 | return !data.parse ? | |||
|
|
188 | new ValueDescriptor(data.$value) : | |||
|
|
189 | new AggregateDescriptor(this._parse(data.$value, typemap)); | |||
|
|
190 | } else if (data.$type || data.$factory) { | |||
|
|
191 | return this._parseService(data, typemap); | |||
|
|
192 | } else if (data instanceof Array) { | |||
|
|
193 | return this._parseArray(data, typemap); | |||
|
|
194 | } | |||
|
|
195 | ||||
|
|
196 | return this._parseObject(data, typemap); | |||
|
|
197 | } | |||
|
|
198 | ||||
|
|
199 | _parseService(data, typemap) { | |||
|
|
200 | var me = this, | |||
|
|
201 | opts: any = { | |||
|
|
202 | owner: this | |||
|
|
203 | }; | |||
|
|
204 | if (data.$type) { | |||
|
|
205 | ||||
|
|
206 | opts.type = data.$type; | |||
|
|
207 | ||||
|
|
208 | if (typeof (data.$type) === "string") { | |||
|
|
209 | typemap[data.$type] = null; | |||
|
|
210 | opts.typeMap = typemap; | |||
|
|
211 | } | |||
|
|
212 | } | |||
|
|
213 | ||||
|
|
214 | if (data.$factory) | |||
|
|
215 | opts.factory = data.$factory; | |||
|
|
216 | ||||
|
|
217 | if (data.services) | |||
|
|
218 | opts.services = me._parseObject(data.services, typemap); | |||
|
|
219 | if (data.inject) | |||
|
|
220 | opts.inject = data.inject instanceof Array ? data.inject.map(function (x) { | |||
|
|
221 | return me._parseObject(x, typemap); | |||
|
|
222 | }) : me._parseObject(data.inject, typemap); | |||
|
|
223 | if (data.params) | |||
|
|
224 | opts.params = me._parse(data.params, typemap); | |||
|
|
225 | ||||
|
|
226 | if (data.activation) { | |||
|
|
227 | if (typeof (data.activation) === "string") { | |||
|
|
228 | switch (data.activation.toLowerCase()) { | |||
|
|
229 | case "singleton": | |||
|
|
230 | opts.activation = ActivationType.SINGLETON; | |||
|
|
231 | break; | |||
|
|
232 | case "container": | |||
|
|
233 | opts.activation = ActivationType.CONTAINER; | |||
|
|
234 | break; | |||
|
|
235 | case "hierarchy": | |||
|
|
236 | opts.activation = ActivationType.HIERARCHY; | |||
|
|
237 | break; | |||
|
|
238 | case "context": | |||
|
|
239 | opts.activation = ActivationType.CONTEXT; | |||
|
|
240 | break; | |||
|
|
241 | case "call": | |||
|
|
242 | opts.activation = ActivationType.CALL; | |||
|
|
243 | break; | |||
|
|
244 | default: | |||
|
|
245 | throw new Error("Unknown activation type: " + | |||
|
|
246 | data.activation); | |||
|
|
247 | } | |||
|
|
248 | } else { | |||
|
|
249 | opts.activation = Number(data.activation); | |||
|
|
250 | } | |||
|
|
251 | } | |||
|
|
252 | ||||
|
|
253 | if (data.cleanup) | |||
|
|
254 | opts.cleanup = data.cleanup; | |||
|
|
255 | ||||
|
|
256 | return new ServiceDescriptor(opts); | |||
|
|
257 | } | |||
|
|
258 | ||||
|
|
259 | _parseObject(data, typemap) { | |||
|
|
260 | if (data.constructor && | |||
|
|
261 | data.constructor.prototype !== Object.prototype) | |||
|
|
262 | return new ValueDescriptor(data); | |||
|
|
263 | ||||
|
|
264 | var o = {}; | |||
|
|
265 | ||||
|
|
266 | for (var p in data) | |||
|
|
267 | o[p] = this._parse(data[p], typemap); | |||
|
|
268 | ||||
|
|
269 | return o; | |||
|
|
270 | } | |||
|
|
271 | ||||
|
|
272 | _parseArray(data, typemap) { | |||
|
|
273 | if (data.constructor && | |||
|
|
274 | data.constructor.prototype !== Array.prototype) | |||
|
|
275 | return new ValueDescriptor(data); | |||
|
|
276 | ||||
|
|
277 | var me = this; | |||
|
|
278 | return data.map(function (x) { | |||
|
|
279 | return me._parse(x, typemap); | |||
|
|
280 | }); | |||
|
|
281 | } | |||
|
|
282 | } No newline at end of file | |||
| @@ -0,0 +1,11 | |||||
|
|
1 | import { ActivationContext } from "./ActivationContext"; | |||
|
|
2 | import { isNull } from "../safe"; | |||
|
|
3 | ||||
|
|
4 | export interface Descriptor { | |||
|
|
5 | activate(context: ActivationContext, name?: string); | |||
|
|
6 | } | |||
|
|
7 | ||||
|
|
8 | export function isDescriptor(instance): instance is Descriptor { | |||
|
|
9 | return (!isNull(instance)) && | |||
|
|
10 | ('activate' in instance); | |||
|
|
11 | } No newline at end of file | |||
| @@ -0,0 +1,98 | |||||
|
|
1 | import { isNull, argumentNotEmptyString, each } from "../safe"; | |||
|
|
2 | ||||
|
|
3 | class ReferenceDescriptor extends Descriptor { | |||
|
|
4 | _name: string | |||
|
|
5 | ||||
|
|
6 | _lazy = false | |||
|
|
7 | ||||
|
|
8 | _optional = false | |||
|
|
9 | ||||
|
|
10 | _default: any | |||
|
|
11 | ||||
|
|
12 | _services: object | |||
|
|
13 | ||||
|
|
14 | constructor(name: string, lazy: boolean, optional: boolean, def, services: object) { | |||
|
|
15 | super(); | |||
|
|
16 | argumentNotEmptyString(name, "name"); | |||
|
|
17 | this._name = name; | |||
|
|
18 | this._lazy = Boolean(lazy); | |||
|
|
19 | this._optional = Boolean(optional); | |||
|
|
20 | this._default = def; | |||
|
|
21 | this._services = services; | |||
|
|
22 | } | |||
|
|
23 | ||||
|
|
24 | activate(context: ActivationContext, name: string) { | |||
|
|
25 | var me = this; | |||
|
|
26 | ||||
|
|
27 | context.enter(name, this, true); | |||
|
|
28 | ||||
|
|
29 | // добавляем сервисы | |||
|
|
30 | if (me._services) { | |||
|
|
31 | for (var p in me._services) { | |||
|
|
32 | var sv = me._services[p]; | |||
|
|
33 | context.register(p, sv instanceof Descriptor ? sv : new AggregateDescriptor(sv)); | |||
|
|
34 | } | |||
|
|
35 | } | |||
|
|
36 | ||||
|
|
37 | if (me._lazy) { | |||
|
|
38 | // сохраняем контекст активации | |||
|
|
39 | context = context.clone(); | |||
|
|
40 | return function (cfg) { | |||
|
|
41 | // защищаем контекст на случай исключения в процессе | |||
|
|
42 | // активации | |||
|
|
43 | var ct = context.clone(); | |||
|
|
44 | try { | |||
|
|
45 | if (cfg) | |||
|
|
46 | each(cfg, function (v, k) { | |||
|
|
47 | ct.register(k, v instanceof Descriptor ? v : new AggregateDescriptor(v)); | |||
|
|
48 | }); | |||
|
|
49 | return me._optional ? ct.getService(me._name, me._default) : ct | |||
|
|
50 | .getService(me._name); | |||
|
|
51 | } catch (error) { | |||
|
|
52 | throw new ActivationError(me._name, ct.getStack(), error); | |||
|
|
53 | } | |||
|
|
54 | }; | |||
|
|
55 | } | |||
|
|
56 | ||||
|
|
57 | var v = me._optional ? | |||
|
|
58 | context.getService(me._name, me._default) : | |||
|
|
59 | context.getService(me._name); | |||
|
|
60 | ||||
|
|
61 | context.leave(); | |||
|
|
62 | return v; | |||
|
|
63 | } | |||
|
|
64 | ||||
|
|
65 | isInstanceCreated() { | |||
|
|
66 | return false; | |||
|
|
67 | } | |||
|
|
68 | ||||
|
|
69 | getInstance() { | |||
|
|
70 | throw new Error("The reference descriptor doesn't allowed to hold an instance"); | |||
|
|
71 | } | |||
|
|
72 | ||||
|
|
73 | toString() { | |||
|
|
74 | var opts = []; | |||
|
|
75 | if (this._optional) | |||
|
|
76 | opts.push("optional"); | |||
|
|
77 | if (this._lazy) | |||
|
|
78 | opts.push("lazy"); | |||
|
|
79 | ||||
|
|
80 | var parts = [ | |||
|
|
81 | "@ref " | |||
|
|
82 | ]; | |||
|
|
83 | if (opts.length) { | |||
|
|
84 | parts.push("{"); | |||
|
|
85 | parts.push(opts.join()); | |||
|
|
86 | parts.push("} "); | |||
|
|
87 | } | |||
|
|
88 | ||||
|
|
89 | parts.push(this._name); | |||
|
|
90 | ||||
|
|
91 | if (!isNull(this._default)) { | |||
|
|
92 | parts.push(" = "); | |||
|
|
93 | parts.push(this._default); | |||
|
|
94 | } | |||
|
|
95 | ||||
|
|
96 | return parts.join(""); | |||
|
|
97 | } | |||
|
|
98 | } No newline at end of file | |||
| @@ -0,0 +1,15 | |||||
|
|
1 | { | |||
|
|
2 | "compilerOptions": { | |||
|
|
3 | "target": "es5", | |||
|
|
4 | "module": "amd", | |||
|
|
5 | "sourceMap": true, | |||
|
|
6 | "outDir" : "build/dist", | |||
|
|
7 | "declaration": true, | |||
|
|
8 | "lib": [ | |||
|
|
9 | "es2015" | |||
|
|
10 | ] | |||
|
|
11 | }, | |||
|
|
12 | "include" : [ | |||
|
|
13 | "src/ts/**/*.ts" | |||
|
|
14 | ] | |||
|
|
15 | } No newline at end of file | |||
| @@ -0,0 +1,15 | |||||
|
|
1 | { | |||
|
|
2 | "compilerOptions": { | |||
|
|
3 | "target": "es5", | |||
|
|
4 | "module": "amd", | |||
|
|
5 | "sourceMap": true, | |||
|
|
6 | "outDir" : "build/test", | |||
|
|
7 | "moduleResolution": "node", | |||
|
|
8 | "lib": [ | |||
|
|
9 | "ES2015" | |||
|
|
10 | ] | |||
|
|
11 | }, | |||
|
|
12 | "include" : [ | |||
|
|
13 | "test/ts/**/*.ts" | |||
|
|
14 | ] | |||
|
|
15 | } No newline at end of file | |||
| @@ -40,7 +40,7 task _buildTs(dependsOn: _npmInstall, ty | |||||
| 40 | inputs.file('tsc.json') |
|
40 | inputs.file('tsc.json') | |
| 41 | outputs.dir(distDir) |
|
41 | outputs.dir(distDir) | |
| 42 |
|
42 | |||
| 43 | commandLine 'node_modules/.bin/tsc', '-p', 'tsc.json' |
|
43 | commandLine 'node_modules/.bin/tsc', '-p', 'tsconfig.json' | |
| 44 | } |
|
44 | } | |
| 45 |
|
45 | |||
| 46 | task _packageMeta(type: Copy) { |
|
46 | task _packageMeta(type: Copy) { | |
| @@ -80,7 +80,7 task buildTests(dependsOn: _localInstall | |||||
| 80 | inputs.file('tsc.test.json') |
|
80 | inputs.file('tsc.test.json') | |
| 81 | outputs.dir(testDir) |
|
81 | outputs.dir(testDir) | |
| 82 |
|
82 | |||
| 83 | commandLine 'node_modules/.bin/tsc', '-p', 'tsc.test.json' |
|
83 | commandLine 'node_modules/.bin/tsc', '-p', 'tsconfig.test.json' | |
| 84 | } |
|
84 | } | |
| 85 |
|
85 | |||
| 86 | task test(dependsOn: [copyJsTests, buildTests], type: Exec) { |
|
86 | task test(dependsOn: [copyJsTests, buildTests], type: Exec) { | |
| @@ -16,7 +16,7 | |||||
| 16 | "integrity": "sha512-xil0KO5wkPoixdBWGIGolPv9dekf6dVkjjJLAFYchfKcd4DICou67rgGCIO7wAh3i5Ff/6j9IDgZz+GU9cMaqQ==", |
|
16 | "integrity": "sha512-xil0KO5wkPoixdBWGIGolPv9dekf6dVkjjJLAFYchfKcd4DICou67rgGCIO7wAh3i5Ff/6j9IDgZz+GU9cMaqQ==", | |
| 17 | "dev": true, |
|
17 | "dev": true, | |
| 18 | "requires": { |
|
18 | "requires": { | |
| 19 |
"@types/node": " |
|
19 | "@types/node": "*" | |
| 20 | } |
|
20 | } | |
| 21 | }, |
|
21 | }, | |
| 22 | "balanced-match": { |
|
22 | "balanced-match": { | |
| @@ -31,7 +31,7 | |||||
| 31 | "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", |
|
31 | "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", | |
| 32 | "dev": true, |
|
32 | "dev": true, | |
| 33 | "requires": { |
|
33 | "requires": { | |
| 34 | "balanced-match": "1.0.0", |
|
34 | "balanced-match": "^1.0.0", | |
| 35 | "concat-map": "0.0.1" |
|
35 | "concat-map": "0.0.1" | |
| 36 | } |
|
36 | } | |
| 37 | }, |
|
37 | }, | |
| @@ -59,8 +59,8 | |||||
| 59 | "integrity": "sha1-g6c/L+pWmJj7c3GTyPhzyvbUXJQ=", |
|
59 | "integrity": "sha1-g6c/L+pWmJj7c3GTyPhzyvbUXJQ=", | |
| 60 | "dev": true, |
|
60 | "dev": true, | |
| 61 | "requires": { |
|
61 | "requires": { | |
| 62 | "foreach": "2.0.5", |
|
62 | "foreach": "^2.0.5", | |
| 63 |
"object-keys": " |
|
63 | "object-keys": "^1.0.8" | |
| 64 | } |
|
64 | } | |
| 65 | }, |
|
65 | }, | |
| 66 | "defined": { |
|
66 | "defined": { | |
| @@ -86,11 +86,11 | |||||
| 86 | "integrity": "sha512-C8Fx/0jFmV5IPoMOFPA9P9G5NtqW+4cOPit3MIuvR2t7Ag2K15EJTpxnHAYTzL+aYQJIESYeXZmDBfOBE1HcpA==", |
|
86 | "integrity": "sha512-C8Fx/0jFmV5IPoMOFPA9P9G5NtqW+4cOPit3MIuvR2t7Ag2K15EJTpxnHAYTzL+aYQJIESYeXZmDBfOBE1HcpA==", | |
| 87 | "dev": true, |
|
87 | "dev": true, | |
| 88 | "requires": { |
|
88 | "requires": { | |
| 89 | "es-to-primitive": "1.1.1", |
|
89 | "es-to-primitive": "^1.1.1", | |
| 90 | "function-bind": "1.1.1", |
|
90 | "function-bind": "^1.1.1", | |
| 91 |
"has": " |
|
91 | "has": "^1.0.1", | |
| 92 | "is-callable": "1.1.3", |
|
92 | "is-callable": "^1.1.3", | |
| 93 | "is-regex": "1.0.4" |
|
93 | "is-regex": "^1.0.4" | |
| 94 | } |
|
94 | } | |
| 95 | }, |
|
95 | }, | |
| 96 | "es-to-primitive": { |
|
96 | "es-to-primitive": { | |
| @@ -99,9 +99,9 | |||||
| 99 | "integrity": "sha1-RTVSSKiJeQNLZ5Lhm7gfK3l13Q0=", |
|
99 | "integrity": "sha1-RTVSSKiJeQNLZ5Lhm7gfK3l13Q0=", | |
| 100 | "dev": true, |
|
100 | "dev": true, | |
| 101 | "requires": { |
|
101 | "requires": { | |
| 102 |
"is-callable": " |
|
102 | "is-callable": "^1.1.1", | |
| 103 | "is-date-object": "1.0.1", |
|
103 | "is-date-object": "^1.0.1", | |
| 104 | "is-symbol": "1.0.1" |
|
104 | "is-symbol": "^1.0.1" | |
| 105 | } |
|
105 | } | |
| 106 | }, |
|
106 | }, | |
| 107 | "faucet": { |
|
107 | "faucet": { | |
| @@ -111,12 +111,12 | |||||
| 111 | "dev": true, |
|
111 | "dev": true, | |
| 112 | "requires": { |
|
112 | "requires": { | |
| 113 | "defined": "0.0.0", |
|
113 | "defined": "0.0.0", | |
| 114 | "duplexer": "0.1.1", |
|
114 | "duplexer": "~0.1.1", | |
| 115 | "minimist": "0.0.5", |
|
115 | "minimist": "0.0.5", | |
| 116 |
"sprintf": " |
|
116 | "sprintf": "~0.1.3", | |
| 117 |
"tap-parser": " |
|
117 | "tap-parser": "~0.4.0", | |
| 118 |
"tape": " |
|
118 | "tape": "~2.3.2", | |
| 119 | "through2": "0.2.3" |
|
119 | "through2": "~0.2.3" | |
| 120 | }, |
|
120 | }, | |
| 121 | "dependencies": { |
|
121 | "dependencies": { | |
| 122 | "deep-equal": { |
|
122 | "deep-equal": { | |
| @@ -143,12 +143,12 | |||||
| 143 | "integrity": "sha1-Lnzgox3wn41oUWZKcYQuDKUFevc=", |
|
143 | "integrity": "sha1-Lnzgox3wn41oUWZKcYQuDKUFevc=", | |
| 144 | "dev": true, |
|
144 | "dev": true, | |
| 145 | "requires": { |
|
145 | "requires": { | |
| 146 |
"deep-equal": " |
|
146 | "deep-equal": "~0.1.0", | |
| 147 | "defined": "0.0.0", |
|
147 | "defined": "~0.0.0", | |
| 148 |
"inherits": " |
|
148 | "inherits": "~2.0.1", | |
| 149 | "jsonify": "0.0.0", |
|
149 | "jsonify": "~0.0.0", | |
| 150 | "resumer": "0.0.0", |
|
150 | "resumer": "~0.0.0", | |
| 151 |
"through": " |
|
151 | "through": "~2.3.4" | |
| 152 | } |
|
152 | } | |
| 153 | } |
|
153 | } | |
| 154 | } |
|
154 | } | |
| @@ -159,7 +159,7 | |||||
| 159 | "integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==", |
|
159 | "integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==", | |
| 160 | "dev": true, |
|
160 | "dev": true, | |
| 161 | "requires": { |
|
161 | "requires": { | |
| 162 | "is-callable": "1.1.3" |
|
162 | "is-callable": "^1.1.3" | |
| 163 | } |
|
163 | } | |
| 164 | }, |
|
164 | }, | |
| 165 | "foreach": { |
|
165 | "foreach": { | |
| @@ -186,12 +186,12 | |||||
| 186 | "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", |
|
186 | "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", | |
| 187 | "dev": true, |
|
187 | "dev": true, | |
| 188 | "requires": { |
|
188 | "requires": { | |
| 189 | "fs.realpath": "1.0.0", |
|
189 | "fs.realpath": "^1.0.0", | |
| 190 |
"inflight": " |
|
190 | "inflight": "^1.0.4", | |
| 191 |
"inherits": "2 |
|
191 | "inherits": "2", | |
| 192 | "minimatch": "3.0.4", |
|
192 | "minimatch": "^3.0.4", | |
| 193 |
"once": " |
|
193 | "once": "^1.3.0", | |
| 194 |
"path-is-absolute": " |
|
194 | "path-is-absolute": "^1.0.0" | |
| 195 | } |
|
195 | } | |
| 196 | }, |
|
196 | }, | |
| 197 | "has": { |
|
197 | "has": { | |
| @@ -200,7 +200,7 | |||||
| 200 | "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", |
|
200 | "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", | |
| 201 | "dev": true, |
|
201 | "dev": true, | |
| 202 | "requires": { |
|
202 | "requires": { | |
| 203 | "function-bind": "1.1.1" |
|
203 | "function-bind": "^1.1.1" | |
| 204 | } |
|
204 | } | |
| 205 | }, |
|
205 | }, | |
| 206 | "inflight": { |
|
206 | "inflight": { | |
| @@ -209,8 +209,8 | |||||
| 209 | "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", |
|
209 | "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", | |
| 210 | "dev": true, |
|
210 | "dev": true, | |
| 211 | "requires": { |
|
211 | "requires": { | |
| 212 |
"once": " |
|
212 | "once": "^1.3.0", | |
| 213 |
"wrappy": "1 |
|
213 | "wrappy": "1" | |
| 214 | } |
|
214 | } | |
| 215 | }, |
|
215 | }, | |
| 216 | "inherits": { |
|
216 | "inherits": { | |
| @@ -237,7 +237,7 | |||||
| 237 | "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", |
|
237 | "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", | |
| 238 | "dev": true, |
|
238 | "dev": true, | |
| 239 | "requires": { |
|
239 | "requires": { | |
| 240 |
"has": " |
|
240 | "has": "^1.0.1" | |
| 241 | } |
|
241 | } | |
| 242 | }, |
|
242 | }, | |
| 243 | "is-symbol": { |
|
243 | "is-symbol": { | |
| @@ -264,7 +264,7 | |||||
| 264 | "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", |
|
264 | "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", | |
| 265 | "dev": true, |
|
265 | "dev": true, | |
| 266 | "requires": { |
|
266 | "requires": { | |
| 267 |
"brace-expansion": " |
|
267 | "brace-expansion": "^1.1.7" | |
| 268 | } |
|
268 | } | |
| 269 | }, |
|
269 | }, | |
| 270 | "minimist": { |
|
270 | "minimist": { | |
| @@ -291,7 +291,7 | |||||
| 291 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", |
|
291 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", | |
| 292 | "dev": true, |
|
292 | "dev": true, | |
| 293 | "requires": { |
|
293 | "requires": { | |
| 294 |
"wrappy": "1 |
|
294 | "wrappy": "1" | |
| 295 | } |
|
295 | } | |
| 296 | }, |
|
296 | }, | |
| 297 | "path-is-absolute": { |
|
297 | "path-is-absolute": { | |
| @@ -312,10 +312,10 | |||||
| 312 | "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", |
|
312 | "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", | |
| 313 | "dev": true, |
|
313 | "dev": true, | |
| 314 | "requires": { |
|
314 | "requires": { | |
| 315 |
"core-util-is": " |
|
315 | "core-util-is": "~1.0.0", | |
| 316 |
"inherits": " |
|
316 | "inherits": "~2.0.1", | |
| 317 | "isarray": "0.0.1", |
|
317 | "isarray": "0.0.1", | |
| 318 |
"string_decoder": " |
|
318 | "string_decoder": "~0.10.x" | |
| 319 | } |
|
319 | } | |
| 320 | }, |
|
320 | }, | |
| 321 | "requirejs": { |
|
321 | "requirejs": { | |
| @@ -330,7 +330,7 | |||||
| 330 | "integrity": "sha512-c7rwLofp8g1U+h1KNyHL/jicrKg1Ek4q+Lr33AL65uZTinUZHe30D5HlyN5V9NW0JX1D5dXQ4jqW5l7Sy/kGfw==", |
|
330 | "integrity": "sha512-c7rwLofp8g1U+h1KNyHL/jicrKg1Ek4q+Lr33AL65uZTinUZHe30D5HlyN5V9NW0JX1D5dXQ4jqW5l7Sy/kGfw==", | |
| 331 | "dev": true, |
|
331 | "dev": true, | |
| 332 | "requires": { |
|
332 | "requires": { | |
| 333 | "path-parse": "1.0.5" |
|
333 | "path-parse": "^1.0.5" | |
| 334 | } |
|
334 | } | |
| 335 | }, |
|
335 | }, | |
| 336 | "resumer": { |
|
336 | "resumer": { | |
| @@ -339,7 +339,7 | |||||
| 339 | "integrity": "sha1-8ej0YeQGS6Oegq883CqMiT0HZ1k=", |
|
339 | "integrity": "sha1-8ej0YeQGS6Oegq883CqMiT0HZ1k=", | |
| 340 | "dev": true, |
|
340 | "dev": true, | |
| 341 | "requires": { |
|
341 | "requires": { | |
| 342 |
"through": " |
|
342 | "through": "~2.3.4" | |
| 343 | } |
|
343 | } | |
| 344 | }, |
|
344 | }, | |
| 345 | "sprintf": { |
|
345 | "sprintf": { | |
| @@ -354,9 +354,9 | |||||
| 354 | "integrity": "sha1-0E3iyJ4Tf019IG8Ia17S+ua+jOo=", |
|
354 | "integrity": "sha1-0E3iyJ4Tf019IG8Ia17S+ua+jOo=", | |
| 355 | "dev": true, |
|
355 | "dev": true, | |
| 356 | "requires": { |
|
356 | "requires": { | |
| 357 | "define-properties": "1.1.2", |
|
357 | "define-properties": "^1.1.2", | |
| 358 |
"es-abstract": " |
|
358 | "es-abstract": "^1.5.0", | |
| 359 |
"function-bind": " |
|
359 | "function-bind": "^1.0.2" | |
| 360 | } |
|
360 | } | |
| 361 | }, |
|
361 | }, | |
| 362 | "string_decoder": { |
|
362 | "string_decoder": { | |
| @@ -371,8 +371,8 | |||||
| 371 | "integrity": "sha1-pOrhkMENdsehEZIf84u+TVjwnuo=", |
|
371 | "integrity": "sha1-pOrhkMENdsehEZIf84u+TVjwnuo=", | |
| 372 | "dev": true, |
|
372 | "dev": true, | |
| 373 | "requires": { |
|
373 | "requires": { | |
| 374 |
"inherits": " |
|
374 | "inherits": "~2.0.1", | |
| 375 |
"readable-stream": " |
|
375 | "readable-stream": "~1.1.11" | |
| 376 | } |
|
376 | } | |
| 377 | }, |
|
377 | }, | |
| 378 | "tape": { |
|
378 | "tape": { | |
| @@ -381,19 +381,19 | |||||
| 381 | "integrity": "sha512-6fKIXknLpoe/Jp4rzHKFPpJUHDHDqn8jus99IfPnHIjyz78HYlefTGD3b5EkbQzuLfaEvmfPK3IolLgq2xT3kw==", |
|
381 | "integrity": "sha512-6fKIXknLpoe/Jp4rzHKFPpJUHDHDqn8jus99IfPnHIjyz78HYlefTGD3b5EkbQzuLfaEvmfPK3IolLgq2xT3kw==", | |
| 382 | "dev": true, |
|
382 | "dev": true, | |
| 383 | "requires": { |
|
383 | "requires": { | |
| 384 | "deep-equal": "1.0.1", |
|
384 | "deep-equal": "~1.0.1", | |
| 385 | "defined": "1.0.0", |
|
385 | "defined": "~1.0.0", | |
| 386 | "for-each": "0.3.3", |
|
386 | "for-each": "~0.3.3", | |
| 387 | "function-bind": "1.1.1", |
|
387 | "function-bind": "~1.1.1", | |
| 388 | "glob": "7.1.2", |
|
388 | "glob": "~7.1.2", | |
| 389 | "has": "1.0.3", |
|
389 | "has": "~1.0.3", | |
| 390 | "inherits": "2.0.3", |
|
390 | "inherits": "~2.0.3", | |
| 391 | "minimist": "1.2.0", |
|
391 | "minimist": "~1.2.0", | |
| 392 | "object-inspect": "1.6.0", |
|
392 | "object-inspect": "~1.6.0", | |
| 393 | "resolve": "1.7.1", |
|
393 | "resolve": "~1.7.1", | |
| 394 | "resumer": "0.0.0", |
|
394 | "resumer": "~0.0.0", | |
| 395 | "string.prototype.trim": "1.1.2", |
|
395 | "string.prototype.trim": "~1.1.2", | |
| 396 | "through": "2.3.8" |
|
396 | "through": "~2.3.8" | |
| 397 | } |
|
397 | } | |
| 398 | }, |
|
398 | }, | |
| 399 | "through": { |
|
399 | "through": { | |
| @@ -408,8 +408,8 | |||||
| 408 | "integrity": "sha1-6zKE2k6jEbbMis42U3SKUqvyWj8=", |
|
408 | "integrity": "sha1-6zKE2k6jEbbMis42U3SKUqvyWj8=", | |
| 409 | "dev": true, |
|
409 | "dev": true, | |
| 410 | "requires": { |
|
410 | "requires": { | |
| 411 |
"readable-stream": " |
|
411 | "readable-stream": "~1.1.9", | |
| 412 |
"xtend": " |
|
412 | "xtend": "~2.1.1" | |
| 413 | } |
|
413 | } | |
| 414 | }, |
|
414 | }, | |
| 415 | "typescript": { |
|
415 | "typescript": { | |
| @@ -430,7 +430,7 | |||||
| 430 | "integrity": "sha1-bv7MKk2tjmlixJAbM3znuoe10os=", |
|
430 | "integrity": "sha1-bv7MKk2tjmlixJAbM3znuoe10os=", | |
| 431 | "dev": true, |
|
431 | "dev": true, | |
| 432 | "requires": { |
|
432 | "requires": { | |
| 433 | "object-keys": "0.4.0" |
|
433 | "object-keys": "~0.4.0" | |
| 434 | }, |
|
434 | }, | |
| 435 | "dependencies": { |
|
435 | "dependencies": { | |
| 436 | "object-keys": { |
|
436 | "object-keys": { | |
| @@ -8,7 +8,7 | |||||
| 8 |
|
8 | |||
| 9 | declare var window: any; |
|
9 | declare var window: any; | |
| 10 |
|
10 | |||
| 11 |
let _window |
|
11 | let _window: any = 'undefined' !== typeof window ? window : null; | |
| 12 |
|
12 | |||
| 13 | // Unique ID creation requires a high quality random # generator. We |
|
13 | // Unique ID creation requires a high quality random # generator. We | |
| 14 | // feature |
|
14 | // feature | |
| @@ -41,7 +41,7 function setupBrowser() { | |||||
| 41 | // If all else fails, use Math.random(). It's fast, but is of |
|
41 | // If all else fails, use Math.random(). It's fast, but is of | |
| 42 | // unspecified |
|
42 | // unspecified | |
| 43 | // quality. |
|
43 | // quality. | |
| 44 |
let |
|
44 | let _rnds = new Array(16); | |
| 45 | _rng = function () { |
|
45 | _rng = function () { | |
| 46 | for (var i = 0, r; i < 16; i++) { |
|
46 | for (var i = 0, r; i < 16; i++) { | |
| 47 | if ((i & 0x03) === 0) { |
|
47 | if ((i & 0x03) === 0) { | |
| @@ -92,8 +92,8 for (let i = 0; i < 256; i++) { | |||||
| 92 | } |
|
92 | } | |
| 93 |
|
93 | |||
| 94 | // **`parse()` - Parse a UUID into it's component bytes** |
|
94 | // **`parse()` - Parse a UUID into it's component bytes** | |
| 95 |
function parse(s, buf?, offset?) |
|
95 | function _parse(s, buf?, offset?): Array<string> { | |
| 96 |
let i = (buf && offset) || 0, |
|
96 | let i = (buf && offset) || 0, ii = 0; | |
| 97 |
|
97 | |||
| 98 | buf = buf || []; |
|
98 | buf = buf || []; | |
| 99 | s.toLowerCase().replace(/[0-9a-f]{2}/g, function (oct) { |
|
99 | s.toLowerCase().replace(/[0-9a-f]{2}/g, function (oct) { | |
| @@ -111,8 +111,8 function parse(s, buf?, offset?) : Array | |||||
| 111 | } |
|
111 | } | |
| 112 |
|
112 | |||
| 113 | // **`unparse()` - Convert UUID byte array (ala parse()) into a string** |
|
113 | // **`unparse()` - Convert UUID byte array (ala parse()) into a string** | |
| 114 |
function unparse(buf, offset?) |
|
114 | function _unparse(buf, offset?): string { | |
| 115 |
let i = offset || 0, |
|
115 | let i = offset || 0, bth = _byteToHex; | |
| 116 | return bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + |
|
116 | return bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + | |
| 117 | bth[buf[i++]] + '-' + bth[buf[i++]] + bth[buf[i++]] + '-' + |
|
117 | bth[buf[i++]] + '-' + bth[buf[i++]] + bth[buf[i++]] + '-' + | |
| 118 | bth[buf[i++]] + bth[buf[i++]] + '-' + bth[buf[i++]] + |
|
118 | bth[buf[i++]] + bth[buf[i++]] + '-' + bth[buf[i++]] + | |
| @@ -143,10 +143,10 let _nodeId = [ | |||||
| 143 | let _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff; |
|
143 | let _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff; | |
| 144 |
|
144 | |||
| 145 | // Previous uuid creation time |
|
145 | // Previous uuid creation time | |
| 146 |
let _lastMSecs = 0, |
|
146 | let _lastMSecs = 0, _lastNSecs = 0; | |
| 147 |
|
147 | |||
| 148 | // See https://github.com/broofa/node-uuid for API details |
|
148 | // See https://github.com/broofa/node-uuid for API details | |
| 149 |
function v1(options?, buf?, offset?) |
|
149 | function _v1(options?, buf?, offset?): string { | |
| 150 | let i = buf && offset || 0; |
|
150 | let i = buf && offset || 0; | |
| 151 | let b = buf || []; |
|
151 | let b = buf || []; | |
| 152 |
|
152 | |||
| @@ -225,13 +225,13 function v1(options?, buf?, offset?) : s | |||||
| 225 | b[i + n] = node[n]; |
|
225 | b[i + n] = node[n]; | |
| 226 | } |
|
226 | } | |
| 227 |
|
227 | |||
| 228 | return buf ? buf : unparse(b); |
|
228 | return buf ? buf : _unparse(b); | |
| 229 | } |
|
229 | } | |
| 230 |
|
230 | |||
| 231 | // **`v4()` - Generate random UUID** |
|
231 | // **`v4()` - Generate random UUID** | |
| 232 |
|
232 | |||
| 233 | // See https://github.com/broofa/node-uuid for API details |
|
233 | // See https://github.com/broofa/node-uuid for API details | |
| 234 |
function v4(options?, buf?, offset?) |
|
234 | function _v4(options?, buf?, offset?): string { | |
| 235 | // Deprecated - 'format' argument, as supported in v1.2 |
|
235 | // Deprecated - 'format' argument, as supported in v1.2 | |
| 236 | let i = buf && offset || 0; |
|
236 | let i = buf && offset || 0; | |
| 237 |
|
237 | |||
| @@ -254,29 +254,17 function v4(options?, buf?, offset?) : s | |||||
| 254 | } |
|
254 | } | |
| 255 | } |
|
255 | } | |
| 256 |
|
256 | |||
| 257 | return buf || unparse(rnds); |
|
257 | return buf || _unparse(rnds); | |
|
|
258 | } | |||
|
|
259 | ||||
|
|
260 | export function Uuid() { | |||
|
|
261 | ||||
| 258 | } |
|
262 | } | |
| 259 |
|
263 | |||
| 260 | // Export public API |
|
264 | export namespace Uuid { | |
| 261 | const empty = "00000000-0000-0000-0000-000000000000"; |
|
265 | export const v4 = _v4; | |
| 262 |
|
266 | export const v1 = _v1; | ||
| 263 | interface uuid { |
|
267 | export const empty = "00000000-0000-0000-0000-000000000000"; | |
| 264 | (options?, buf?, offset?) : string; |
|
268 | export const parse = _parse; | |
| 265 | v1(options?, buf?, offset?) : string; |
|
269 | export const unparse = _unparse; | |
| 266 | v4(options?, buf?, offset?) : string; |
|
270 | } No newline at end of file | |
| 267 | readonly empty: string; |
|
|||
| 268 | parse(s, buf?, offset?) : Array<string>; |
|
|||
| 269 | unparse(buf, offset?) : string; |
|
|||
| 270 | } |
|
|||
| 271 |
|
||||
| 272 | export = <uuid>(() =>{ |
|
|||
| 273 | var f : any = function(options?, buf?, offset?) : string { |
|
|||
| 274 | return v4(options, buf, offset); |
|
|||
| 275 | }; |
|
|||
| 276 | f.v1 = v1; |
|
|||
| 277 | f.v4 = v4; |
|
|||
| 278 | f.empty = empty; |
|
|||
| 279 | f.parse = parse; |
|
|||
| 280 | f.unparse = unparse; |
|
|||
| 281 | return f; |
|
|||
| 282 | })(); No newline at end of file |
|
|||
| @@ -45,7 +45,7 export function isNullOrEmptyString(str) | |||||
| 45 | return true; |
|
45 | return true; | |
| 46 | } |
|
46 | } | |
| 47 |
|
47 | |||
| 48 | export function isNotEmptyArray(arg) { |
|
48 | export function isNotEmptyArray(arg): arg is Array<any> { | |
| 49 | return (arg instanceof Array && arg.length > 0); |
|
49 | return (arg instanceof Array && arg.length > 0); | |
| 50 | } |
|
50 | } | |
| 51 |
|
51 | |||
| @@ -61,7 +61,7 export function isNotEmptyArray(arg) { | |||||
| 61 | * @returns Результат вызова функции <c>cb</c>, либо <c>undefined</c> |
|
61 | * @returns Результат вызова функции <c>cb</c>, либо <c>undefined</c> | |
| 62 | * если достигнут конец массива. |
|
62 | * если достигнут конец массива. | |
| 63 | */ |
|
63 | */ | |
| 64 | export function each(obj, cb, thisArg) { |
|
64 | export function each(obj, cb, thisArg?) { | |
| 65 | argumentNotNull(cb, "cb"); |
|
65 | argumentNotNull(cb, "cb"); | |
| 66 | var i, x; |
|
66 | var i, x; | |
| 67 | if (obj instanceof Array) { |
|
67 | if (obj instanceof Array) { | |
| @@ -81,11 +81,55 export function each(obj, cb, thisArg) { | |||||
| 81 | } |
|
81 | } | |
| 82 | } |
|
82 | } | |
| 83 |
|
83 | |||
|
|
84 | /** Copies property values from a source object to the destination and returns | |||
|
|
85 | * the destination onject. | |||
|
|
86 | * | |||
|
|
87 | * @param dest The destination object into which properties from the source | |||
|
|
88 | * object will be copied. | |||
|
|
89 | * @param source The source of values which will be copied to the destination | |||
|
|
90 | * object. | |||
|
|
91 | * @param template An optional parameter specifies which properties should be | |||
|
|
92 | * copied from the source and how to map them to the destination. If the | |||
|
|
93 | * template is an array it contains the list of property names to copy from the | |||
|
|
94 | * source to the destination. In case of object the templates contains the map | |||
|
|
95 | * where keys are property names in the source and the values are property | |||
|
|
96 | * names in the destination object. If the template isn't specified then the | |||
|
|
97 | * own properties of the source are entirely copied to the destination. | |||
|
|
98 | * | |||
|
|
99 | */ | |||
|
|
100 | export function mixin<T,S>(dest: T, source: S, template?: string[] | object) : T & S { | |||
|
|
101 | argumentNotNull(dest, "to"); | |||
|
|
102 | let _res = <T & S>dest; | |||
|
|
103 | ||||
|
|
104 | if (template instanceof Array) { | |||
|
|
105 | for(let i = 0; i < template.length; i++) { | |||
|
|
106 | let p = template[i]; | |||
|
|
107 | if (p in source) | |||
|
|
108 | _res[p] = source[p]; | |||
|
|
109 | } | |||
|
|
110 | } else if (template) { | |||
|
|
111 | let keys = Object.keys(source); | |||
|
|
112 | for(let i = 0; i < keys.length; i++) { | |||
|
|
113 | let p = keys[i]; | |||
|
|
114 | if (p in template) | |||
|
|
115 | _res[template[p]] = source[p]; | |||
|
|
116 | } | |||
|
|
117 | } else { | |||
|
|
118 | let keys = Object.keys(source); | |||
|
|
119 | for(let i = 0; i < keys.length; i++) { | |||
|
|
120 | let p = keys[i]; | |||
|
|
121 | _res[p] = source[p]; | |||
|
|
122 | } | |||
|
|
123 | } | |||
|
|
124 | ||||
|
|
125 | return _res; | |||
|
|
126 | } | |||
|
|
127 | ||||
| 84 | /** Wraps the specified function to emulate an asynchronous execution. |
|
128 | /** Wraps the specified function to emulate an asynchronous execution. | |
| 85 | * @param{Object} thisArg [Optional] Object which will be passed as 'this' to the function. |
|
129 | * @param{Object} thisArg [Optional] Object which will be passed as 'this' to the function. | |
| 86 | * @param{Function|String} fn [Required] Function wich will be wrapped. |
|
130 | * @param{Function|String} fn [Required] Function wich will be wrapped. | |
| 87 | */ |
|
131 | */ | |
| 88 |
export function async(_fn: (...args: any[]) => any, thisArg) |
|
132 | export function async(_fn: (...args: any[]) => any, thisArg): (...args: any[]) => PromiseLike<any> { | |
| 89 | let fn = _fn; |
|
133 | let fn = _fn; | |
| 90 |
|
134 | |||
| 91 | if (arguments.length == 2 && !(fn instanceof Function)) |
|
135 | if (arguments.length == 2 && !(fn instanceof Function)) | |
| @@ -94,7 +138,7 export function async(_fn: (...args: any | |||||
| 94 | if (fn == null) |
|
138 | if (fn == null) | |
| 95 | throw new Error("The function must be specified"); |
|
139 | throw new Error("The function must be specified"); | |
| 96 |
|
140 | |||
| 97 |
function wrapresult(x, e?) |
|
141 | function wrapresult(x, e?): PromiseLike<any> { | |
| 98 | if (e) { |
|
142 | if (e) { | |
| 99 | return { |
|
143 | return { | |
| 100 | then: function (cb, eb) { |
|
144 | then: function (cb, eb) { | |
| @@ -129,19 +173,18 export function async(_fn: (...args: any | |||||
| 129 | }; |
|
173 | }; | |
| 130 | } |
|
174 | } | |
| 131 |
|
175 | |||
| 132 |
export function delegate(target, _method: ( |
|
176 | export function delegate<T, K extends keyof T>(target: T, _method: (K | Function)) { | |
| 133 |
let method |
|
177 | let method; | |
| 134 |
|
178 | |||
| 135 | if (!(_method instanceof Function)) { |
|
179 | if (!(_method instanceof Function)) { | |
| 136 | argumentNotNull(target, "target"); |
|
180 | argumentNotNull(target, "target"); | |
| 137 | method = target[_method]; |
|
181 | method = target[_method]; | |
|
|
182 | if (!(method instanceof Function)) | |||
|
|
183 | throw new Error("'method' argument must be a Function or a method name"); | |||
| 138 | } else { |
|
184 | } else { | |
| 139 | method = _method; |
|
185 | method = _method; | |
| 140 | } |
|
186 | } | |
| 141 |
|
187 | |||
| 142 | if (!(method instanceof Function)) |
|
|||
| 143 | throw new Error("'method' argument must be a Function or a method name"); |
|
|||
| 144 |
|
||||
| 145 | return function () { |
|
188 | return function () { | |
| 146 | return method.apply(target, arguments); |
|
189 | return method.apply(target, arguments); | |
| 147 | }; |
|
190 | }; | |
| 1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
| 1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
| 1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
| 1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now
