Skip to content

Commit 7b4c923

Browse files
committed
feat(any-by): add in-flight status object for observability
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 10f3dea commit 7b4c923

2 files changed

Lines changed: 149 additions & 4 deletions

File tree

lib/node_modules/@stdlib/utils/async/any-by/lib/factory.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
// MODULES //
2222

2323
var isFunction = require( '@stdlib/assert/is-function' );
24-
var isCollection = require( '@stdlib/assert/is-collection' );
2524
var format = require( '@stdlib/string/format' );
2625
var PINF = require( '@stdlib/constants/float64/pinf' );
2726
var validate = require( './validate.js' );
@@ -130,6 +129,7 @@ function factory( options, predicate ) {
130129
* @returns {void}
131130
*/
132131
function anyByAsync( collection, done ) {
132+
<<<<<<< HEAD
133133
var timer = createTimer();
134134

135135
if ( !isCollection( collection ) ) {
@@ -139,21 +139,68 @@ function factory( options, predicate ) {
139139
throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );
140140
}
141141
return limit( collection, opts, f, clbk );
142+
=======
143+
var status;
144+
145+
status = {
146+
'status': 'pending',
147+
'ndone': 0,
148+
'nerrors': 0
149+
};
150+
151+
// Wrap the predicate to track progress:
152+
limit( collection, opts, wrapper, clbk );
153+
154+
return status;
155+
>>>>>>> f1a651cded5 (feat(any-by): add in-flight status object for observability)
142156

143157
/**
144-
* Callback invoked upon completion.
158+
* Wraps the predicate function to track the number of completed invocations.
145159
*
146160
* @private
147-
* @param {*} [error] - error
148-
* @param {boolean} bool - test result
161+
* @param {*} value - collection element
162+
* @param {Callback} next - callback to invoke upon predicate completion
163+
* @returns {void}
164+
*/
165+
function wrapper( value, next ) {
166+
f( value, onResult );
167+
168+
/**
169+
* Callback invoked upon predicate completion.
170+
*
171+
* @private
172+
* @param {(Error|null)} err - error argument
173+
* @param {*} result - predicate result
174+
* @returns {void}
175+
*/
176+
function onResult( err, result ) {
177+
status.ndone += 1;
178+
next( err, result );
179+
}
180+
}
181+
182+
/**
183+
* Callback invoked upon completion of all predicate invocations.
184+
*
185+
* @private
186+
* @param {(Error|null)} error - error argument
187+
* @param {boolean} bool - whether at least one element passed the predicate
149188
* @returns {void}
150189
*/
151190
function clbk( error, bool ) {
152191
var duration = timer();
153192
if ( error ) {
193+
<<<<<<< HEAD
154194
return done( error, false, duration );
155195
}
156196
done( null, bool, duration );
197+
=======
198+
status.status = 'fail';
199+
return done( error, false );
200+
}
201+
status.status = 'ok';
202+
done( null, bool );
203+
>>>>>>> f1a651cded5 (feat(any-by): add in-flight status object for observability)
157204
}
158205
}
159206
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// cspell:words ndone
22+
23+
// MODULES //
24+
25+
var tape = require( 'tape' );
26+
var factory = require( './../lib/factory.js' );
27+
28+
29+
// TESTS //
30+
31+
tape( 'main export is a function', function test( t ) {
32+
t.ok( true, __filename );
33+
t.ok( typeof factory === 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'the function returns a status object that tracks progress (short-circuit)', function test( t ) {
38+
var anyByAsync;
39+
var files;
40+
var op;
41+
42+
anyByAsync = factory( predicate );
43+
files = [ './file1.js', './file2.js' ];
44+
op = anyByAsync( files, done );
45+
46+
// Verify the status object immediately after invocation:
47+
t.equal( typeof op, 'object', 'returns an object' );
48+
t.equal( op.status, 'pending', 'initial status is pending' );
49+
t.equal( op.ndone, 0, 'initial ndone is 0' );
50+
51+
function predicate( value, next ) {
52+
setTimeout( onTimeout, 0 );
53+
54+
function onTimeout() {
55+
next( null, true );
56+
}
57+
}
58+
59+
function done( err, result ) {
60+
if ( err ) {
61+
t.fail( err.message );
62+
return t.end();
63+
}
64+
t.equal( result, true, 'returns expected result' );
65+
t.equal( op.status, 'ok', 'status is ok upon completion' );
66+
67+
// Short-circuit: only one file was processed:
68+
t.equal( op.ndone, 1, 'short-circuited after first match' );
69+
t.end();
70+
}
71+
});
72+
73+
tape( 'the function returns a status object that tracks a full scan', function test( t ) {
74+
var files;
75+
var op;
76+
77+
files = [ './file1.js', './file2.js' ];
78+
op = factory( predicate )( files, done );
79+
80+
function predicate( value, next ) {
81+
setTimeout( onTimeout, 50 );
82+
83+
function onTimeout() {
84+
// Always false, so all files must be checked:
85+
next( null, false );
86+
}
87+
}
88+
89+
function done( err, result ) {
90+
if ( err ) {
91+
t.fail( err.message );
92+
return t.end();
93+
}
94+
t.equal( result, false, 'returns expected result' );
95+
t.equal( op.ndone, 2, 'all operations completed for a full scan' );
96+
t.end();
97+
}
98+
});

0 commit comments

Comments
 (0)