Skip to content

Commit

Permalink
refactor(val): reduce bundle size by implementing val and readonly va…
Browse files Browse the repository at this point in the history
…l together
  • Loading branch information
crimx committed Apr 15, 2024
1 parent cf5b732 commit 2aca8a6
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 351 deletions.
2 changes: 1 addition & 1 deletion src/collections/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readonlyVal } from "../readonly-val";
import type { ReadonlyVal } from "../typings";
import { strictEqual } from "../utils";
import { readonlyVal } from "../val";

/**
* A reactive list. Similar to an Array except bracket-notation(e.g. `arr[0]`) is not allowed to get/set elements.
Expand Down
2 changes: 1 addition & 1 deletion src/collections/map.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readonlyVal } from "../readonly-val";
import type { ReadonlyVal } from "../typings";
import { strictEqual } from "../utils";
import { readonlyVal } from "../val";

/**
* A reactive map inherited from `Map`.
Expand Down
2 changes: 1 addition & 1 deletion src/collections/set.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readonlyVal } from "../readonly-val";
import type { ReadonlyVal } from "../typings";
import { readonlyVal } from "../val";

/**
* A reactive set inherited from `Set`.
Expand Down
8 changes: 4 additions & 4 deletions src/flatten-from.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import type {
ValVersion,
} from "./typings";

import { ReadonlyValImpl } from "./readonly-val";
import { Subscribers } from "./subscribers";
import { INIT_VALUE, isVal, strictEqual } from "./utils";
import { ValImpl } from "./val";

class FlattenFromImpl<
TValOrValue = any,
TValue = UnwrapVal<TValOrValue>
> extends ReadonlyValImpl<TValue> {
> extends ValImpl<TValue> {
public constructor(
getValue: () => TValOrValue,
listen: (handler: () => void) => ValDisposer | void | undefined,
Expand All @@ -26,7 +26,7 @@ class FlattenFromImpl<
let notified = false;

let innerMaybeVal: TValOrValue | undefined;
let innerVal: ReadonlyValImpl<TValue> | undefined | null;
let innerVal: ValImpl<TValue> | undefined | null;
let innerDisposer: ValDisposer | undefined | null;

const computeValue = (): TValue => {
Expand Down Expand Up @@ -57,7 +57,7 @@ class FlattenFromImpl<
if (!strictEqual(maybeVal, innerMaybeVal)) {
innerMaybeVal = maybeVal;
innerVal = isVal(maybeVal)
? (maybeVal as unknown as ReadonlyValImpl<TValue>)
? (maybeVal as unknown as ValImpl<TValue>)
: null;
innerDisposer?.();
innerDisposer = innerVal && innerVal.$valCompute(notify);
Expand Down
4 changes: 2 additions & 2 deletions src/flatten.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReadonlyValImpl } from "./readonly-val";
import type { ReadonlyVal, UnwrapVal, ValConfig } from "./typings";
import type { ValImpl } from "./val";

import { flattenFrom } from "./flatten-from";
import { identity } from "./utils";
Expand Down Expand Up @@ -50,7 +50,7 @@ export function flatten<TSrcValue = any, TValOrValue = any>(
export function flatten<
TSrcValue = any,
TValOrValue = any,
TSrcVal extends ReadonlyValImpl = ReadonlyValImpl<TSrcValue>
TSrcVal extends ValImpl = ValImpl<TSrcValue>
>(
val: TSrcVal,
get: (value: TSrcValue) => TValOrValue = identity as any,
Expand Down
4 changes: 2 additions & 2 deletions src/from.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ReadonlyValImpl } from "./readonly-val";
import { Subscribers } from "./subscribers";
import type {
ReadonlyVal,
Expand All @@ -7,8 +6,9 @@ import type {
ValVersion,
} from "./typings";
import { INIT_VALUE } from "./utils";
import { ValImpl } from "./val";

class FromImpl<TValue = any> extends ReadonlyValImpl<TValue> {
class FromImpl<TValue = any> extends ValImpl<TValue> {
public constructor(
getValue: () => TValue,
listen: (notify: () => void) => ValDisposer | void | undefined,
Expand Down
18 changes: 7 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export { combine, type CombineValTransform } from "./combine";
export { derive, type DerivedValTransform } from "./derive";
export { flatten } from "./flatten";
export { flattenFrom } from "./flatten-from";
export { from } from "./from";
export { nextTick } from "./scheduler";
export type {
FlattenVal,
ReadonlyVal,
Expand All @@ -10,16 +16,6 @@ export type {
ValSubscriber,
ValVersion,
} from "./typings";

export { nextTick } from "./scheduler";
export { arrayShallowEqual, identity, isVal, strictEqual } from "./utils";

export { combine, type CombineValTransform } from "./combine";
export { derive, type DerivedValTransform } from "./derive";
export { flatten } from "./flatten";
export { flattenFrom } from "./flatten-from";
export { from } from "./from";
export { groupVals, readonlyVal } from "./readonly-val";
export { val } from "./val";

export { groupVals, readonlyVal, val } from "./val";
export { reaction, setValue, subscribe, unsubscribe } from "./value-enhancer";
263 changes: 0 additions & 263 deletions src/readonly-val.ts

This file was deleted.

0 comments on commit 2aca8a6

Please sign in to comment.