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

Breaking: remove callbacks in favor of promises #50

Merged
merged 2 commits into from Nov 10, 2022
Merged

Breaking: remove callbacks in favor of promises #50

merged 2 commits into from Nov 10, 2022

Conversation

vweevers
Copy link
Member

@vweevers vweevers commented Nov 8, 2022

Applies to both the public and private API. If you were already using promises in the public API, nothing changed, except for subtle timing differences. In the private API, it means that function signatures like _get(key, options, callback) have changed to async _get(key, options).

In addition:

  • Remove the deprecated iterator.end() alias of .close() because we'd otherwise have to keep callback support on this one method.
  • Replace use of process.nextTick() with queueMicrotask() because there isn't a better time to introduce timing differences
  • Lay the groundwork for supporting AbortSignal on iterators (not functional yet). I'll probably implement (and explain) this in a follow-up PR.

TODO:

  • Update tests
  • Fix remaining tests (mainly bugs in opening & closing)
  • Check browser & Node.js support of JS features (like signals)
  • Canary-test on memory-level
  • Canary-test on classic-level (just 2 or 3 methods)
  • Benchmark (later)

@vweevers vweevers added the semver-major Changes that break backward compatibility label Nov 8, 2022
@vweevers vweevers added this to the 2.0.0 milestone Nov 8, 2022
@vweevers
Copy link
Member Author

vweevers commented Nov 8, 2022

Ouch: I wondered why tests are passing in Node 12 (I didn't expect it to, for lack of AbortController). Turns out, they aren't passing, but the exit code is 0. Must be a bug in faucet which was recently updated.

@vweevers vweevers marked this pull request as ready for review November 9, 2022 19:59
@vweevers
Copy link
Member Author

vweevers commented Nov 9, 2022

Now comes the fun part: using promises in the C++ of classic-level.

Edit: that's surprisingly easy (at least for basic methods like put()) and performance is roughly the same. No blockers :)

abstract-iterator.js Outdated Show resolved Hide resolved
test/self.js Outdated Show resolved Hide resolved
@vweevers vweevers merged commit 9f0225a into v2 Nov 10, 2022
@vweevers vweevers deleted the promises branch November 10, 2022 15:40
vweevers added a commit that referenced this pull request Nov 10, 2022
The breaking changes are described in UPGRADING.md.
vweevers added a commit that referenced this pull request Nov 14, 2022
In #50 I made an early start with this, but I made two mistakes:

1. Putting the signal in per-method options, e.g. `next({ signal })`,
   rather than in constructor options, i.e. `iterator({ signal })`.
   The former would not be accessible in a `for await...of` and
   come with a performance penalty.
2. Thinking it could replace `abortOnClose` (#21), which is a
   separate issue. I'll leave it at that, because it only matters
   for `many-level` and can be solved later.

Anyway, this PR enables the following:

```
const abortController = new AbortController()
const signal = abortController.signal

// Will result in 'aborted' log
abortController.abort()

try {
  for await (const entry of db.iterator({ signal })) {
    console.log(entry)
  }
} catch (err) {
  if (err.code === 'LEVEL_ABORTED') {
    console.log('aborted')
  }
}
```

It also fixes small bugs in the `open()` and `close()` methods of
multiple classes, as follow-up for #50.
vweevers added a commit that referenced this pull request Jan 27, 2024
The breaking changes are described in UPGRADING.md.
vweevers added a commit that referenced this pull request Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-major Changes that break backward compatibility
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

1 participant