+2
-1
| { | ||
| "name": "pear-ref", | ||
| "version": "1.0.0", | ||
| "version": "1.0.1", | ||
| "main": "index.js", | ||
@@ -8,2 +8,3 @@ "type": "commonjs", | ||
| "license": "Apache-2.0", | ||
| "files": [], | ||
| "scripts": { | ||
@@ -10,0 +11,0 @@ "test:gen": "brittle -r test/all.js test/*.test.js", |
| name: Test | ||
| on: | ||
| push: | ||
| branches: [ main ] | ||
| pull_request: | ||
| branches: [ main ] | ||
| jobs: | ||
| test: | ||
| strategy: | ||
| matrix: | ||
| include: | ||
| - os: ubuntu-latest | ||
| platform: linux | ||
| arch: x64 | ||
| - os: macos-latest | ||
| platform: darwin | ||
| arch: x64 | ||
| - os: windows-latest | ||
| platform: win32 | ||
| arch: x64 | ||
| runs-on: ${{ matrix.os }} | ||
| name: ${{ matrix.platform }}-${{ matrix.arch }} | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Setup bare | ||
| run: npm i -g bare-runtime | ||
| - name: Install dependencies | ||
| run: npm i | ||
| - name: Lint | ||
| run: npm run lint | ||
| - name: Test | ||
| run: npm run cov |
-13
| Copyright 2025 Holepunch Inc | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. |
-13
| // This runner is auto-generated by Brittle | ||
| runTests() | ||
| async function runTests () { | ||
| const test = (await import('brittle')).default | ||
| test.pause() | ||
| await import('./index.test.js') | ||
| test.resume() | ||
| } |
| 'use strict' | ||
| const test = require('brittle') | ||
| const ref = require('..') | ||
| const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) | ||
| const reset = (teardown) => { | ||
| const start = ref.refs | ||
| teardown(() => { | ||
| while (ref.refs > start) ref.unref() | ||
| while (ref.refs < start) ref.ref() | ||
| }) | ||
| return start | ||
| } | ||
| test('ref increments and emits; unref decrements and emits', (t) => { | ||
| const start = reset(t.teardown) | ||
| t.plan(4) | ||
| ref.once('ref', n => t.is(n, start + 1)) | ||
| ref.ref() | ||
| t.is(ref.refs, start + 1) | ||
| ref.once('unref', n => t.is(n, start)) | ||
| ref.unref() | ||
| t.is(ref.refs, start) | ||
| }) | ||
| test('track resolves and balances refs', async (t) => { | ||
| const start = reset(t.teardown) | ||
| const p = ref.track(Promise.resolve('ok')) | ||
| t.is(ref.refs, start + 1, 'increment happens immediately') | ||
| const v = await p | ||
| t.is(v, 'ok') | ||
| t.is(ref.refs, start, 'balanced after resolve') | ||
| }) | ||
| test('track rejects and still balances refs', async (t) => { | ||
| const start = reset(t.teardown) | ||
| const err = new Error('boom') | ||
| const p = ref.track(Promise.reject(err)) | ||
| t.is(ref.refs, start + 1, 'increment happens immediately') | ||
| try { | ||
| await p | ||
| t.fail('should have thrown') | ||
| } catch (e) { | ||
| t.is(e, err) | ||
| } | ||
| t.is(ref.refs, start, 'balanced after reject') | ||
| }) | ||
| test('concurrent track calls emit paired ref/unref', async (t) => { | ||
| const start = reset(t.teardown) | ||
| let refEvents = 0 | ||
| let unrefEvents = 0 | ||
| const onRef = () => { refEvents++ } | ||
| const onUnref = () => { unrefEvents++ } | ||
| ref.on('ref', onRef) | ||
| ref.on('unref', onUnref) | ||
| try { | ||
| await Promise.all([ | ||
| ref.track(wait(10)), | ||
| ref.track(wait(5)) | ||
| ]) | ||
| } finally { | ||
| ref.off('ref', onRef) | ||
| ref.off('unref', onUnref) | ||
| } | ||
| t.is(refEvents, 2) | ||
| t.is(unrefEvents, 2) | ||
| t.is(ref.refs, start) | ||
| }) |
12618
-20.48%4
-50%22
-76.6%