|
1 | 1 | import { patch } from 'superfine' |
2 | 2 |
|
| 3 | +/** |
| 4 | + * Motivation: |
| 5 | + * Saving data into classes (folder/file) is convenient. Displaying them, however, is a problem. |
| 6 | + * Using Aurelia or Vue would bring a massive overhead for the otherwise simple plugin. So my |
| 7 | + * goal was to sync data with the view as simple as possible, and virtual-dom based solutions |
| 8 | + * do a great job. However, there is a problem with state management. You can't use the instance |
| 9 | + * as a component (if you can please open an issue). But you can define generic component and |
| 10 | + * send the state as a prop. I wanted to avoid duplication of state. Managing state is difficult |
| 11 | + * as well because the structure is not flat. So updating such a structure with Immutable and Redux |
| 12 | + * seemed complicated. |
| 13 | + * |
| 14 | + * The solution is that everytime a change occurs, the whole new virtual dom is created, and changes |
| 15 | + * are reflected. This class saves entry points for every view and in case of change it does the job. |
| 16 | + */ |
| 17 | + |
3 | 18 | export class Views |
4 | 19 | { |
5 | 20 | private views = new Map<string, { element: HTMLElement, renderFn: () => any, lastNode: any }>(); |
6 | 21 |
|
7 | | - |
8 | | - applyView( name: string, renderFn: () => any, element: HTMLElement ) |
| 22 | + /** |
| 23 | + * Create DOM structure and append it to given element. This will create a "view". |
| 24 | + * |
| 25 | + * @param name - name of the "view" (so we can in future ask for redraw by name) |
| 26 | + * @param renderFn - function that generates virtual DOM |
| 27 | + * @param element - element, where "view" will be appended |
| 28 | + */ |
| 29 | + applyView( name: string, renderFn: () => any, element: HTMLElement ): void |
9 | 30 | { |
10 | 31 | this.views.set( name, { element, renderFn, lastNode: undefined } ); |
11 | 32 | this.redrawView( name ); |
12 | 33 | } |
13 | 34 |
|
14 | 35 |
|
15 | | - redrawView( name ) |
| 36 | + /** |
| 37 | + * This creates virtual dom reflecting current state, compares with last state and redraws |
| 38 | + * changes. |
| 39 | + * |
| 40 | + * @param name - name of "view" to redraw |
| 41 | + */ |
| 42 | + redrawView( name: string ): void |
16 | 43 | { |
17 | 44 | if ( this.views.has( name ) ) |
18 | 45 | { |
|
0 commit comments