Skip to content

Commit afbf771

Browse files
committed
feat: update logs wording
1 parent 7243556 commit afbf771

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

packages/runtime/src/execute/expression.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
} from '../errors';
1919
import type { JobModule, ExecutionContext } from '../types';
2020
import { ModuleInfoMap } from '../modules/linker';
21-
import { checkAndClearNullState, nullState } from '../util/null-state';
21+
import {
22+
clearNullState,
23+
isNullState,
24+
createNullState,
25+
} from '../util/null-state';
2226

2327
export type ExecutionErrorWrapper = {
2428
state: any;
@@ -52,7 +56,7 @@ export default (
5256
// Create the main reducer function
5357
const reducer = (execute || defaultExecute)(
5458
...operations.map((op, idx) =>
55-
wrapOperation(op, logger, `${idx + 1}`, opts.immutableState, `${idx}`)
59+
wrapOperation(op, logger, `${idx + 1}`, opts.immutableState)
5660
)
5761
);
5862

@@ -101,29 +105,25 @@ export const wrapOperation = (
101105
fn: Operation,
102106
logger: Logger,
103107
name: string,
104-
immutableState?: boolean,
105-
prevName?: string
108+
immutableState?: boolean
106109
) => {
107110
return async (state: State) => {
108111
logger.debug(`Starting operation ${name}`);
109112
const start = new Date().getTime();
110-
if (checkAndClearNullState(state)) {
111-
logger.warn(`Operation ${name} might fail!`);
113+
if (isNullState(state)) {
114+
clearNullState(state);
112115
logger.warn(
113-
`The previous operation ${prevName} didn't return a state. did you forget?`
116+
`WARNING: No state was passed into operation ${name}. Did the previous operation return state?`
114117
);
115118
}
116119
const newState = immutableState ? clone(state) : state;
117120

118-
if (typeof fn !== 'function') {
119-
logger.warn(`Are you sure ${name} is an operation?`);
120-
logger.debug(`Operation ${name} isn't a valid operation`);
121-
const duration = printDuration(new Date().getTime() - start);
122-
logger.debug(`Operation ${name} skipped in ${duration}`);
123-
return newState;
124-
}
121+
let result = await fn(newState);
125122

126-
const result = (await fn(newState)) || nullState();
123+
if (!result) {
124+
logger.debug(`Warning: operation ${name} did not return state`);
125+
result = createNullState();
126+
}
127127

128128
// TODO should we warn if an operation does not return state?
129129
// the trick is saying WHICH operation without source mapping
+5-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
const NULL_STATE = Symbol('NullState');
1+
// This module manages a special state object with a hidden null symbol.
2+
// Used to track when operations and jobs do not return their own state
3+
4+
const NULL_STATE = Symbol('null_state');
25

36
// The good thing about using a Symbol is that even if we forget to clean the object.
47
// it's still represented as {}, because symbols aren't visible as keys
5-
export function nullState() {
8+
export function createNullState() {
69
return { [NULL_STATE]: true };
710
}
811

@@ -13,9 +16,3 @@ export function isNullState(state: any) {
1316
export function clearNullState(state: any) {
1417
if (typeof state === 'object') delete state[NULL_STATE];
1518
}
16-
17-
export function checkAndClearNullState(state: any) {
18-
const isNull = isNullState(state);
19-
if (isNull) clearNullState(state);
20-
return isNull;
21-
}

packages/runtime/test/execute/step.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,15 @@ test.serial("warn if a previous step don't return state", async (t) => {
297297

298298
// @ts-ignore ts complains that the step does not return state
299299
const result = await execute(context, step, state);
300-
const warn = logger._find('warn', /Operation \d+ might fail!/);
300+
const warn = logger._find(
301+
'warn',
302+
/WARNING: No state was passed into operation/
303+
);
301304
t.truthy(warn);
302305

303306
const warnPrev = logger._find(
304307
'warn',
305-
/The previous operation \d+ didn't return a state. did you forget\?/
308+
/WARNING: No state was passed into operation/
306309
);
307310
t.truthy(warnPrev);
308311
});

0 commit comments

Comments
 (0)