-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[restructure] Simplify and flatten caching
This is some of the precursor work to the new API: 1. Simplify the cell cache in general 2. Wrap columns and rows in proxies that add `meta` properties 3. Yield `value cell column row` as described in the new API 4. Have CollapseTree return the list of parents, which allows us to correctly figure out selection state in tree tables (more work and tests on that coming in future PRs) 5. Create a "row wrapper" component. This is necessary for wrapping the rows in proxy objects, since it should be done _after_ VC has done its work in order to remain stable, but before we yield to the `tr` because we don't want to complicate the `tr` logic (its been a struggling point on the refactor branch)
- Loading branch information
Showing
22 changed files
with
270 additions
and
209 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import ObjectProxy from '@ember/object/proxy'; | ||
import EmberObject from '@ember/object'; | ||
|
||
const META_CACHE_MAP = new WeakMap(); | ||
|
||
export function metaCacheFor(obj, Class) { | ||
if (META_CACHE_MAP.has(obj) === false) { | ||
META_CACHE_MAP.set(obj, Class.create ? Class.create() : new Class()); | ||
} | ||
|
||
return META_CACHE_MAP.get(obj); | ||
} | ||
|
||
export default class MetaProxy extends ObjectProxy { | ||
meta = metaCacheFor(this.content, EmberObject); | ||
} |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import Component from '@ember/component'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
import EmberObject, { get, set } from '@ember/object'; | ||
import { A as emberA } from '@ember/array'; | ||
import ObjectProxy from '@ember/object/proxy'; | ||
|
||
import { tagName } from '@ember-decorators/component'; | ||
import { argument } from '@ember-decorators/argument'; | ||
|
||
import { computed } from '@ember-decorators/object'; | ||
import { readOnly } from '@ember-decorators/object/computed'; | ||
|
||
import { metaCacheFor } from '../../-private/meta-cache'; | ||
import CellProxy from '../../utils/cell-proxy'; | ||
|
||
class TableRowMeta extends EmberObject { | ||
@readOnly('_parents.length') depth; | ||
|
||
@computed('_value', '_selectedRows.[]', '_parents.[]') | ||
get isSelected() { | ||
let selectedRows = get(this, '_selectedRows'); | ||
return selectedRows.includes(this.get('_value')) || this.get('_parents').some((row) => selectedRows.includes(row)); | ||
} | ||
} | ||
|
||
class TableRowProxy extends ObjectProxy { | ||
@computed('content') | ||
get meta() { | ||
return metaCacheFor(this.get('content'), TableRowMeta); | ||
} | ||
} | ||
|
||
@tagName('') | ||
export default class RowWrapper extends Component { | ||
layout = hbs` | ||
{{yield proxy cells}} | ||
`; | ||
|
||
@argument row; | ||
|
||
@argument rowIndex; | ||
|
||
@argument selectedRows; | ||
|
||
@argument columns; | ||
|
||
_cells = emberA(); | ||
_proxy = TableRowProxy.create(); | ||
|
||
@computed('row', 'selectedRows.[]') | ||
get proxy() { | ||
let proxy = this._proxy; | ||
|
||
let { value: rowValue, parents, isCollapsed } = this.get('row'); | ||
let rowIndex = this.get('rowIndex'); | ||
|
||
proxy.set('content', rowValue); | ||
|
||
let meta = proxy.get('meta'); | ||
|
||
set(meta, '_selectedRows', this.get('selectedRows')); | ||
set(meta, '_value', rowValue); | ||
set(meta, '_parents', parents); | ||
|
||
set(meta, 'index', rowIndex); | ||
set(meta, 'isCollapsed', isCollapsed); | ||
|
||
return proxy; | ||
} | ||
|
||
@computed('row.value', 'columns.[]') | ||
get cells() { | ||
let row = this.get('proxy'); | ||
let columns = this.get('columns'); | ||
let numColumns = get(columns, 'length'); | ||
|
||
let { _cells } = this; | ||
|
||
if (numColumns !== _cells.length) { | ||
while (_cells.length < numColumns) { | ||
_cells.push(CellProxy.create()); | ||
} | ||
|
||
while (_cells.length > numColumns) { | ||
_cells.pop(); | ||
} | ||
} | ||
|
||
return columns.map((column, i) => { | ||
let valuePath = get(column, 'valuePath'); | ||
|
||
let cell = _cells[i]; | ||
set(cell, 'row', row); | ||
set(cell, 'valuePath', valuePath); | ||
|
||
return { | ||
value: get(row, valuePath), | ||
cellValue: cell, | ||
columnValue: column, | ||
rowValue: row | ||
}; | ||
}); | ||
} | ||
} |
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
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
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
Oops, something went wrong.