master
svinokot 6 months ago
parent bdd013ea51
commit 385b99c604
  1. 0
      .gitignore
  2. 0
      LICENSE
  3. 0
      index.js
  4. 0
      nbproject/project.properties
  5. 0
      nbproject/project.xml
  6. 0
      package-lock.json
  7. 2
      package.json
  8. 0
      readme.md
  9. 4
      src/lens-js.js
  10. 19
      src/lens-utils.js
  11. 0
      test/cascade.html
  12. 0
      test/chain.html
  13. 0
      test/index.html
  14. 0
      test/inherit.html
  15. 0
      test/random.html
  16. 48
      test/tree-notify.html

@ -1,6 +1,6 @@
{
"name": "@vovikilelik/lens-js",
"version": "1.6.2",
"version": "1.6.3",
"keywords": [
"util",
"functional",

@ -126,7 +126,7 @@ export class Lens {
}
_fire(diffs, currentDiff) {
this._attachments.forEach(callback => callback(new AttachEvent(currentDiff, diffs)));
this._attachments.forEach(callback => callback(new AttachEvent(currentDiff, diffs), this));
this._chain && this._chain._fire && this._chain._fire(diffs, currentDiff);
}
@ -138,7 +138,7 @@ export class Lens {
Object.keys(this._children).forEach((key) => {
const child = this._children[key];
(value[key] !== undefined) && child._notify && child._notify(value[key]);
child._notify && child._notify(value[key]);
});
}

@ -109,11 +109,16 @@ export const transform = (to, from) => (current) => new Lens(
current
);
export const createLens = (initData, validator = v => v) => {
const store = {lens: {...initData}};
return new Lens(
() => store.lens,
(value) => store.lens = validator(value, store.lens)
);
export const createLens = (initData, mapper) => {
const store = {lens: { ...initData }};
return mapper
? new Lens(
() => store.lens,
(value) => store.lens = mapper(value, store.lens)
)
: new Lens(
() => store.lens,
(value) => store.lens = value
);
};

@ -0,0 +1,48 @@
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
<script type="module">
import { Lens } from '../src/lens-js.js';
import { createLens } from '../src/lens-utils.js';
const data0 = {};
const data1 = { tree: { node: { value: 'test' } } };
const data2 = { tree: { node: { value: 'test2' } } };
const data3 = { tree: { node: { value2: 'test3' } } };
const data4 = { tree: undefined };
const callback = ({ current, diffs }) => {
if (current) {
console.log('Этап 2');
} else if (diffs.length > 0) {
console.log('Этап 1');
} else {
console.log('Этап 3');
}
};
const lens = createLens(data0);
lens.go('tree').go('node').attach(callback);
lens.set(data1);
lens.set(data2);
lens.set(data3);
lens.set(data4);
console.log(lens.get());
</script>
</body>
</html>
Loading…
Cancel
Save