chronograph-npm 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cg.d.ts +6 -0
- package/dist/cg.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/cg.js +51 -5
package/dist/cg.d.ts
CHANGED
|
@@ -8,6 +8,12 @@ export class CG {
|
|
|
8
8
|
* @param {Object} param0.variable
|
|
9
9
|
*/
|
|
10
10
|
static override onChronographUpdate({ chronograph }: any): Promise<void>;
|
|
11
|
+
constructor({ homey }: {
|
|
12
|
+
homey: any;
|
|
13
|
+
});
|
|
14
|
+
homey: any;
|
|
15
|
+
isReady: boolean;
|
|
16
|
+
ready: any;
|
|
11
17
|
init({}?: {}): Promise<CG>;
|
|
12
18
|
api: any;
|
|
13
19
|
realtimeFunction(event: any, data: any): Promise<void>;
|
package/dist/cg.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cg.d.ts","sourceRoot":"","sources":["../src/cg.js"],"names":[],"mappings":"AAAA;IAGI;;
|
|
1
|
+
{"version":3,"file":"cg.d.ts","sourceRoot":"","sources":["../src/cg.js"],"names":[],"mappings":"AAAA;IAGI;;oBAwBC;IA+BD,sBAQC;IAgCD;;;OAGG;IACH,yEAGC;IA7ED;;OAWC;IATG,WAA6B;IAC7B,iBAAoB;IAIpB,WAAyC;IAM7C,2BAaC;IAVW,SAAqC;IAyBjD,uDAMC;IAGD,eAAe;IACf,kBAeC;CAeJ"}
|
package/package.json
CHANGED
package/src/cg.js
CHANGED
|
@@ -3,7 +3,6 @@ class CG {
|
|
|
3
3
|
|
|
4
4
|
static async init({ homey } = {}) {
|
|
5
5
|
let cg = new CG({ homey });
|
|
6
|
-
CG.Current = cg;
|
|
7
6
|
|
|
8
7
|
homey.app.__CG_OnUninit = homey.app.onUninit;
|
|
9
8
|
homey.app.onUninit = async function (args) {
|
|
@@ -21,23 +20,38 @@ class CG {
|
|
|
21
20
|
};
|
|
22
21
|
|
|
23
22
|
try {
|
|
24
|
-
|
|
23
|
+
await cg.init();
|
|
25
24
|
} catch (error) {
|
|
26
25
|
|
|
27
26
|
}
|
|
28
27
|
return cg;
|
|
29
28
|
}
|
|
29
|
+
constructor({homey}) {
|
|
30
|
+
CG.Current = this;
|
|
31
|
+
this.homey = CG.homey = homey;
|
|
32
|
+
this.isReady = false;
|
|
33
|
+
|
|
34
|
+
CG._defer = new Defer();
|
|
35
|
+
|
|
36
|
+
this.ready = CG.ready = CG._defer.promise;
|
|
37
|
+
CG.ready.then(() => {
|
|
38
|
+
CG.isReady = this.isReady = true;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
30
41
|
|
|
31
42
|
async init({ } = {}) {
|
|
32
43
|
if (!this.api) {
|
|
33
44
|
try {
|
|
34
45
|
this.api = await this.getAppApi(true);
|
|
35
46
|
this.api.on('realtime', this.realtimeFunction);
|
|
47
|
+
await this.api.get('/chronographs')
|
|
48
|
+
if(!CG.isReady && CG._defer) CG._defer.resolve();
|
|
36
49
|
} catch (error) {
|
|
37
50
|
this.homey.error('CG.init', error);
|
|
38
51
|
return this;
|
|
39
52
|
}
|
|
40
53
|
}
|
|
54
|
+
return this;
|
|
41
55
|
}
|
|
42
56
|
|
|
43
57
|
|
|
@@ -54,10 +68,10 @@ class CG {
|
|
|
54
68
|
|
|
55
69
|
|
|
56
70
|
async realtimeFunction(event, data) {
|
|
57
|
-
|
|
58
|
-
if (event === 'chronograph_changed') {
|
|
59
|
-
CG.onChronographUpdate(data);
|
|
71
|
+
if(!CG.isReady && CG._defer) CG._defer.resolve();
|
|
60
72
|
|
|
73
|
+
if (event === 'chronograph_event') {
|
|
74
|
+
CG.onChronographUpdate(data);
|
|
61
75
|
}
|
|
62
76
|
}
|
|
63
77
|
|
|
@@ -97,4 +111,36 @@ class CG {
|
|
|
97
111
|
|
|
98
112
|
|
|
99
113
|
|
|
114
|
+
class Defer {
|
|
115
|
+
get isDefered() { return !!this._isDefered; }
|
|
116
|
+
set isDefered(v) { this._isDefered = v; }
|
|
117
|
+
|
|
118
|
+
get promise() { return this._promise; }
|
|
119
|
+
set promise(v) { this._promise = v; }
|
|
120
|
+
/**
|
|
121
|
+
* @param {Number} timeout
|
|
122
|
+
*/
|
|
123
|
+
constructor(timeout, originalPromise, onTimeout) {
|
|
124
|
+
this._promise = new Promise((resolve, reject) => {
|
|
125
|
+
this._resolve = resolve;
|
|
126
|
+
this._reject = reject;
|
|
127
|
+
});
|
|
128
|
+
if (timeout) {
|
|
129
|
+
this.timeout = BL.homey.setTimeout(function () {
|
|
130
|
+
if (!this.isDefered && this.promise) {
|
|
131
|
+
this.reject();
|
|
132
|
+
//if (this.promise.resolve) this.promise.resolve();
|
|
133
|
+
if (onTimeout) onTimeout();
|
|
134
|
+
}
|
|
135
|
+
}.bind(this), timeout);
|
|
136
|
+
}
|
|
137
|
+
if (originalPromise && originalPromise.then) originalPromise.then((val1, val2, val3, val4) => this.resolve(val1, val2, val3, val4)).catch((val1, val2, val3, val4) => this.reject(val1, val2, val3, val4));
|
|
138
|
+
}
|
|
139
|
+
then(fun) { this._promise.then(fun); return this; }
|
|
140
|
+
catch(fun) { this._promise.catch(fun); return this; }
|
|
141
|
+
finally(fun) { this._promise.finally(fun); return this; }
|
|
142
|
+
resolve(val1, val2, val3, val4) { if (this.timeout) clearTimeout(this.timeout); this.isDefered = true; this._resolve(val1, val2, val3, val4); return this.promise; }
|
|
143
|
+
reject(val1, val2, val3, val4) { if (this.timeout) clearTimeout(this.timeout); this.isDefered = true; this._reject(val1, val2, val3, val4); return this.promise; }
|
|
144
|
+
}
|
|
145
|
+
|
|
100
146
|
module.exports = { CG };
|