Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit 1f4c8ee

Browse files
Adding support for caching and batching to the library
1 parent f7f9440 commit 1f4c8ee

10 files changed

Lines changed: 563 additions & 57 deletions

File tree

server-root/scratchpad.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ require(["pnp"], function (pnp) {
2626
});
2727
}
2828

29-
pnp.setup({
30-
headers: {
31-
"Accept": "application/json; odata=verbose"
32-
}
33-
});
29+
// pnp.setup({
30+
// headers: {
31+
// "Accept": "application/json; odata=verbose"
32+
// }
33+
// });
3434

35-
pnp.sp.web.lists.getByTitle("Config3").items.add({ Title: "Another Item" }).then(function (result) {
36-
show(result.data);
37-
});
35+
// pnp.thing(show);
36+
37+
// pnp.sp.web.lists.getByTitle("Config3").items.add({ Title: "Another Item" }).then(function (result) {
38+
// show(result.data);
39+
// });
3840

3941
// pnp.sp.web.lists.getByTitle("Config3").items.get().then(show);
4042

src/configuration/pnplibconfig.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,77 @@
11
import { TypedHash } from "../collections/collections";
22

33
export interface LibraryConfiguration {
4+
5+
/**
6+
* Any headers to apply to all requests
7+
*/
48
headers?: TypedHash<string>;
9+
10+
/**
11+
* Allows caching to be global disabled, default: false
12+
*/
13+
globalCacheDisable?: boolean;
14+
15+
/**
16+
* Defines the default store used by the usingCaching method, default: session
17+
*/
18+
defaultCachingStore?: "session" | "local";
19+
20+
/**
21+
* Defines the default timeout in seconds used by the usingCaching method, default 30
22+
*/
23+
defaultCachingTimeoutSeconds?: number;
524
}
625

726
export class RuntimeConfigImpl {
827

928
constructor() {
29+
// these are our default values for the library
1030
this._headers = null;
31+
this._defaultCachingStore = "session";
32+
this._defaultCachingTimeoutSeconds = 30;
33+
this._globalCacheDisable = false;
1134
}
1235

1336
private _headers: TypedHash<string>;
37+
private _defaultCachingStore: "session" | "local";
38+
private _defaultCachingTimeoutSeconds: number;
39+
private _globalCacheDisable: boolean;
1440

1541
public set(config: LibraryConfiguration): void {
1642

17-
// add any headers that are supplied
1843
if (config.hasOwnProperty("headers")) {
1944
this._headers = config.headers;
2045
}
46+
47+
if (config.hasOwnProperty("globalCacheDisable")) {
48+
this._globalCacheDisable = config.globalCacheDisable;
49+
}
50+
51+
if (config.hasOwnProperty("defaultCachingStore")) {
52+
this._defaultCachingStore = config.defaultCachingStore;
53+
}
54+
55+
if (config.hasOwnProperty("defaultCachingTimeoutSeconds")) {
56+
this._defaultCachingTimeoutSeconds = config.defaultCachingTimeoutSeconds;
57+
}
2158
}
2259

2360
public get headers(): TypedHash<string> {
2461
return this._headers;
2562
}
63+
64+
public get defaultCachingStore(): "session" | "local" {
65+
return this._defaultCachingStore;
66+
}
67+
68+
public get defaultCachingTimeoutSeconds(): number {
69+
return this._defaultCachingTimeoutSeconds;
70+
}
71+
72+
public get globalCacheDisable(): boolean {
73+
return this._globalCacheDisable;
74+
}
2675
}
2776

2877
let _runtimeConfig = new RuntimeConfigImpl();

src/sharepoint/rest/caching.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { ODataParser } from "./odata";
2+
import { PnPClientStore, PnPClientStorage } from "../../utils/storage";
3+
import { Util } from "../../utils/util";
4+
import { RuntimeConfig } from "../../configuration/pnplibconfig";
5+
6+
export interface ICachingOptions {
7+
expiration?: Date;
8+
storeName?: "session" | "local";
9+
key: string;
10+
}
11+
12+
export class CachingOptions implements ICachingOptions {
13+
14+
constructor(public key: string) {}
15+
16+
protected static storage = new PnPClientStorage();
17+
18+
public expiration = Util.dateAdd(new Date(), "second", RuntimeConfig.defaultCachingTimeoutSeconds);
19+
20+
public storeName: "session" | "local" = RuntimeConfig.defaultCachingStore;
21+
22+
public get store(): PnPClientStore {
23+
if (this.storeName === "local") {
24+
return CachingOptions.storage.local;
25+
} else {
26+
return CachingOptions.storage.session;
27+
}
28+
}
29+
}
30+
31+
export class CachingParserWrapper<T, U> implements ODataParser<T, U> {
32+
33+
constructor(
34+
private _parser: ODataParser<T, U>,
35+
private _cacheOptions: CachingOptions) { }
36+
37+
public parse(response: Response): Promise<U> {
38+
39+
// add this to the cache based on the options
40+
return this._parser.parse(response).then(data => {
41+
42+
this._cacheOptions.store.put(this._cacheOptions.key, data, this._cacheOptions.expiration);
43+
44+
return data;
45+
});
46+
}
47+
}

src/sharepoint/rest/items.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export class Items extends QueryableCollection {
6060
*/
6161
public add(properties: TypedHash<string | number | boolean> = {}): Promise<ItemAddResult> {
6262

63+
this.addBatchDependency();
64+
6365
let parentList = this.getParent(QueryableInstance);
6466

6567
return parentList.select("ListItemEntityTypeFullName").getAs<any, { ListItemEntityTypeFullName: string }>().then((d) => {
@@ -68,12 +70,16 @@ export class Items extends QueryableCollection {
6870
"__metadata": { "type": d.ListItemEntityTypeFullName },
6971
}, properties));
7072

71-
return this.postAs<any, { Id: number }>({ body: postBody }).then((data) => {
73+
let promise = this.postAs<any, { Id: number }>({ body: postBody }).then((data) => {
7274
return {
7375
data: data,
7476
item: this.getById(data.Id),
7577
};
7678
});
79+
80+
this.clearBatchDependency();
81+
82+
return promise;
7783
});
7884
}
7985
}
@@ -171,6 +177,8 @@ export class Item extends QueryableSecurable {
171177
*/
172178
public update(properties: TypedHash<string | number | boolean>, eTag = "*"): Promise<ItemUpdateResult> {
173179

180+
this.addBatchDependency();
181+
174182
let parentList = this.getParent(QueryableInstance, this.parentUrl.substr(0, this.parentUrl.lastIndexOf("/")));
175183

176184
return parentList.select("ListItemEntityTypeFullName").getAs<any, { ListItemEntityTypeFullName: string }>().then((d) => {
@@ -179,7 +187,7 @@ export class Item extends QueryableSecurable {
179187
"__metadata": { "type": d.ListItemEntityTypeFullName },
180188
}, properties));
181189

182-
return this.post({
190+
let promise = this.post({
183191
body: postBody,
184192
headers: {
185193
"IF-Match": eTag,
@@ -191,6 +199,10 @@ export class Item extends QueryableSecurable {
191199
item: this,
192200
};
193201
});
202+
203+
this.clearBatchDependency();
204+
205+
return promise;
194206
});
195207
}
196208

src/sharepoint/rest/lists.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ export class Lists extends QueryableCollection {
7777
/*tslint:enable */
7878

7979
/**
80-
* Ensures that the specified list exists in the collection (note: settings are not updated if the list exists)
80+
* Ensures that the specified list exists in the collection (note: settings are not updated if the list exists,
81+
* not supported for batching)
8182
*
8283
* @param title The new list's title
8384
* @param description The new list's description
@@ -88,14 +89,18 @@ export class Lists extends QueryableCollection {
8889
/*tslint:disable max-line-length */
8990
public ensure(title: string, description = "", template = 100, enableContentTypes = false, additionalSettings: TypedHash<string | number | boolean> = {}): Promise<ListEnsureResult> {
9091

92+
if (this.hasBatch) {
93+
throw new Error("The ensure method is not supported as part of a batch.");
94+
}
95+
9196
return new Promise((resolve, reject) => {
9297

9398
let list: List = this.getByTitle(title);
9499

95100
list.get().then((d) => resolve({ created: false, list: list, data: d })).catch(() => {
96101

97102
this.add(title, description, template, enableContentTypes, additionalSettings).then((r) => {
98-
resolve({ created: true, list: this.getByTitle(title), data: r.data })
103+
resolve({ created: true, list: this.getByTitle(title), data: r.data });
99104
});
100105

101106
}).catch((e) => reject(e));

0 commit comments

Comments
 (0)