@coaction/history 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,17 +1,15 @@
1
1
  # @coaction/history
2
2
 
3
- ![Node CI](https://github.com/coactionjs/coaction/workflows/Node%20CI/badge.svg)
4
- [![npm](https://img.shields.io/npm/v/@coaction/history.svg)](https://www.npmjs.com/package/@coaction/history)
5
- ![license](https://img.shields.io/npm/l/@coaction/history)
3
+ ![Node CI](https://github.com/coactionjs/coaction/workflows/Node%20CI/badge.svg) [![npm](https://img.shields.io/npm/v/@coaction/history.svg)](https://www.npmjs.com/package/@coaction/history) ![license](https://img.shields.io/npm/l/@coaction/history)
6
4
 
7
5
  A undo/redo middleware for Coaction.
8
6
 
9
7
  ## Installation
10
8
 
11
- You can install it via npm, yarn or pnpm.
9
+ Install it with pnpm:
12
10
 
13
11
  ```sh
14
- npm install coaction @coaction/history
12
+ pnpm add coaction @coaction/history
15
13
  ```
16
14
 
17
15
  ## Usage
package/dist/index.js CHANGED
@@ -114,6 +114,7 @@ const cloneSnapshotList = (snapshots) => snapshots.map((snapshot) => toSnapshot(
114
114
  const history = (options = {}) => (store) => {
115
115
  if (store.share === "client") throw new Error("history() is not supported in client store mode. Apply history() to the main shared store instead.");
116
116
  const { limit = 100, partialize = (state) => state } = options;
117
+ if (!Number.isInteger(limit) || limit < 0) throw new Error("history limit must be a non-negative integer.");
117
118
  const applyHistorySnapshot = options.partialize ? applyPartialSnapshot : applySnapshot;
118
119
  const applyStore = store.apply?.bind(store);
119
120
  const past = [];
@@ -145,14 +146,17 @@ const history = (options = {}) => (store) => {
145
146
  lastSnapshot = current;
146
147
  };
147
148
  const applyTimeTravelSnapshot = (snapshot, current) => {
149
+ const nextState = toSnapshot(store.getPureState());
150
+ applyHistorySnapshot(nextState, snapshot, current);
148
151
  if (applyStore && hasCircularReference(snapshot)) {
149
- const nextState = toSnapshot(store.getPureState());
150
- applyHistorySnapshot(nextState, snapshot, current);
151
- applyStore(nextState);
152
+ baseSetState(nextState, () => {
153
+ applyStore(nextState);
154
+ return [];
155
+ });
152
156
  return;
153
157
  }
154
- baseSetState((draft) => {
155
- applyHistorySnapshot(draft, snapshot, current);
158
+ baseSetState(nextState, () => {
159
+ return (0, coaction.applyRootReplacementWithPatches)(store, nextState, { applyExactReplacement: !store.share && applyStore ? (replacementState) => applyStore(replacementState) : void 0 });
156
160
  });
157
161
  };
158
162
  const cancelReadySubscription = (0, coaction.onStoreReady)(store, () => {
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { onStoreReady } from "coaction";
1
+ import { applyRootReplacementWithPatches, onStoreReady } from "coaction";
2
2
  //#region packages/coaction-history/src/index.ts
3
3
  const historySuppressionSymbol = Symbol.for("coaction.history.suppress");
4
4
  const isUnsafeKey = (key) => typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
@@ -113,6 +113,7 @@ const cloneSnapshotList = (snapshots) => snapshots.map((snapshot) => toSnapshot(
113
113
  const history = (options = {}) => (store) => {
114
114
  if (store.share === "client") throw new Error("history() is not supported in client store mode. Apply history() to the main shared store instead.");
115
115
  const { limit = 100, partialize = (state) => state } = options;
116
+ if (!Number.isInteger(limit) || limit < 0) throw new Error("history limit must be a non-negative integer.");
116
117
  const applyHistorySnapshot = options.partialize ? applyPartialSnapshot : applySnapshot;
117
118
  const applyStore = store.apply?.bind(store);
118
119
  const past = [];
@@ -144,14 +145,17 @@ const history = (options = {}) => (store) => {
144
145
  lastSnapshot = current;
145
146
  };
146
147
  const applyTimeTravelSnapshot = (snapshot, current) => {
148
+ const nextState = toSnapshot(store.getPureState());
149
+ applyHistorySnapshot(nextState, snapshot, current);
147
150
  if (applyStore && hasCircularReference(snapshot)) {
148
- const nextState = toSnapshot(store.getPureState());
149
- applyHistorySnapshot(nextState, snapshot, current);
150
- applyStore(nextState);
151
+ baseSetState(nextState, () => {
152
+ applyStore(nextState);
153
+ return [];
154
+ });
151
155
  return;
152
156
  }
153
- baseSetState((draft) => {
154
- applyHistorySnapshot(draft, snapshot, current);
157
+ baseSetState(nextState, () => {
158
+ return applyRootReplacementWithPatches(store, nextState, { applyExactReplacement: !store.share && applyStore ? (replacementState) => applyStore(replacementState) : void 0 });
155
159
  });
156
160
  };
157
161
  const cancelReadySubscription = onStoreReady(store, () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coaction/history",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "A undo/redo middleware for Coaction",
5
5
  "keywords": [
6
6
  "coaction",
@@ -30,9 +30,14 @@
30
30
  "types": "dist/index.d.ts",
31
31
  "exports": {
32
32
  ".": {
33
- "types": "./dist/index.d.ts",
34
- "import": "./dist/index.mjs",
35
- "require": "./dist/index.js",
33
+ "import": {
34
+ "types": "./dist/index.d.mts",
35
+ "default": "./dist/index.mjs"
36
+ },
37
+ "require": {
38
+ "types": "./dist/index.d.ts",
39
+ "default": "./dist/index.js"
40
+ },
36
41
  "default": "./dist/index.mjs"
37
42
  },
38
43
  "./package.json": "./package.json"
@@ -42,10 +47,10 @@
42
47
  "provenance": true
43
48
  },
44
49
  "devDependencies": {
45
- "coaction": "2.0.0"
50
+ "coaction": "2.1.0"
46
51
  },
47
52
  "peerDependencies": {
48
- "coaction": "^2.0.0"
53
+ "coaction": ">=2.1.0 <3"
49
54
  },
50
55
  "authors": [
51
56
  "Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"