Skip to content

Commit 7cc6136

Browse files
authored
refactor: use single internal args buffer (#1286)
* refactor: use single internal args buffer * refactor: enhance `box` type * refactor: enhance `box` API * chore: update bundle
1 parent ab7e109 commit 7cc6136

5 files changed

Lines changed: 33 additions & 21 deletions

File tree

.size-limit.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"README.md",
1616
"LICENSE"
1717
],
18-
"limit": "121.301 kB",
18+
"limit": "121.40 kB",
1919
"brotli": false,
2020
"gzip": false
2121
},
@@ -29,7 +29,7 @@
2929
"build/globals.js",
3030
"build/deno.js"
3131
],
32-
"limit": "812.40 kB",
32+
"limit": "812.46 kB",
3333
"brotli": false,
3434
"gzip": false
3535
},
@@ -62,7 +62,7 @@
6262
"README.md",
6363
"LICENSE"
6464
],
65-
"limit": "868.90 kB",
65+
"limit": "869.00 kB",
6666
"brotli": false,
6767
"gzip": false
6868
}

build/cli.js

100755100644
File mode changed.

build/core.cjs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,13 @@ var defaults = resolveDefaults({
434434
timeoutSignal: SIGTERM
435435
});
436436
var storage = new import_node_async_hooks.AsyncLocalStorage();
437-
var snapshots = [];
438-
var delimiters = [];
437+
var box = ((box2 = []) => ({
438+
push(item) {
439+
if (box2.length > 0) throw new Fail(`Box is busy`);
440+
box2.push(item);
441+
},
442+
loot: box2.pop.bind(box2)
443+
}))();
439444
var getStore = () => storage.getStore() || defaults;
440445
var getSnapshot = (opts, from, cmd) => __spreadProps(__spreadValues({}, opts), {
441446
ac: opts.ac || new AbortController(),
@@ -464,7 +469,7 @@ var $ = new Proxy(
464469
pieces,
465470
args
466471
);
467-
snapshots.push(getSnapshot(opts, from, cmd));
472+
box.push(getSnapshot(opts, from, cmd));
468473
const pp = new ProcessPromise(import_util.noop);
469474
if (!pp.isHalted()) pp.run();
470475
return pp.sync ? pp.output : pp;
@@ -501,14 +506,15 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
501506
this._resolve = import_util.noop;
502507
// Stream-like API
503508
this.writable = true;
504-
if (snapshots.length) {
505-
this._snapshot = snapshots.pop();
509+
const snapshot = box.loot();
510+
if (snapshot) {
511+
this._snapshot = snapshot;
506512
this._resolve = resolve;
507513
this._reject = (v) => {
508514
reject(v);
509515
if (this.sync) throw v;
510516
};
511-
if (this._snapshot.halt) this._stage = "halted";
517+
if (snapshot.halt) this._stage = "halted";
512518
} else _ProcessPromise.disarm(this);
513519
}
514520
run() {
@@ -913,7 +919,7 @@ var _ProcessOutput = class _ProcessOutput extends Error {
913919
return encoding === "utf8" ? this.toString() : this.buffer().toString(encoding);
914920
}
915921
lines(delimiter) {
916-
delimiters.push(delimiter);
922+
box.push(delimiter);
917923
return [...this];
918924
}
919925
toString() {
@@ -927,7 +933,7 @@ var _ProcessOutput = class _ProcessOutput extends Error {
927933
}
928934
*[Symbol.iterator]() {
929935
const memo = [];
930-
const dlmtr = delimiters.pop() || this._dto.delimiter || $.delimiter || DLMTR;
936+
const dlmtr = box.loot() || this._dto.delimiter || $.delimiter || DLMTR;
931937
for (const chunk of this._dto.store.stdall) {
932938
yield* __yieldStar((0, import_util.getLines)(chunk, memo, dlmtr));
933939
}

src/core.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,13 @@ export interface Shell<
162162

163163
// Internal storages
164164
const storage = new AsyncLocalStorage<Options>()
165-
const snapshots: Snapshot[] = []
166-
const delimiters: Options['delimiter'][] = []
165+
const box = (<B extends Snapshot | Snapshot['delimiter']>(box: B[] = []) => ({
166+
push(item: B): void {
167+
if (box.length > 0) throw new Fail(`Box is busy`)
168+
box.push(item)
169+
},
170+
loot: box.pop.bind(box) as <T extends B>() => T | undefined,
171+
}))()
167172

168173
const getStore = () => storage.getStore() || defaults
169174

@@ -202,7 +207,7 @@ export const $: $ = new Proxy<$>(
202207
pieces as TemplateStringsArray,
203208
args
204209
) as string
205-
snapshots.push(getSnapshot(opts, from, cmd))
210+
box.push(getSnapshot(opts, from, cmd))
206211
const pp = new ProcessPromise(noop)
207212

208213
if (!pp.isHalted()) pp.run()
@@ -260,14 +265,15 @@ export class ProcessPromise extends Promise<ProcessOutput> {
260265
executor(...args)
261266
})
262267

263-
if (snapshots.length) {
264-
this._snapshot = snapshots.pop()!
268+
const snapshot = box.loot<Snapshot>()
269+
if (snapshot) {
270+
this._snapshot = snapshot
265271
this._resolve = resolve!
266272
this._reject = (v: ProcessOutput) => {
267273
reject!(v)
268274
if (this.sync) throw v
269275
}
270-
if (this._snapshot.halt) this._stage = 'halted'
276+
if (snapshot.halt) this._stage = 'halted'
271277
} else ProcessPromise.disarm(this)
272278
}
273279

@@ -800,7 +806,7 @@ export class ProcessOutput extends Error {
800806
}
801807

802808
lines(delimiter?: string | RegExp): string[] {
803-
delimiters.push(delimiter)
809+
box.push(delimiter)
804810
return [...this]
805811
}
806812

@@ -818,8 +824,8 @@ export class ProcessOutput extends Error {
818824

819825
*[Symbol.iterator](): Iterator<string> {
820826
const memo: (string | undefined)[] = []
821-
const dlmtr =
822-
delimiters.pop() || this._dto.delimiter || $.delimiter || DLMTR
827+
// prettier-ignore
828+
const dlmtr = box.loot<Options['delimiter']>() || this._dto.delimiter || $.delimiter || DLMTR
823829

824830
for (const chunk of this._dto.store.stdall) {
825831
yield* getLines(chunk, memo, dlmtr)

test/core.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ describe('core', () => {
219219

220220
try {
221221
$([null])
222-
throw new Err('unreachable')
222+
throw new Error('unreachable')
223223
} catch (e) {
224224
assert.ok(e instanceof Fail)
225225
assert.match(e.message, /malformed/i)

0 commit comments

Comments
 (0)