Skip to content

Commit

Permalink
fix: Handle skipPolling option properly
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Feb 22, 2024
1 parent 1744996 commit 4dbccbc
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/adapters/action/dispatcher-http-hook-mastodon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,26 @@ describe("DispatcherHttp", () => {

await expect(promise).rejects.toBeInstanceOf(Error);
});

it("skips media polling if `skipPolling` is passed", async () => {
const http = new HttpMockImpl();
const dispatcher = new HttpActionDispatcher(
http,
new HttpActionDispatcherHookMastodon(http),
);

httpPost.mockResolvedValueOnce({ id: "1" });

const media = await dispatcher.dispatch({
type: "create",
path: "/api/v2/media",
data: {
skipPolling: true,
},
meta: {},
});

expect(media).toHaveProperty("id", "1");
expect(httpGet).toHaveBeenCalledTimes(0);
});
});
5 changes: 4 additions & 1 deletion src/adapters/action/dispatcher-http-hook-mastodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type HttpMetaParams,
} from "../../interfaces";
import { type mastodon } from "../../mastodon";
import { sleep } from "../../utils";
import { isRecord, sleep } from "../../utils";
import { MastoHttpError, MastoTimeoutError } from "../errors";
import { type HttpAction, type HttpActionType } from "./dispatcher-http";

Expand Down Expand Up @@ -105,6 +105,9 @@ export class HttpActionDispatcherHookMastodon
afterDispatch(action: AnyAction, result: unknown): unknown {
if (action.type === "create" && action.path === "/api/v2/media") {
const media = result as mastodon.v1.MediaAttachment;
if (isRecord(action.data) && action.data?.skipPolling === true) {
return media;
}
return waitForMediaAttachment(media.id, this.mediaTimeout, this.http);
}

Expand Down
2 changes: 1 addition & 1 deletion src/adapters/serializers/flatten-record.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isRecord } from "./is-record";
import { isRecord } from "../../utils";

export const flattenRecord = (
object: unknown,
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/serializers/rails-query-string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isRecord } from "./is-record";
import { isRecord } from "../../utils";

const flatten = (object: unknown, parent = ""): [string, unknown][] => {
if (Array.isArray(object)) {
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/serializers/transform-keys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isRecord } from "./is-record";
import { isRecord } from "../../utils";

const _transformKeys = <T>(
data: unknown,
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./sleep";
export * from "./is-record";
export * from "./noop";
export * from "./exponential-backoff";
export * from "./promise-with-resolvers";
Expand Down
File renamed without changes.

0 comments on commit 4dbccbc

Please sign in to comment.