| @@ -40,7 +40,7 export class Cancellation implements ICa | |||||
| 40 | else |
|
40 | else | |
| 41 | this._cbs.push(cb); |
|
41 | this._cbs.push(cb); | |
| 42 |
|
42 | |||
| 43 |
|
|
43 | const me = this; | |
| 44 | return { |
|
44 | return { | |
| 45 |
|
|
45 | destroy() { | |
| 46 |
|
|
46 | me._unregister(cb); | |
| @@ -51,7 +51,7 export class Cancellation implements ICa | |||||
| 51 |
|
51 | |||
| 52 | private _unregister(cb) { |
|
52 | private _unregister(cb) { | |
| 53 |
|
|
53 | if (this._cbs) { | |
| 54 |
|
|
54 | const i = this._cbs.indexOf(cb); | |
| 55 |
|
|
55 | if (i >= 0) | |
| 56 |
|
|
56 | this._cbs.splice(i, 1); | |
| 57 | } |
|
57 | } | |
| @@ -63,7 +63,6 export class Cancellation implements ICa | |||||
| 63 |
|
63 | |||
| 64 | this._reason = (reason = reason || new Error("Operation cancelled")); |
|
64 | this._reason = (reason = reason || new Error("Operation cancelled")); | |
| 65 |
|
65 | |||
| 66 |
|
||||
| 67 | if (this._cbs) { |
|
66 | if (this._cbs) { | |
| 68 | this._cbs.forEach(cb => cb(reason)); |
|
67 | this._cbs.forEach(cb => cb(reason)); | |
| 69 | this._cbs = null; |
|
68 | this._cbs = null; | |
| @@ -86,4 +85,4 export class Cancellation implements ICa | |||||
| 86 |
|
|
85 | return destroyed; | |
| 87 | } |
|
86 | } | |
| 88 | }; |
|
87 | }; | |
| 89 | } No newline at end of file |
|
88 | } | |
| @@ -1,23 +1,18 | |||||
| 1 |
import { IObservable, IDestroyable, ICancellation } from |
|
1 | import { IObservable, IDestroyable, ICancellation } from "./interfaces"; | |
| 2 |
import { Cancellation } from |
|
2 | import { Cancellation } from "./Cancellation"; | |
| 3 |
import { argumentNotNull } from |
|
3 | import { argumentNotNull } from "./safe"; | |
| 4 |
|
||||
| 5 |
|
4 | |||
| 6 | interface Handler<T> { |
|
5 | type Handler<T> = (x: T) => void; | |
| 7 | (x: T): void |
|
|||
| 8 | } |
|
|||
| 9 |
|
6 | |||
| 10 | interface Initializer<T> { |
|
7 | type Initializer<T> = (notify: Handler<T>, error?: (e: any) => void, complete?: () => void) => void; | |
| 11 | (notify: Handler<T>, error?: (e: any) => void, complete?: () => void): void; |
|
|||
| 12 | } |
|
|||
| 13 |
|
8 | |||
| 14 |
|
|
9 | // TODO: think about to move this interfaces.ts and make it public | |
| 15 | interface IObserver<T> { |
|
10 | interface IObserver<T> { | |
| 16 | next(event: T): void |
|
11 | next(event: T): void; | |
| 17 |
|
12 | |||
| 18 | error(e: any): void |
|
13 | error(e: any): void; | |
| 19 |
|
14 | |||
| 20 | complete(): void |
|
15 | complete(): void; | |
| 21 | } |
|
16 | } | |
| 22 |
|
17 | |||
| 23 | const noop = () => {}; |
|
18 | const noop = () => { }; | |
| @@ -27,10 +22,9 export class Observable<T> implements IO | |||||
| 27 |
|
22 | |||
| 28 | private _observers = new Array<IObserver<T>>(); |
|
23 | private _observers = new Array<IObserver<T>>(); | |
| 29 |
|
24 | |||
|
|
25 | private _complete: boolean; | |||
| 30 |
|
26 | |||
| 31 |
private _ |
|
27 | private _error: any; | |
| 32 |
|
||||
| 33 | private _error: any |
|
|||
| 34 |
|
28 | |||
| 35 | constructor(func?: Initializer<T>) { |
|
29 | constructor(func?: Initializer<T>) { | |
| 36 | if (func) |
|
30 | if (func) | |
| @@ -54,10 +48,10 export class Observable<T> implements IO | |||||
| 54 | on(next: Handler<T>, error?: Handler<any>, complete?: () => void): IDestroyable { |
|
48 | on(next: Handler<T>, error?: Handler<any>, complete?: () => void): IDestroyable { | |
| 55 | argumentNotNull(next, "next"); |
|
49 | argumentNotNull(next, "next"); | |
| 56 |
|
50 | |||
| 57 |
|
|
51 | const me = this; | |
| 58 |
|
52 | |||
| 59 |
|
|
53 | const observer: IObserver<T> & IDestroyable = { | |
| 60 |
|
|
54 | next, | |
| 61 | error: error ? error.bind(null) : noop, |
|
55 | error: error ? error.bind(null) : noop, | |
| 62 | complete: complete ? complete.bind(null) : noop, |
|
56 | complete: complete ? complete.bind(null) : noop, | |
| 63 |
|
57 | |||
| @@ -68,7 +62,6 export class Observable<T> implements IO | |||||
| 68 |
|
62 | |||
| 69 | this._addObserver(observer); |
|
63 | this._addObserver(observer); | |
| 70 |
|
64 | |||
| 71 |
|
||||
| 72 | return observer; |
|
65 | return observer; | |
| 73 | } |
|
66 | } | |
| 74 |
|
67 | |||
| @@ -95,14 +88,14 export class Observable<T> implements IO | |||||
| 95 | */ |
|
88 | */ | |
| 96 | next(ct: ICancellation = Cancellation.none): Promise<T> { |
|
89 | next(ct: ICancellation = Cancellation.none): Promise<T> { | |
| 97 | return new Promise<T>((resolve, reject) => { |
|
90 | return new Promise<T>((resolve, reject) => { | |
| 98 |
|
|
91 | const observer: IObserver<T> = { | |
| 99 | next: resolve, |
|
92 | next: resolve, | |
| 100 | error: reject, |
|
93 | error: reject, | |
| 101 | complete: () => reject("No more events are available") |
|
94 | complete: () => reject("No more events are available") | |
| 102 | }; |
|
95 | }; | |
| 103 |
|
96 | |||
| 104 | if (this._addOnce(observer) && ct.isSupported()) { |
|
97 | if (this._addOnce(observer) && ct.isSupported()) { | |
| 105 |
ct.register( |
|
98 | ct.register(e => { | |
| 106 | this._removeOnce(observer); |
|
99 | this._removeOnce(observer); | |
| 107 | reject(e); |
|
100 | reject(e); | |
| 108 | }); |
|
101 | }); | |
| @@ -131,48 +124,44 export class Observable<T> implements IO | |||||
| 131 | } |
|
124 | } | |
| 132 |
|
125 | |||
| 133 | private _removeOnce(d: IObserver<T>) { |
|
126 | private _removeOnce(d: IObserver<T>) { | |
| 134 |
|
|
127 | const i = this._once.indexOf(d); | |
| 135 | if (i >= 0) |
|
128 | if (i >= 0) | |
| 136 | this._once.splice(i, 1); |
|
129 | this._once.splice(i, 1); | |
| 137 | } |
|
130 | } | |
| 138 |
|
131 | |||
| 139 | private _removeObserver(d: IObserver<T>) { |
|
132 | private _removeObserver(d: IObserver<T>) { | |
| 140 |
|
|
133 | const i = this._observers.indexOf(d); | |
| 141 | if (i >= 0) |
|
134 | if (i >= 0) | |
| 142 | this._observers.splice(i, 1); |
|
135 | this._observers.splice(i, 1); | |
| 143 | } |
|
136 | } | |
| 144 |
|
137 | |||
| 145 | private _notify(guard: (observer: IObserver<T>) => void) { |
|
138 | private _notify(guard: (observer: IObserver<T>) => void) { | |
| 146 |
|
|
139 | this._once.forEach(guard); | |
| 147 | for (let i = 0; i < this._once.length; i++) |
|
|||
| 148 | guard(this._once[i]); |
|
|||
| 149 |
|
|
140 | this._once = []; | |
| 150 | } |
|
|||
| 151 |
|
141 | |||
| 152 | for (let i = 0; i < this._observers.length; i++) |
|
142 | this._observers.forEach(guard); | |
| 153 | guard(this._observers[i]); |
|
|||
| 154 | } |
|
143 | } | |
| 155 |
|
144 | |||
| 156 | protected _notifyNext(evt: T) { |
|
145 | protected _notifyNext(evt: T) { | |
| 157 |
|
|
146 | const guard = (observer: IObserver<T>) => { | |
| 158 | try { |
|
147 | try { | |
| 159 | observer.next(evt); |
|
148 | observer.next(evt); | |
| 160 | } catch (e) { |
|
149 | } catch (e) { | |
| 161 | this.onObserverException(e); |
|
150 | this.onObserverException(e); | |
| 162 | } |
|
151 | } | |
| 163 | } |
|
152 | }; | |
| 164 |
|
153 | |||
| 165 | this._notify(guard); |
|
154 | this._notify(guard); | |
| 166 | } |
|
155 | } | |
| 167 |
|
156 | |||
| 168 | protected _notifyError(e: any) { |
|
157 | protected _notifyError(e: any) { | |
| 169 |
|
|
158 | const guard = (observer: IObserver<T>) => { | |
| 170 | try { |
|
159 | try { | |
| 171 | observer.error(e); |
|
160 | observer.error(e); | |
| 172 | } catch (e) { |
|
161 | } catch (e) { | |
| 173 | this.onObserverException(e); |
|
162 | this.onObserverException(e); | |
| 174 | } |
|
163 | } | |
| 175 | } |
|
164 | }; | |
| 176 |
|
165 | |||
| 177 | this._notify(guard); |
|
166 | this._notify(guard); | |
| 178 | this._observers = []; |
|
167 | this._observers = []; | |
| @@ -180,16 +169,16 export class Observable<T> implements IO | |||||
| 180 | } |
|
169 | } | |
| 181 |
|
170 | |||
| 182 | protected _notifyCompleted() { |
|
171 | protected _notifyCompleted() { | |
| 183 |
|
|
172 | const guard = (observer: IObserver<T>) => { | |
| 184 | try { |
|
173 | try { | |
| 185 | observer.complete(); |
|
174 | observer.complete(); | |
| 186 | } catch (e) { |
|
175 | } catch (e) { | |
| 187 | this.onObserverException(e); |
|
176 | this.onObserverException(e); | |
| 188 | } |
|
177 | } | |
| 189 | } |
|
178 | }; | |
| 190 |
|
179 | |||
| 191 | this._notify(guard); |
|
180 | this._notify(guard); | |
| 192 | this._observers = []; |
|
181 | this._observers = []; | |
| 193 | this._complete = true; |
|
182 | this._complete = true; | |
| 194 | } |
|
183 | } | |
| 195 | } No newline at end of file |
|
184 | } | |
| @@ -8,7 +8,7 | |||||
| 8 |
|
8 | |||
| 9 | declare var window: any; |
|
9 | declare var window: any; | |
| 10 |
|
10 | |||
| 11 |
|
|
11 | const _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 | |
| @@ -19,14 +19,14 let _rng; | |||||
| 19 |
|
19 | |||
| 20 | function setupBrowser() { |
|
20 | function setupBrowser() { | |
| 21 | // Allow for MSIE11 msCrypto |
|
21 | // Allow for MSIE11 msCrypto | |
| 22 |
|
|
22 | const _crypto = _window.crypto || _window.msCrypto; | |
| 23 |
|
23 | |||
| 24 | if (!_rng && _crypto && _crypto.getRandomValues) { |
|
24 | if (!_rng && _crypto && _crypto.getRandomValues) { | |
| 25 | // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto |
|
25 | // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto | |
| 26 | // |
|
26 | // | |
| 27 | // Moderately fast, high quality |
|
27 | // Moderately fast, high quality | |
| 28 | try { |
|
28 | try { | |
| 29 |
|
|
29 | const _rnds8 = new Uint8Array(16); | |
| 30 | _rng = function whatwgRNG() { |
|
30 | _rng = function whatwgRNG() { | |
| 31 | _crypto.getRandomValues(_rnds8); |
|
31 | _crypto.getRandomValues(_rnds8); | |
| 32 | return _rnds8; |
|
32 | return _rnds8; | |
| @@ -41,9 +41,9 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 |
|
|
44 | const _rnds = new Array(16); | |
| 45 |
_rng = |
|
45 | _rng = () => { | |
| 46 |
for ( |
|
46 | for (let i = 0, r; i < 16; i++) { | |
| 47 | if ((i & 0x03) === 0) { |
|
47 | if ((i & 0x03) === 0) { | |
| 48 | r = Math.random() * 0x100000000; |
|
48 | r = Math.random() * 0x100000000; | |
| 49 | } |
|
49 | } | |
| @@ -52,7 +52,7 function setupBrowser() { | |||||
| 52 |
|
52 | |||
| 53 | return _rnds; |
|
53 | return _rnds; | |
| 54 | }; |
|
54 | }; | |
| 55 |
if ( |
|
55 | if ("undefined" !== typeof console && console.warn) { | |
| 56 | console.warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()"); |
|
56 | console.warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()"); | |
| 57 | } |
|
57 | } | |
| 58 | } |
|
58 | } | |
| @@ -63,12 +63,10 function setupNode() { | |||||
| 63 | // http://nodejs.org/docs/v0.6.2/api/crypto.html |
|
63 | // http://nodejs.org/docs/v0.6.2/api/crypto.html | |
| 64 | // |
|
64 | // | |
| 65 | // Moderately fast, high quality |
|
65 | // Moderately fast, high quality | |
| 66 |
if ( |
|
66 | if ("function" === typeof require) { | |
| 67 | try { |
|
67 | try { | |
| 68 |
|
|
68 | const _rb = require("crypto").randomBytes; | |
| 69 |
_rng = _rb && |
|
69 | _rng = _rb && (() => _rb(16)); | |
| 70 | return _rb(16); |
|
|||
| 71 | }; |
|
|||
| 72 | _rng(); |
|
70 | _rng(); | |
| 73 | } catch (e) { /**/ } |
|
71 | } catch (e) { /**/ } | |
| 74 | } |
|
72 | } | |
| @@ -81,22 +79,22 if (_window) { | |||||
| 81 | } |
|
79 | } | |
| 82 |
|
80 | |||
| 83 | // Buffer class to use |
|
81 | // Buffer class to use | |
| 84 |
|
|
82 | const BufferClass = ("function" === typeof Buffer) ? Buffer : Array; | |
| 85 |
|
83 | |||
| 86 | // Maps for number <-> hex string conversion |
|
84 | // Maps for number <-> hex string conversion | |
| 87 |
|
|
85 | const _byteToHex = []; | |
| 88 |
|
|
86 | const _hexToByte = {}; | |
| 89 | for (let i = 0; i < 256; i++) { |
|
87 | for (let i = 0; i < 256; i++) { | |
| 90 | _byteToHex[i] = (i + 0x100).toString(16).substr(1); |
|
88 | _byteToHex[i] = (i + 0x100).toString(16).substr(1); | |
| 91 | _hexToByte[_byteToHex[i]] = i; |
|
89 | _hexToByte[_byteToHex[i]] = i; | |
| 92 | } |
|
90 | } | |
| 93 |
|
91 | |||
| 94 | // **`parse()` - Parse a UUID into it's component bytes** |
|
92 | // **`parse()` - Parse a UUID into it's component bytes** | |
| 95 | function _parse(s, buf?, offset?): Array<string> { |
|
93 | export function _parse(s, buf?, offset?): Array<string> { | |
| 96 |
|
|
94 | const i = (buf && offset) || 0; let ii = 0; | |
| 97 |
|
95 | |||
| 98 | buf = buf || []; |
|
96 | buf = buf || []; | |
| 99 |
s.toLowerCase().replace(/[0-9a-f]{2}/g, |
|
97 | s.toLowerCase().replace(/[0-9a-f]{2}/g, oct => { | |
| 100 | if (ii < 16) { // Don't overflow! |
|
98 | if (ii < 16) { // Don't overflow! | |
| 101 | buf[i + ii++] = _hexToByte[oct]; |
|
99 | buf[i + ii++] = _hexToByte[oct]; | |
| 102 | } |
|
100 | } | |
| @@ -112,11 +110,11 function _parse(s, buf?, offset?): Array | |||||
| 112 |
|
110 | |||
| 113 | // **`unparse()` - Convert UUID byte array (ala parse()) into a string** |
|
111 | // **`unparse()` - Convert UUID byte array (ala parse()) into a string** | |
| 114 | function _unparse(buf, offset?): string { |
|
112 | function _unparse(buf, offset?): string { | |
| 115 |
let i = offset || 0 |
|
113 | let i = offset || 0; const bth = _byteToHex; | |
| 116 | return bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + |
|
114 | return bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + | |
| 117 |
bth[buf[i++]] + |
|
115 | bth[buf[i++]] + "-" + bth[buf[i++]] + bth[buf[i++]] + "-" + | |
| 118 |
bth[buf[i++]] + bth[buf[i++]] + |
|
116 | bth[buf[i++]] + bth[buf[i++]] + "-" + bth[buf[i++]] + | |
| 119 |
bth[buf[i++]] + |
|
117 | bth[buf[i++]] + "-" + bth[buf[i++]] + bth[buf[i++]] + | |
| 120 | bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]]; |
|
118 | bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]]; | |
| 121 | } |
|
119 | } | |
| 122 |
|
120 | |||
| @@ -126,11 +124,11 function _unparse(buf, offset?): string | |||||
| 126 | // and http://docs.python.org/library/uuid.html |
|
124 | // and http://docs.python.org/library/uuid.html | |
| 127 |
|
125 | |||
| 128 | // random #'s we need to init node and clockseq |
|
126 | // random #'s we need to init node and clockseq | |
| 129 |
|
|
127 | const _seedBytes = _rng(); | |
| 130 |
|
128 | |||
| 131 | // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = |
|
129 | // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = | |
| 132 | // 1) |
|
130 | // 1) | |
| 133 |
|
|
131 | const _nodeId = [ | |
| 134 | _seedBytes[0] | 0x01, |
|
132 | _seedBytes[0] | 0x01, | |
| 135 | _seedBytes[1], |
|
133 | _seedBytes[1], | |
| 136 | _seedBytes[2], |
|
134 | _seedBytes[2], | |
| @@ -143,12 +141,12 let _nodeId = [ | |||||
| 143 | let _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff; |
|
141 | let _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff; | |
| 144 |
|
142 | |||
| 145 | // Previous uuid creation time |
|
143 | // Previous uuid creation time | |
| 146 |
let _lastMSecs = 0 |
|
144 | let _lastMSecs = 0; let _lastNSecs = 0; | |
| 147 |
|
145 | |||
| 148 | // See https://github.com/broofa/node-uuid for API details |
|
146 | // See https://github.com/broofa/node-uuid for API details | |
| 149 | function _v1(options?, buf?, offset?): string { |
|
147 | export function _v1(options?, buf?, offset?): string { | |
| 150 | let i = buf && offset || 0; |
|
148 | let i = buf && offset || 0; | |
| 151 |
|
|
149 | const b = buf || []; | |
| 152 |
|
150 | |||
| 153 | options = options || {}; |
|
151 | options = options || {}; | |
| 154 |
|
152 | |||
| @@ -170,7 +168,7 function _v1(options?, buf?, offset?): s | |||||
| 170 | let nsecs = (options.nsecs != null) ? options.nsecs : _lastNSecs + 1; |
|
168 | let nsecs = (options.nsecs != null) ? options.nsecs : _lastNSecs + 1; | |
| 171 |
|
169 | |||
| 172 | // Time since last uuid creation (in msecs) |
|
170 | // Time since last uuid creation (in msecs) | |
| 173 |
|
|
171 | const dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs) / 10000; | |
| 174 |
|
172 | |||
| 175 | // Per 4.2.1.2, Bump clockseq on clock regression |
|
173 | // Per 4.2.1.2, Bump clockseq on clock regression | |
| 176 | if (dt < 0 && options.clockseq == null) { |
|
174 | if (dt < 0 && options.clockseq == null) { | |
| @@ -187,7 +185,7 function _v1(options?, buf?, offset?): s | |||||
| 187 | // Per 4.2.1.2 Throw error if too many uuids are requested |
|
185 | // Per 4.2.1.2 Throw error if too many uuids are requested | |
| 188 | if (nsecs >= 10000) { |
|
186 | if (nsecs >= 10000) { | |
| 189 | throw new Error( |
|
187 | throw new Error( | |
| 190 |
|
|
188 | "uuid.v1(): Can't create more than 10M uuids/sec"); | |
| 191 | } |
|
189 | } | |
| 192 |
|
190 | |||
| 193 | _lastMSecs = msecs; |
|
191 | _lastMSecs = msecs; | |
| @@ -198,14 +196,14 function _v1(options?, buf?, offset?): s | |||||
| 198 | msecs += 12219292800000; |
|
196 | msecs += 12219292800000; | |
| 199 |
|
197 | |||
| 200 | // `time_low` |
|
198 | // `time_low` | |
| 201 |
|
|
199 | const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000; | |
| 202 | b[i++] = tl >>> 24 & 0xff; |
|
200 | b[i++] = tl >>> 24 & 0xff; | |
| 203 | b[i++] = tl >>> 16 & 0xff; |
|
201 | b[i++] = tl >>> 16 & 0xff; | |
| 204 | b[i++] = tl >>> 8 & 0xff; |
|
202 | b[i++] = tl >>> 8 & 0xff; | |
| 205 | b[i++] = tl & 0xff; |
|
203 | b[i++] = tl & 0xff; | |
| 206 |
|
204 | |||
| 207 | // `time_mid` |
|
205 | // `time_mid` | |
| 208 |
|
|
206 | const tmh = (msecs / 0x100000000 * 10000) & 0xfffffff; | |
| 209 | b[i++] = tmh >>> 8 & 0xff; |
|
207 | b[i++] = tmh >>> 8 & 0xff; | |
| 210 | b[i++] = tmh & 0xff; |
|
208 | b[i++] = tmh & 0xff; | |
| 211 |
|
209 | |||
| @@ -220,7 +218,7 function _v1(options?, buf?, offset?): s | |||||
| 220 | b[i++] = clockseq & 0xff; |
|
218 | b[i++] = clockseq & 0xff; | |
| 221 |
|
219 | |||
| 222 | // `node` |
|
220 | // `node` | |
| 223 |
|
|
221 | const node = options.node || _nodeId; | |
| 224 | for (let n = 0; n < 6; n++) { |
|
222 | for (let n = 0; n < 6; n++) { | |
| 225 | b[i + n] = node[n]; |
|
223 | b[i + n] = node[n]; | |
| 226 | } |
|
224 | } | |
| @@ -231,17 +229,17 function _v1(options?, buf?, offset?): s | |||||
| 231 | // **`v4()` - Generate random UUID** |
|
229 | // **`v4()` - Generate random UUID** | |
| 232 |
|
230 | |||
| 233 | // See https://github.com/broofa/node-uuid for API details |
|
231 | // See https://github.com/broofa/node-uuid for API details | |
| 234 | function _v4(options?, buf?, offset?): string { |
|
232 | export function _v4(options?, buf?, offset?): string { | |
| 235 | // Deprecated - 'format' argument, as supported in v1.2 |
|
233 | // Deprecated - 'format' argument, as supported in v1.2 | |
| 236 |
|
|
234 | const i = buf && offset || 0; | |
| 237 |
|
235 | |||
| 238 |
if (typeof (options) === |
|
236 | if (typeof (options) === "string") { | |
| 239 |
buf = (options === |
|
237 | buf = (options === "binary") ? new BufferClass(16) : null; | |
| 240 | options = null; |
|
238 | options = null; | |
| 241 | } |
|
239 | } | |
| 242 | options = options || {}; |
|
240 | options = options || {}; | |
| 243 |
|
241 | |||
| 244 |
|
|
242 | const rnds = options.random || (options.rng || _rng)(); | |
| 245 |
|
243 | |||
| 246 | // Per 4.4, set bits for version and `clock_seq_hi_and_reserved` |
|
244 | // Per 4.4, set bits for version and `clock_seq_hi_and_reserved` | |
| 247 | rnds[6] = (rnds[6] & 0x0f) | 0x40; |
|
245 | rnds[6] = (rnds[6] & 0x0f) | 0x40; | |
| @@ -266,5 +264,4 export namespace Uuid { | |||||
| 266 | export const v1 = _v1; |
|
264 | export const v1 = _v1; | |
| 267 | export const empty = "00000000-0000-0000-0000-000000000000"; |
|
265 | export const empty = "00000000-0000-0000-0000-000000000000"; | |
| 268 | export const parse = _parse; |
|
266 | export const parse = _parse; | |
| 269 | export const unparse = _unparse; |
|
267 | } | |
| 270 | } No newline at end of file |
|
|||
| @@ -3,28 +3,26 import { argumentNotNull, argumentNotEmp | |||||
| 3 | import { Descriptor, ServiceMap, isDescriptor } from "./interfaces"; |
|
3 | import { Descriptor, ServiceMap, isDescriptor } from "./interfaces"; | |
| 4 | import { Container } from "./Container"; |
|
4 | import { Container } from "./Container"; | |
| 5 |
|
5 | |||
| 6 |
|
|
6 | const trace = TraceSource.get("@implab/core/di/ActivationContext"); | |
| 7 |
|
7 | |||
| 8 |
export |
|
8 | export interface ActivationContextInfo { | |
| 9 | name: string |
|
9 | name: string; | |
| 10 |
|
10 | |||
| 11 |
service: |
|
11 | service: Descriptor; | |
| 12 |
|
12 | |||
| 13 | scope: ServiceMap |
|
13 | scope: ServiceMap; | |
| 14 | } |
|
14 | } | |
| 15 |
|
15 | |||
|
|
16 | export class ActivationContext { | |||
|
|
17 | _cache: object; | |||
| 16 |
|
18 | |||
| 17 | export class ActivationContext { |
|
19 | _services: ServiceMap; | |
| 18 | _cache: object |
|
|||
| 19 |
|
||||
| 20 | _services: ServiceMap |
|
|||
| 21 |
|
20 | |||
| 22 | _stack: ActivationContextInfo[] |
|
21 | _stack: ActivationContextInfo[]; | |
| 23 |
|
22 | |||
| 24 |
_visited: |
|
23 | _visited: object; | |
| 25 |
|
24 | |||
| 26 |
container: |
|
25 | container: Container; | |
| 27 |
|
||||
| 28 |
|
26 | |||
| 29 | constructor(container: Container, services: ServiceMap, cache?: object, visited?) { |
|
27 | constructor(container: Container, services: ServiceMap, cache?: object, visited?) { | |
| 30 | argumentNotNull(container, "container"); |
|
28 | argumentNotNull(container, "container"); | |
| @@ -38,13 +36,13 export class ActivationContext { | |||||
| 38 | } |
|
36 | } | |
| 39 |
|
37 | |||
| 40 | getService(name, def?): any { |
|
38 | getService(name, def?): any { | |
| 41 |
|
|
39 | const d = this._services[name]; | |
| 42 |
|
40 | |||
| 43 | if (!d) |
|
41 | if (!d) | |
| 44 | if (arguments.length > 1) |
|
42 | if (arguments.length > 1) | |
| 45 | return def; |
|
43 | return def; | |
| 46 | else |
|
44 | else | |
| 47 |
throw new Error( |
|
45 | throw new Error(`Service ${name} not found`); | |
| 48 |
|
46 | |||
| 49 | return d.activate(this, name); |
|
47 | return d.activate(this, name); | |
| 50 | } |
|
48 | } | |
| @@ -68,47 +66,43 export class ActivationContext { | |||||
| 68 | this._cache, |
|
66 | this._cache, | |
| 69 | this._visited |
|
67 | this._visited | |
| 70 | ); |
|
68 | ); | |
| 71 |
|
||||
| 72 | } |
|
69 | } | |
| 73 |
|
70 | |||
| 74 | has(id) { |
|
71 | has(id: string) { | |
| 75 | return id in this._cache; |
|
72 | return id in this._cache; | |
| 76 | } |
|
73 | } | |
| 77 |
|
74 | |||
| 78 | get(id) { |
|
75 | get(id: string) { | |
| 79 | return this._cache[id]; |
|
76 | return this._cache[id]; | |
| 80 | } |
|
77 | } | |
| 81 |
|
78 | |||
| 82 | store(id, value) { |
|
79 | store(id: string, value) { | |
| 83 | return (this._cache[id] = value); |
|
80 | return (this._cache[id] = value); | |
| 84 | } |
|
81 | } | |
| 85 |
|
82 | |||
| 86 |
parse(data: |
|
83 | parse(data: object, name: string) { | |
| 87 | var me = this; |
|
|||
| 88 | if (isPrimitive(data)) |
|
84 | if (isPrimitive(data)) | |
| 89 | return data; |
|
85 | return data; | |
| 90 |
|
86 | |||
| 91 | if (isDescriptor(data)) { |
|
87 | if (isDescriptor(data)) { | |
| 92 | return data.activate(this, name); |
|
88 | return data.activate(this, name); | |
| 93 | } else if (data instanceof Array) { |
|
89 | } else if (data instanceof Array) { | |
| 94 |
|
|
90 | this.enter(name); | |
| 95 |
|
|
91 | const v = data.map( (x, i) => this.parse(x, `[${i}]`)); | |
| 96 | return me.parse(x, "." + i); |
|
92 | this.leave(); | |
| 97 | }); |
|
|||
| 98 | me.leave(); |
|
|||
| 99 | return v; |
|
93 | return v; | |
| 100 | } else { |
|
94 | } else { | |
| 101 |
|
|
95 | this.enter(name); | |
| 102 |
|
|
96 | const result = {}; | |
| 103 |
for ( |
|
97 | for (const p in data) | |
| 104 |
result[p] = |
|
98 | result[p] = this.parse(data[p], "." + p); | |
| 105 |
|
|
99 | this.leave(); | |
| 106 | return result; |
|
100 | return result; | |
| 107 | } |
|
101 | } | |
| 108 | } |
|
102 | } | |
| 109 |
|
103 | |||
| 110 | visit(id) { |
|
104 | visit(id: string) { | |
| 111 |
|
|
105 | const count = this._visited[id] || 0; | |
| 112 | this._visited[id] = count + 1; |
|
106 | this._visited[id] = count + 1; | |
| 113 | return count; |
|
107 | return count; | |
| 114 | } |
|
108 | } | |
| @@ -117,12 +111,12 export class ActivationContext { | |||||
| 117 | return this._stack.slice().reverse(); |
|
111 | return this._stack.slice().reverse(); | |
| 118 | } |
|
112 | } | |
| 119 |
|
113 | |||
| 120 | enter(name, d?, localize?) { |
|
114 | enter(name: string, d?: Descriptor, localize?: boolean) { | |
| 121 | if (trace.isLogEnabled()) |
|
115 | if (trace.isLogEnabled()) | |
| 122 | trace.log("enter " + name + " " + (d || "") + |
|
116 | trace.log("enter " + name + " " + (d || "") + | |
| 123 | (localize ? " localize" : "")); |
|
117 | (localize ? " localize" : "")); | |
| 124 | this._stack.push({ |
|
118 | this._stack.push({ | |
| 125 |
|
|
119 | name, | |
| 126 | service: d, |
|
120 | service: d, | |
| 127 | scope: this._services |
|
121 | scope: this._services | |
| 128 | }); |
|
122 | }); | |
| @@ -131,10 +125,10 export class ActivationContext { | |||||
| 131 | } |
|
125 | } | |
| 132 |
|
126 | |||
| 133 | leave() { |
|
127 | leave() { | |
| 134 |
|
|
128 | const ctx = this._stack.pop(); | |
| 135 | this._services = ctx.scope; |
|
129 | this._services = ctx.scope; | |
| 136 |
|
130 | |||
| 137 | if (trace.isLogEnabled()) |
|
131 | if (trace.isLogEnabled()) | |
| 138 | trace.log("leave " + ctx.name + " " + (ctx.service || "")); |
|
132 | trace.log("leave " + ctx.name + " " + (ctx.service || "")); | |
| 139 | } |
|
133 | } | |
| 140 | } No newline at end of file |
|
134 | } | |
| @@ -1,13 +1,13 | |||||
| 1 | import { ActivationContextInfo } from "./ActivationContext"; |
|
1 | import { ActivationContextInfo } from "./ActivationContext"; | |
| 2 |
|
2 | |||
| 3 | export class ActivationError { |
|
3 | export class ActivationError { | |
| 4 | activationStack: ActivationContextInfo[] |
|
4 | activationStack: ActivationContextInfo[]; | |
| 5 |
|
5 | |||
| 6 | service: string |
|
6 | service: string; | |
| 7 |
|
7 | |||
| 8 | innerException: any |
|
8 | innerException: any; | |
| 9 |
|
9 | |||
| 10 | message: string |
|
10 | message: string; | |
| 11 |
|
11 | |||
| 12 | constructor(service: string, activationStack: ActivationContextInfo[], innerException) { |
|
12 | constructor(service: string, activationStack: ActivationContextInfo[], innerException) { | |
| 13 | this.message = "Failed to activate the service"; |
|
13 | this.message = "Failed to activate the service"; | |
| @@ -17,7 +17,7 export class ActivationError { | |||||
| 17 | } |
|
17 | } | |
| 18 |
|
18 | |||
| 19 | toString() { |
|
19 | toString() { | |
| 20 |
|
|
20 | const parts = [this.message]; | |
| 21 | if (this.service) |
|
21 | if (this.service) | |
| 22 | parts.push("when activating: " + this.service.toString()); |
|
22 | parts.push("when activating: " + this.service.toString()); | |
| 23 |
|
23 | |||
| @@ -26,12 +26,11 export class ActivationError { | |||||
| 26 |
|
26 | |||
| 27 | if (this.activationStack) { |
|
27 | if (this.activationStack) { | |
| 28 | parts.push("at"); |
|
28 | parts.push("at"); | |
| 29 |
this.activationStack |
|
29 | this.activationStack | |
| 30 | parts.push(" " + x.name + " " + |
|
30 | .forEach(x => parts.push(` ${x.name} ${x.service ? x.service.toString() : ""}`)); | |
| 31 | (x.service ? x.service.toString() : "")); |
|
31 | ||
| 32 | }); |
|
|||
| 33 | } |
|
32 | } | |
| 34 |
|
33 | |||
| 35 | return parts.join("\n"); |
|
34 | return parts.join("\n"); | |
| 36 | } |
|
35 | } | |
| 37 | } No newline at end of file |
|
36 | } | |
| @@ -1,11 +1,11 | |||||
| 1 | import { Descriptor } from "./interfaces"; |
|
1 | import { Descriptor } from "./interfaces"; | |
| 2 | import { ActivationContext } from "./ActivationContext"; |
|
2 | import { ActivationContext } from "./ActivationContext"; | |
| 3 |
|
3 | |||
| 4 |
export class AggregateDescriptor |
|
4 | export class AggregateDescriptor implements Descriptor { | |
| 5 |
_value: |
|
5 | _value: object; | |
| 6 |
|
6 | |||
| 7 |
constructor(value: |
|
7 | constructor(value: object) { | |
| 8 |
|
8 | this._value = value; | ||
| 9 | } |
|
9 | } | |
| 10 |
|
10 | |||
| 11 | activate(context: ActivationContext, name: string) { |
|
11 | activate(context: ActivationContext, name: string) { | |
| @@ -18,7 +18,7 export class AggregateDescriptor<T> impl | |||||
| 18 | isInstanceCreated(): boolean { |
|
18 | isInstanceCreated(): boolean { | |
| 19 | return false; |
|
19 | return false; | |
| 20 | } |
|
20 | } | |
| 21 |
getInstance(): |
|
21 | getInstance(): any { | |
| 22 |
throw new Error("Not supported |
|
22 | throw new Error("Not supported"); | |
| 23 | } |
|
23 | } | |
| 24 | } |
|
24 | } | |
| @@ -176,10 +176,6 export class Container { | |||||
| 176 | owner: this |
|
176 | owner: this | |
| 177 | }; |
|
177 | }; | |
| 178 |
|
178 | |||
| 179 | function guard<T>(fn: () => PromiseLike<T>) { |
|
|||
| 180 | return fn(); |
|
|||
| 181 | } |
|
|||
| 182 |
|
||||
| 183 | if (data.$type) { |
|
179 | if (data.$type) { | |
| 184 | if (data.$type instanceof Function) |
|
180 | if (data.$type instanceof Function) | |
| 185 | opts.type = data.$type; |
|
181 | opts.type = data.$type; | |
| @@ -202,7 +198,7 export class Container { | |||||
| 202 | if (data.inject instanceof Array) |
|
198 | if (data.inject instanceof Array) | |
| 203 | opts.inject = await Promise.all(data.inject.map(x => this._parseObject(x, resolver))); |
|
199 | opts.inject = await Promise.all(data.inject.map(x => this._parseObject(x, resolver))); | |
| 204 | else |
|
200 | else | |
| 205 | opts.inject = this._parseObject(data.inject, resolver); |
|
201 | opts.inject = [await this._parseObject(data.inject, resolver)]; | |
| 206 |
|
202 | |||
| 207 | if (data.params) |
|
203 | if (data.params) | |
| 208 | opts.params = this._parse(data.params, resolver); |
|
204 | opts.params = this._parse(data.params, resolver); | |
| @@ -240,7 +236,7 export class Container { | |||||
| 240 | return new ServiceDescriptor(opts); |
|
236 | return new ServiceDescriptor(opts); | |
| 241 | } |
|
237 | } | |
| 242 |
|
238 | |||
| 243 |
_parseObject(data: |
|
239 | _parseObject(data: object, resolver: ModuleResolverBase) { | |
| 244 | if (data.constructor && |
|
240 | if (data.constructor && | |
| 245 | data.constructor.prototype !== Object.prototype) |
|
241 | data.constructor.prototype !== Object.prototype) | |
| 246 | return new ValueDescriptor(data); |
|
242 | return new ValueDescriptor(data); | |
| @@ -248,16 +244,16 export class Container { | |||||
| 248 | const o = {}; |
|
244 | const o = {}; | |
| 249 |
|
245 | |||
| 250 | for (const p in data) |
|
246 | for (const p in data) | |
| 251 |
o[p] = this._parse(data[p], |
|
247 | o[p] = this._parse(data[p], resolver); | |
| 252 |
|
248 | |||
| 253 | return o; |
|
249 | return o; | |
| 254 | } |
|
250 | } | |
| 255 |
|
251 | |||
| 256 | _parseArray(data, typemap) { |
|
252 | _parseArray(data: Array<any>, resolver: ModuleResolverBase) { | |
| 257 | if (data.constructor && |
|
253 | if (data.constructor && | |
| 258 | data.constructor.prototype !== Array.prototype) |
|
254 | data.constructor.prototype !== Array.prototype) | |
| 259 | return new ValueDescriptor(data); |
|
255 | return new ValueDescriptor(data); | |
| 260 |
|
256 | |||
| 261 |
return data.map(x => this._parse(x, |
|
257 | return data.map(x => this._parse(x, resolver)); | |
| 262 | } |
|
258 | } | |
| 263 | } |
|
259 | } | |
| @@ -4,15 +4,15 import { ServiceMap, Descriptor } from " | |||||
| 4 | import { ActivationError } from "./ActivationError"; |
|
4 | import { ActivationError } from "./ActivationError"; | |
| 5 |
|
5 | |||
| 6 | export class ReferenceDescriptor implements Descriptor { |
|
6 | export class ReferenceDescriptor implements Descriptor { | |
| 7 | _name: string |
|
7 | _name: string; | |
| 8 |
|
8 | |||
| 9 | _lazy = false |
|
9 | _lazy = false; | |
| 10 |
|
10 | |||
| 11 | _optional = false |
|
11 | _optional = false; | |
| 12 |
|
12 | |||
| 13 | _default: any |
|
13 | _default: any; | |
| 14 |
|
14 | |||
| 15 | _services: ServiceMap |
|
15 | _services: ServiceMap; | |
| 16 |
|
16 | |||
| 17 | constructor(name: string, lazy: boolean, optional: boolean, def, services: ServiceMap) { |
|
17 | constructor(name: string, lazy: boolean, optional: boolean, def, services: ServiceMap) { | |
| 18 | argumentNotEmptyString(name, "name"); |
|
18 | argumentNotEmptyString(name, "name"); | |
| @@ -24,45 +24,51 export class ReferenceDescriptor impleme | |||||
| 24 | } |
|
24 | } | |
| 25 |
|
25 | |||
| 26 | activate(context: ActivationContext, name: string) { |
|
26 | activate(context: ActivationContext, name: string) { | |
| 27 | var me = this; |
|
|||
| 28 |
|
27 | |||
| 29 | context.enter(name, this, true); |
|
28 | if (this._lazy) { | |
|
|
29 | // сохраняем контекст активации | |||
|
|
30 | context = context.clone(); | |||
| 30 |
|
31 | |||
| 31 | // добавляем сервисы |
|
32 | // добавляем сервисы | |
| 32 |
if ( |
|
33 | if (this._services) { | |
| 33 |
for ( |
|
34 | for (const p of Object.keys(this._services)) | |
| 34 |
|
|
35 | context.register(p, this._services[p]); | |
| 35 | context.register(p, sv); |
|
|||
| 36 | } |
|
|||
| 37 | } |
|
36 | } | |
| 38 |
|
37 | |||
| 39 | if (me._lazy) { |
|
38 | return (cfg: ServiceMap) => { | |
| 40 | // сохраняем контекст активации |
|
|||
| 41 | context = context.clone(); |
|
|||
| 42 | return function (cfg: ServiceMap) { |
|
|||
| 43 | // защищаем контекст на случай исключения в процессе |
|
39 | // защищаем контекст на случай исключения в процессе | |
| 44 | // активации |
|
40 | // активации | |
| 45 |
|
|
41 | const ct = context.clone(); | |
| 46 | try { |
|
42 | try { | |
| 47 | if (cfg) |
|
43 | if (cfg) { | |
| 48 |
for( |
|
44 | for (const k in cfg) | |
| 49 |
ct.register(k, cfg[ |
|
45 | ct.register(k, cfg[k]); | |
|
|
46 | } | |||
| 50 |
|
47 | |||
| 51 |
return |
|
48 | return this._optional ? ct.getService(this._name, this._default) : ct | |
| 52 |
.getService( |
|
49 | .getService(this._name); | |
| 53 | } catch (error) { |
|
50 | } catch (error) { | |
| 54 |
throw new ActivationError( |
|
51 | throw new ActivationError(this._name, ct.getStack(), error); | |
| 55 | } |
|
52 | } | |
| 56 | }; |
|
53 | }; | |
|
|
54 | } else { | |||
|
|
55 | context.enter(name, this, !!this._services); | |||
|
|
56 | ||||
|
|
57 | // добавляем сервисы | |||
|
|
58 | if (this._services) { | |||
|
|
59 | for (const p of Object.keys(this._services)) | |||
|
|
60 | context.register(p, this._services[p]); | |||
| 57 | } |
|
61 | } | |
| 58 |
|
62 | |||
| 59 |
|
|
63 | const v = this._optional ? | |
| 60 |
context.getService( |
|
64 | context.getService(this._name, this._default) : | |
| 61 |
context.getService( |
|
65 | context.getService(this._name); | |
| 62 |
|
66 | |||
| 63 | context.leave(); |
|
67 | context.leave(); | |
|
|
68 | ||||
| 64 | return v; |
|
69 | return v; | |
| 65 | } |
|
70 | } | |
|
|
71 | } | |||
| 66 |
|
72 | |||
| 67 | isInstanceCreated() { |
|
73 | isInstanceCreated() { | |
| 68 | return false; |
|
74 | return false; | |
| @@ -73,13 +79,13 export class ReferenceDescriptor impleme | |||||
| 73 | } |
|
79 | } | |
| 74 |
|
80 | |||
| 75 | toString() { |
|
81 | toString() { | |
| 76 |
|
|
82 | const opts = []; | |
| 77 | if (this._optional) |
|
83 | if (this._optional) | |
| 78 | opts.push("optional"); |
|
84 | opts.push("optional"); | |
| 79 | if (this._lazy) |
|
85 | if (this._lazy) | |
| 80 | opts.push("lazy"); |
|
86 | opts.push("lazy"); | |
| 81 |
|
87 | |||
| 82 |
|
|
88 | const parts = [ | |
| 83 | "@ref " |
|
89 | "@ref " | |
| 84 | ]; |
|
90 | ]; | |
| 85 | if (opts.length) { |
|
91 | if (opts.length) { | |
| @@ -97,4 +103,4 export class ReferenceDescriptor impleme | |||||
| 97 |
|
103 | |||
| 98 | return parts.join(""); |
|
104 | return parts.join(""); | |
| 99 | } |
|
105 | } | |
| 100 | } No newline at end of file |
|
106 | } | |
| @@ -1,8 +1,8 | |||||
| 1 | import { ActivationContext } from "./ActivationContext"; |
|
1 | import { ActivationContext } from "./ActivationContext"; | |
| 2 |
import { Descriptor, ActivationType, ServiceMap |
|
2 | import { Descriptor, ActivationType, ServiceMap } from "./interfaces"; | |
| 3 | import { Container } from "./Container"; |
|
3 | import { Container } from "./Container"; | |
| 4 | import { argumentNotNull, isPrimitive, oid } from "../safe"; |
|
4 | import { argumentNotNull, isPrimitive, oid, isPromise } from "../safe"; | |
| 5 | import { ClientResponse } from "http"; |
|
5 | import { Constructor, Factory } from "../interfaces"; | |
| 6 |
|
6 | |||
| 7 | let cacheId = 0; |
|
7 | let cacheId = 0; | |
| 8 |
|
8 | |||
| @@ -12,12 +12,12 function injectMethod(target, method, co | |||||
| 12 | throw new Error("Method '" + method + "' not found"); |
|
12 | throw new Error("Method '" + method + "' not found"); | |
| 13 |
|
13 | |||
| 14 | if (args instanceof Array) |
|
14 | if (args instanceof Array) | |
| 15 | m.apply(target, context.parse(args, "." + method)); |
|
15 | return m.apply(target, context.parse(args, "." + method)); | |
| 16 | else |
|
16 | else | |
| 17 | m.call(target, context.parse(args, "." + method)); |
|
17 | return m.call(target, context.parse(args, "." + method)); | |
| 18 | } |
|
18 | } | |
| 19 |
|
19 | |||
| 20 | function makeClenupCallback(target, method: (instance) => void | string) { |
|
20 | function makeClenupCallback(target, method: ((instance) => void) | string) { | |
| 21 | if (typeof (method) === "string") { |
|
21 | if (typeof (method) === "string") { | |
| 22 | return () => { |
|
22 | return () => { | |
| 23 | target[method](); |
|
23 | target[method](); | |
| @@ -29,26 +29,26 function makeClenupCallback(target, meth | |||||
| 29 | } |
|
29 | } | |
| 30 | } |
|
30 | } | |
| 31 |
|
31 | |||
| 32 |
export interface ServiceDescriptorParams |
|
32 | export interface ServiceDescriptorParams { | |
| 33 | activation?: ActivationType; |
|
33 | activation?: ActivationType; | |
| 34 |
|
34 | |||
| 35 | owner: Container; |
|
35 | owner: Container; | |
| 36 |
|
36 | |||
| 37 |
type?: Constructor |
|
37 | type?: Constructor; | |
| 38 |
|
38 | |||
| 39 |
factory?: Factory |
|
39 | factory?: Factory; | |
| 40 |
|
40 | |||
| 41 | params?; |
|
41 | params?; | |
| 42 |
|
42 | |||
| 43 | inject?; |
|
43 | inject?: object[]; | |
| 44 |
|
44 | |||
| 45 | services?: ServiceMap; |
|
45 | services?: ServiceMap; | |
| 46 |
|
46 | |||
| 47 |
cleanup?: ( |
|
47 | cleanup?: ((x) => void) | string; | |
| 48 | } |
|
48 | } | |
| 49 |
|
49 | |||
| 50 |
export class ServiceDescriptor |
|
50 | export class ServiceDescriptor implements Descriptor { | |
| 51 |
_instance |
|
51 | _instance; | |
| 52 |
|
52 | |||
| 53 | _hasInstance = false; |
|
53 | _hasInstance = false; | |
| 54 |
|
54 | |||
| @@ -56,21 +56,21 export class ServiceDescriptor<T = {}> i | |||||
| 56 |
|
56 | |||
| 57 | _services: ServiceMap; |
|
57 | _services: ServiceMap; | |
| 58 |
|
58 | |||
| 59 |
_type: Constructor |
|
59 | _type: Constructor = null; | |
| 60 |
|
60 | |||
| 61 |
_factory: Factory |
|
61 | _factory: Factory = null; | |
| 62 |
|
62 | |||
| 63 | _params; |
|
63 | _params; | |
| 64 |
|
64 | |||
| 65 |
_inject: |
|
65 | _inject: object[]; | |
| 66 |
|
66 | |||
| 67 |
_cleanup: ( |
|
67 | _cleanup: ((x) => void) | string; | |
| 68 |
|
68 | |||
| 69 | _cacheId: any; |
|
69 | _cacheId: any; | |
| 70 |
|
70 | |||
| 71 | _owner: Container; |
|
71 | _owner: Container; | |
| 72 |
|
72 | |||
| 73 |
constructor(opts: ServiceDescriptorParams |
|
73 | constructor(opts: ServiceDescriptorParams) { | |
| 74 | argumentNotNull(opts, "opts"); |
|
74 | argumentNotNull(opts, "opts"); | |
| 75 | argumentNotNull(opts.owner, "owner"); |
|
75 | argumentNotNull(opts.owner, "owner"); | |
| 76 |
|
76 | |||
| @@ -90,7 +90,7 export class ServiceDescriptor<T = {}> i | |||||
| 90 | this._params = opts.params; |
|
90 | this._params = opts.params; | |
| 91 |
|
91 | |||
| 92 | if (opts.inject) |
|
92 | if (opts.inject) | |
| 93 |
this._inject = opts.inject |
|
93 | this._inject = opts.inject; | |
| 94 |
|
94 | |||
| 95 | if (opts.services) |
|
95 | if (opts.services) | |
| 96 | this._services = opts.services; |
|
96 | this._services = opts.services; | |
| @@ -219,9 +219,17 export class ServiceDescriptor<T = {}> i | |||||
| 219 |
|
219 | |||
| 220 | if (!this._factory) { |
|
220 | if (!this._factory) { | |
| 221 | const ctor = this._type; |
|
221 | const ctor = this._type; | |
|
|
222 | if (this._params && this._params.length) { | |||
| 222 |
this._factory = (...args) |
|
223 | this._factory = (...args) => { | |
| 223 | return new ctor(...args); |
|
224 | const t = Object.create(ctor.prototype); | |
|
|
225 | const inst = ctor.apply(t, args); | |||
|
|
226 | return isPrimitive(inst) ? t : inst; | |||
| 224 | }; |
|
227 | }; | |
|
|
228 | } else { | |||
|
|
229 | this._factory = () => { | |||
|
|
230 | return new ctor(); | |||
|
|
231 | }; | |||
|
|
232 | } | |||
| 225 | } |
|
233 | } | |
| 226 |
|
234 | |||
| 227 | if (this._params === undefined) { |
|
235 | if (this._params === undefined) { | |
| @@ -1,23 +1,23 | |||||
| 1 | import { Descriptor } from "./interfaces"; |
|
1 | import { Descriptor } from "./interfaces"; | |
| 2 | import { ActivationContext } from "./ActivationContext"; |
|
2 | import { ActivationContext } from "./ActivationContext"; | |
| 3 |
|
3 | |||
| 4 |
export class ValueDescriptor |
|
4 | export class ValueDescriptor implements Descriptor { | |
| 5 |
_value |
|
5 | _value; | |
| 6 |
|
6 | |||
| 7 |
constructor(value |
|
7 | constructor(value) { | |
| 8 | this._value = value; |
|
8 | this._value = value; | |
| 9 | } |
|
9 | } | |
| 10 |
|
10 | |||
| 11 | activate(context: ActivationContext, name: string) { |
|
11 | activate(context: ActivationContext, name: string) { | |
| 12 | context.enter(name); |
|
12 | context.enter(name); | |
| 13 |
|
|
13 | const v = this._value; | |
| 14 | context.leave(); |
|
14 | context.leave(); | |
| 15 |
return v; |
|
15 | return v; | |
| 16 | } |
|
16 | } | |
| 17 | isInstanceCreated(): boolean { |
|
17 | isInstanceCreated(): boolean { | |
| 18 | return true; |
|
18 | return true; | |
| 19 | } |
|
19 | } | |
| 20 |
getInstance() |
|
20 | getInstance() { | |
| 21 | return this._value; |
|
21 | return this._value; | |
| 22 | } |
|
22 | } | |
| 23 | } No newline at end of file |
|
23 | } | |
| @@ -1,5 +1,6 | |||||
| 1 | import { isNull } from "../safe"; |
|
1 | import { isNull } from "../safe"; | |
| 2 | import { ActivationContext } from "./ActivationContext"; |
|
2 | import { ActivationContext } from "./ActivationContext"; | |
|
|
3 | import { Constructor, Factory } from "../interfaces"; | |||
| 3 |
|
4 | |||
| 4 | export interface Descriptor { |
|
5 | export interface Descriptor { | |
| 5 | activate(context: ActivationContext, name?: string); |
|
6 | activate(context: ActivationContext, name?: string); | |
| @@ -7,10 +8,6 export interface Descriptor { | |||||
| 7 | getInstance(); |
|
8 | getInstance(); | |
| 8 | } |
|
9 | } | |
| 9 |
|
10 | |||
| 10 | export type Constructor<T = {}> = new (...args: any[]) => T; |
|
|||
| 11 |
|
||||
| 12 | export type Factory<T = {}> = (...args: any[]) => T; |
|
|||
| 13 |
|
||||
| 14 | export function isDescriptor(instance): instance is Descriptor { |
|
11 | export function isDescriptor(instance): instance is Descriptor { | |
| 15 | return (!isNull(instance)) && |
|
12 | return (!isNull(instance)) && | |
| 16 | ("activate" in instance); |
|
13 | ("activate" in instance); | |
| @@ -1,3 +1,7 | |||||
|
|
1 | export type Constructor<T = {}> = new (...args: any[]) => T; | |||
|
|
2 | ||||
|
|
3 | export type Factory<T = {}> = (...args: any[]) => T; | |||
|
|
4 | ||||
| 1 | export interface IDestroyable { |
|
5 | export interface IDestroyable { | |
| 2 | destroy(); |
|
6 | destroy(); | |
| 3 | } |
|
7 | } | |
| @@ -73,4 +77,4 export interface ICancellable { | |||||
| 73 | export interface IObservable<T> { |
|
77 | export interface IObservable<T> { | |
| 74 | on(next: (x:T) => void, error?: (e:any) => void, complete?:() => void): IDestroyable; |
|
78 | on(next: (x: T) => void, error?: (e: any) => void, complete?: () => void): IDestroyable; | |
| 75 |
next(ct?: ICancellation) |
|
79 | next(ct?: ICancellation): Promise<T>; | |
| 76 | } No newline at end of file |
|
80 | } | |
| @@ -1,7 +1,7 | |||||
| 1 | let _nextOid = 0; |
|
1 | let _nextOid = 0; | |
| 2 | const _oid = Symbol("__oid"); |
|
2 | const _oid = Symbol("__oid"); | |
| 3 |
|
3 | |||
| 4 | export function oid(instance: object) { |
|
4 | export function oid(instance: object): string { | |
| 5 | if (isNull(instance)) |
|
5 | if (isNull(instance)) | |
| 6 | return null; |
|
6 | return null; | |
| 7 |
|
7 | |||
| @@ -52,6 +52,10 export function isString(val) { | |||||
| 52 | return typeof (val) === "string" || val instanceof String; |
|
52 | return typeof (val) === "string" || val instanceof String; | |
| 53 | } |
|
53 | } | |
| 54 |
|
54 | |||
|
|
55 | export function isPromise(val): val is PromiseLike<any> { | |||
|
|
56 | return "then" in val && val.then instanceof Function; | |||
|
|
57 | } | |||
|
|
58 | ||||
| 55 | export function isNullOrEmptyString(str) { |
|
59 | export function isNullOrEmptyString(str) { | |
| 56 | if (str === null || str === undefined || |
|
60 | if (str === null || str === undefined || | |
| 57 | ((typeof (str) === "string" || str instanceof String) && str.length === 0)) |
|
61 | ((typeof (str) === "string" || str instanceof String) && str.length === 0)) | |
| @@ -229,18 +233,17 export function pmap(items, cb) { | |||||
| 229 | argumentNotNull(cb, "cb"); |
|
233 | argumentNotNull(cb, "cb"); | |
| 230 |
|
234 | |||
| 231 | if (items && items.then instanceof Function) |
|
235 | if (items && items.then instanceof Function) | |
| 232 |
return items.then( |
|
236 | return items.then(data => pmap(data, cb)); | |
| 233 | return pmap(data, cb); |
|
|||
| 234 | }); |
|
|||
| 235 |
|
237 | |||
| 236 | if (isNull(items) || !items.length) |
|
238 | if (isNull(items) || !items.length) | |
| 237 | return items; |
|
239 | return items; | |
| 238 |
|
240 | |||
| 239 |
|
|
241 | let i = 0; | |
| 240 |
|
|
242 | const result = []; | |
| 241 |
|
243 | |||
| 242 | function next() { |
|
244 | function next() { | |
| 243 |
|
|
245 | let r; | |
|
|
246 | let ri; | |||
| 244 |
|
247 | |||
| 245 | function chain(x) { |
|
248 | function chain(x) { | |
| 246 | result[ri] = x; |
|
249 | result[ri] = x; | |
| @@ -251,7 +254,7 export function pmap(items, cb) { | |||||
| 251 | r = cb(items[i], i); |
|
254 | r = cb(items[i], i); | |
| 252 | ri = i; |
|
255 | ri = i; | |
| 253 | i++; |
|
256 | i++; | |
| 254 |
if (r |
|
257 | if (isPromise(r)) { | |
| 255 | return r.then(chain); |
|
258 | return r.then(chain); | |
| 256 | } else { |
|
259 | } else { | |
| 257 | result[ri] = r; |
|
260 | result[ri] = r; | |
| @@ -276,12 +279,10 export function pmap(items, cb) { | |||||
| 276 | * обещание, либо первый элемент. |
|
279 | * обещание, либо первый элемент. | |
| 277 | * @async |
|
280 | * @async | |
| 278 | */ |
|
281 | */ | |
| 279 |
export function first(sequence: any, |
|
282 | export function first(sequence, cb: (x) => any, err: (x) => any) { | |
| 280 | if (sequence) { |
|
283 | if (sequence) { | |
| 281 | if (sequence.then instanceof Function) { |
|
284 | if (isPromise(sequence)) { | |
| 282 |
return sequence.then( |
|
285 | return sequence.then(res => first(res, cb, err)); | |
| 283 | return first(res, cb, err); |
|
|||
| 284 | }, err); |
|
|||
| 285 | } else if (sequence && "length" in sequence) { |
|
286 | } else if (sequence && "length" in sequence) { | |
| 286 | if (sequence.length === 0) { |
|
287 | if (sequence.length === 0) { | |
| 287 | if (err) |
|
288 | if (err) | |
| @@ -299,7 +300,7 export function first(sequence: any, cb: | |||||
| 299 | throw new Error("The sequence is required"); |
|
300 | throw new Error("The sequence is required"); | |
| 300 | } |
|
301 | } | |
| 301 |
|
302 | |||
| 302 |
export function destroy(d |
|
303 | export function destroy(d) { | |
| 303 |
if (d && |
|
304 | if (d && "destroy" in d) | |
| 304 | d.destroy(); |
|
305 | d.destroy(); | |
| 305 | } No newline at end of file |
|
306 | } | |
| @@ -1,3 +1,3 | |||||
| 1 |
|
|
1 | define(["./ActivatableTests", "./trace-test", "./TraceSourceTests", "./CancellationTests"]); | |
| 2 | //define(["./CancellationTests"]); |
|
2 | //define(["./CancellationTests"]); | |
| 3 | define(["./ObservableTests"]); No newline at end of file |
|
3 | //define(["./ObservableTests"]); No newline at end of file | |
| @@ -1,13 +1,13 | |||||
| 1 |
import * as tape from |
|
1 | import * as tape from "tape"; | |
| 2 |
import |
|
2 | import { Uuid } from "@implab/core/Uuid"; | |
| 3 |
|
3 | |||
| 4 |
tape( |
|
4 | tape("simple", t => { | |
| 5 | t.pass("sync assert"); |
|
5 | t.pass("sync assert"); | |
| 6 | setTimeout(() => { |
|
6 | setTimeout(() => { | |
| 7 | t.pass("async assert"); |
|
7 | t.pass("async assert"); | |
| 8 |
t.comment( |
|
8 | t.comment(Uuid()); | |
| 9 |
t.ok( |
|
9 | t.ok(Uuid() !== Uuid()); | |
| 10 | // end should be called after the last assertion |
|
10 | // end should be called after the last assertion | |
| 11 | t.end(); |
|
11 | t.end(); | |
| 12 | }, 100); |
|
12 | }, 100); | |
| 13 | }); No newline at end of file |
|
13 | }); | |
| @@ -1,6 +1,6 | |||||
| 1 | { |
|
1 | { | |
| 2 | "compilerOptions": { |
|
2 | "compilerOptions": { | |
| 3 |
"target": "es |
|
3 | "target": "es3", | |
| 4 | "module": "amd", |
|
4 | "module": "amd", | |
| 5 | "sourceMap": true, |
|
5 | "sourceMap": true, | |
| 6 | "outDir" : "build/dist", |
|
6 | "outDir" : "build/dist", | |
| @@ -1,12 +1,12 | |||||
| 1 | { |
|
1 | { | |
| 2 | "compilerOptions": { |
|
2 | "compilerOptions": { | |
| 3 |
"target": "es |
|
3 | "target": "es3", | |
| 4 | "module": "amd", |
|
4 | "module": "amd", | |
| 5 | "sourceMap": true, |
|
5 | "sourceMap": true, | |
| 6 | "outDir" : "build/test", |
|
6 | "outDir" : "build/test", | |
| 7 | "moduleResolution": "node", |
|
7 | "moduleResolution": "node", | |
| 8 | "lib": [ |
|
8 | "lib": [ | |
| 9 |
" |
|
9 | "es2015" | |
| 10 | ] |
|
10 | ] | |
| 11 | }, |
|
11 | }, | |
| 12 | "include" : [ |
|
12 | "include" : [ | |
General Comments 0
You need to be logged in to leave comments.
Login now
