Skip to content

Commit

Permalink
fix(utils): Adds skipSubscribe option to proxyWithHistory (#227)
Browse files Browse the repository at this point in the history
* Adds autosave option to proxyWithHistory

* Update src/utils/proxyWithHistory.ts

Co-authored-by: Daishi Kato <[email protected]>

* Update src/utils/proxyWithHistory.ts

Co-authored-by: Daishi Kato <[email protected]>

* Update src/utils/proxyWithHistory.ts

Co-authored-by: Daishi Kato <[email protected]>

* Prettier

Co-authored-by: Daishi Kato <[email protected]>
  • Loading branch information
promontis and dai-shi authored Aug 29, 2021
1 parent 4819960 commit 2fb2847
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/utils/proxyWithHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { proxy, ref, snapshot, subscribe } from '../vanilla'
* count: 1,
* })
*/
export const proxyWithHistory = <V>(initialValue: V) => {
export const proxyWithHistory = <V>(initialValue: V, skipSubscribe = false) => {
const proxyObject = proxy({
value: initialValue,
history: ref({
Expand Down Expand Up @@ -59,18 +59,22 @@ export const proxyWithHistory = <V>(initialValue: V) => {
proxyObject.history.snapshots.push(snapshot(proxyObject).value as V)
++proxyObject.history.index
},
subscribe: () =>
subscribe(proxyObject, (ops) => {
if (
ops.some(
(op) =>
op[1][0] === 'value' &&
(op[0] !== 'set' || op[2] !== proxyObject.history.wip)
)
) {
proxyObject.saveHistory()
}
}),
})
proxyObject.saveHistory()
subscribe(proxyObject, (ops) => {
if (
ops.some(
(op) =>
op[1][0] === 'value' &&
(op[0] !== 'set' || op[2] !== proxyObject.history.wip)
)
) {
proxyObject.saveHistory()
}
})
if (!skipSubscribe) {
proxyObject.subscribe()
}
return proxyObject
}

0 comments on commit 2fb2847

Please sign in to comment.