Skip to content

Commit

Permalink
result
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Apr 24, 2023
1 parent bbc3251 commit c1a1477
Show file tree
Hide file tree
Showing 8 changed files with 715 additions and 185 deletions.
751 changes: 664 additions & 87 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@
},
"dependencies": {
"@hazae41/bytes": "^1.0.4",
"@hazae41/mutex": "^1.2.1",
"@hazae41/result": "^1.0.0"
"@hazae41/mutex": "^1.2.5",
"@hazae41/result": "^1.0.2"
},
"peerDependencies": {
"react": ">=17"
},
"devDependencies": {
"@hazae41/phobos": "^1.0.10",
"@rollup/plugin-typescript": "^11.0.0",
"@types/node": "^18.11.18",
"@types/react": "^18.0.26",
"rimraf": "^4.1.0",
"rollup": "^3.10.0",
"rollup-plugin-dts": "^5.1.1",
"rollup-plugin-node-externals": "^5.1.0",
"typescript": "^4.9.4"
"@rollup/plugin-typescript": "^11.1.0",
"@types/node": "^18.16.0",
"@types/react": "^18.0.38",
"rimraf": "^5.0.0",
"rollup": "^3.21.0",
"rollup-plugin-dts": "^5.3.0",
"rollup-plugin-node-externals": "^5.1.2",
"typescript": "^5.0.4"
},
"exports": {
".": {
Expand Down
51 changes: 19 additions & 32 deletions src/mods/result/data.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { Ok } from "@hazae41/result"
import { Promiseable } from "libs/promises/promises.js"
import { Error } from "./error.js"
import { Times } from "./times.js"

export interface DataInit<D> extends Times {
readonly data: D
export interface DataInit<T> extends Times {
readonly data: T
}

export class Data<D> implements DataInit<D> {
export class Data<T> extends Ok<T> implements DataInit<T> {

constructor(
readonly data: D,
readonly data: T,
readonly times: Times = {}
) { }
) {
super(data)
}

static from<D>(init: DataInit<D>) {
static from<T>(init: DataInit<T>) {
const { data, time, cooldown, expiration } = init
return new this(data, { time, cooldown, expiration })
}
Expand All @@ -30,47 +33,31 @@ export class Data<D> implements DataInit<D> {
return this.times.expiration
}

unwrap() {
return this.data
isData(): this is Data<T> {
return true
}

isError(): false {
return false
}

/**
* Map this data into another, throwing if mapper throws
* @param mutator
* @returns
*/
async map<M>(mapper: (data: D) => Promiseable<M>) {
async map<M>(mapper: (data: T) => Promiseable<M>) {
return new Data<M>(await mapper(this.data), this.times)
}

/**
* Try to map this data into another, returning Error if mapper throws
* @param mapper
* @returns
*/
async tryMap<M>(mapper: (data: D) => Promiseable<M>) {
async tryMap<M>(mapper: (data: T) => Promiseable<M>) {
try {
return await this.map(mapper)
} catch (error: unknown) {
return new Error(error, this.times)
}
}

/**
* Map this data into another, throwing if mapper throws
* @param mutator
* @returns
*/
mapSync<M>(mapper: (data: D) => M) {
mapSync<M>(mapper: (data: T) => M) {
return new Data<M>(mapper(this.data), this.times)
}

/**
* Try to map this data into another, returning Error if mapper throws
* @param mapper
* @returns
*/
tryMapSync<M>(mapper: (data: D) => M) {
tryMapSync<M>(mapper: (data: T) => M) {
try {
return this.mapSync(mapper)
} catch (error: unknown) {
Expand Down
33 changes: 12 additions & 21 deletions src/mods/result/error.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Err } from "@hazae41/result"
import { Times } from "./times.js"

export interface ErrorInit<E = unknown> extends Times {
readonly error: E
export interface ErrorInit<T = unknown> extends Times {
readonly error: T
}

export class Error<E = unknown> implements ErrorInit<E> {
export class Error<T = unknown> extends Err<T> implements ErrorInit<T> {

constructor(
readonly error: E,
readonly error: T,
readonly times: Times = {}
) { }
) {
super(error)
}

static from(init: ErrorInit) {
const { error, time, cooldown, expiration } = init
Expand All @@ -28,24 +31,12 @@ export class Error<E = unknown> implements ErrorInit<E> {
return this.times.expiration
}

unwrap(): never {
throw this.error
}

map(mapper: unknown) {
return this
}

tryMap(mapper: unknown) {
return this
}

mapSync(mapper: unknown) {
return this
isData(): false {
return false
}

tryMapSync(mapper: unknown) {
return this
isError(): this is Error<T> {
return true
}

}
Expand Down
33 changes: 4 additions & 29 deletions src/mods/result/result.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { Wrapper } from "@hazae41/result"
import { Promiseable } from "libs/promises/promises.js"
import { Data, DataInit } from "./data.js"
import { Error, ErrorInit } from "./error.js"
import { Times } from "./times.js"

export type ResultInit<D = unknown, E = unknown> =
export type FetchResultInit<D = unknown, E = unknown> =
| DataInit<D>
| ErrorInit<E>

export type Result<D = unknown, E = unknown> =
export type FetchResult<D = unknown, E = unknown> =
| Data<D>
| Error<E>

export namespace Result {
export namespace FetchResult {

export function from<D>(init: ResultInit<D>) {
export function from<D>(init: FetchResultInit<D>) {
if ("error" in init)
return Error.from(init)
else
Expand All @@ -29,28 +28,4 @@ export namespace Result {
}
}

export async function wrap<D>(callback: () => Promiseable<D>, times: Times = {}) {
return new Data(await callback(), times)
}

export async function tryWrap<D>(callback: () => Promiseable<D>, times: Times = {}) {
try {
return await wrap(callback, times)
} catch (error: unknown) {
return new Error(error, times)
}
}

export function wrapSync<D>(callback: () => D, times: Times = {}) {
return new Data(callback(), times)
}

export function tryWrapSync<D>(callback: () => D, times: Times = {}) {
try {
return wrapSync(callback, times)
} catch (error: unknown) {
return new Error(error, times)
}
}

}
4 changes: 2 additions & 2 deletions src/mods/single/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Time } from "libs/time/time.js";
import { Core } from "mods/core/core.js";
import { DEFAULT_COOLDOWN, DEFAULT_EXPIRATION, DEFAULT_SERIALIZER, DEFAULT_TIMEOUT } from "mods/defaults.js";
import { AbortError } from "mods/errors/abort.js";
import { ResultInit } from "mods/result/result.js";
import { FetchResultInit } from "mods/result/result.js";
import { Fetcher } from "mods/types/fetcher.js";
import { QueryParams } from "mods/types/params.js";
import { State } from "mods/types/state.js";
Expand Down Expand Up @@ -155,7 +155,7 @@ export namespace Single {

const generator = updater({ signal })

let final: ResultInit<D> | void
let final: FetchResultInit<D> | void

while (true) {
const { done, value } = await generator.next()
Expand Down
4 changes: 2 additions & 2 deletions src/mods/types/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ResultInit } from "mods/result/result.js"
import { FetchResultInit } from "mods/result/result.js"

export type Fetcher<D = unknown, K = unknown> =
(key: K, more: FetcherMore) => Promise<ResultInit<D>>
(key: K, more: FetcherMore) => Promise<FetchResultInit<D>>

export interface FetcherMore {
readonly signal?: AbortSignal,
Expand Down
4 changes: 2 additions & 2 deletions src/mods/types/updater.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ResultInit } from "mods/result/result.js"
import { FetchResultInit } from "mods/result/result.js"
import { OptimisticYield } from "./optimism.js"

export type Updater<D> =
(more: UpdaterMore) => AsyncGenerator<OptimisticYield<D>, ResultInit<D> | void>
(more: UpdaterMore) => AsyncGenerator<OptimisticYield<D>, FetchResultInit<D> | void>

export interface UpdaterMore {
signal?: AbortSignal
Expand Down

0 comments on commit c1a1477

Please sign in to comment.