|
| 1 | +import type { Reducer } from 'react'; |
1 | 2 | import { AlgebraicType, ProductType } from '../lib/algebraic_type'; |
2 | 3 | import RawModuleDef from '../lib/autogen/raw_module_def_type'; |
3 | 4 | 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'; |
4 | 6 | import type RawTableDefV9 from '../lib/autogen/raw_table_def_v_9_type'; |
5 | 7 | import type Typespace from '../lib/autogen/typespace_type'; |
6 | 8 | import { ConnectionId } from '../lib/connection_id'; |
7 | 9 | import { Identity } from '../lib/identity'; |
8 | 10 | import { Timestamp } from '../lib/timestamp'; |
9 | 11 | import { BinaryReader, BinaryWriter } from '../sdk'; |
| 12 | +import { AutoIncOverflow, SpacetimeError, UniqueAlreadyExists } from './errors'; |
10 | 13 | import { |
11 | | - MODULE_DEF, |
12 | | - REDUCERS, |
| 14 | + Range, |
| 15 | + type Bound, |
13 | 16 | type DbView, |
| 17 | + type Index, |
| 18 | + type IndexVal, |
| 19 | + type RangedIndex, |
14 | 20 | type ReducerCtx, |
15 | | - type Table, |
16 | | - type UntypedTableDef, |
| 21 | + type RowObj, |
17 | 22 | type RowType, |
18 | | - type TryInsertError, |
19 | | - type Result, |
| 23 | + type Table, |
20 | 24 | type TableMethods, |
21 | | - type Index, |
22 | 25 | type UniqueIndex, |
23 | | - type RangedIndex, |
24 | | - type IndexVal, |
25 | | - type IndexScanRangeBounds, |
26 | | - Range, |
27 | | - type Bound, |
28 | 26 | } 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>[] = []; |
30 | 66 |
|
31 | 67 | type u8 = number; |
32 | 68 | type u16 = number; |
@@ -521,154 +557,3 @@ function wrapSyscall<F extends (...args: any[]) => any>( |
521 | 557 | }, |
522 | 558 | }[name]; |
523 | 559 | } |
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