Skip to content

Commit 8c9c520

Browse files
committed
Changed to using @matrixai/resources for withF and withG
1 parent bd00f2a commit 8c9c520

File tree

6 files changed

+33
-117
lines changed

6 files changed

+33
-117
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"@matrixai/async-init": "^1.6.0",
2424
"@matrixai/logger": "^2.1.0",
25+
"@matrixai/resources": "^1.0.0",
2526
"@matrixai/workers": "^1.2.5",
2627
"abstract-leveldown": "^7.2.0",
2728
"level": "7.0.1",

src/DB.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
AbstractIteratorOptions,
44
} from 'abstract-leveldown';
55
import type { LevelDB } from 'level';
6+
import type { ResourceAcquire } from '@matrixai/resources';
67
import type {
78
POJO,
89
FileSystem,
@@ -12,7 +13,6 @@ import type {
1213
DBLevel,
1314
DBIterator,
1415
DBOps,
15-
ResourceAcquire,
1616
} from './types';
1717
import level from 'level';
1818
import subleveldown from 'subleveldown';

src/types.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,6 @@ type DBOp =
7171

7272
type DBOps = Array<DBOp>;
7373

74-
type ResourceAcquire<Resource = void> = () => Promise<
75-
readonly [ResourceRelease, Resource?]
76-
>;
77-
78-
type ResourceRelease = (e?: Error) => Promise<void>;
79-
80-
type Resources<T extends readonly ResourceAcquire<any>[]> = {
81-
[K in keyof T]: T[K] extends ResourceAcquire<infer R> ? R : never;
82-
};
83-
8474
export type {
8575
POJO,
8676
FileSystem,
@@ -91,7 +81,4 @@ export type {
9181
DBIterator,
9282
DBOp,
9383
DBOps,
94-
ResourceAcquire,
95-
ResourceRelease,
96-
Resources,
9784
};

src/utils.ts

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import type {
2-
DBDomain,
3-
ResourceAcquire,
4-
ResourceRelease,
5-
Resources,
6-
} from './types';
7-
1+
import type { DBDomain } from './types';
82
import sublevelprefixer from 'sublevel-prefixer';
93
import * as dbErrors from './errors';
104

@@ -56,83 +50,11 @@ function fromArrayBuffer(
5650
return Buffer.from(b, offset, length);
5751
}
5852

59-
/**
60-
* Make sure to explicitly declare or cast `acquires` as a tuple using `[ResourceAcquire...]` or `as const`
61-
*/
62-
async function withF<
63-
ResourceAcquires extends
64-
| readonly [ResourceAcquire<unknown>]
65-
| readonly ResourceAcquire<unknown>[],
66-
T,
67-
>(
68-
acquires: ResourceAcquires,
69-
f: (resources: Resources<ResourceAcquires>) => Promise<T>,
70-
): Promise<T> {
71-
const releases: Array<ResourceRelease> = [];
72-
const resources: Array<unknown> = [];
73-
let e_: Error | undefined;
74-
try {
75-
for (const acquire of acquires) {
76-
const [release, resource] = await acquire();
77-
releases.push(release);
78-
resources.push(resource);
79-
}
80-
return await f(resources as unknown as Resources<ResourceAcquires>);
81-
} catch (e) {
82-
e_ = e;
83-
throw e;
84-
} finally {
85-
releases.reverse();
86-
for (const release of releases) {
87-
await release(e_);
88-
}
89-
}
90-
}
91-
92-
/**
93-
* Make sure to explicitly declare or cast `acquires` as a tuple using `[ResourceAcquire...]` or `as const`
94-
*/
95-
async function* withG<
96-
ResourceAcquires extends
97-
| readonly [ResourceAcquire<unknown>]
98-
| readonly ResourceAcquire<unknown>[],
99-
T = unknown,
100-
TReturn = any,
101-
TNext = unknown,
102-
>(
103-
acquires: ResourceAcquires,
104-
g: (
105-
resources: Resources<ResourceAcquires>,
106-
) => AsyncGenerator<T, TReturn, TNext>,
107-
): AsyncGenerator<T, TReturn, TNext> {
108-
const releases: Array<ResourceRelease> = [];
109-
const resources: Array<unknown> = [];
110-
let e_: Error | undefined;
111-
try {
112-
for (const acquire of acquires) {
113-
const [release, resource] = await acquire();
114-
releases.push(release);
115-
resources.push(resource);
116-
}
117-
return yield* g(resources as unknown as Resources<ResourceAcquires>);
118-
} catch (e) {
119-
e_ = e;
120-
throw e;
121-
} finally {
122-
releases.reverse();
123-
for (const release of releases) {
124-
await release(e_);
125-
}
126-
}
127-
}
128-
12953
export {
13054
prefix,
13155
domainPath,
13256
serialize,
13357
deserialize,
13458
toArrayBuffer,
13559
fromArrayBuffer,
136-
withF,
137-
withG,
13860
};

tests/DBTransaction.test.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import os from 'os';
22
import path from 'path';
33
import fs from 'fs';
44
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
5+
import { withF } from '@matrixai/resources';
56
import DB from '@/DB';
67
import DBTransaction from '@/DBTransaction';
78
import * as utils from '@/utils';
@@ -53,7 +54,7 @@ describe(DBTransaction.name, () => {
5354
expect(await db.dump(db.transactionsDb)).toStrictEqual({});
5455
});
5556
test('get, put and del', async () => {
56-
const p = utils.withF([db.transaction()], async ([tran]) => {
57+
const p = withF([db.transaction()], async ([tran]) => {
5758
expect(await tran.get([], 'foo')).toBeUndefined();
5859
// Add foo -> bar to the transaction
5960
await tran.put([], 'foo', 'bar');
@@ -83,19 +84,19 @@ describe(DBTransaction.name, () => {
8384
expect(await db.dump(db.transactionsDb)).toStrictEqual({});
8485
});
8586
test('no dirty reads', async () => {
86-
await utils.withF([db.transaction()], async ([tran1]) => {
87+
await withF([db.transaction()], async ([tran1]) => {
8788
expect(await tran1.get([], 'hello')).toBeUndefined();
88-
await utils.withF([db.transaction()], async ([tran2]) => {
89+
await withF([db.transaction()], async ([tran2]) => {
8990
await tran2.put([], 'hello', 'world');
9091
// `tran2` has not yet committed
9192
expect(await tran1.get([], 'hello')).toBeUndefined();
9293
});
9394
});
9495
await db.clear();
95-
await utils.withF([db.transaction()], async ([tran1]) => {
96+
await withF([db.transaction()], async ([tran1]) => {
9697
expect(await tran1.get([], 'hello')).toBeUndefined();
9798
await tran1.put([], 'hello', 'foo');
98-
await utils.withF([db.transaction()], async ([tran2]) => {
99+
await withF([db.transaction()], async ([tran2]) => {
99100
// `tran1` has not yet committed
100101
expect(await tran2.get([], 'hello')).toBeUndefined();
101102
await tran2.put([], 'hello', 'bar');
@@ -105,19 +106,19 @@ describe(DBTransaction.name, () => {
105106
});
106107
});
107108
test('non-repeatable reads', async () => {
108-
await utils.withF([db.transaction()], async ([tran1]) => {
109+
await withF([db.transaction()], async ([tran1]) => {
109110
expect(await tran1.get([], 'hello')).toBeUndefined();
110-
await utils.withF([db.transaction()], async ([tran2]) => {
111+
await withF([db.transaction()], async ([tran2]) => {
111112
await tran2.put([], 'hello', 'world');
112113
});
113114
// `tran2` is now committed
114115
expect(await tran1.get([], 'hello')).toBe('world');
115116
});
116117
await db.clear();
117-
await utils.withF([db.transaction()], async ([tran1]) => {
118+
await withF([db.transaction()], async ([tran1]) => {
118119
expect(await tran1.get([], 'hello')).toBeUndefined();
119120
await tran1.put([], 'hello', 'foo');
120-
await utils.withF([db.transaction()], async ([tran2]) => {
121+
await withF([db.transaction()], async ([tran2]) => {
121122
// `tran1` has not yet committed
122123
expect(await tran2.get([], 'hello')).toBeUndefined();
123124
await tran2.put([], 'hello', 'bar');
@@ -132,7 +133,7 @@ describe(DBTransaction.name, () => {
132133
await db.put([], '2', '2');
133134
await db.put([], '3', '3');
134135
let rows: Array<[string, string]>;
135-
await utils.withF([db.transaction()], async ([tran1]) => {
136+
await withF([db.transaction()], async ([tran1]) => {
136137
rows = [];
137138
for await (const [k, v] of await tran1.iterator()) {
138139
rows.push([k.toString(), JSON.parse(v.toString())]);
@@ -142,7 +143,7 @@ describe(DBTransaction.name, () => {
142143
['2', '2'],
143144
['3', '3'],
144145
]);
145-
await utils.withF([db.transaction()], async ([tran2]) => {
146+
await withF([db.transaction()], async ([tran2]) => {
146147
await tran2.del([], '1');
147148
await tran2.put([], '4', '4');
148149
rows = [];
@@ -167,9 +168,9 @@ describe(DBTransaction.name, () => {
167168
});
168169
});
169170
test('lost updates', async () => {
170-
await utils.withF([db.transaction()], async ([tran1]) => {
171+
await withF([db.transaction()], async ([tran1]) => {
171172
await tran1.put([], 'hello', 'foo');
172-
await utils.withF([db.transaction()], async ([tran2]) => {
173+
await withF([db.transaction()], async ([tran2]) => {
173174
await tran2.put([], 'hello', 'bar');
174175
});
175176
expect(await tran1.get([], 'hello')).toBe('foo');
@@ -200,7 +201,7 @@ describe(DBTransaction.name, () => {
200201
await db.put([], 'e', 'e');
201202
await db.put([], 'h', 'h');
202203
await db.put([], 'k', 'k');
203-
await utils.withF([db.transaction()], async ([tran]) => {
204+
await withF([db.transaction()], async ([tran]) => {
204205
await tran.put([], 'a', '1');
205206
await tran.put([], 'c', '3');
206207
await tran.put([], 'e', '5');
@@ -247,7 +248,7 @@ describe(DBTransaction.name, () => {
247248
await db.put([], 'e', 'e');
248249
await db.put([], 'h', 'h');
249250
await db.put([], 'k', 'k');
250-
await utils.withF([db.transaction()], async ([tran]) => {
251+
await withF([db.transaction()], async ([tran]) => {
251252
await tran.put([], 'a', '1');
252253
await tran.put([], 'c', '3');
253254
await tran.put([], 'e', '5');
@@ -294,7 +295,7 @@ describe(DBTransaction.name, () => {
294295
await db.put([], 'd', 'd');
295296
await db.put([], 'e', 'e');
296297
await db.put([], 'h', 'h');
297-
await utils.withF([db.transaction()], async ([tran]) => {
298+
await withF([db.transaction()], async ([tran]) => {
298299
await tran.put([], 'a', '1');
299300
await tran.put([], 'c', '3');
300301
await tran.put([], 'e', '5');
@@ -337,7 +338,7 @@ describe(DBTransaction.name, () => {
337338
await db.put([], 'd', 'd');
338339
await db.put([], 'e', 'e');
339340
await db.put([], 'h', 'h');
340-
await utils.withF([db.transaction()], async ([tran]) => {
341+
await withF([db.transaction()], async ([tran]) => {
341342
await tran.put([], 'a', '1');
342343
await tran.put([], 'c', '3');
343344
await tran.put([], 'e', '5');
@@ -380,7 +381,7 @@ describe(DBTransaction.name, () => {
380381
await db.put([], 'd', 'd');
381382
await db.put([], 'e', 'e');
382383
await db.put([], 'h', 'h');
383-
await utils.withF([db.transaction()], async ([tran]) => {
384+
await withF([db.transaction()], async ([tran]) => {
384385
await tran.put([], 'a', '1');
385386
await tran.put([], 'c', '3');
386387
await tran.put([], 'e', '5');
@@ -419,7 +420,7 @@ describe(DBTransaction.name, () => {
419420
await db.put([], 'd', 'd');
420421
await db.put([], 'e', 'e');
421422
await db.put([], 'h', 'h');
422-
await utils.withF([db.transaction()], async ([tran]) => {
423+
await withF([db.transaction()], async ([tran]) => {
423424
await tran.put([], 'a', '1');
424425
await tran.put([], 'c', '3');
425426
await tran.put([], 'e', '5');
@@ -464,7 +465,7 @@ describe(DBTransaction.name, () => {
464465
await db.put([], 'e', 'e');
465466
await db.put([], 'h', 'h');
466467
await db.put([], 'k', 'k');
467-
await utils.withF([db.transaction()], async ([tran]) => {
468+
await withF([db.transaction()], async ([tran]) => {
468469
await tran.put([], 'a', '1');
469470
await tran.put([], 'c', '3');
470471
await tran.put([], 'e', '5');
@@ -510,7 +511,7 @@ describe(DBTransaction.name, () => {
510511
await db.put([], 'e', 'e');
511512
await db.put([], 'h', 'h');
512513
await db.put([], 'k', 'k');
513-
await utils.withF([db.transaction()], async ([tran]) => {
514+
await withF([db.transaction()], async ([tran]) => {
514515
await tran.put([], 'a', '1');
515516
await tran.put([], 'c', '3');
516517
await tran.put([], 'e', '5');
@@ -558,7 +559,7 @@ describe(DBTransaction.name, () => {
558559
results.push(2);
559560
});
560561
const mockFailure = jest.fn();
561-
await utils.withF([db.transaction()], async ([tran]) => {
562+
await withF([db.transaction()], async ([tran]) => {
562563
tran.queueSuccess(mockSuccess1);
563564
tran.queueSuccess(mockSuccess2);
564565
tran.queueFailure(mockFailure);
@@ -578,7 +579,7 @@ describe(DBTransaction.name, () => {
578579
results.push(2);
579580
});
580581
await expect(
581-
utils.withF([db.transaction()], async ([tran]) => {
582+
withF([db.transaction()], async ([tran]) => {
582583
tran.queueSuccess(mockSuccess);
583584
tran.queueFailure(mockFailure1);
584585
tran.queueFailure(mockFailure2);
@@ -595,7 +596,7 @@ describe(DBTransaction.name, () => {
595596
await db.put([], '2', 'b');
596597
const mockFailure = jest.fn();
597598
await expect(
598-
utils.withF([db.transaction()], async ([tran]) => {
599+
withF([db.transaction()], async ([tran]) => {
599600
await tran.put([], '1', '1');
600601
await tran.put([], '2', '2');
601602
tran.queueFailure(mockFailure);

0 commit comments

Comments
 (0)