Skip to content

Commit

Permalink
Merge pull request #161 from evert/better-prefer-push
Browse files Browse the repository at this point in the history
Prefer-push is opt-in and more reliable
  • Loading branch information
evert authored Nov 4, 2019
2 parents 57680e3 + 69ef93b commit bd1a167
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 68 deletions.
13 changes: 13 additions & 0 deletions src/follower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import { LinkVariables } from './types';
abstract class Follower<T> implements PromiseLike<T> {

protected prefetchEnabled: boolean;
protected preferPushEnabled: boolean;

preFetch(): this {
this.prefetchEnabled = true;
return this;
}

preferPush(): this {
this.preferPushEnabled = true;
return this;
}

abstract then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
abstract catch<TResult1 = T, TResult2 = never>(onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;

Expand Down Expand Up @@ -103,6 +109,9 @@ export class FollowerOne<T = any> extends Follower<Resource<T>> {
private async fetchLinkedResource(): Promise<Resource<T>> {

const resource = await this.resource;
if (this.preferPushEnabled) {
resource.addNextRefreshHeader('Prefer-Push', this.rel);
}
const link = await resource.link(this.rel);
let href;

Expand Down Expand Up @@ -172,6 +181,10 @@ export class FollowerMany<T = any> extends Follower<Array<Resource<T>>> {
private async fetchLinkedResources(): Promise<Array<Resource<T>>> {

const resource = await this.resource;
if (this.preferPushEnabled) {
resource.addNextRefreshHeader('Prefer-Push', this.rel);
}

const links = await resource.links(this.rel);
let href;

Expand Down
125 changes: 57 additions & 68 deletions src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,18 @@ export default class Resource<TResource = any, TPatch = Partial<TResource>> {
* be used to set this value.
*
* It's also possible for resources to get a mimetype through a link.
*
* If the mimetype was "null" when doing the request, the chosen mimetype
* will come from the first item in Client.resourceTypes
*/
contentType: string | null;

private inFlightRefresh: Promise<any> | null = null;

/**
* A list of rels that should be added to a Prefer-Push header.
*/
private preferPushRels: Set<string>;
private inFlightRefresh: Promise<TResource> | null = null;

constructor(client: Ketting, uri: string, contentType: string | null = null) {

this.client = client;
this.uri = uri;
this.repr = null;
this.contentType = contentType;
this.preferPushRels = new Set();
this.nextRefreshHeaders = {};

}

Expand Down Expand Up @@ -171,70 +163,58 @@ export default class Resource<TResource = any, TPatch = Partial<TResource>> {
* This function will return the a parsed JSON object, like the get
* function does.
*/
async refresh(): Promise<TResource> {
refresh(): Promise<TResource> {

let response: Response;
let body: string;
if (this.inFlightRefresh) {
return this.inFlightRefresh;
}

// If 2 systems request a 'refresh' at the exact same time, this mechanism
// will coalesc them into one.
if (!this.inFlightRefresh) {
const refreshFunc = async (): Promise<TResource> => {

const headers: { [name: string]: string } = {
Accept: this.contentType ? this.contentType : this.client.representorHelper.getAcceptHeader()
Accept: this.contentType ? this.contentType : this.client.representorHelper.getAcceptHeader(),
...this.nextRefreshHeaders,
};

if (this.preferPushRels.size > 0) {
headers['Prefer-Push'] = Array.from(this.preferPushRels).join(' ');
headers.Prefer = 'transclude="' + Array.from(this.preferPushRels).join(';') + '"';
}

this.inFlightRefresh = this.fetchAndThrow({
const response = await this.fetchAndThrow({
method: 'GET' ,
headers
}).then( result1 => {
response = result1;
return response.text();
})
.then( result2 => {
body = result2;
return [response, body];
});

try {
await this.inFlightRefresh;
} finally {
this.inFlightRefresh = null;
}
this.nextRefreshHeaders = {};
this.inFlightRefresh = null;

} else {
// Something else asked for refresh, so we piggypack on it.
[response, body] = await this.inFlightRefresh;
const body = await response.text();

}
this.repr = this.client.representorHelper.createFromResponse(
this.uri,
response,
body,
) as any as Representator<TResource>;

this.repr = this.client.representorHelper.createFromResponse(
this.uri,
response!,
body!,
) as any as Representator<TResource>;
if (!this.contentType) {
this.contentType = this.repr.contentType;
}

if (!this.contentType) {
this.contentType = this.repr.contentType;
}
for (const [subUri, subBody] of Object.entries(this.repr.getEmbedded())) {
const subResource = this.go(subUri);
subResource.repr = this.client.representorHelper.create(
subUri,
this.repr.contentType,
null,
new Map(),
);
subResource.repr.setBody(subBody);
}

for (const [subUri, subBody] of Object.entries(this.repr.getEmbedded())) {
const subResource = this.go(subUri);
subResource.repr = this.client.representorHelper.create(
subUri,
this.repr.contentType,
null,
new Map(),
);
subResource.repr.setBody(subBody);
}
return this.repr.getBody();

};

return this.repr.getBody();
const refreshResult = refreshFunc();
this.inFlightRefresh = refreshResult;

return refreshResult;

}

Expand All @@ -247,11 +227,6 @@ export default class Resource<TResource = any, TPatch = Partial<TResource>> {
async links(rel?: string): Promise<Link[]> {

const r = await this.representation();

// After we got a representation, it no longer makes sense to remember
// the rels we want to add to Prefer-Push.
this.preferPushRels = new Set();

return r.getLinks(rel);

}
Expand All @@ -269,11 +244,6 @@ export default class Resource<TResource = any, TPatch = Partial<TResource>> {
async link(rel: string): Promise<Link> {

const r = await this.representation();

// After we got a representation, it no longer makes sense to remember
// the rels we want to add to Prefer-Push.
this.preferPushRels = new Set();

return r.getLink(rel);

}
Expand Down Expand Up @@ -437,4 +407,23 @@ export default class Resource<TResource = any, TPatch = Partial<TResource>> {

}


/**
* A set of HTTP headers that will be sent along with the next call to Refresh()
*/
private nextRefreshHeaders: { [name: string]: string };

/**
* When a HTTP header gets added here, it will automatically get sent along
* to the next call to refresh().
*
* This means that the next time a GET request is done, these headers will be
* added. This list gets cleared after the GET request.
*/
addNextRefreshHeader(name: string, value: string): void {

this.nextRefreshHeaders[name] = value;

}

}

0 comments on commit bd1a167

Please sign in to comment.