Skip to content

Commit 90ff06c

Browse files
committed
Remove cruft
1 parent ef93119 commit 90ff06c

6 files changed

Lines changed: 249 additions & 521 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
export class SpacetimeError {
2+
public readonly code: number;
3+
public readonly message: string;
4+
constructor(code: number) {
5+
const proto = Object.getPrototypeOf(this);
6+
let cls;
7+
if (error_protoypes.has(proto)) {
8+
cls = proto.constructor;
9+
if (code !== cls.CODE)
10+
throw new TypeError(`invalid error code for ${cls.name}`);
11+
} else if (proto === SpacetimeError.prototype) {
12+
cls = errno_to_class.get(code);
13+
if (!cls) throw new RangeError(`unknown error code ${code}`);
14+
} else {
15+
throw new TypeError('cannot subclass SpacetimeError');
16+
}
17+
Object.setPrototypeOf(this, cls.prototype);
18+
this.code = cls.CODE;
19+
this.message = cls.MESSAGE;
20+
}
21+
}
22+
23+
export class HostCallFailure extends SpacetimeError {
24+
static CODE = 1;
25+
static MESSAGE = 'ABI called by host returned an error';
26+
constructor() {
27+
super(HostCallFailure.CODE);
28+
}
29+
}
30+
export class NotInTransaction extends SpacetimeError {
31+
static CODE = 2;
32+
static MESSAGE = 'ABI call can only be made while in a transaction';
33+
constructor() {
34+
super(NotInTransaction.CODE);
35+
}
36+
}
37+
export class BsatnDecodeError extends SpacetimeError {
38+
static CODE = 3;
39+
static MESSAGE = "Couldn't decode the BSATN to the expected type";
40+
constructor() {
41+
super(BsatnDecodeError.CODE);
42+
}
43+
}
44+
export class NoSuchTable extends SpacetimeError {
45+
static CODE = 4;
46+
static MESSAGE = 'No such table';
47+
constructor() {
48+
super(NoSuchTable.CODE);
49+
}
50+
}
51+
export class NoSuchIndex extends SpacetimeError {
52+
static CODE = 5;
53+
static MESSAGE = 'No such index';
54+
constructor() {
55+
super(NoSuchIndex.CODE);
56+
}
57+
}
58+
export class NoSuchIter extends SpacetimeError {
59+
static CODE = 6;
60+
static MESSAGE = 'The provided row iterator is not valid';
61+
constructor() {
62+
super(NoSuchIter.CODE);
63+
}
64+
}
65+
export class NoSuchConsoleTimer extends SpacetimeError {
66+
static CODE = 7;
67+
static MESSAGE = 'The provided console timer does not exist';
68+
constructor() {
69+
super(NoSuchConsoleTimer.CODE);
70+
}
71+
}
72+
export class NoSuchBytes extends SpacetimeError {
73+
static CODE = 8;
74+
static MESSAGE = 'The provided bytes source or sink is not valid';
75+
constructor() {
76+
super(NoSuchBytes.CODE);
77+
}
78+
}
79+
export class NoSpace extends SpacetimeError {
80+
static CODE = 9;
81+
static MESSAGE = 'The provided sink has no more space left';
82+
constructor() {
83+
super(NoSpace.CODE);
84+
}
85+
}
86+
export class BufferTooSmall extends SpacetimeError {
87+
static CODE = 11;
88+
static MESSAGE = 'The provided buffer is not large enough to store the data';
89+
constructor() {
90+
super(BufferTooSmall.CODE);
91+
}
92+
}
93+
export class UniqueAlreadyExists extends SpacetimeError {
94+
static CODE = 12;
95+
static MESSAGE = 'Value with given unique identifier already exists';
96+
constructor() {
97+
super(UniqueAlreadyExists.CODE);
98+
}
99+
}
100+
export class ScheduleAtDelayTooLong extends SpacetimeError {
101+
static CODE = 13;
102+
static MESSAGE = 'Specified delay in scheduling row was too long';
103+
constructor() {
104+
super(ScheduleAtDelayTooLong.CODE);
105+
}
106+
}
107+
export class IndexNotUnique extends SpacetimeError {
108+
static CODE = 14;
109+
static MESSAGE = 'The index was not unique';
110+
constructor() {
111+
super(IndexNotUnique.CODE);
112+
}
113+
}
114+
export class NoSuchRow extends SpacetimeError {
115+
static CODE = 15;
116+
static MESSAGE = 'The row was not found, e.g., in an update call';
117+
constructor() {
118+
super(NoSuchRow.CODE);
119+
}
120+
}
121+
export class AutoIncOverflow extends SpacetimeError {
122+
static CODE = 16;
123+
static MESSAGE = 'The auto-increment sequence overflowed';
124+
constructor() {
125+
super(AutoIncOverflow.CODE);
126+
}
127+
}
128+
129+
const error_subclasses = [
130+
HostCallFailure,
131+
NotInTransaction,
132+
BsatnDecodeError,
133+
NoSuchTable,
134+
NoSuchIndex,
135+
NoSuchIter,
136+
NoSuchConsoleTimer,
137+
NoSuchBytes,
138+
NoSpace,
139+
BufferTooSmall,
140+
UniqueAlreadyExists,
141+
ScheduleAtDelayTooLong,
142+
IndexNotUnique,
143+
NoSuchRow,
144+
];
145+
146+
const error_protoypes = new Set(error_subclasses.map(cls => cls.prototype));
147+
148+
const errno_to_class = new Map(
149+
error_subclasses.map(cls => [cls.CODE as number, cls])
150+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
export * from './type_builders';
22
export * from './type_util';
3+
export * from './schema';
4+
5+
import './rt';

crates/bindings-typescript/src/server/rt.ts

Lines changed: 49 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,68 @@
1+
import type { Reducer } from 'react';
12
import { AlgebraicType, ProductType } from '../lib/algebraic_type';
23
import RawModuleDef from '../lib/autogen/raw_module_def_type';
34
import type RawModuleDefV9 from '../lib/autogen/raw_module_def_v_9_type';
5+
import type RawReducerDefV9 from '../lib/autogen/raw_reducer_def_v_9_type';
46
import type RawTableDefV9 from '../lib/autogen/raw_table_def_v_9_type';
57
import type Typespace from '../lib/autogen/typespace_type';
68
import { ConnectionId } from '../lib/connection_id';
79
import { Identity } from '../lib/identity';
810
import { Timestamp } from '../lib/timestamp';
911
import { BinaryReader, BinaryWriter } from '../sdk';
12+
import { AutoIncOverflow, SpacetimeError, UniqueAlreadyExists } from './errors';
1013
import {
11-
MODULE_DEF,
12-
REDUCERS,
14+
Range,
15+
type Bound,
1316
type DbView,
17+
type Index,
18+
type IndexVal,
19+
type RangedIndex,
1420
type ReducerCtx,
15-
type Table,
16-
type UntypedTableDef,
21+
type RowObj,
1722
type RowType,
18-
type TryInsertError,
19-
type Result,
23+
type Table,
2024
type TableMethods,
21-
type Index,
2225
type UniqueIndex,
23-
type RangedIndex,
24-
type IndexVal,
25-
type IndexScanRangeBounds,
26-
Range,
27-
type Bound,
2826
} from './schema';
29-
import type { InferTypeOfTypeBuilder, ColumnBuilder } from './type_builders';
27+
28+
/*****************************************************************
29+
* the run‑time catalogue that we are filling
30+
*****************************************************************/
31+
export const MODULE_DEF: RawModuleDefV9 = {
32+
typespace: { types: [] },
33+
tables: [],
34+
reducers: [],
35+
types: [],
36+
miscExports: [],
37+
rowLevelSecurity: [],
38+
};
39+
40+
/*****************************************************************
41+
* internal: pushReducer() helper used by reducer() and lifecycle wrappers
42+
*****************************************************************/
43+
export function pushReducer(
44+
name: string,
45+
params: RowObj,
46+
fn: Reducer<any, any>,
47+
lifecycle?: RawReducerDefV9['lifecycle']
48+
): void {
49+
const paramType: ProductType = {
50+
elements: Object.entries(params).map(([n, c]) => ({
51+
name: n,
52+
algebraicType: ('typeBuilder' in c ? c.typeBuilder : c).algebraicType,
53+
})),
54+
};
55+
56+
MODULE_DEF.reducers.push({
57+
name,
58+
params: paramType,
59+
lifecycle, // <- lifecycle flag lands here
60+
});
61+
62+
REDUCERS.push(fn);
63+
}
64+
65+
const REDUCERS: Reducer<any, any>[] = [];
3066

3167
type u8 = number;
3268
type u16 = number;
@@ -521,154 +557,3 @@ function wrapSyscall<F extends (...args: any[]) => any>(
521557
},
522558
}[name];
523559
}
524-
525-
export class SpacetimeError {
526-
public readonly code: u16;
527-
public readonly message: string;
528-
constructor(code: u16) {
529-
const proto = Object.getPrototypeOf(this);
530-
let cls;
531-
if (error_protoypes.has(proto)) {
532-
cls = proto.constructor;
533-
if (code !== cls.CODE)
534-
throw new TypeError(`invalid error code for ${cls.name}`);
535-
} else if (proto === SpacetimeError.prototype) {
536-
cls = errno_to_class.get(code);
537-
if (!cls) throw new RangeError(`unknown error code ${code}`);
538-
} else {
539-
throw new TypeError('cannot subclass SpacetimeError');
540-
}
541-
Object.setPrototypeOf(this, cls.prototype);
542-
this.code = cls.CODE;
543-
this.message = cls.MESSAGE;
544-
}
545-
}
546-
547-
export class HostCallFailure extends SpacetimeError {
548-
static CODE = 1;
549-
static MESSAGE = 'ABI called by host returned an error';
550-
constructor() {
551-
super(HostCallFailure.CODE);
552-
}
553-
}
554-
export class NotInTransaction extends SpacetimeError {
555-
static CODE = 2;
556-
static MESSAGE = 'ABI call can only be made while in a transaction';
557-
constructor() {
558-
super(NotInTransaction.CODE);
559-
}
560-
}
561-
export class BsatnDecodeError extends SpacetimeError {
562-
static CODE = 3;
563-
static MESSAGE = "Couldn't decode the BSATN to the expected type";
564-
constructor() {
565-
super(BsatnDecodeError.CODE);
566-
}
567-
}
568-
export class NoSuchTable extends SpacetimeError {
569-
static CODE = 4;
570-
static MESSAGE = 'No such table';
571-
constructor() {
572-
super(NoSuchTable.CODE);
573-
}
574-
}
575-
export class NoSuchIndex extends SpacetimeError {
576-
static CODE = 5;
577-
static MESSAGE = 'No such index';
578-
constructor() {
579-
super(NoSuchIndex.CODE);
580-
}
581-
}
582-
export class NoSuchIter extends SpacetimeError {
583-
static CODE = 6;
584-
static MESSAGE = 'The provided row iterator is not valid';
585-
constructor() {
586-
super(NoSuchIter.CODE);
587-
}
588-
}
589-
export class NoSuchConsoleTimer extends SpacetimeError {
590-
static CODE = 7;
591-
static MESSAGE = 'The provided console timer does not exist';
592-
constructor() {
593-
super(NoSuchConsoleTimer.CODE);
594-
}
595-
}
596-
export class NoSuchBytes extends SpacetimeError {
597-
static CODE = 8;
598-
static MESSAGE = 'The provided bytes source or sink is not valid';
599-
constructor() {
600-
super(NoSuchBytes.CODE);
601-
}
602-
}
603-
export class NoSpace extends SpacetimeError {
604-
static CODE = 9;
605-
static MESSAGE = 'The provided sink has no more space left';
606-
constructor() {
607-
super(NoSpace.CODE);
608-
}
609-
}
610-
export class BufferTooSmall extends SpacetimeError {
611-
static CODE = 11;
612-
static MESSAGE = 'The provided buffer is not large enough to store the data';
613-
constructor() {
614-
super(BufferTooSmall.CODE);
615-
}
616-
}
617-
export class UniqueAlreadyExists extends SpacetimeError {
618-
static CODE = 12;
619-
static MESSAGE = 'Value with given unique identifier already exists';
620-
constructor() {
621-
super(UniqueAlreadyExists.CODE);
622-
}
623-
}
624-
export class ScheduleAtDelayTooLong extends SpacetimeError {
625-
static CODE = 13;
626-
static MESSAGE = 'Specified delay in scheduling row was too long';
627-
constructor() {
628-
super(ScheduleAtDelayTooLong.CODE);
629-
}
630-
}
631-
export class IndexNotUnique extends SpacetimeError {
632-
static CODE = 14;
633-
static MESSAGE = 'The index was not unique';
634-
constructor() {
635-
super(IndexNotUnique.CODE);
636-
}
637-
}
638-
export class NoSuchRow extends SpacetimeError {
639-
static CODE = 15;
640-
static MESSAGE = 'The row was not found, e.g., in an update call';
641-
constructor() {
642-
super(NoSuchRow.CODE);
643-
}
644-
}
645-
export class AutoIncOverflow extends SpacetimeError {
646-
static CODE = 16;
647-
static MESSAGE = 'The auto-increment sequence overflowed';
648-
constructor() {
649-
super(AutoIncOverflow.CODE);
650-
}
651-
}
652-
653-
const error_subclasses = [
654-
HostCallFailure,
655-
NotInTransaction,
656-
BsatnDecodeError,
657-
NoSuchTable,
658-
NoSuchIndex,
659-
NoSuchIter,
660-
NoSuchConsoleTimer,
661-
NoSuchBytes,
662-
NoSpace,
663-
BufferTooSmall,
664-
UniqueAlreadyExists,
665-
ScheduleAtDelayTooLong,
666-
IndexNotUnique,
667-
NoSuchRow,
668-
];
669-
670-
const error_protoypes = new Set(error_subclasses.map(cls => cls.prototype));
671-
672-
const errno_to_class = new Map(
673-
error_subclasses.map(cls => [cls.CODE as number, cls])
674-
);

0 commit comments

Comments
 (0)