spacetimedb 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE.txt +2 -2
  2. package/dist/browser/svelte/index.mjs +4 -1
  3. package/dist/browser/svelte/index.mjs.map +1 -1
  4. package/dist/browser/vue/index.mjs +4 -1
  5. package/dist/browser/vue/index.mjs.map +1 -1
  6. package/dist/index.browser.mjs +11 -2
  7. package/dist/index.browser.mjs.map +1 -1
  8. package/dist/index.cjs +11 -2
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.mjs +11 -2
  11. package/dist/index.mjs.map +1 -1
  12. package/dist/min/index.browser.mjs +1 -1
  13. package/dist/min/index.browser.mjs.map +1 -1
  14. package/dist/min/sdk/index.browser.mjs +1 -1
  15. package/dist/min/sdk/index.browser.mjs.map +1 -1
  16. package/dist/sdk/db_connection_impl.d.ts.map +1 -1
  17. package/dist/sdk/event.d.ts +1 -1
  18. package/dist/sdk/event.d.ts.map +1 -1
  19. package/dist/sdk/index.browser.mjs +11 -2
  20. package/dist/sdk/index.browser.mjs.map +1 -1
  21. package/dist/sdk/index.cjs +11 -2
  22. package/dist/sdk/index.cjs.map +1 -1
  23. package/dist/sdk/index.mjs +11 -2
  24. package/dist/sdk/index.mjs.map +1 -1
  25. package/dist/sdk/websocket_test_adapter.d.ts.map +1 -1
  26. package/dist/server/index.mjs +20 -12
  27. package/dist/server/index.mjs.map +1 -1
  28. package/dist/server/procedures.d.ts.map +1 -1
  29. package/dist/server/reducers.d.ts +1 -1
  30. package/dist/server/reducers.d.ts.map +1 -1
  31. package/dist/server/rng.d.ts +0 -5
  32. package/dist/server/rng.d.ts.map +1 -1
  33. package/dist/svelte/index.cjs +4 -1
  34. package/dist/svelte/index.cjs.map +1 -1
  35. package/dist/svelte/index.mjs +4 -1
  36. package/dist/svelte/index.mjs.map +1 -1
  37. package/dist/svelte/useTable.d.ts +2 -3
  38. package/dist/svelte/useTable.d.ts.map +1 -1
  39. package/dist/vue/index.cjs +4 -1
  40. package/dist/vue/index.cjs.map +1 -1
  41. package/dist/vue/index.mjs +4 -1
  42. package/dist/vue/index.mjs.map +1 -1
  43. package/dist/vue/useTable.d.ts +2 -3
  44. package/dist/vue/useTable.d.ts.map +1 -1
  45. package/package.json +1 -1
  46. package/src/sdk/db_connection_impl.ts +16 -2
  47. package/src/sdk/event.ts +1 -1
  48. package/src/sdk/websocket_test_adapter.ts +1 -0
  49. package/src/server/procedures.ts +14 -4
  50. package/src/server/reducers.ts +19 -10
  51. package/src/server/rng.ts +0 -6
  52. package/src/svelte/useTable.ts +4 -2
  53. package/src/vue/useTable.ts +4 -2
@@ -1,6 +1,7 @@
1
1
  import { type DeepReadonly, type Ref } from 'vue';
2
2
  import type { RowType, UntypedTableDef } from '../lib/table';
3
3
  import type { Prettify } from '../lib/type_util';
4
+ import { type Query } from '../lib/query';
4
5
  export interface UseTableCallbacks<RowType> {
5
6
  onInsert?: (row: RowType) => void;
6
7
  onDelete?: (row: RowType) => void;
@@ -17,9 +18,7 @@ export interface UseTableCallbacks<RowType> {
17
18
  * @param callbacks - Optional callbacks for row insert, delete, and update events.
18
19
  * @returns A tuple of [rows, isReady].
19
20
  */
20
- export declare function useTable<TableDef extends UntypedTableDef>(query: {
21
- toSql(): string;
22
- } & Record<string, any>, callbacks?: UseTableCallbacks<Prettify<RowType<TableDef>>>): [
21
+ export declare function useTable<TableDef extends UntypedTableDef>(query: Query<TableDef>, callbacks?: UseTableCallbacks<Prettify<RowType<TableDef>>>): [
23
22
  DeepReadonly<Ref<readonly Prettify<RowType<TableDef>>[]>>,
24
23
  DeepReadonly<Ref<boolean>>
25
24
  ];
@@ -1 +1 @@
1
- {"version":3,"file":"useTable.d.ts","sourceRoot":"","sources":["../../src/vue/useTable.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,YAAY,EACjB,KAAK,GAAG,EACT,MAAM,KAAK,CAAC;AAKb,OAAO,KA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AC;IAClC,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,CAAC;IAClC,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;CACvD;AAkBD;;;;;;;;;;GAUG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,SAAS,eAAe,EACvD,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,EACtB,SAAS,CAAC,EAAE,iBAAiB,CAAC,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,GACzD;IACD,YAAY,CAAC,GAAG,CAAC,SAAS,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC;IACzD,YAAY,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;CAC3B,CAqKA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spacetimedb",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "API and ABI bindings for the SpacetimeDB TypeScript module library",
5
5
  "homepage": "https://github.com/clockworklabs/SpacetimeDB#readme",
6
6
  "bugs": {
@@ -712,6 +712,7 @@ export class DbConnectionImpl<RemoteModule extends UntypedRemoteModule>
712
712
  }
713
713
  case 'SubscriptionError': {
714
714
  const querySetId = serverMessage.value.querySetId.id;
715
+ const requestId = serverMessage.value.requestId;
715
716
  const error = Error(serverMessage.value.error);
716
717
  const event: Event<never> = {
717
718
  id: this.#nextEventId(),
@@ -723,6 +724,19 @@ export class DbConnectionImpl<RemoteModule extends UntypedRemoteModule>
723
724
  ...eventContext,
724
725
  event: error,
725
726
  };
727
+
728
+ // If the requestId isn't set, that means we already applied the subscription.
729
+ // Since we don't know how to remove the relevant rows from our table cache, we need
730
+ // to kill the connection. Once we have per-query storage, this won't be fatal.
731
+ if (requestId == null) {
732
+ stdbLogger(
733
+ 'error',
734
+ `Disconnecting due to error for a previously applied subscription: ${serverMessage.value.error}`
735
+ );
736
+ this.disconnect();
737
+ break;
738
+ }
739
+
726
740
  const subscription =
727
741
  this.#subscriptionManager.subscriptions.get(querySetId);
728
742
  if (subscription) {
@@ -740,7 +754,7 @@ export class DbConnectionImpl<RemoteModule extends UntypedRemoteModule>
740
754
  case 'TransactionUpdate': {
741
755
  const event: Event<never> = {
742
756
  id: this.#nextEventId(),
743
- tag: 'UnknownTransaction',
757
+ tag: 'Transaction',
744
758
  };
745
759
  const eventContext = this.#makeEventContext(event);
746
760
  const callbacks = this.#applyTransactionUpdates(
@@ -777,7 +791,7 @@ export class DbConnectionImpl<RemoteModule extends UntypedRemoteModule>
777
791
  }
778
792
  : {
779
793
  id: eventId,
780
- tag: 'UnknownTransaction',
794
+ tag: 'Transaction',
781
795
  };
782
796
  const eventContext = this.#makeEventContext(event as any);
783
797
 
package/src/sdk/event.ts CHANGED
@@ -14,5 +14,5 @@ export type Event<Reducer extends ReducerEventInfo> = WithId &
14
14
  | { tag: 'SubscribeApplied' }
15
15
  | { tag: 'UnsubscribeApplied' }
16
16
  | { tag: 'Error'; value: Error }
17
- | { tag: 'UnknownTransaction' }
17
+ | { tag: 'Transaction' }
18
18
  );
@@ -27,6 +27,7 @@ class WebsocketTestAdapter {
27
27
 
28
28
  close(): void {
29
29
  this.closed = true;
30
+ this.onclose?.({ code: 1000, reason: 'normal closure', wasClean: true });
30
31
  }
31
32
 
32
33
  acceptConnection(): void {
@@ -101,12 +101,13 @@ function registerProcedure<
101
101
  Ret extends TypeBuilder<any, any>,
102
102
  >(
103
103
  ctx: SchemaInner,
104
- name: string,
104
+ exportName: string,
105
105
  params: Params,
106
106
  ret: Ret,
107
- fn: ProcedureFn<S, Params, Ret>
107
+ fn: ProcedureFn<S, Params, Ret>,
108
+ opts?: ProcedureOpts
108
109
  ) {
109
- ctx.defineFunction(name);
110
+ ctx.defineFunction(exportName);
110
111
  const paramsType: ProductType = {
111
112
  elements: Object.entries(params).map(([n, c]) => ({
112
113
  name: n,
@@ -118,12 +119,21 @@ function registerProcedure<
118
119
  const returnType = ctx.registerTypesRecursively(ret).algebraicType;
119
120
 
120
121
  ctx.moduleDef.procedures.push({
121
- sourceName: name,
122
+ sourceName: exportName,
122
123
  params: paramsType,
123
124
  returnType,
124
125
  visibility: FunctionVisibility.ClientCallable,
125
126
  });
126
127
 
128
+ if (opts?.name != null) {
129
+ ctx.moduleDef.explicitNames.entries.push({
130
+ tag: 'Function',
131
+ value: {
132
+ sourceName: exportName,
133
+ canonicalName: opts.name,
134
+ },
135
+ });
136
+ }
127
137
  const { typespace } = ctx;
128
138
 
129
139
  ctx.procedures.push({
@@ -31,15 +31,13 @@ export function makeReducerExport<
31
31
  fn: Reducer<any, any>,
32
32
  lifecycle?: Lifecycle
33
33
  ): ReducerExport<S, Params> {
34
- const name = opts?.name;
35
-
36
34
  const reducerExport: ReducerExport<S, Params> = (...args) => fn(...args);
37
35
  reducerExport[exportContext] = ctx;
38
36
  reducerExport[registerExport] = (ctx, exportName) => {
39
- registerReducer(ctx, name ?? exportName, params, fn, lifecycle);
37
+ registerReducer(ctx, exportName, params, fn, opts, lifecycle);
40
38
  ctx.functionExports.set(
41
39
  reducerExport as ReducerExport<any, any>,
42
- name ?? exportName
40
+ exportName
43
41
  );
44
42
  };
45
43
 
@@ -56,19 +54,20 @@ export function makeReducerExport<
56
54
  */
57
55
  export function registerReducer(
58
56
  ctx: SchemaInner,
59
- name: string,
57
+ exportName: string,
60
58
  params: RowObj | RowBuilder<RowObj>,
61
59
  fn: Reducer<any, any>,
60
+ opts?: ReducerOpts,
62
61
  lifecycle?: Lifecycle
63
62
  ): void {
64
- ctx.defineFunction(name);
63
+ ctx.defineFunction(exportName);
65
64
 
66
65
  if (!(params instanceof RowBuilder)) {
67
66
  params = new RowBuilder(params);
68
67
  }
69
68
 
70
69
  if (params.typeName === undefined) {
71
- params.typeName = toPascalCase(name);
70
+ params.typeName = toPascalCase(exportName);
72
71
  }
73
72
 
74
73
  const ref = ctx.registerTypesRecursively(params);
@@ -76,7 +75,7 @@ export function registerReducer(
76
75
  const isLifecycle = lifecycle != null;
77
76
 
78
77
  ctx.moduleDef.reducers.push({
79
- sourceName: name,
78
+ sourceName: exportName,
80
79
  params: paramsType,
81
80
  //ModuleDef validation code is responsible to mark private reducers
82
81
  visibility: FunctionVisibility.ClientCallable,
@@ -85,17 +84,27 @@ export function registerReducer(
85
84
  errReturnType: AlgebraicType.String,
86
85
  });
87
86
 
87
+ if (opts?.name != null) {
88
+ ctx.moduleDef.explicitNames.entries.push({
89
+ tag: 'Function',
90
+ value: {
91
+ sourceName: exportName,
92
+ canonicalName: opts.name,
93
+ },
94
+ });
95
+ }
96
+
88
97
  if (isLifecycle) {
89
98
  ctx.moduleDef.lifeCycleReducers.push({
90
99
  lifecycleSpec: lifecycle,
91
- functionName: name,
100
+ functionName: exportName,
92
101
  });
93
102
  }
94
103
 
95
104
  // If the function isn't named (e.g. `function foobar() {}`), give it the same
96
105
  // name as the reducer so that it's clear what it is in in backtraces.
97
106
  if (!fn.name) {
98
- Object.defineProperty(fn, 'name', { value: name, writable: false });
107
+ Object.defineProperty(fn, 'name', { value: exportName, writable: false });
99
108
  }
100
109
 
101
110
  ctx.reducers.push(fn);
package/src/server/rng.ts CHANGED
@@ -4,12 +4,6 @@ import { unsafeUniformIntDistribution } from 'pure-rand/distribution/UnsafeUnifo
4
4
  import { xoroshiro128plus } from 'pure-rand/generator/XoroShiro';
5
5
  import type { Timestamp } from '../lib/timestamp';
6
6
 
7
- declare global {
8
- interface Math {
9
- random(): never;
10
- }
11
- }
12
-
13
7
  type IntArray =
14
8
  | Int8Array
15
9
  | Uint8Array
@@ -10,6 +10,8 @@ import {
10
10
  evaluateBooleanExpr,
11
11
  getQueryAccessorName,
12
12
  getQueryWhereClause,
13
+ type Query,
14
+ toSql,
13
15
  } from '../lib/query';
14
16
 
15
17
  export interface UseTableCallbacks<RowType> {
@@ -46,13 +48,13 @@ function classifyMembership(
46
48
  * @returns A tuple of [rows, isReady].
47
49
  */
48
50
  export function useTable<TableDef extends UntypedTableDef>(
49
- query: { toSql(): string } & Record<string, any>,
51
+ query: Query<TableDef>,
50
52
  callbacks?: UseTableCallbacks<Prettify<RowType<TableDef>>>
51
53
  ): [Readable<readonly Prettify<RowType<TableDef>>[]>, Readable<boolean>] {
52
54
  type Row = RowType<TableDef>;
53
55
  const accessorName = getQueryAccessorName(query);
54
56
  const whereExpr = getQueryWhereClause(query);
55
- const querySql = query.toSql();
57
+ const querySql = toSql(query);
56
58
 
57
59
  let connectionStore;
58
60
  try {
@@ -14,7 +14,9 @@ import type { UntypedRemoteModule } from '../sdk/spacetime_module';
14
14
  import type { RowType, UntypedTableDef } from '../lib/table';
15
15
  import type { Prettify } from '../lib/type_util';
16
16
  import {
17
+ type Query,
17
18
  type BooleanExpr,
19
+ toSql,
18
20
  evaluateBooleanExpr,
19
21
  getQueryAccessorName,
20
22
  getQueryWhereClause,
@@ -54,7 +56,7 @@ function classifyMembership(
54
56
  * @returns A tuple of [rows, isReady].
55
57
  */
56
58
  export function useTable<TableDef extends UntypedTableDef>(
57
- query: { toSql(): string } & Record<string, any>,
59
+ query: Query<TableDef>,
58
60
  callbacks?: UseTableCallbacks<Prettify<RowType<TableDef>>>
59
61
  ): [
60
62
  DeepReadonly<Ref<readonly Prettify<RowType<TableDef>>[]>>,
@@ -63,7 +65,7 @@ export function useTable<TableDef extends UntypedTableDef>(
63
65
  type Row = RowType<TableDef>;
64
66
  const accessorName = getQueryAccessorName(query);
65
67
  const whereExpr = getQueryWhereClause(query);
66
- const querySql = query.toSql();
68
+ const querySql = toSql(query);
67
69
 
68
70
  let conn;
69
71
  try {