diff --git a/src/decorators/monitored.ts b/src/decorators/monitored.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/functions/monitored.ts b/src/functions/monitored.ts new file mode 100644 index 0000000..4c15b78 --- /dev/null +++ b/src/functions/monitored.ts @@ -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; diff --git a/src/utils.ts b/src/utils.ts index 0f796cb..4989a94 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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; @@ -97,6 +98,30 @@ function timer( }; } +/** + * Monitor resource. + * Use it with `withF` or `withG`. + */ +function monitor( + lockBox: LockBox, + lockConstructor: new () => RWLock, + locksPending?: Map, +): ResourceAcquire> { + return async () => { + const monitor = new Monitor( + lockBox, + lockConstructor, + locksPending + ); + return [ + async () => { + await monitor.unlockAll(); + }, + monitor + ]; + }; +} + function isPromiseLike(v: any): v is PromiseLike { return v != null && typeof v.then === 'function'; } @@ -139,6 +164,7 @@ export { checkContextCancellable, checkContextTimed, timer, + monitor, isPromiseLike, isGenerator, isAsyncGenerator,