{"size": 723, "ext": "js", "max_stars_repo_path": "packages/tree/src/newAsyncTreeWalker.js", "max_stars_repo_name": "statewalker/statewalker", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/tree/src/newAsyncTreeWalker.js", "max_issues_repo_name": "statewalker/statewalker", "max_issues_repo_issues_event_min_datetime": "2020-07-17T08:08:04.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-18T08:07:56.000Z", "max_forks_repo_path": "packages/tree/src/newAsyncTreeWalker.js", "max_forks_repo_name": "statewalker/statewalker", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 14, "max_forks_count": null, "avg_line_length": 30.125, "max_line_length": 80, "alphanum_fraction": 0.6293222683}
import MODE from './MODE.js';

export default function newAsyncTreeWalker(
  before, after,
  context = { stack: [], status: MODE.NONE }
) {
  return async (getNode) => {
    let status = context.status;
    const prev = status & MODE.EXIT;
    if (prev) await after(context);
    if (status & MODE.ENTER) await context.stack.push(context.current);
    let node = await getNode(context);
    if (node) {
      context.current = node;
      status = context.status = prev ? MODE.NEXT : MODE.FIRST;
      await before(context);
    } else {
      node = context.current = await context.stack.pop();
      status = context.status = node ? prev ? MODE.LAST : MODE.LEAF : MODE.NONE;
    }
    return status !== MODE.NONE;
  }
}
