Skip to content

Commit 27dc413

Browse files
authored
feat: add Actor.useState() helper (#98)
1 parent 8bfeb8e commit 27dc413

11 files changed

Lines changed: 292 additions & 184 deletions

File tree

package-lock.json

Lines changed: 235 additions & 161 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/actor-scraper/cheerio-scraper/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "module",
77
"dependencies": {
88
"@apify/scraper-tools": "^1.0.0",
9-
"@crawlee/cheerio": "^3.0.0",
9+
"@crawlee/cheerio": "^3.1.0",
1010
"apify": "^3.0.0"
1111
},
1212
"devDependencies": {

packages/actor-scraper/playwright-scraper/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"type": "module",
77
"dependencies": {
88
"@apify/scraper-tools": "^1.0.0",
9-
"@crawlee/core": "^3.0.0",
10-
"@crawlee/playwright": "^3.0.0",
11-
"@crawlee/utils": "^3.0.0",
9+
"@crawlee/core": "^3.1.0",
10+
"@crawlee/playwright": "^3.1.0",
11+
"@crawlee/utils": "^3.1.0",
1212
"apify": "^3.0.0",
1313
"playwright": "*"
1414
},

packages/actor-scraper/playwright-scraper/src/internals/crawler_setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
PlaywrightCrawler,
1212
PlaywrightCrawlerOptions,
1313
PlaywrightLaunchContext,
14-
BrowserCrawlerEnqueueLinksOptions,
14+
EnqueueLinksOptions,
1515
log,
1616
} from '@crawlee/playwright';
1717
import { Awaitable, Dictionary } from '@crawlee/utils';
@@ -342,7 +342,7 @@ export class CrawlerSetup implements CrawlerSetupOptions {
342342
return;
343343
}
344344

345-
const enqueueOptions: BrowserCrawlerEnqueueLinksOptions = {
345+
const enqueueOptions: EnqueueLinksOptions = {
346346
globs: this.input.globs,
347347
pseudoUrls: this.input.pseudoUrls,
348348
transformRequestFunction: (requestOptions) => {

packages/actor-scraper/puppeteer-scraper/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "module",
77
"dependencies": {
88
"@apify/scraper-tools": "^1.0.0",
9-
"@crawlee/puppeteer": "^3.0.0",
9+
"@crawlee/puppeteer": "^3.1.0",
1010
"apify": "^3.0.0",
1111
"puppeteer": "*"
1212
},

packages/actor-scraper/puppeteer-scraper/src/internals/crawler_setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
PuppeteerCrawlingContext,
1212
PuppeteerCrawler,
1313
PuppeteerCrawlerOptions,
14-
BrowserCrawlerEnqueueLinksOptions,
14+
EnqueueLinksOptions,
1515
log,
1616
} from '@crawlee/puppeteer';
1717
import { Awaitable, Dictionary } from '@crawlee/utils';
@@ -341,7 +341,7 @@ export class CrawlerSetup implements CrawlerSetupOptions {
341341
return;
342342
}
343343

344-
const enqueueOptions: BrowserCrawlerEnqueueLinksOptions = {
344+
const enqueueOptions: EnqueueLinksOptions = {
345345
globs: this.input.globs,
346346
pseudoUrls: this.input.pseudoUrls,
347347
transformRequestFunction: (requestOptions) => {

packages/actor-scraper/web-scraper/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "module",
88
"dependencies": {
99
"@apify/scraper-tools": "^1.0.0",
10-
"@crawlee/puppeteer": "^3.0.0",
10+
"@crawlee/puppeteer": "^3.1.0",
1111
"apify": "^3.0.0",
1212
"content-type": "^1.0.4",
1313
"devtools-server": "^0.0.2",

packages/apify/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959
"@apify/log": "^2.1.4",
6060
"@apify/timeout": "^0.3.0",
6161
"@apify/utilities": "^2.2.2",
62-
"@crawlee/core": "^3.0.0",
63-
"@crawlee/types": "^3.0.0",
64-
"@crawlee/utils": "^3.0.0",
62+
"@crawlee/core": "^3.1.0",
63+
"@crawlee/types": "^3.1.0",
64+
"@crawlee/utils": "^3.1.0",
6565
"apify-client": "^2.6.0",
6666
"ow": "^0.28.1",
6767
"semver": "^7.3.7",

packages/apify/src/actor.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type {
2222
EventTypeName,
2323
IStorage,
2424
RecordOptions,
25+
UseStateOptions,
2526
} from '@crawlee/core';
2627
import {
2728
Configuration as CoreConfiguration,
@@ -823,6 +824,41 @@ export class Actor<Data extends Dictionary = Dictionary> {
823824
return !!process.env[ENV_VARS.IS_AT_HOME];
824825
}
825826

827+
/**
828+
* Easily create and manage state values. All state values are automatically persisted.
829+
*
830+
* Values can be modified by simply using the assignment operator.
831+
*
832+
* @param name The name of the store to use.
833+
* @param defaultValue If the store does not yet have a value in it, the value will be initialized with the `defaultValue` you provide.
834+
* @param options An optional object parameter where a custom `keyValueStoreName` and `config` can be passed in.
835+
*/
836+
async useState<State extends Dictionary = Dictionary>(
837+
name?: string,
838+
defaultValue = {} as State,
839+
options?: UseStateOptions,
840+
) {
841+
const kvStore = await KeyValueStore.open(options?.keyValueStoreName, { config: options?.config || Configuration.getGlobalConfig() });
842+
return kvStore.getAutoSavedValue<State>(name || 'APIFY_GLOBAL_STATE', defaultValue);
843+
}
844+
845+
/**
846+
* Easily create and manage state values. All state values are automatically persisted.
847+
*
848+
* Values can be modified by simply using the assignment operator.
849+
*
850+
* @param name The name of the store to use.
851+
* @param defaultValue If the store does not yet have a value in it, the value will be initialized with the `defaultValue` you provide.
852+
* @param options An optional object parameter where a custom `keyValueStoreName` and `config` can be passed in.
853+
*/
854+
static async useState<State extends Dictionary = Dictionary>(
855+
name?: string,
856+
defaultValue = {} as State,
857+
options?: UseStateOptions,
858+
) {
859+
return Actor.getDefaultInstance().useState<State>(name, defaultValue, options);
860+
}
861+
826862
/**
827863
* Runs the main user function that performs the job of the actor
828864
* and terminates the process when the user function finishes.

packages/scraper-tools/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@
4040
},
4141
"devDependencies": {
4242
"apify": "^3.0.0",
43-
"@crawlee/browser-pool": "^3.0.0",
44-
"@crawlee/core": "^3.0.0",
45-
"@crawlee/types": "^3.0.0",
46-
"@crawlee/utils": "^3.0.0"
43+
"@crawlee/browser-pool": "^3.1.0",
44+
"@crawlee/core": "^3.1.0",
45+
"@crawlee/types": "^3.1.0",
46+
"@crawlee/utils": "^3.1.0"
4747
},
4848
"peerDependencies": {
4949
"apify": "^3.0.0",
50-
"@crawlee/browser-pool": "^3.0.0",
51-
"@crawlee/core": "^3.0.0",
52-
"@crawlee/types": "^3.0.0",
53-
"@crawlee/utils": "^3.0.0"
50+
"@crawlee/browser-pool": "^3.1.0",
51+
"@crawlee/core": "^3.1.0",
52+
"@crawlee/types": "^3.1.0",
53+
"@crawlee/utils": "^3.1.0"
5454
},
5555
"peerDependenciesMeta": {
5656
"@crawlee/puppeteer": {

0 commit comments

Comments
 (0)