Skip to content

Commit 250a382

Browse files
committed
WIP
1 parent 708b4ac commit 250a382

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

package-lock.json

Lines changed: 1 addition & 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
@@ -25,6 +25,7 @@
2525
"@matrixai/async-cancellable": "^1.1.1",
2626
"@matrixai/async-locks": "^4.0.0",
2727
"@matrixai/errors": "^1.1.7",
28+
"@matrixai/resources": "^1.1.5",
2829
"@matrixai/timer": "^1.1.1"
2930
},
3031
"devDependencies": {

src/decorators/monitored.ts

Whitespace-only changes.

src/functions/monitored.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
// IF you have a `@monitored`
3+
// That means you have `@timedCancellable` at thes ame time
4+
// You can do `@timed` `@cancellable`
5+
// But instead `@timedCancellable` combines them all
6+
// SO @monitored does it too
7+
// Cause if you can lock things... you can also use the timer and monitor
8+
// Due to the usageo f the locking ctx
9+
// await ctx.monitor.lock(...)
10+
// someotherfunction(ctx)
11+
// it passes the monitor down there
12+
// Well the problem is that whe nyou do it
13+
// you'd need to have the timer monitor applied?
14+
// you don'tn eed to apply the ctx
15+
// the ctx is automtaically applied( so the function ctx of timer and signal)
16+
// isautomatically applied to all LOCKS
17+
// when you do this you also need to use the ability to create a monitor
18+
// obj.withMonitor(async (monitor) => {
19+
// })
20+
21+
// I wnat to see to quickly prototyep what this will look like
22+
23+
24+
function monitored() {
25+
26+
}
27+
28+
export default monitored;

src/utils.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import type { ResourceAcquire } from '@matrixai/resources';
2+
import type { PromiseCancellableController } from '@matrixai/async-cancellable';
13
import { Timer } from '@matrixai/timer';
4+
import { Monitor, LockBox, RWLockReader, RWLockWriter } from '@matrixai/async-locks';
25

36
const AsyncFunction = (async () => {}).constructor;
47
const GeneratorFunction = function* () {}.constructor;
@@ -59,6 +62,63 @@ function checkContextTimed(
5962
}
6063
}
6164

65+
/**
66+
* Timer resource
67+
* Use it with `withF` or `withG`.
68+
*/
69+
function timer<T = void>(
70+
handlerOrOpts?:
71+
| ((signal: AbortSignal) => T | PromiseLike<T>)
72+
| {
73+
handler?: (signal: AbortSignal) => T | PromiseLike<T>;
74+
delay?: number;
75+
lazy?: boolean;
76+
controller?: PromiseCancellableController;
77+
},
78+
delay: number = 0,
79+
lazy: boolean = false,
80+
controller?: PromiseCancellableController,
81+
): ResourceAcquire<Timer<T>> {
82+
return async () => {
83+
let timer: Timer<T>;
84+
if (typeof handlerOrOpts === 'function') {
85+
timer = new Timer(handlerOrOpts, delay, lazy, controller);
86+
} else {
87+
timer = new Timer(handlerOrOpts);
88+
}
89+
return [
90+
async () => {
91+
timer.cancel();
92+
},
93+
timer
94+
];
95+
};
96+
}
97+
98+
/**
99+
* Monitor resource.
100+
* Use it with `withF` or `withG`.
101+
*/
102+
function monitor<RWLock extends RWLockReader | RWLockWriter>(
103+
lockBox: LockBox<RWLock>,
104+
lockConstructor: new () => RWLock,
105+
locksPending?: Map<string, { count: number }>,
106+
): ResourceAcquire<Monitor<RWLock>> {
107+
return async () => {
108+
const monitor = new Monitor<RWLock>(
109+
lockBox,
110+
lockConstructor,
111+
locksPending
112+
);
113+
return [
114+
async () => {
115+
await monitor.unlockAll();
116+
},
117+
monitor
118+
];
119+
};
120+
}
121+
62122
function isPromiseLike(v: any): v is PromiseLike<unknown> {
63123
return v != null && typeof v.then === 'function';
64124
}
@@ -100,6 +160,8 @@ export {
100160
getContextIndex,
101161
checkContextCancellable,
102162
checkContextTimed,
163+
timer,
164+
monitor,
103165
isPromiseLike,
104166
isGenerator,
105167
isAsyncGenerator,

0 commit comments

Comments
 (0)