wardens 0.5.1 → 0.6.0-rc.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +8 -0
- package/README.md +1 -1
- package/dist/wardens.cjs +1 -1
- package/dist/wardens.js +103 -74
- package/package.json +4 -4
- package/src/__tests__/resource-api.test.ts +186 -0
- package/src/__tests__/{allocation.test.ts → root-lifecycle.test.ts} +14 -14
- package/src/__tests__/root-lifecycles.test.ts +236 -0
- package/src/__tests__/roots.test.ts +236 -0
- package/src/__tests__/{types.test-d.ts → utility-types.test-d.ts} +2 -2
- package/src/{state.ts → global-weakrefs.ts} +11 -11
- package/src/index.ts +4 -3
- package/src/inherited-context.ts +32 -0
- package/src/{resource-context.ts → resource-controls.ts} +32 -8
- package/src/{allocation.ts → resource-lifecycle.ts} +11 -9
- package/src/root-lifecycle.ts +22 -0
- package/src/{types.ts → utility-types.ts} +3 -3
- package/src/__tests__/resource-context.test.ts +0 -75
- /package/src/{proxy.ts → wrap-with-proxy.ts} +0 -0
@@ -1,5 +1,5 @@
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2
|
-
import type
|
2
|
+
import type ResourceControls from './resource-controls';
|
3
3
|
|
4
4
|
/**
|
5
5
|
* Represents an arbitrary stateful resource that is asynchronously provisioned
|
@@ -7,14 +7,14 @@ import type ResourceContext from './resource-context';
|
|
7
7
|
* first tears down the children.
|
8
8
|
*/
|
9
9
|
export interface ResourceFactory<Value extends object> {
|
10
|
-
(resource:
|
10
|
+
(resource: ResourceControls): Promise<Resource<Value>>;
|
11
11
|
}
|
12
12
|
|
13
13
|
export interface ParametrizedResourceFactory<
|
14
14
|
Value extends object,
|
15
15
|
Args extends Array<unknown>,
|
16
16
|
> {
|
17
|
-
(resource:
|
17
|
+
(resource: ResourceControls, ...args: Args): Promise<Resource<Value>>;
|
18
18
|
}
|
19
19
|
|
20
20
|
export interface Resource<Value extends object> {
|
@@ -1,75 +0,0 @@
|
|
1
|
-
import ResourceContext from '../resource-context';
|
2
|
-
import { create } from '../allocation';
|
3
|
-
|
4
|
-
describe('ResourceContext', () => {
|
5
|
-
async function Test() {
|
6
|
-
return { value: {} };
|
7
|
-
}
|
8
|
-
|
9
|
-
it('can spawn children of its own', async () => {
|
10
|
-
const Child = async () => ({
|
11
|
-
value: { child: true },
|
12
|
-
});
|
13
|
-
|
14
|
-
const Parent = async (resource: ResourceContext) => {
|
15
|
-
return { value: await resource.create(Child) };
|
16
|
-
};
|
17
|
-
|
18
|
-
await expect(create(Parent)).resolves.toEqual({ child: true });
|
19
|
-
});
|
20
|
-
|
21
|
-
it('can deallocate child resources on demand', async () => {
|
22
|
-
const spy = vi.fn();
|
23
|
-
|
24
|
-
const Child = async () => ({
|
25
|
-
value: { child: true },
|
26
|
-
destroy: spy,
|
27
|
-
});
|
28
|
-
|
29
|
-
const Parent = async (resource: ResourceContext) => {
|
30
|
-
const child = await resource.create(Child);
|
31
|
-
await resource.destroy(child);
|
32
|
-
|
33
|
-
return {
|
34
|
-
value: { parent: true },
|
35
|
-
};
|
36
|
-
};
|
37
|
-
|
38
|
-
await expect(create(Parent)).resolves.toEqual({ parent: true });
|
39
|
-
expect(spy).toHaveBeenCalled();
|
40
|
-
});
|
41
|
-
|
42
|
-
it('fails to destroy resources owned by someone else', async () => {
|
43
|
-
const test = await create(Test);
|
44
|
-
|
45
|
-
const Sneaky = async (resource: ResourceContext) => {
|
46
|
-
await resource.destroy(test);
|
47
|
-
return { value: {} };
|
48
|
-
};
|
49
|
-
|
50
|
-
await expect(create(Sneaky)).rejects.toThrow(/do not own/i);
|
51
|
-
});
|
52
|
-
|
53
|
-
it('binds create/destroy handlers to the class instance', async () => {
|
54
|
-
async function Allocator({ create, destroy }: ResourceContext) {
|
55
|
-
const test = await create(Test);
|
56
|
-
await destroy(test);
|
57
|
-
|
58
|
-
return { value: [] };
|
59
|
-
}
|
60
|
-
|
61
|
-
await expect(create(Allocator)).resolves.not.toThrow();
|
62
|
-
});
|
63
|
-
|
64
|
-
it('indicates if a resource was already destroyed', async () => {
|
65
|
-
async function Allocator(resource: ResourceContext) {
|
66
|
-
const test = await resource.create(Test);
|
67
|
-
await resource.destroy(test);
|
68
|
-
await resource.destroy(test);
|
69
|
-
|
70
|
-
return { value: [] };
|
71
|
-
}
|
72
|
-
|
73
|
-
await expect(create(Allocator)).rejects.toThrow(/already destroyed/i);
|
74
|
-
});
|
75
|
-
});
|
File without changes
|