Store object props in global state experiment

This commit is contained in:
josdejong 2020-06-02 14:48:01 +02:00
parent 55ee6361d6
commit 41c633e124
3 changed files with 33 additions and 7 deletions

View File

@ -1,10 +1,10 @@
<script> <script>
import { tick } from 'svelte' import { tick, beforeUpdate, afterUpdate } from 'svelte'
import { import {
DEFAULT_LIMIT, DEFAULT_LIMIT,
STATE_EXPANDED, STATE_EXPANDED,
STATE_LIMIT, STATE_LIMIT,
SCROLL_DURATION SCROLL_DURATION, STATE_PROPS
} from './constants.js' } from './constants.js'
import SearchBox from './SearchBox.svelte' import SearchBox from './SearchBox.svelte'
import Icon from 'svelte-awesome' import Icon from 'svelte-awesome'
@ -21,6 +21,13 @@
let divContents let divContents
beforeUpdate(() => {
console.time('render')
})
afterUpdate(() => {
console.timeEnd('render')
})
export let json = {} export let json = {}
export let onChangeJson = () => { export let onChangeJson = () => {
} }
@ -208,6 +215,15 @@
state = setIn(state, path.concat(STATE_LIMIT), limit) state = setIn(state, path.concat(STATE_LIMIT), limit)
} }
/**
* Update object properties
* @param {Path} path
* @param {Object} props
*/
function handleUpdateProps (path, props) {
state = setIn(state, path.concat(STATE_PROPS), props)
}
/** /**
* Expand all nodes on given path * Expand all nodes on given path
* @param {Path} path * @param {Path} path
@ -325,6 +341,7 @@
onPatch={handlePatch} onPatch={handlePatch}
onExpand={handleExpand} onExpand={handleExpand}
onLimit={handleLimit} onLimit={handleLimit}
onUpdateProps={handleUpdateProps}
/> />
<div class='bottom'></div> <div class='bottom'></div>
</div> </div>

View File

@ -1,7 +1,9 @@
<script> <script>
import { isObject } from 'lodash-es'
import { onMount } from 'svelte'
import { import {
DEBOUNCE_DELAY, DEFAULT_LIMIT, DEBOUNCE_DELAY, DEFAULT_LIMIT,
STATE_EXPANDED, STATE_LIMIT, STATE_EXPANDED, STATE_LIMIT, STATE_PROPS,
STATE_SEARCH_PROPERTY, STATE_SEARCH_PROPERTY,
STATE_SEARCH_VALUE STATE_SEARCH_VALUE
} from './constants.js' } from './constants.js'
@ -24,9 +26,11 @@
export let onChangeKey export let onChangeKey
export let onExpand export let onExpand
export let onLimit export let onLimit
export let onUpdateProps
$: expanded = state && state[STATE_EXPANDED] $: expanded = state && state[STATE_EXPANDED]
$: limit = state && state[STATE_LIMIT] || DEFAULT_LIMIT $: limit = state && state[STATE_LIMIT] || DEFAULT_LIMIT
$: props = state && state[STATE_PROPS] || []
const escapeUnicode = false // TODO: pass via options const escapeUnicode = false // TODO: pass via options
@ -36,14 +40,16 @@
$: type = valueType (value) $: type = valueType (value)
let prevValue = undefined let prevValue = undefined
let props = undefined // let props = undefined
$: if (value !== prevValue) { $: if (isObject(value) && value !== prevValue) {
prevValue = value prevValue = value
const updatedProps = updateProps(value, props)
props = updateProps(value, props) onUpdateProps(path, updatedProps)
} }
// $: console.log('props', props)
$: limited = type === 'array' && value.length > limit $: limited = type === 'array' && value.length > limit
$: items = type === 'array' $: items = type === 'array'
@ -254,6 +260,7 @@
onPatch={onPatch} onPatch={onPatch}
onExpand={onExpand} onExpand={onExpand}
onLimit={onLimit} onLimit={onLimit}
onUpdateProps={onUpdateProps}
/> />
{/each} {/each}
{#if limited} {#if limited}
@ -308,6 +315,7 @@
onPatch={onPatch} onPatch={onPatch}
onExpand={onExpand} onExpand={onExpand}
onLimit={onLimit} onLimit={onLimit}
onUpdateProps={onUpdateProps}
/> />
{/each} {/each}
</div> </div>

View File

@ -1,6 +1,7 @@
export const STATE_EXPANDED = '$jse:expanded' export const STATE_EXPANDED = '$jse:expanded'
export const STATE_LIMIT = '$jse:limit' export const STATE_LIMIT = '$jse:limit'
export const STATE_PROPS = '$jse:props'
export const STATE_SEARCH_PROPERTY = '$jse:search:property' export const STATE_SEARCH_PROPERTY = '$jse:search:property'
export const STATE_SEARCH_VALUE = '$jse:search:value' export const STATE_SEARCH_VALUE = '$jse:search:value'