Skip to content

Commit 4105e57

Browse files
authored
Implement require-pickup-in-persist rule (#163)
1 parent 5399f72 commit 4105e57

16 files changed

+206
-0
lines changed

config/scope.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
rules: {
33
"effector/strict-effect-handlers": "error",
4+
"effector/require-pickup-in-persist": "error",
45
},
56
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# effector/require-pickup-in-persist
2+
3+
Requires every `persist` call of the [`effector-storage`](https://github.com/yumauri/effector-storage) library to include a `pickup` event when using [`Scope`s](https://effector.dev/api/effector/scope/). This ensures the correct initial value is loaded into the store for each `Scope`.
4+
5+
```ts
6+
import { persist } from "effector-storage/query";
7+
8+
const $store = createStore("example");
9+
10+
// 👎 no pickup, does not work with Scope
11+
persist({ store: $store });
12+
```
13+
14+
```ts
15+
const pickup = createEvent();
16+
17+
// 👍 pickup is specified
18+
persist({ store: $store, pickup });
19+
```

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
"no-guard": require("./rules/no-guard/no-guard"),
1919
"mandatory-scope-binding": require("./rules/mandatory-scope-binding/mandatory-scope-binding"),
2020
"prefer-useUnit": require("./rules/prefer-useUnit/prefer-useUnit"),
21+
"require-pickup-in-persist": require("./rules/require-pickup-in-persist/require-pickup-in-persist"),
2122
"no-patronum-debug": require("./rules/no-patronum-debug/no-patronum-debug"),
2223
},
2324
configs: {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createStore, createEvent } from "effector";
2+
3+
import { persist } from "effector-storage/local";
4+
5+
const $store = createStore("example");
6+
const updated = createEvent();
7+
8+
const appStarted = createEvent();
9+
10+
persist({
11+
source: $store.updates,
12+
target: updated,
13+
14+
pickup: appStarted,
15+
16+
key: "store",
17+
keyPrefix: "local",
18+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createStore, createEvent } from "effector";
2+
3+
import { persist } from "effector-storage";
4+
import { local as localAdapter } from "effector-storage/local";
5+
6+
const $store = createStore("example");
7+
const pickup = createEvent();
8+
9+
persist({ store: $store, pickup, adapter: localAdapter });
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createStore } from "effector";
2+
3+
import { persist } from "other-persist";
4+
import { persist as persistNested } from "other-persist/nested";
5+
6+
const $store = createStore("example");
7+
8+
persist({ store: $store });
9+
persistNested({ store: $store });
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createStore, createEvent } from "effector";
2+
3+
import { persist as persistQuery } from "effector-storage/query";
4+
5+
const $store = createStore("example");
6+
const pickup = createEvent();
7+
8+
persistQuery({ store: $store, pickup });
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createStore, createEvent } from "effector";
2+
3+
import { persist as persistAsync } from "@effector-storage/react-native-async-storage";
4+
5+
const $store = createStore("example");
6+
const pickup = createEvent();
7+
8+
persistAsync({ store: $store, pickup });
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createStore } from "effector";
2+
3+
import { persist } from "effector-storage";
4+
5+
const randomCall = () => ({ store: createStore() });
6+
7+
persist();
8+
persist("invalid");
9+
persist(randomCall());
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { createStore, createEvent } from "effector";
2+
3+
import { persist } from "effector-storage/local";
4+
5+
const $store = createStore("example");
6+
const updated = createEvent();
7+
8+
persist({
9+
source: $store,
10+
target: updated,
11+
12+
key: "store",
13+
keyPrefix: "local",
14+
});

0 commit comments

Comments
 (0)