Skip to content

Commit

Permalink
Rework Caching and Pagination Logic
Browse files Browse the repository at this point in the history
Add a new RawData event which returns the un-parsed raw response in Queryable
Update Caching Behavior to hook into rawData event to set cached data
Update Caching Behavior to override send with cached response
  • Loading branch information
bcameron1231 committed Nov 28, 2023
1 parent 1b3b6fa commit d96c7be
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
20 changes: 10 additions & 10 deletions docs/queryable/behaviors.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,20 @@ export function Caching(props?: ICachingProps): TimelinePipe<Queryable> {

const cached = getCachedValue();

// we need to ensure that result stays "undefined" unless we mean to set null as the result
// we need to ensure that result stays "undefined" unless we mean to set null as the result
if (cached === null) {

// if we don't have a cached result we need to get it after the request is sent and parsed
this.on.post(async function (url: URL, result: any) {

setCachedValue(result);

return [url, result];
});
// if we don't have a cached result we need to get it after the request is sent. Get the raw value (un-parsed) to store into cache
this.on.rawData(noInherit(async function (response) {
setCachedValue(response);
}));

} else {

result = cached;
// if we find it in cache, override send request, and continue flow through timeline and parsers.
this.on.auth.clear();
this.on.send.replace(async function (this: Queryable) {
return new Response(cached, {});
});
}
}

Expand Down
16 changes: 8 additions & 8 deletions packages/queryable/behaviors/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ export function Caching(props?: ICachingProps): TimelinePipe<Queryable> {
// we need to ensure that result stays "undefined" unless we mean to set null as the result
if (cached === null) {

// if we don't have a cached result we need to get it after the request is sent and parsed
this.on.post(noInherit(async function (url: URL, result: any) {

setCachedValue(result);

return [url, result];
// if we don't have a cached result we need to get it after the request is sent. Get the raw value (un-parsed) to store into cache
this.on.rawData(noInherit(async function (response) {
setCachedValue(response);
}));

} else {

result = cached;
// if we find it in cache, override send request, and continue flow through timeline and parsers.
this.on.auth.clear();
this.on.send.replace(async function (this: Queryable) {
return new Response(cached, {});
});
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/queryable/queryable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const DefaultMoments = {
parse: asyncReduce<QueryableParseObserver>(),
post: asyncReduce<QueryablePostObserver>(),
data: broadcast<QueryableDataObserver>(),
rawData: broadcast<QueryableDataObserver>(),
} as const;

export type QueryableInit = Queryable<any> | string | [Queryable<any>, string];
Expand Down Expand Up @@ -179,6 +180,10 @@ export class Queryable<R> extends Timeline<typeof DefaultMoments> implements IQu
let response = await this.emit.send(requestUrl, init);
log("Emitted send");

log("Emitting rawData");
this.emit.rawData(await response.clone().text());
log("Emitted rawData");

log("Emitting parse");
[requestUrl, response, result] = await this.emit.parse(requestUrl, response, result);
log("Emitted parse");
Expand Down
2 changes: 1 addition & 1 deletion test/graph/onedrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import findupSync from "findup-sync";
import "@pnp/graph/users";
import "@pnp/graph/onedrive";
import { getRandomString, stringIsNullOrEmpty } from "@pnp/core";
import { IAnalyticsOptions, IItemOptions } from "@pnp/graph/onedrive/types";
import { IItemOptions } from "@pnp/graph/onedrive/types";

// give ourselves a single reference to the projectRoot
const projectRoot = path.resolve(path.dirname(findupSync("package.json")));
Expand Down

0 comments on commit d96c7be

Please sign in to comment.