Skip to content

Commit b79681b

Browse files
authored
Fixed plum circular dependency. (#291)
1 parent 75de8e3 commit b79681b

21 files changed

+197
-104
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@types/node": "^20.11.13",
4040
"@vitest/coverage-v8": "0.33.0",
4141
"@vitest/ui": "0.33.0",
42+
"dpdm": "^3.14.0",
4243
"husky": "^9.0.11",
4344
"jsdom": "^24.0.0",
4445
"prettier": "^3.2.4",

packages/typegpu/src/createRuntime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
type TgpuPlum,
1414
type Unsubscribe,
1515
isPlum,
16-
} from './tgpuPlum';
16+
} from './tgpuPlumTypes';
1717
import type {
1818
ComputePipelineExecutorOptions,
1919
ComputePipelineOptions,

packages/typegpu/src/data/struct.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ import {
1111
object,
1212
} from 'typed-binary';
1313
import { RecursiveDataTypeError } from '../errors';
14+
import type { TgpuNamable } from '../namable';
1415
import { code } from '../tgpuCode';
1516
import { TgpuIdentifier } from '../tgpuIdentifier';
16-
import type {
17-
AnyTgpuData,
18-
ResolutionCtx,
19-
TgpuData,
20-
TgpuNamable,
21-
} from '../types';
17+
import type { AnyTgpuData, ResolutionCtx, TgpuData } from '../types';
2218
import { TgpuAlignedImpl } from './align';
2319
import alignIO from './alignIO';
2420
import { TgpuSizedImpl } from './size';

packages/typegpu/src/experimental/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type {
3838
export type { TgpuCode } from '../tgpuCode';
3939
export type { TgpuConst } from '../tgpuConstant';
4040
export type { TgpuFn } from '../tgpuFunction';
41-
export type { TgpuPlum } from '../tgpuPlum';
41+
export type { TgpuPlum } from '../tgpuPlumTypes';
4242
export type { TgpuSettable } from '../settableTrait';
4343
export type { TgpuFn as TgpuFnExperimental } from '../tgpuFunctionExperimental';
4444
export type { TgpuVar } from '../tgpuVariable';

packages/typegpu/src/namable.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Can be assigned a name. Not to be confused with
3+
* being able to HAVE a name.
4+
*/
5+
export interface TgpuNamable {
6+
$name(label?: string | undefined): this;
7+
}
8+
9+
export function isNamable(value: unknown): value is TgpuNamable {
10+
return (
11+
!!value &&
12+
(typeof value === 'object' || typeof value === 'function') &&
13+
'$name' in value
14+
);
15+
}

packages/typegpu/src/plumStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type Getter,
55
type TgpuPlum,
66
isExternalPlum,
7-
} from './tgpuPlum';
7+
} from './tgpuPlumTypes';
88

99
export type PlumListener<T> = (newValue: T) => unknown;
1010
type Unsubscribe = () => void;

packages/typegpu/src/tgpuBuffer.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { BufferWriter, type Parsed } from 'typed-binary';
2-
import type { TgpuPlum } from './tgpuPlum';
3-
import {
4-
type AnyTgpuData,
5-
type TgpuAllocatable,
6-
type TgpuNamable,
7-
isGPUBuffer,
8-
} from './types';
2+
import type { TgpuNamable } from './namable';
3+
import type { TgpuPlum } from './tgpuPlumTypes';
4+
import { type AnyTgpuData, type TgpuAllocatable, isGPUBuffer } from './types';
95

106
// ----------
117
// Public API

packages/typegpu/src/tgpuCode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { getBuiltinInfo } from './builtin';
2+
import type { TgpuNamable } from './namable';
23
import {
34
type Eventual,
45
type InlineResolve,
56
type ResolutionCtx,
67
type SlotValuePair,
7-
type TgpuNamable,
88
type TgpuResolvable,
99
type TgpuSlot,
1010
type Wgsl,

packages/typegpu/src/tgpuConstant.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { TgpuNamable } from './namable';
12
import { code } from './tgpuCode';
23
import { TgpuIdentifier } from './tgpuIdentifier';
3-
import type { ResolutionCtx, TgpuNamable, TgpuResolvable, Wgsl } from './types';
4+
import type { ResolutionCtx, TgpuResolvable, Wgsl } from './types';
45

56
// ----------
67
// Public API

packages/typegpu/src/tgpuFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import type { TgpuNamable } from './namable';
12
import { code } from './tgpuCode';
23
import { TgpuIdentifier } from './tgpuIdentifier';
34
import type {
45
Eventual,
56
InlineResolve,
67
ResolutionCtx,
78
SlotValuePair,
8-
TgpuNamable,
99
TgpuResolvable,
1010
TgpuSlot,
1111
Wgsl,

packages/typegpu/src/tgpuFunctionExperimental.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { type AsCallable, CallableImpl } from './callable';
2+
import type { TgpuNamable } from './namable';
23
import { code } from './tgpuCode';
34
import { TgpuIdentifier } from './tgpuIdentifier';
45
import { isPointer } from './types';
56
import type {
67
AnyTgpuData,
78
ResolutionCtx,
89
TgpuFnArgument,
9-
TgpuNamable,
1010
TgpuResolvable,
1111
TgpuValue,
1212
Wgsl,

packages/typegpu/src/tgpuIdentifier.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { ResolutionCtx, TgpuNamable, TgpuResolvable } from './types';
1+
import type { TgpuNamable } from './namable';
2+
import type { ResolutionCtx, TgpuResolvable } from './types';
23

34
/**
45
* Helpful when creating new Resolvable types. For internal use.

packages/typegpu/src/tgpuPlum.ts

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,17 @@
11
import { type TgpuSettable, TgpuSettableTrait } from './settableTrait';
2-
import type { TgpuNamable, TgpuResolvable, Wgsl } from './types';
2+
import {
3+
type Getter,
4+
type TgpuExternalPlum,
5+
TgpuExternalPlumTrait,
6+
type TgpuPlum,
7+
type Unsubscribe,
8+
} from './tgpuPlumTypes';
9+
import type { TgpuResolvable, Wgsl } from './types';
310

411
// ----------
512
// Public API
613
// ----------
714

8-
export type Getter = <T>(plum: TgpuPlum<T>) => T;
9-
export type Unsubscribe = () => unknown;
10-
export type ExtractPlumValue<T> = T extends TgpuPlum<infer TValue>
11-
? TValue
12-
: never;
13-
14-
export interface TgpuPlum<TValue = unknown> extends TgpuNamable {
15-
readonly __brand: 'TgpuPlum';
16-
17-
/**
18-
* Computes the value of this plum. Circumvents the store
19-
* memoization, so use with care.
20-
*/
21-
compute(get: Getter): TValue;
22-
}
23-
24-
export const TgpuExternalPlumTrait = Symbol(
25-
`This plum's value is sourced from outside the runtime.`,
26-
);
27-
export interface TgpuExternalPlum {
28-
readonly [TgpuExternalPlumTrait]: true;
29-
30-
readonly version: number;
31-
subscribe(listener: () => unknown): Unsubscribe;
32-
}
33-
34-
export function isExternalPlum(
35-
value: unknown | TgpuExternalPlum,
36-
): value is TgpuExternalPlum {
37-
return (value as TgpuExternalPlum)[TgpuExternalPlumTrait] === true;
38-
}
39-
4015
/**
4116
* Creates a computed plum. Its value depends on the plums read using `get`
4217
* inside the `compute` function, so cannot be set imperatively.
@@ -90,10 +65,6 @@ export function plumFromEvent<T>(
9065
return new TgpuExternalPlumImpl(subscribe, getLatest);
9166
}
9267

93-
export function isPlum<T>(value: TgpuPlum<T> | unknown): value is TgpuPlum<T> {
94-
return (value as TgpuPlum).__brand === 'TgpuPlum';
95-
}
96-
9768
// --------------
9869
// Implementation
9970
// --------------

packages/typegpu/src/tgpuPlumTypes.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { TgpuNamable } from './namable';
2+
3+
export type Getter = <T>(plum: TgpuPlum<T>) => T;
4+
export type Unsubscribe = () => unknown;
5+
export type ExtractPlumValue<T> = T extends TgpuPlum<infer TValue>
6+
? TValue
7+
: never;
8+
9+
export interface TgpuPlum<TValue = unknown> extends TgpuNamable {
10+
readonly __brand: 'TgpuPlum';
11+
12+
/**
13+
* Computes the value of this plum. Circumvents the store
14+
* memoization, so use with care.
15+
*/
16+
compute(get: Getter): TValue;
17+
}
18+
19+
export const TgpuExternalPlumTrait = Symbol(
20+
`This plum's value is sourced from outside the runtime.`,
21+
);
22+
23+
export interface TgpuExternalPlum {
24+
readonly [TgpuExternalPlumTrait]: true;
25+
26+
readonly version: number;
27+
subscribe(listener: () => unknown): Unsubscribe;
28+
}
29+
30+
export function isExternalPlum(
31+
value: unknown | TgpuExternalPlum,
32+
): value is TgpuExternalPlum {
33+
return (value as TgpuExternalPlum)[TgpuExternalPlumTrait] === true;
34+
}
35+
36+
export function isPlum<T>(value: TgpuPlum<T> | unknown): value is TgpuPlum<T> {
37+
return (value as TgpuPlum).__brand === 'TgpuPlum';
38+
}

packages/typegpu/src/tgpuRuntime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { SimpleTgpuData, TgpuArray } from './data';
44
import type { PlumListener } from './plumStore';
55
import type { TgpuSettable } from './settableTrait';
66
import type { BoundTgpuCode, TgpuCode } from './tgpuCode';
7-
import type { ExtractPlumValue, TgpuPlum, Unsubscribe } from './tgpuPlum';
7+
import type { ExtractPlumValue, TgpuPlum, Unsubscribe } from './tgpuPlumTypes';
88
import type { TgpuSampler } from './tgpuSampler';
99
import type {
1010
TgpuAnyTexture,

packages/typegpu/src/tgpuSampler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import type { TgpuNamable } from './namable';
12
import { TgpuIdentifier } from './tgpuIdentifier';
23
import type {
34
ResolutionCtx,
4-
TgpuNamable,
55
TgpuRenderResource,
66
TgpuSamplerType,
77
} from './types';

packages/typegpu/src/tgpuTexture.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { vec4f, vec4i, vec4u } from './data';
2+
import type { TgpuNamable } from './namable';
23
import { TgpuIdentifier } from './tgpuIdentifier';
34
import { isSampler } from './tgpuSampler';
45
import type {
@@ -9,7 +10,6 @@ import type {
910
TexelFormat,
1011
TextureScalarFormat,
1112
TextureUsage,
12-
TgpuNamable,
1313
TgpuRenderResource,
1414
TgpuRenderResourceType,
1515
} from './types';

packages/typegpu/src/tgpuVariable.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1+
import type { TgpuNamable } from './namable';
12
import { code } from './tgpuCode';
23
import { TgpuIdentifier } from './tgpuIdentifier';
3-
import type {
4-
AnyTgpuData,
5-
ResolutionCtx,
6-
TgpuNamable,
7-
TgpuResolvable,
8-
Wgsl,
9-
} from './types';
4+
import type { AnyTgpuData, ResolutionCtx, TgpuResolvable, Wgsl } from './types';
105

116
// ----------
127
// Public API

packages/typegpu/src/types.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { ISchema, Parsed } from 'typed-binary';
22
import type { Builtin } from './builtin';
33
import type { F32, I32, U32, Vec4f, Vec4i, Vec4u } from './data';
4+
import type { TgpuNamable } from './namable';
45
import type { TgpuIdentifier } from './tgpuIdentifier';
5-
import type { TgpuPlum } from './tgpuPlum';
6+
import type { TgpuPlum } from './tgpuPlumTypes';
67

78
export type Wgsl = string | number | TgpuResolvable | symbol | boolean;
89

@@ -37,14 +38,6 @@ export interface TgpuResolvable {
3738
resolve(ctx: ResolutionCtx): string;
3839
}
3940

40-
/**
41-
* Can be assigned a name. Not to be confused with
42-
* being able to HAVE a name.
43-
*/
44-
export interface TgpuNamable {
45-
$name(label?: string | undefined): this;
46-
}
47-
4841
export function isResolvable(value: unknown): value is TgpuResolvable {
4942
return (
5043
!!value &&
@@ -53,14 +46,6 @@ export function isResolvable(value: unknown): value is TgpuResolvable {
5346
);
5447
}
5548

56-
export function isNamable(value: unknown): value is TgpuNamable {
57-
return (
58-
!!value &&
59-
(typeof value === 'object' || typeof value === 'function') &&
60-
'$name' in value
61-
);
62-
}
63-
6449
export function isWgsl(value: unknown): value is Wgsl {
6550
return (
6651
typeof value === 'number' ||

packages/typegpu/tests/plumStore.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it, vi } from 'vitest';
22
import { type PlumListener, PlumStore } from '../src/plumStore';
3-
import { type Getter, plum, plumFromEvent } from '../src/tgpuPlum';
3+
import { plum, plumFromEvent } from '../src/tgpuPlum';
4+
import type { Getter } from '../src/tgpuPlumTypes';
45

56
function makeSubject<T>(initial: T) {
67
let value = initial;

0 commit comments

Comments
 (0)