Skip to content

Commit

Permalink
feat: add Actor.useState() helper (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan authored Oct 13, 2022
1 parent 8bfeb8e commit 27dc413
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 184 deletions.
396 changes: 235 additions & 161 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/actor-scraper/cheerio-scraper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"dependencies": {
"@apify/scraper-tools": "^1.0.0",
"@crawlee/cheerio": "^3.0.0",
"@crawlee/cheerio": "^3.1.0",
"apify": "^3.0.0"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/actor-scraper/playwright-scraper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"type": "module",
"dependencies": {
"@apify/scraper-tools": "^1.0.0",
"@crawlee/core": "^3.0.0",
"@crawlee/playwright": "^3.0.0",
"@crawlee/utils": "^3.0.0",
"@crawlee/core": "^3.1.0",
"@crawlee/playwright": "^3.1.0",
"@crawlee/utils": "^3.1.0",
"apify": "^3.0.0",
"playwright": "*"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
PlaywrightCrawler,
PlaywrightCrawlerOptions,
PlaywrightLaunchContext,
BrowserCrawlerEnqueueLinksOptions,
EnqueueLinksOptions,
log,
} from '@crawlee/playwright';
import { Awaitable, Dictionary } from '@crawlee/utils';
Expand Down Expand Up @@ -342,7 +342,7 @@ export class CrawlerSetup implements CrawlerSetupOptions {
return;
}

const enqueueOptions: BrowserCrawlerEnqueueLinksOptions = {
const enqueueOptions: EnqueueLinksOptions = {
globs: this.input.globs,
pseudoUrls: this.input.pseudoUrls,
transformRequestFunction: (requestOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/actor-scraper/puppeteer-scraper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"dependencies": {
"@apify/scraper-tools": "^1.0.0",
"@crawlee/puppeteer": "^3.0.0",
"@crawlee/puppeteer": "^3.1.0",
"apify": "^3.0.0",
"puppeteer": "*"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
PuppeteerCrawlingContext,
PuppeteerCrawler,
PuppeteerCrawlerOptions,
BrowserCrawlerEnqueueLinksOptions,
EnqueueLinksOptions,
log,
} from '@crawlee/puppeteer';
import { Awaitable, Dictionary } from '@crawlee/utils';
Expand Down Expand Up @@ -341,7 +341,7 @@ export class CrawlerSetup implements CrawlerSetupOptions {
return;
}

const enqueueOptions: BrowserCrawlerEnqueueLinksOptions = {
const enqueueOptions: EnqueueLinksOptions = {
globs: this.input.globs,
pseudoUrls: this.input.pseudoUrls,
transformRequestFunction: (requestOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/actor-scraper/web-scraper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "module",
"dependencies": {
"@apify/scraper-tools": "^1.0.0",
"@crawlee/puppeteer": "^3.0.0",
"@crawlee/puppeteer": "^3.1.0",
"apify": "^3.0.0",
"content-type": "^1.0.4",
"devtools-server": "^0.0.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/apify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
"@apify/log": "^2.1.4",
"@apify/timeout": "^0.3.0",
"@apify/utilities": "^2.2.2",
"@crawlee/core": "^3.0.0",
"@crawlee/types": "^3.0.0",
"@crawlee/utils": "^3.0.0",
"@crawlee/core": "^3.1.0",
"@crawlee/types": "^3.1.0",
"@crawlee/utils": "^3.1.0",
"apify-client": "^2.6.0",
"ow": "^0.28.1",
"semver": "^7.3.7",
Expand Down
36 changes: 36 additions & 0 deletions packages/apify/src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
EventTypeName,
IStorage,
RecordOptions,
UseStateOptions,
} from '@crawlee/core';
import {
Configuration as CoreConfiguration,
Expand Down Expand Up @@ -823,6 +824,41 @@ export class Actor<Data extends Dictionary = Dictionary> {
return !!process.env[ENV_VARS.IS_AT_HOME];
}

/**
* Easily create and manage state values. All state values are automatically persisted.
*
* Values can be modified by simply using the assignment operator.
*
* @param name The name of the store to use.
* @param defaultValue If the store does not yet have a value in it, the value will be initialized with the `defaultValue` you provide.
* @param options An optional object parameter where a custom `keyValueStoreName` and `config` can be passed in.
*/
async useState<State extends Dictionary = Dictionary>(
name?: string,
defaultValue = {} as State,
options?: UseStateOptions,
) {
const kvStore = await KeyValueStore.open(options?.keyValueStoreName, { config: options?.config || Configuration.getGlobalConfig() });
return kvStore.getAutoSavedValue<State>(name || 'APIFY_GLOBAL_STATE', defaultValue);
}

/**
* Easily create and manage state values. All state values are automatically persisted.
*
* Values can be modified by simply using the assignment operator.
*
* @param name The name of the store to use.
* @param defaultValue If the store does not yet have a value in it, the value will be initialized with the `defaultValue` you provide.
* @param options An optional object parameter where a custom `keyValueStoreName` and `config` can be passed in.
*/
static async useState<State extends Dictionary = Dictionary>(
name?: string,
defaultValue = {} as State,
options?: UseStateOptions,
) {
return Actor.getDefaultInstance().useState<State>(name, defaultValue, options);
}

/**
* Runs the main user function that performs the job of the actor
* and terminates the process when the user function finishes.
Expand Down
16 changes: 8 additions & 8 deletions packages/scraper-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@
},
"devDependencies": {
"apify": "^3.0.0",
"@crawlee/browser-pool": "^3.0.0",
"@crawlee/core": "^3.0.0",
"@crawlee/types": "^3.0.0",
"@crawlee/utils": "^3.0.0"
"@crawlee/browser-pool": "^3.1.0",
"@crawlee/core": "^3.1.0",
"@crawlee/types": "^3.1.0",
"@crawlee/utils": "^3.1.0"
},
"peerDependencies": {
"apify": "^3.0.0",
"@crawlee/browser-pool": "^3.0.0",
"@crawlee/core": "^3.0.0",
"@crawlee/types": "^3.0.0",
"@crawlee/utils": "^3.0.0"
"@crawlee/browser-pool": "^3.1.0",
"@crawlee/core": "^3.1.0",
"@crawlee/types": "^3.1.0",
"@crawlee/utils": "^3.1.0"
},
"peerDependenciesMeta": {
"@crawlee/puppeteer": {
Expand Down
2 changes: 0 additions & 2 deletions test/apify/actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,6 @@ describe('Actor', () => {
const defaultStore = await KeyValueStore.open();

const oldGet = defaultStore.getValue;
// @ts-expect-error TODO use spyOn instead of this
defaultStore.getValue = async (key) => expect(key).toBe('key-1');

await Actor.getValue('key-1');
Expand All @@ -1023,7 +1022,6 @@ describe('Actor', () => {
const defaultStore = await KeyValueStore.open();

const oldGet = defaultStore.getValue;
// @ts-expect-error TODO use spyOn instead of this
defaultStore.getValue = async (key) => expect(key).toBe('key-1');

await Actor.getValue('key-1');
Expand Down

0 comments on commit 27dc413

Please sign in to comment.