Skip to content

Commit 008a3b6

Browse files
committed
chore: use const enums
1 parent 1fe4f41 commit 008a3b6

File tree

6 files changed

+47
-30
lines changed

6 files changed

+47
-30
lines changed

packages/core/src/internal/BehaviorObservable.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ import { Observable } from "rxjs"
33
export interface BehaviorObservable<T> extends Observable<T> {
44
getValue: () => any
55
}
6+
7+
export const enum Action {
8+
Error,
9+
Value,
10+
Suspense,
11+
}

packages/core/src/internal/react-enhancer.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Observable, noop } from "rxjs"
22
import { take, filter, tap } from "rxjs/operators"
33
import { SUSPENSE } from "../SUSPENSE"
4-
import { BehaviorObservable } from "./BehaviorObservable"
4+
import { BehaviorObservable, Action } from "./BehaviorObservable"
55
import { EMPTY_VALUE } from "./empty-value"
66

77
const reactEnhancer = <T>(source$: Observable<T>): BehaviorObservable<T> => {
@@ -42,9 +42,11 @@ const reactEnhancer = <T>(source$: Observable<T>): BehaviorObservable<T> => {
4242
}
4343
}) as BehaviorObservable<T>
4444

45-
let promise: any
46-
let error = EMPTY_VALUE
47-
const getValue = () => {
45+
let promise: undefined | { type: Action.Suspense; payload: Promise<T | void> }
46+
let error:
47+
| typeof EMPTY_VALUE
48+
| { type: Action.Error; payload: any } = EMPTY_VALUE
49+
const getValue = (): { type: Action; payload: any } => {
4850
let timeoutToken
4951
if (error !== EMPTY_VALUE) {
5052
clearTimeout(timeoutToken)
@@ -56,25 +58,27 @@ const reactEnhancer = <T>(source$: Observable<T>): BehaviorObservable<T> => {
5658

5759
try {
5860
return {
59-
type: "v",
61+
type: Action.Value,
6062
payload: (source$ as BehaviorObservable<T>).getValue(),
6163
}
6264
} catch (e) {
6365
if (promise) return promise
6466

65-
let value = EMPTY_VALUE
67+
let value:
68+
| typeof EMPTY_VALUE
69+
| { type: Action.Value; payload: T } = EMPTY_VALUE
6670
promise = {
67-
type: "s",
71+
type: Action.Suspense,
6872
payload: result
6973
.pipe(
7074
filter((x) => x !== (SUSPENSE as any)),
7175
take(1),
7276
tap({
7377
next(v) {
74-
value = { type: "v", payload: v }
78+
value = { type: Action.Value, payload: v }
7579
},
7680
error(e) {
77-
error = { type: "e", payload: e }
81+
error = { type: Action.Error, payload: e }
7882
timeoutToken = setTimeout(() => {
7983
error = EMPTY_VALUE
8084
}, 50)
@@ -99,7 +103,7 @@ const reactEnhancer = <T>(source$: Observable<T>): BehaviorObservable<T> => {
99103
return promise
100104
}
101105
}
102-
result.getValue = getValue as () => T | Promise<T>
106+
result.getValue = getValue
103107
return result
104108
}
105109

packages/core/src/internal/useObservable.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { useEffect, useReducer } from "react"
2-
import { BehaviorObservable } from "./BehaviorObservable"
2+
import { BehaviorObservable, Action } from "./BehaviorObservable"
33
import { SUSPENSE } from "../SUSPENSE"
44
import { Observable } from "rxjs"
55

6-
const ERROR: "e" = "e"
7-
const VALUE: "v" = "v"
8-
type Action = "e" | "v" | "s"
9-
106
const reducer = (
117
current: { type: Action; payload: any },
128
action: { type: Action; payload: any },
@@ -47,21 +43,21 @@ export const useObservable = <O>(
4743
dispatch(source$.getValue())
4844
} else {
4945
dispatch({
50-
type: VALUE,
46+
type: Action.Value,
5147
payload: value,
5248
})
5349
}
5450
},
5551
(error) =>
5652
dispatch({
57-
type: ERROR,
53+
type: Action.Error,
5854
payload: error,
5955
}),
6056
)
6157
return () => subscription.unsubscribe()
6258
}, [source$])
6359

6460
const { type, payload } = state
65-
if (type === VALUE) return payload
61+
if (type === Action.Value) return payload
6662
throw payload
6763
}

packages/utils/src/collect.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
distinctUntilChanged,
88
skipWhile,
99
} from "rxjs/operators"
10-
import { set, del, collector } from "./internal-utils"
10+
import { CollectorAction, collector } from "./internal-utils"
1111

1212
const defaultFilter = (source$: Observable<any>) =>
1313
source$.pipe(ignoreElements(), startWith(true), endWith(false))
@@ -37,6 +37,12 @@ export const collect = <K, V>(
3737

3838
return (source$: Observable<GroupedObservable<K, V>>) =>
3939
collector(source$, (o) =>
40-
map((x) => ({ t: x ? set : del, k: o.key, v: o }))(enhancer(o)),
40+
map((x) => ({
41+
t: x
42+
? (CollectorAction.Set as const)
43+
: (CollectorAction.Delete as const),
44+
k: o.key,
45+
v: o,
46+
}))(enhancer(o)),
4147
)
4248
}

packages/utils/src/collectValues.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Observable, GroupedObservable, OperatorFunction } from "rxjs"
22
import { map, endWith } from "rxjs/operators"
3-
import { set, del, collector } from "./internal-utils"
3+
import { CollectorAction, collector } from "./internal-utils"
44

55
/**
66
* A pipeable operator that collects all the GroupedObservables emitted by
@@ -12,7 +12,7 @@ export const collectValues = <K, V>(): OperatorFunction<
1212
> => (source$: Observable<GroupedObservable<K, V>>): Observable<Map<K, V>> =>
1313
collector(source$, (inner$) =>
1414
inner$.pipe(
15-
map((v) => ({ t: set, k: inner$.key, v })),
16-
endWith({ t: del, k: inner$.key }),
15+
map((v) => ({ t: CollectorAction.Set as const, k: inner$.key, v })),
16+
endWith({ t: CollectorAction.Delete, k: inner$.key }),
1717
),
1818
)

packages/utils/src/internal-utils.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,29 @@ export const scanWithDefaultValue = <I, O>(
3737
return source.pipe(scan(accumulator, seed), defaultStart(seed))
3838
})
3939

40-
export const set = "s" as const
41-
export const del = "d" as const
42-
export const complete = "c" as const
40+
export const enum CollectorAction {
41+
Set,
42+
Delete,
43+
Complete,
44+
}
4345

4446
export const collector = <K, V, VV>(
4547
source: Observable<GroupedObservable<K, V>>,
4648
enhancer: (
4749
source: GroupedObservable<K, V>,
48-
) => Observable<{ t: "d"; k: K } | { t: "s"; k: K; v: VV }>,
50+
) => Observable<
51+
| { t: CollectorAction.Delete; k: K }
52+
| { t: CollectorAction.Set; k: K; v: VV }
53+
>,
4954
): Observable<Map<K, VV>> =>
5055
source.pipe(
5156
publish((x) => x.pipe(mergeMap(enhancer), takeUntil(takeLast(1)(x)))),
52-
endWith({ t: complete }),
57+
endWith({ t: CollectorAction.Complete as const }),
5358
scanWithDefaultValue(
5459
(acc, val) => {
55-
if (val.t === set) {
60+
if (val.t === CollectorAction.Set) {
5661
acc.set(val.k, val.v)
57-
} else if (val.t === del) {
62+
} else if (val.t === CollectorAction.Delete) {
5863
acc.delete(val.k)
5964
} else {
6065
acc.clear()

0 commit comments

Comments
 (0)