Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mem to fix its types #2545

Merged
merged 3 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"intervalometer": "^1.0.5",
"linkify-issues": "2.0.0-nolookbehind",
"linkify-urls": "3.1.0-nolookbehind",
"mem": "^5.1.1",
"mem": "^6.0.1",
"onetime": "^5.0.0",
"p-filter": "^2.1.0",
"select-dom": "^5.1.0",
Expand Down
3 changes: 0 additions & 3 deletions source/features/hidden-review-comments-indicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ const addIndicator = mem((commentThread: HTMLElement): void => {
</td>
</tr>
);
}, {
// TODO [mem@>=6]: Drop argument
cacheKey: (element: HTMLElement) => element
});

// Add indicator when the `show-inline-notes` class is removed (i.e. the comments are hidden)
Expand Down
6 changes: 0 additions & 6 deletions source/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ interface Window {
content: GlobalFetch;
}

// Drop after https://github.com/sindresorhus/p-memoize/issues/9
declare module 'mem' {
function mem<T = VoidFunction>(fn: T, options?: AnyObject): T;
export = mem;
}

declare module 'size-plugin';

// Custom UI events specific to RGH
Expand Down
4 changes: 4 additions & 0 deletions source/libs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export const v3 = mem(async (
}

throw await getError(apiResponse);
}, {
cacheKey: JSON.stringify
});

export const v4 = mem(async (
Expand Down Expand Up @@ -155,6 +157,8 @@ export const v4 = mem(async (
}

throw await getError(apiResponse as JsonObject);
}, {
cacheKey: JSON.stringify
});

async function getError(apiResponse: JsonObject): Promise<RefinedGitHubAPIError> {
Expand Down
14 changes: 9 additions & 5 deletions source/libs/fetch-dom.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import domify from 'doma';
import mem from 'mem';
import domify from 'doma';

async function fetchDom(url: string): Promise<DocumentFragment>;
async function fetchDom<TElement extends Element>(url: string, selector: string): Promise<TElement | null>;
async function fetchDom<TElement extends Element>(url: string, selector: string): Promise<TElement | undefined>;
async function fetchDom(url: string, selector?: string): Promise<Node | undefined> {
const urlObject = new URL(url, location.origin);
const response = await fetch(String(urlObject));
const absoluteURL = new URL(url, location.origin).toString(); // Firefox `fetch`es from the content script, so relative URLs fail
const response = await fetch(absoluteURL);
const dom = domify(await response.text());
return selector ? dom.querySelector(selector) || undefined : dom;
if (selector) {
return dom.querySelector(selector) || undefined;
}

return dom;
}

export default mem(fetchDom);