Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Integrate Monitor into a @monitored context #2

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added src/decorators/monitored.ts
Empty file.
28 changes: 28 additions & 0 deletions src/functions/monitored.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

// IF you have a `@monitored`
// That means you have `@timedCancellable` at thes ame time
// You can do `@timed` `@cancellable`
// But instead `@timedCancellable` combines them all
// SO @monitored does it too
// Cause if you can lock things... you can also use the timer and monitor
// Due to the usageo f the locking ctx
// await ctx.monitor.lock(...)
// someotherfunction(ctx)
// it passes the monitor down there
// Well the problem is that whe nyou do it
// you'd need to have the timer monitor applied?
// you don'tn eed to apply the ctx
// the ctx is automtaically applied( so the function ctx of timer and signal)
// isautomatically applied to all LOCKS
// when you do this you also need to use the ability to create a monitor
// obj.withMonitor(async (monitor) => {
// })

// I wnat to see to quickly prototyep what this will look like


function monitored() {

}

export default monitored;
26 changes: 26 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ResourceAcquire } from '@matrixai/resources';
import type { PromiseCancellableController } from '@matrixai/async-cancellable';
import { Timer } from '@matrixai/timer';
import { Monitor, LockBox, RWLockReader, RWLockWriter } from '@matrixai/async-locks';

const AsyncFunction = (async () => {}).constructor;
const GeneratorFunction = function* () {}.constructor;
Expand Down Expand Up @@ -97,6 +98,30 @@ function timer<T = void>(
};
}

/**
* Monitor resource.
* Use it with `withF` or `withG`.
*/
function monitor<RWLock extends RWLockReader | RWLockWriter>(
lockBox: LockBox<RWLock>,
lockConstructor: new () => RWLock,
locksPending?: Map<string, { count: number }>,
): ResourceAcquire<Monitor<RWLock>> {
return async () => {
const monitor = new Monitor<RWLock>(
lockBox,
lockConstructor,
locksPending
);
return [
async () => {
await monitor.unlockAll();
},
monitor
];
};
}

function isPromiseLike(v: any): v is PromiseLike<unknown> {
return v != null && typeof v.then === 'function';
}
Expand Down Expand Up @@ -139,6 +164,7 @@ export {
checkContextCancellable,
checkContextTimed,
timer,
monitor,
isPromiseLike,
isGenerator,
isAsyncGenerator,
Expand Down