Skip to content
This repository has been archived by the owner on Nov 23, 2020. It is now read-only.

Commit

Permalink
[new package] @reffect/undoable (#7)
Browse files Browse the repository at this point in the history
* feat: initial commit for reffect-undoable package, add simplest implementation undo redo

* feat: create simple undo redo store extension package

* chore: change build config (output plugins)

* chore: remove build files

* chore: add gitignore for reffect/undoable

* chore: lock file update

* feat: rewrite undoable to reffect store with couple effects (undo, redo)

* chore: up size limits for reffect undoable package; chore: change keywords for undoable

* docs: update readme for monorepo (add Install section); docs: add readme for undoable package
  • Loading branch information
js2me authored May 14, 2020
1 parent 33c2076 commit eef6f37
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 15 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ Reffect — is a declarative and reactive multi-store state manager for JavaScri
[![npm](https://img.shields.io/npm/v/@reffect/logger?style=flat-square&color=blue)](https://www.npmjs.com/package/@reffect/logger) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/@reffect/logger?style=flat-square&color=blue)](https://bundlephobia.com/result?p=@reffect/logger)
- [`@reffect/localstore`](https://github.com/acacode/reffect/tree/master/packages/localstore) - store middleware to synchronize store with local storage key
[![npm](https://img.shields.io/npm/v/@reffect/localstore?style=flat-square&color=blue)](https://www.npmjs.com/package/@reffect/localstore) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/@reffect/localstore?style=flat-square&color=blue)](https://bundlephobia.com/result?p=@reffect/localstore)
- [`@reffect/undoable`](https://github.com/acacode/reffect/tree/master/packages/undoable) - store extension which provides undo/redo effects and store history
[![npm](https://img.shields.io/npm/v/@reffect/undoable?style=flat-square&color=blue)](https://www.npmjs.com/package/@reffect/undoable) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/@reffect/undoable?style=flat-square&color=blue)](https://bundlephobia.com/result?p=@reffect/undoable)

## Install

Before at all you need to install main package:

```bash
$ npm i -S @reffect/core
# or using yarn
$ yarn install @reffect/core
```

If project is using [React](https://github.com/facebook/react) you need to install [@reffect/react](https://github.com/acacode/reffect/tree/master/packages/react) (pack of React hooks which simplify usage with React application)

```bash
$ npm i -S @reffect/react
```

## Examples

Expand Down
35 changes: 20 additions & 15 deletions build/utils/createBuildConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,28 @@ const createBuildConfig = ({
output: {
file: outputPathToFile,
format: outputFormat,
sourcemap,
sourcemap: sourcemap,
plugins: [
defaultPlugins &&
terser({
sourcemap: sourcemap,
compress: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
warnings: false,
arguments: true,
unsafe_Function: true,
module: true,
passes: 30
}
})
].filter(Boolean),
...commonOutput
},
external: id => externalDeps.includes(id),
context: projectPath,
plugins: [
defaultPlugins &&
terser({
compress: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
warnings: false,
arguments: true,
unsafe_Function: true,
module: true,
passes: 15
}
}),
defaultPlugins &&
typescript({
allowJs: false,
Expand All @@ -64,7 +67,9 @@ const createBuildConfig = ({
noImplicitReturns: true,
noImplicitThis: true,
noUnusedLocals: true,
sourceMap: false,
inlineSourceMap: true,
inlineSources: true,
sourceMap: true,
strictNullChecks: true,
suppressImplicitAnyIndexErrors: true,
exclude: ["node_modules", "**/*.spec.ts", "**/*.test.ts"]
Expand Down
4 changes: 4 additions & 0 deletions packages/undoable/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
reffect-undoable.*.js
reffect-undoable.d.ts
reffect-undoable.*.js.map
package-lock.json
17 changes: 17 additions & 0 deletions packages/undoable/.size-limit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"path": "reffect-undoable.es.js",
"webpack": false,
"limit": "410 B"
},
{
"path": "reffect-undoable.cjs.js",
"webpack": false,
"limit": "450 B"
},
{
"path": "reffect-undoable.umd.js",
"webpack": false,
"limit": "550 B"
}
]
106 changes: 106 additions & 0 deletions packages/undoable/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<div align="center">

[![reffect logo](https://raw.githubusercontent.com/acacode/reffect/master/assets/reffect.png)](https://github.com/acacode/reffect)
[![npm](https://img.shields.io/npm/v/@reffect/react?style=flat-square&color=blue)](https://www.npmjs.com/package/@reffect/react)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/@reffect/react?style=flat-square&color=blue)](https://bundlephobia.com/result?p=@reffect/react)
[![license](https://img.shields.io/github/license/acacode/reffect?style=flat-square&color=blue)](https://github.com/acacode/reffect)

<div align="left">

Reffect — is a declarative and reactive multi-store state manager for JavaScript/TypeScript applications inspired by [Reatom](https://github.com/artalar/reatom) and [Effector](https://github.com/zerobias/effector)

# @reffect/undoable

Extension for [`Reffect`](https://github.com/acacode/reffect) stores.
Provides undo/redo effects and store history.

## How to use

Import `undoable` function from package:

```ts
import { undoable } from "@reffect/undoable";
```

Call `undoable` and send to it your store at first argument:

```ts
import { store } from "@reffect/core";
const storeRef = store({ foo: "bar" });
const { history, undo, redo } = undoable(storeRef, middlewares, limit);
// `middlewares` it is array of reffect store middlewares
// `limit` means limit for state history
```

### undo (`VoidFunction`)

This function move state of wrapped store to the previous value

```ts
undo();
```

### redo (`VoidFunction`)

This function move state of wrapped store to the next value (if `undo()` was called early)

```ts
redo();
```

### history (`{ past: Store[]; present: Store; future: Store[]; }`)

It's reffect store which have state history of wrapped store

```ts
import { manage } from "@reffect/core";

manage(history).subscribe((payload, prevState, currState) => console.log(payload, prevState, currState));
```

## Examples

```ts
import { store, effect } from "@reffect/core";
import { undoable } from "@reffect/undoable";

const keyboards = store({ list: [] });
const { history, undo, redo } = undoable(keyboards);

const addKeyboard = effect(keyboards, name => ({ list: [...keyboards.list, name] }));

addKeyboard("Das Keyboard 4Q");
addKeyboard("Leopold FC900R");
addKeyboard("Leopold FC750R");

console.log(keyboards.list); // ["Das Keyboard 4Q", "Leopold FC900R", "Leopold FC750R"]
undo();
console.log(keyboards.list); // ["Das Keyboard 4Q", "Leopold FC900R"]
undo();
console.log(keyboards.list); // ["Das Keyboard 4Q"]
redo();
console.log(keyboards.list); // ["Das Keyboard 4Q", "Leopold FC900R"]
redo();
console.log(keyboards.list); // ["Das Keyboard 4Q", "Leopold FC900R", "Leopold FC750R"]

console.log(history);
/*
{
past: [
{
list: [],
},
{
list: ["Das Keyboard 4Q"],
},
{
list: ["Das Keyboard 4Q", "Leopold FC900R"],
},
],
present: {
list: ["Das Keyboard 4Q", "Leopold FC900R", "Leopold FC750R"]
},
future: []
}
*/
```
50 changes: 50 additions & 0 deletions packages/undoable/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@reffect/undoable",
"version": "0.0.1",
"description": "Reffect stores extension",
"scripts": {
"build": "node ../../build/build.js",
"size": "size-limit"
},
"author": "acacode",
"license": "MIT",
"readme": "README.md",
"private": false,
"sideEffects": false,
"main": "reffect-undoable.cjs.js",
"typings": "reffect-undoable.d.ts",
"umd:main": "reffect-undoable.umd.js",
"jsnext:main": "reffect-undoable.es.js",
"module": "reffect-undoable.es.js",
"peerDependencies": { "@reffect/core": "^1.5.0" },
"devDependencies": {
"@reffect/core": "^1.5.0",
"@size-limit/preset-small-lib": "^4.2.1",
"size-limit": "^4.2.1"
},
"files": [
"LICENSE",
"reffect-undoable.d.ts",
"reffect-undoable.cjs.js",
"reffect-undoable.cjs.js.map",
"reffect-undoable.es.js",
"reffect-undoable.es.js.map",
"reffect-undoable.umd.js",
"reffect-undoable.umd.js.map"
],
"keywords": [
"redux",
"effector",
"mobx",
"state",
"store",
"flux",
"reactive",
"state-manager",
"state manager",
"undo",
"undoable",
"reffect"
],
"gitHead": "7636d0e5d6b6ba2f17c0b415f0761b559ae2a20c"
}
68 changes: 68 additions & 0 deletions packages/undoable/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { StoreType, manage, store, effect, StoreMiddleware } from "@reffect/core";

type Undoable<Store extends StoreType> = {
history: StoreHistory<Store>;
undo: VoidFunction;
redo: VoidFunction;
};

export type StoreHistory<Store extends StoreType> = {
past: Store[];
present: Store;
future: Store[];
};

export const undoable = <Store extends StoreType>(
storeRef: Store,
middlewares?: StoreMiddleware<StoreHistory<Store>>[],
limit = 15,
): Undoable<Store> => {
let updateFromUndoable = false;
const storeRefManager = manage(storeRef);
const storeHistory = store<StoreHistory<Store>>(
{
past: [],
present: storeRefManager.initialState as Store,
future: [],
},
`${storeRefManager.name}-history`,
middlewares,
);

const undoRedo = (isRedo?: boolean): Partial<StoreHistory<Store>> | void => {
const historyArray = storeHistory[isRedo ? "future" : "past"];
if (historyArray.length) {
const sliced = historyArray.slice();
const present = sliced[isRedo ? "shift" : "pop"]();
if (present) {
updateFromUndoable = true;
storeRefManager.partialUpdate(present);
return {
present,
future: isRedo ? sliced : [storeHistory.present, ...storeHistory.future],
past: isRedo ? [...storeHistory.past, storeHistory.present] : sliced,
};
}
}
};

storeRefManager.subscribe((partialUpdate, prevState, currState) => {
if (updateFromUndoable) {
updateFromUndoable = false;
} else {
const pastLength = storeHistory.past.length;

manage(storeHistory).partialUpdate({
present: currState,
future: storeHistory.future.length ? [] : storeHistory.future,
past: [...storeHistory.past.slice(+(pastLength >= limit), pastLength), prevState],
});
}
});

return {
undo: effect(storeHistory, () => undoRedo()),
redo: effect(storeHistory, () => undoRedo(true)),
history: storeHistory,
};
};

0 comments on commit eef6f37

Please sign in to comment.