Skip to content

Commit

Permalink
Refactor materializeWebsite in browser_based_querying (#300)
Browse files Browse the repository at this point in the history
* Refactor materializeWebsite in browser_based_querying

* Fix code from ob1kenobi's suggestion
  • Loading branch information
u9g committed Jun 3, 2023
1 parent 3b34d00 commit a085c0d
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions experiments/browser_based_querying/src/hackernews/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
initialize,
executeQuery,
} from '../../www2/trustfall_wasm';
import debug from "../utils/debug";
import debug from '../utils/debug';
import {
getAskStories,
getBestItems,
Expand Down Expand Up @@ -71,25 +71,25 @@ const _itemPattern = /^https:\/\/news\.ycombinator\.com\/item\?id=(\d+)$/;
const _userPattern = /^https:\/\/news\.ycombinator\.com\/user\?id=(.+)$/;

function materializeWebsite(fetchPort: MessagePort, url: string): Vertex | null {
const itemMatch = url.match(_itemPattern);
let ret: any
if (itemMatch) {
let matcher: RegExpMatchArray | null = null;
let ret: { url: string; __typename: string } | null = null;
if ((matcher = url.match(_itemPattern))) {
// This is an item.
ret = materializeItem(fetchPort, parseInt(itemMatch[1]));
} else {
const userMatch = url.match(_userPattern);
if (userMatch) {
// This is a user.
ret = materializeUser(fetchPort, userMatch[1]);
} else {
// This is some other type of webpage that we don't have a more specific type for.
ret = {
__typename: 'Website'
};
const item = materializeItem(fetchPort, parseInt(matcher[1]));
if (item != null) {
ret = { url, ...item };
}
} else if ((matcher = url.match(_userPattern))) {
// This is a user.
const user = materializeUser(fetchPort, matcher[1]);
if (user != null) {
ret = { url, ...user };
}
} else {
ret = { url, __typename: 'Website' };
}
ret.url = url
return ret

return ret;
}

function* linksInHnMarkup(fetchPort: MessagePort, hnText: string | null): IterableIterator<Vertex> {
Expand Down Expand Up @@ -230,7 +230,7 @@ export class MyAdapter implements Adapter<Vertex> {
break;
}
}
yield * resolvePossiblyLimitedIterator(fetcher(this.fetchPort), limit);
yield* resolvePossiblyLimitedIterator(fetcher(this.fetchPort), limit);
} else if (edge === 'User') {
const username = parameters['name'] as string;
const user = materializeUser(this.fetchPort, username);
Expand Down Expand Up @@ -406,11 +406,7 @@ export class MyAdapter implements Adapter<Vertex> {
edge_name: string,
parameters: JsEdgeParameters
): IterableIterator<ContextAndNeighborsIterator<Vertex>> {
if (
type_name === 'Story' ||
type_name === 'Job' ||
type_name === 'Comment'
) {
if (type_name === 'Story' || type_name === 'Job' || type_name === 'Comment') {
if (edge_name === 'link') {
if (type_name === 'Story') {
// Link submission stories have the submitted URL as a link.
Expand Down

0 comments on commit a085c0d

Please sign in to comment.