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

removing post-install scripts #2956

Merged
merged 4 commits into from
Mar 12, 2024
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
110 changes: 110 additions & 0 deletions docs/concepts/async-paging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Async Paging

With the introduction of the async iterator pattern to both sp/items and all graph collections we wanted to share a discussion for working with async paging.

The easiest example is to process all of the items in a loop. In this example each page of 1000 results is retrieved from the list. The `items` collection itself is AsyncIterable so you can use it directly in the loop.

```TypeScript
for await (const items of sp.web.lists.getByTitle("BigList").items.top(1000)) {
console.log(items.length);
}
```

And a graph example:

```TypeScript
for await (const items of graph.users) {
console.log(items.length);
}
```

## Accessing the Iterator

You might in some cases want to access the iterator object directly from the collection, which you can do using `Symbol.asyncIterator` method:

```TypeScript
const iterator = collection[Symbol.asyncIterator]();
```

## Paging Helper

We are also providing an example paging class to control prev/next paging through the collection using the AsyncIterator. The code here is provided as an example only.

```TypeScript
class AsyncPager<T> {

private iterator: AsyncIterator<T>;

constructor(iterable: AsyncIterable<T>, private pages: T[] = [], private pagePointer = -1, private isDone = false) {
this.iterator = iterable[Symbol.asyncIterator]();
}

/**
* Provides access to the current page of values
*/
async current(): Promise<T> {

// we don't have any pages yet
if (this.pagePointer < 0) {
return this.next();
}

// return the current page
return this.pages[this.pagePointer];
}

/**
* Access the next page, either from the local cache or make a request to load it
*/
async next(): Promise<T> {

// does the page exist?
let page = this.pages[++this.pagePointer];

if (typeof page === "undefined") {

if (this.isDone) {

// if we are already done make sure we don't make any more requests
// and return the last page
--this.pagePointer;

} else {

// get the next page of links
const next = await this.iterator.next();

if (next.done) {

this.isDone = true;

} else {

this.pages.push(next.value);
}
}
}

return this.pages[this.pagePointer];
}

async prev(): Promise<T> {

// handle already at the start
if (this.pagePointer < 1) {
return this.pages[0];
}

// return the previous page moving our pointer
return this.pages[--this.pagePointer];
}
}
```

And some usage:

```TypeScript
const pager = new AsyncPager(sp.web.lists.getByTitle("BigList").items.top(1000));

const items1 = await pager.next();
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ nav:
- '2020': 'news/2020-year-in-review.md'
- 'Library Usage Tips':
- 'Authentication': 'concepts/authentication.md'
- 'Async Paging': 'concepts/async-paging.md'
- 'In SPFx': 'concepts/auth-spfx.md'
- 'In Browser': 'concepts/auth-browser.md'
- 'In NodeJS': 'concepts/auth-nodejs.md'
Expand Down
3 changes: 0 additions & 3 deletions packages/graph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
"description": "pnp - provides a fluent interface to query the Microsoft Graph",
"main": "./index.js",
"typings": "./index",
"scripts": {
"postinstall": "node ./post-install.cjs"
},
"dependencies": {
"@microsoft/microsoft-graph-types": "2.40.0",
"tslib": "2.6.2",
Expand Down
20 changes: 0 additions & 20 deletions packages/graph/post-install.cjs

This file was deleted.

1 change: 1 addition & 0 deletions packages/queryable/behaviors/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export function Caching(props?: ICachingProps): TimelinePipe<Queryable> {
}));

} else {

result = cached;
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/sp/files/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,9 @@ function applyChunckedOperationDefaults(props: Partial<IChunkedOperationProps>):
};
}

/**
* Converts the source into a ReadableStream we can understand
*/
function sourceToReadableStream(source: ValidFileContentSource): ReadableStream {

if (isBlob(source)) {
Expand All @@ -763,7 +766,7 @@ function sourceToReadableStream(source: ValidFileContentSource): ReadableStream

} else {

return source;
return <any>source;
}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/sp/items/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ export class _Items<GetType = any[]> extends _SPCollection<GetType> {

public [Symbol.asyncIterator]() {

const q = SPCollection(this).using(parseBinderWithErrorCheck(async (r) => {
const nextInit = SPCollection(this).using(parseBinderWithErrorCheck(async (r) => {

const json = await r.json();
const nextUrl = hOP(json, "d") && hOP(json.d, "__next") ? json.d.__next : json["odata.nextLink"];
const nextLink = hOP(json, "d") && hOP(json.d, "__next") ? json.d.__next : json["odata.nextLink"];

return <IPagedResult<GetType>>{
hasNext: typeof nextUrl === "string" && nextUrl.length > 0,
nextLink: nextUrl,
hasNext: typeof nextLink === "string" && nextLink.length > 0,
nextLink,
value: parseODataJSON(json),
};
}));
Expand All @@ -79,13 +79,13 @@ export class _Items<GetType = any[]> extends _SPCollection<GetType> {
for (let i = 0; i < queryParams.length; i++) {
const param = this.query.get(queryParams[i]);
if (objectDefinedNotNull(param)) {
q.query.set(queryParams[i], param);
nextInit.query.set(queryParams[i], param);
}
}

return <AsyncIterator<GetType>>{

_next: q,
_next: nextInit,

async next() {

Expand Down
3 changes: 0 additions & 3 deletions packages/sp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
"description": "pnp - provides a fluent api for working with SharePoint REST",
"main": "./index.js",
"typings": "./index",
"scripts": {
"postinstall": "node ./post-install.cjs"
},
"dependencies": {
"tslib": "2.4.1",
"@pnp/core": "0.0.0-PLACEHOLDER",
Expand Down
20 changes: 0 additions & 20 deletions packages/sp/post-install.cjs

This file was deleted.

Loading