tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

disposable-stack-defer-dispose.js (1367B)


      1 // |jit-test| skip-if: !getBuildConfiguration("explicit-resource-management"); --enable-explicit-resource-management
      2 
      3 load(libdir + "asserts.js");
      4 
      5 {
      6  const disposed = [];
      7  const stack = new DisposableStack();
      8  assertEq(stack.defer(() => disposed.push(1)), undefined);
      9  stack.defer(() => disposed.push(2));
     10  assertEq(stack.disposed, false);
     11  stack.dispose();
     12  assertEq(stack.disposed, true);
     13  assertArrayEq(disposed, [2, 1]);
     14  stack.dispose();
     15  assertArrayEq(disposed, [2, 1]);
     16  assertThrowsInstanceOf(() => stack.defer(() => disposed.push(3)), ReferenceError);
     17 }
     18 
     19 {
     20  const values = [];
     21  const stack = new DisposableStack();
     22  stack.defer(() => values.push(1));
     23  stack.defer(() => values.push(2));
     24  assertEq(stack.disposed, false);
     25  {
     26    using stk = stack;
     27    stk.use({
     28      [Symbol.dispose]() {
     29        values.push(3);
     30      },
     31    });
     32    stk.adopt(4, (v) => values.push(v));
     33    stk.defer(() => values.push(5));
     34  }
     35  assertEq(stack.disposed, true);
     36  assertArrayEq(values, [5, 4, 3, 2, 1]);
     37 }
     38 
     39 {
     40  assertThrowsInstanceOf(() => {
     41    const stack = new DisposableStack();
     42    stack.defer(undefined);
     43  }, TypeError);
     44 
     45  assertThrowsInstanceOf(() => {
     46    const stack = new DisposableStack();
     47    stack.defer(null);
     48  }, TypeError);
     49 
     50  assertThrowsInstanceOf(() => {
     51    const stack = new DisposableStack();
     52    stack.defer(1);
     53  }, TypeError);
     54 }