Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onFirstPause utility method #2

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -194,3 +194,8 @@ const loadFiles = gensync(function* () {
]);
});
```

### gensync.onFirstPause(generator, callback)

This method returns a gensync generator which calls `callback` when `generator`
yields an async operation.
18 changes: 17 additions & 1 deletion index.js
Expand Up @@ -71,6 +71,16 @@ module.exports = Object.assign(
}
},
}),
onFirstPause: buildOperation({
name: "onFirstPause",
arity: 2,
sync: function([item]) {
return evaluateSync(item);
},
async: function([item, cb], resolve, reject) {
evaluateAsync(item, resolve, reject, cb);
},
}),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can do what you want with something like this?

  async: function([item, cb], resolve, reject) {
    let completed = false;

    evaluateAsync(item, value => {
      completed = true;
      resolve(value);
    }, err => {
      completed = true;
      reject(value);
    });

    if (!completed) {
      cb();
    }
  },

or alternatively using the existing public API:

const runGenerator = gensync(function* (item) {
  return yield* item;
});

const onFirstPause = gensync({
  name: "onFirstPause",
  arity: 2,
  sync: function([item]) {
    return runGenerator.sync(item);
  },
  errback: function([item, firstPause], cb) {
    let completed = false;

    // Gensync calls `errback` callbacks synchronously if it can, even in the async case, if I remember right
    runGenerator.errback(item, (err, value) => {
      completed = true;
      cb(value);
    });

    if (!completed) {
      firstPause();
    }
  },
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will try the second approach 👍

}
);

Expand Down Expand Up @@ -247,7 +257,8 @@ function evaluateSync(gen) {
return value;
}

function evaluateAsync(gen, resolve, reject) {
function evaluateAsync(gen, resolve, reject, onFirstPause) {
let didFirstPause = false;
(function step() {
try {
let value;
Expand All @@ -271,6 +282,11 @@ function evaluateAsync(gen, resolve, reject) {
assertSuspend(out, gen);

if (!didSyncResume) {
if (!didFirstPause && onFirstPause) {
didFirstPause = true;
onFirstPause();
}

// Callback wasn't called synchronously, so break out of the loop
// and let it call 'step' later.
return;
Expand Down
1 change: 1 addition & 0 deletions index.js.flow
Expand Up @@ -29,4 +29,5 @@ declare module.exports: {

all<Return>(Array<Handler<Return>>): Handler<Return[]>,
race<Return>(Array<Handler<Return>>): Handler<Return>,
onFirstPause<Return>(Handler<Return>, cb: Function): Handler<Return>,
};