Skip to content

Commit 1d018f1

Browse files
authored
fix: use the correct previous state when receiving a space (#143)
1 parent 2559ee2 commit 1d018f1

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

packages/hypergraph-react/src/HypergraphAppContext.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,13 @@ export function HypergraphAppProvider({
354354
let state: SpaceEvents.SpaceState | undefined = undefined;
355355

356356
for (const event of response.events) {
357-
const applyEventResult = await Effect.runPromiseExit(SpaceEvents.applyEvent({ state: undefined, event }));
357+
// Not sure why but type inference doesn't work here
358+
const applyEventResult: Exit.Exit<SpaceEvents.SpaceState, SpaceEvents.ApplyError> =
359+
await Effect.runPromiseExit(SpaceEvents.applyEvent({ state, event }));
358360
if (Exit.isSuccess(applyEventResult)) {
359361
state = applyEventResult.value;
362+
} else {
363+
console.log('Failed to apply event', applyEventResult);
360364
}
361365
}
362366

packages/hypergraph/src/space-events/apply-event.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { secp256k1 } from '@noble/curves/secp256k1';
22
import { sha256 } from '@noble/hashes/sha256';
33
import { Effect, Schema } from 'effect';
4-
import type { ParseError } from 'effect/ParseResult';
54
import { canonicalize, stringToUint8Array } from '../utils/index.js';
65
import { hashEvent } from './hash-event.js';
76
import {
7+
type ApplyError,
88
InvalidEventError,
99
SpaceEvent,
1010
type SpaceInvitation,
@@ -20,10 +20,7 @@ type Params = {
2020

2121
const decodeSpaceEvent = Schema.decodeUnknownEither(SpaceEvent);
2222

23-
export const applyEvent = ({
24-
state,
25-
event: rawEvent,
26-
}: Params): Effect.Effect<SpaceState, ParseError | VerifySignatureError | InvalidEventError> => {
23+
export const applyEvent = ({ state, event: rawEvent }: Params): Effect.Effect<SpaceState, ApplyError> => {
2724
const decodedEvent = decodeSpaceEvent(rawEvent);
2825
if (decodedEvent._tag === 'Left') {
2926
return decodedEvent.left;

packages/hypergraph/src/space-events/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ParseError } from 'effect/ParseResult';
12
import * as Schema from 'effect/Schema';
23
import { SignatureWithRecovery } from '../types.js';
34

@@ -101,3 +102,5 @@ export class VerifySignatureError {
101102
export class InvalidEventError {
102103
readonly _tag = 'InvalidEventError';
103104
}
105+
106+
export type ApplyError = ParseError | VerifySignatureError | InvalidEventError;

0 commit comments

Comments
 (0)