api/classes/managed
Globals / Managed
Class: Managed<A1, E1, R1>
Is a special data structures that encapsulates the acquisition and the release of a resource.
Example:
import * as fs from 'fs'
import {QIO, Managed} from '@qio/core'
// Utility Functions
const fsOpen = (name: string) => QIO.node(cb => fs.open(name, cb))
const fsClose = (fd: number) => QIO.try(() => fs.close(fd))
const fsWrite = (fd: number, text: string) => QIO.node((cb) => fs.write(fd, text, cb))
// Create a managed File Descriptor resource
const fdManaged = (name: string) => Managed.make(fsOpen(name), fsClose)
// Use the `fdManaged` to write the file.
fdManaged('./data.json').use(fd => fsWrite(fd, 'HI'))
In the above example as soon as the fsWrite
is completed the file descriptor is automatically released.
Type parameters
Name | Default |
---|---|
A1 | unknown |
E1 | never |
R1 | unknown |
Hierarchy
- Managed
Index
Methods
Methods
chain
▸ chain<A2, E2, R2>(fn
: (a: A1) => Managed<A2, E2, R2>): Managed<A2, E1 | E2, R1 & R2>
Defined in packages/core/lib/main/Managed.ts:62
Type parameters:
Name |
---|
A2 |
E2 |
R2 |
Parameters:
Name | Type |
---|---|
fn | (a: A1) => Managed<A2, E2, R2> |
Returns: Managed<A2, E1 | E2, R1 & R2>
map
▸ map<A2>(fn
: (a: A1) => A2): Managed<A2, E1, R1>
Defined in packages/core/lib/main/Managed.ts:79
Type parameters:
Name |
---|
A2 |
Parameters:
Name | Type |
---|---|
fn | (a: A1) => A2 |
Returns: Managed<A2, E1, R1>
use
▸ use<A2, E2, R2>(fn
: (a: A1) => QIO<A2, E2, R2>): QIO<A2, E1 | E2, R1 & R2>
Defined in packages/core/lib/main/Managed.ts:87
Type parameters:
Name |
---|
A2 |
E2 |
R2 |
Parameters:
Name | Type |
---|---|
fn | (a: A1) => QIO<A2, E2, R2> |
Returns: QIO<A2, E1 | E2, R1 & R2>
use_
▸ use_<A2, E2, R2>(io
: QIO<A2, E2, R2>): QIO<A2, E1 | E2, R1 & R2>
Defined in packages/core/lib/main/Managed.ts:93
Type parameters:
Name |
---|
A2 |
E2 |
R2 |
Parameters:
Name | Type |
---|---|
io | QIO<A2, E2, R2> |
Returns: QIO<A2, E1 | E2, R1 & R2>
zipWith
▸ zipWith<A2, E2, R2, X>(that
: Managed<A2, E2, R2>, fn
: (a1: A1, a2: A2) => X): Managed<X, E1 | E2, R1 & R2>
Defined in packages/core/lib/main/Managed.ts:97
Type parameters:
Name |
---|
A2 |
E2 |
R2 |
X |
Parameters:
Name | Type |
---|---|
that | Managed<A2, E2, R2> |
fn | (a1: A1, a2: A2) => X |
Returns: Managed<X, E1 | E2, R1 & R2>
make
▸ Static
make<A1, E1, R1, E2, R2>(acquire
: QIO<A1, E1, R1>, release
: (a: A1) => QIO<unknown, E2, R2>): Managed<A1, E1 | E2, R1 & R2>
Defined in packages/core/lib/main/Managed.ts:29
Type parameters:
Name |
---|
A1 |
E1 |
R1 |
E2 |
R2 |
Parameters:
Name | Type |
---|---|
acquire | QIO<A1, E1, R1> |
release | (a: A1) => QIO<unknown, E2, R2> |
Returns: Managed<A1, E1 | E2, R1 & R2>
of
▸ Static
of<A1, E1, R1>(reservation
: QIO<Reservation<A1, E1, R1>, E1, R1>): Managed<A1, E1, R1>
Defined in packages/core/lib/main/Managed.ts:40
Type parameters:
Name |
---|
A1 |
E1 |
R1 |
Parameters:
Name | Type |
---|---|
reservation | QIO<Reservation<A1, E1, R1>, E1, R1> |
Returns: Managed<A1, E1, R1>
zip
▸ Static
zip<A1, E1, R1>(managed
: Array<Managed<A1, E1, R1>>): Managed<A1[], E1, R1>
Defined in packages/core/lib/main/Managed.ts:46
Type parameters:
Name |
---|
A1 |
E1 |
R1 |
Parameters:
Name | Type |
---|---|
managed | Array<Managed<A1, E1, R1>> |
Returns: Managed<A1[], E1, R1>