Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
implement initial unsubscribe from ChainNotify
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeseese committed Jan 12, 2021
1 parent a8e1adc commit 9f88b4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/chains/filecoin/filecoin/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "./things/retrieval-offer";
import Emittery from "emittery";
import { HeadChange, HeadChangeType } from "./things/head-change";
import { SubscriptionMethod, SubscriptionId } from "./types/subscriptions";

export default class FilecoinApi implements types.Api {
readonly [index: string]: (...args: any) => Promise<any>;
Expand Down Expand Up @@ -44,35 +45,50 @@ export default class FilecoinApi implements types.Api {
const subscription = this.#getId();
const promiEvent = PromiEvent.resolve(subscription);

// There currently isn't an unsubscribe method,
// but it would go here
this.#subscriptions.set(subscription.toString(), () => {});

const currentHead = new HeadChange({
type: HeadChangeType.HCCurrent,
val: this.#blockchain.latestTipset()
});

this.#blockchain.on("tipset", (tipset: Tipset) => {
const unsubscribe = this.#blockchain.on("tipset", (tipset: Tipset) => {
const newHead = new HeadChange({
type: HeadChangeType.HCApply,
val: tipset
});

promiEvent.emit("message", {
type: "xrpc.ch.val",
type: SubscriptionMethod.ChannelUpdated,
data: [subscription.toString(), [newHead.serialize()]]
});
});

// There currently isn't an unsubscribe method,
// but it would go here
this.#subscriptions.set(subscription.toString(), unsubscribe);

promiEvent.emit("message", {
type: "xrpc.ch.val",
type: SubscriptionMethod.ChannelUpdated,
data: [subscription.toString(), [currentHead.serialize()]]
});

return promiEvent;
}

[SubscriptionMethod.ChannelClosed](
subscriptionId: SubscriptionId
): Promise<boolean> {
const subscriptions = this.#subscriptions;
const unsubscribe = subscriptions.get(subscriptionId);

if (unsubscribe) {
subscriptions.delete(subscriptionId);
unsubscribe();
return Promise.resolve(true);
} else {
return Promise.resolve(false);
}
}

async "Filecoin.StateListMiners"(): Promise<Array<SerializedMiner>> {
return [this.#blockchain.miner.serialize()];
}
Expand Down
7 changes: 7 additions & 0 deletions src/chains/filecoin/filecoin/src/types/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum SubscriptionMethod {
ChannelUpdated = "xrpc.ch.val",
ChannelClosed = "xrpc.ch.close",
SubscriptionCanceled = "xrpc.cancel"
}

export type SubscriptionId = string;

0 comments on commit 9f88b4e

Please sign in to comment.