This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Использование Immutable.js | |
## Зачем? | |
Как известно главным способом оптимизации является растановка PureRenderMixin добавляющий | |
shouldComponentUpdate компоненту - https://facebook.github.io/react/docs/pure-render-mixin.html | |
Однако у данного способа есть минус - производится только поверхностное сравнение state и props. | |
Если мы изменим их гдето в глубине по ссылке - компонент этого не заметит и не перерендерится | |
Если мы случайно пересоздадим обьект с теми же данными то компонент перерендерится - несмотря на то | |
что данные посути не изменились. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Есть такое понятие функциональная структура данных и связаное с ним понятие "ссылочная прозрачность". | |
Что же это такое? | |
определение: "Выражение e является ссылочно-прозрачным, | |
если возможно заменить любое его подвыражение эквивалентным значением без изменения значения e." | |
Хм... чет как то не стало яснее - посмотрим на примере операций | |
давайте посоздаем структуры данных в иммутабельном стиле на жсе | |
```js | |
var a = {} // создали новый обьект | |
var b = {...a, key: 2} // создали другой обьект основываясь на первом | |
var c = {} // создадим еще пустой обьект |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ids = ['26052399', '26035461', '26035459', '26035454', '26035467'] | |
var links = Array.prototype.slice.call(document.getElementsByClassName('song_button'), 0).filter(a => ids.find(id => id === a.dataset.playSong)) | |
var current = 0; | |
setInterval( | |
() => { | |
var hasPlaying = links.map(a => a.classList.contains('playing')).filter(a => a).length; | |
if (hasPlaying) { | |
console.log('skip') | |
return; | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// библиотечный код | |
var // определение Option и функции которая оброрачивает в нее | |
Some = val => [val], | |
None = [], | |
Of = val => isNil(val) ? None : Some(val), | |
applyOption = fn => curryN(fn.length, compose(Of, fn)), | |
isNone = option => option.length === 0, | |
flatMap = chain, | |
getOrElse = defaultVal => option => isNone(option) ? | |
defaultVal : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onFormSubmit(e) { | |
var data = { | |
"form": { | |
"id": e.source.getId(), | |
"title": e.source.getTitle() ? e.source.getTitle() : "Untitled Form", | |
"is_private": e.source.requiresLogin(), | |
"is_published": e.source.isAcceptingResponses(), | |
}, | |
"response": { | |
"id": e.response.getId(), |