Skip to content

Commit 75de13b

Browse files
committed
Add mocha reporter back, try to make classes thenable
1 parent d1b9f19 commit 75de13b

File tree

11 files changed

+156
-35
lines changed

11 files changed

+156
-35
lines changed

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = (config) => {
3434
os_version: 'High Sierra'
3535
},
3636
},
37-
reporters: ['progress'],
37+
reporters: ['mocha'],
3838
port: 9876,
3939
colors: true,
4040
logLevel: config.LOG_INFO,

src/FeatureExtractor/Mobilenet.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Mobilenet {
4141
this.mapStringToIndex = [];
4242
this.usageType = null;
4343
this.ready = callCallback(this.loadModel(), callback);
44+
this.then = this.ready.then;
4445
}
4546

4647
async loadModel() {

src/FeatureExtractor/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ const featureExtractor = (model, optionsOrCallback, cb) => {
2828
}
2929

3030
if (modelName === 'mobilenet') {
31-
const instance = new Mobilenet(options, callback);
32-
return callback ? instance : instance.ready;
31+
return new Mobilenet(options, callback);
3332
}
3433
throw new Error(`${modelName} is not a valid model.`);
3534
};

src/LSTM/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class LSTM {
3131
temperature: 0.5,
3232
};
3333
this.ready = callCallback(this.loadCheckpoints(model), callback);
34+
this.then = this.ready.then.bind(this.ready);
3435
}
3536

3637
async loadCheckpoints(path) {
@@ -152,8 +153,7 @@ class LSTM {
152153
}
153154

154155
const LSTMGenerator = (modelPath = './', callback) => {
155-
const instance = new LSTM(modelPath);
156-
return callback ? instance : instance.ready;
156+
return new LSTM(modelPath, callback);
157157
};
158158

159159
export default LSTMGenerator;

src/PoseNet/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class PoseNet extends EventEmitter {
3535
this.minConfidence = options.minConfidence || DEFAULTS.minConfidence;
3636
this.multiplier = options.multiplier || DEFAULTS.multiplier;
3737
this.ready = callCallback(this.load(), callback);
38+
this.then = this.ready.then;
3839
}
3940

4041
async load() {
@@ -115,8 +116,7 @@ const poseNet = (videoOrOptionsOrCallback, optionsOrCallback, cb) => {
115116
detectionType = optionsOrCallback;
116117
}
117118

118-
const instance = new PoseNet(video, options, detectionType, callback);
119-
return callback ? instance : instance.ready;
119+
return new PoseNet(video, options, detectionType, callback);
120120
};
121121

122122
export default poseNet;

src/StyleTransfer/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class StyleTransfer extends Video {
2626
this.epsilonScalar = tf.scalar(1e-3);
2727
this.video = null;
2828
this.ready = callCallback(this.load(model), callback);
29+
this.then = this.ready.then;
2930
}
3031

3132
async load(model) {
@@ -139,8 +140,7 @@ const styleTransfer = (model, videoOrCallback, cb) => {
139140
callback = videoOrCallback;
140141
}
141142

142-
const instance = new StyleTransfer(model, video, callback);
143-
return callback ? instance : instance.ready;
143+
return new StyleTransfer(model, video, callback);
144144
};
145145

146146
export default styleTransfer;

src/Word2vec/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,39 @@ class Word2Vec {
2626
};
2727

2828
this.ready = callCallback(loadModel(model), callback);
29+
this.then = this.ready.then.bind(this.ready);
2930
}
3031

31-
add(inputs, max = 1) {
32+
async add(inputs, max = 1) {
33+
await this.ready;
3234
const sum = Word2Vec.addOrSubtract(this.model, inputs, 'ADD');
3335
return Word2Vec.nearest(this.model, sum, inputs.length, inputs.length + max);
3436
}
3537

36-
subtract(inputs, max = 1) {
38+
async subtract(inputs, max = 1) {
39+
await this.ready;
3740
const subtraction = Word2Vec.addOrSubtract(this.model, inputs, 'SUBTRACT');
3841
return Word2Vec.nearest(this.model, subtraction, inputs.length, inputs.length + max);
3942
}
4043

41-
average(inputs, max = 1) {
44+
async average(inputs, max = 1) {
45+
await this.ready;
4246
const sum = Word2Vec.addOrSubtract(this.model, inputs, 'ADD');
4347
const avg = tf.div(sum, tf.tensor(inputs.length));
4448
return Word2Vec.nearest(this.model, avg, inputs.length, inputs.length + max);
4549
}
4650

47-
nearest(input, max = 10) {
51+
async nearest(input, max = 10) {
52+
await this.ready;
4853
const vector = this.model[input];
4954
if (!vector) {
5055
return null;
5156
}
5257
return Word2Vec.nearest(this.model, vector, 1, max + 1);
5358
}
5459

55-
getRandomWord() {
60+
async getRandomWord() {
61+
await this.ready;
5662
const words = Object.keys(this.model);
5763
return words[Math.floor(Math.random() * words.length)];
5864
}
@@ -100,8 +106,7 @@ class Word2Vec {
100106
}
101107

102108
const word2vec = (model, cb) => {
103-
const instance = new Word2Vec(model, cb);
104-
return cb ? instance : instance.ready;
109+
return new Word2Vec(model, cb);
105110
};
106111

107112
export default word2vec;

src/Word2vec/index_test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ describe('initialize word2vec', () => {
1515
}));
1616
});
1717

18-
it('computes nearest words', () => {
19-
expect(word2vec.nearest('love', 5).map(v => v.vector))
18+
it('computes nearest words', async () => {
19+
const nearest = await word2vec.nearest('love', 5);
20+
expect(nearest.map(v => v.vector))
2021
.toEqual(['loved', 'loves', 'hate', 'wonderful', 'beautiful']);
2122
});
2223
});

src/YOLO/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class YOLOBase extends Video {
4444
this.modelReady = false;
4545
this.isPredicting = false;
4646
this.ready = callCallback(this.loadModel(), callback);
47+
this.then = this.ready.then;
4748
}
4849

4950
async loadModel() {
@@ -155,8 +156,7 @@ const YOLO = (videoOrOptionsOrCallback, optionsOrCallback, cb) => {
155156
callback = optionsOrCallback;
156157
}
157158

158-
const instance = new YOLOBase(video, options, callback);
159-
return cb ? instance : instance.ready;
159+
return new YOLOBase(video, options, callback);
160160
};
161161

162162
export default YOLO;

src/utils/callcallback.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
// https://opensource.org/licenses/MIT
55

66
export default function callCallback(promise, callback) {
7-
return callback ? promise.then((result) => {
8-
callback(undefined, result);
9-
}, (error) => {
10-
callback(error);
11-
}) : promise;
7+
if (callback) {
8+
promise.then((result) => {
9+
callback(undefined, result);
10+
return result;
11+
});
12+
}
13+
return promise;
1214
}

0 commit comments

Comments
 (0)