wallace 0.14.1 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,77 +2,103 @@ import { countOffset } from "../offsetter";
2
2
 
3
3
  /**
4
4
  * Repeats nested components, yielding from the pool sequentially.
5
- *
6
- * COMPILER_MODS:
7
- * if allowRepeaterSiblings is false the last two parameters are removed.
8
5
  */
9
- export function SequentialRepeater(componentDefinition, adjustmentTracker, initialIndex) {
6
+ export function SequentialRepeater(
7
+ componentDefinition,
8
+ /* #INCLUDE-IF: allowRepeaterSiblings */ adjustmentTracker,
9
+ /* #INCLUDE-IF: allowRepeaterSiblings */ initialIndex
10
+ ) {
10
11
  this.d = componentDefinition;
12
+ /* #INCLUDE-IF: allowDismount */ this.s = componentDefinition.pool;
13
+ this.p = [];
11
14
  this.c = 0; // Child count
12
- if (wallaceConfig.flags.allowRepeaterSiblings) {
13
- this.a = adjustmentTracker;
14
- this.i = initialIndex;
15
- }
15
+ /* #INCLUDE-IF: allowRepeaterSiblings */ this.a = adjustmentTracker;
16
+ /* #INCLUDE-IF: allowRepeaterSiblings */ this.i = initialIndex;
16
17
  }
17
18
 
18
19
  /**
19
20
  * Updates the element's childNodes to match the items.
20
21
  * Performance is important.
21
- *
22
- * @param {DOMElement} parent - The DOM element to patch.
23
- * @param {Array} items - Array of items which will be passed as props.
24
- * @param {Array} pool - pool of component instances.
25
- * @param {any} ctrl - The parent item's controller.
26
22
  */
27
- SequentialRepeater.prototype.patch = function (parent, items, pool, ctrl) {
28
- const componentDefinition = this.d,
29
- previousChildCount = this.c,
30
- itemsLength = items.length,
31
- childNodes = parent.childNodes;
23
+ SequentialRepeater.prototype = {
24
+ patch: function (parent, items, /* #INCLUDE-IF: allowCtrl */ ctrl) {
25
+ const componentDefinition = this.d,
26
+ /* #INCLUDE-IF: allowDismount */ sharedPool = this.s,
27
+ previousChildCount = this.c,
28
+ pool = this.p,
29
+ itemsLength = items.length,
30
+ childNodes = parent.childNodes;
32
31
 
33
- let i = 0,
34
- offset = 0,
35
- component,
36
- nextElement,
37
- initialIndex,
38
- offsetTracker,
39
- endOfRange = previousChildCount,
40
- poolCount = pool.length;
32
+ let i = 0,
33
+ offset = 0,
34
+ component,
35
+ nextElement,
36
+ initialIndex,
37
+ offsetTracker,
38
+ endOfRange = previousChildCount,
39
+ poolCount = pool.length,
40
+ originalPoolCount = poolCount;
41
41
 
42
- if (wallaceConfig.flags.allowRepeaterSiblings) {
43
- initialIndex = this.i;
44
- offsetTracker = this.a;
45
- if (offsetTracker) {
46
- // The repeat element has siblings
47
- offset = countOffset(offsetTracker, initialIndex);
48
- endOfRange += offset;
49
- nextElement = childNodes[endOfRange] || null;
42
+ if (wallaceConfig.flags.allowRepeaterSiblings) {
43
+ initialIndex = this.i;
44
+ offsetTracker = this.a;
45
+ if (offsetTracker) {
46
+ // The repeat element has siblings
47
+ offset = countOffset(offsetTracker, initialIndex);
48
+ endOfRange += offset;
49
+ nextElement = childNodes[endOfRange] || null;
50
+ }
50
51
  }
51
- }
52
- while (i < itemsLength) {
53
- if (i < poolCount) {
54
- component = pool[i];
55
- } else {
56
- component = new componentDefinition();
57
- pool.push(component);
58
- poolCount++;
52
+ while (i < itemsLength) {
53
+ if (i < poolCount) {
54
+ component = pool[i];
55
+ } else {
56
+ if (wallaceConfig.flags.allowDismount) {
57
+ component = sharedPool.pop() || new componentDefinition();
58
+ } else {
59
+ component = new componentDefinition();
60
+ }
61
+ pool.push(component);
62
+ poolCount++;
63
+ }
64
+ component.render(items[i], /* #INCLUDE-IF: allowCtrl */ ctrl);
65
+ if (i >= previousChildCount) {
66
+ parent.insertBefore(component.el, nextElement);
67
+ }
68
+ i++;
59
69
  }
60
- component.render(items[i], ctrl);
61
- if (i >= previousChildCount) {
62
- parent.insertBefore(component.el, nextElement);
70
+ this.c = itemsLength;
71
+
72
+ let removeAtIndex = offset + previousChildCount - 1;
73
+ const stopAtIndex = offset + itemsLength - 1;
74
+ for (let i = removeAtIndex; i > stopAtIndex; i--) {
75
+ parent.removeChild(childNodes[i]);
63
76
  }
64
- i++;
65
- }
66
- this.c = itemsLength;
67
77
 
68
- let removeAtIndex = offset + previousChildCount - 1;
69
- const stopatIndex = offset + itemsLength - 1;
70
- for (let i = removeAtIndex; i > stopatIndex; i--) {
71
- parent.removeChild(childNodes[i]);
72
- }
73
- if (wallaceConfig.flags.allowRepeaterSiblings) {
78
+ /* #INCLUDE-IF: allowRepeaterSiblings */
74
79
  if (offsetTracker) {
75
80
  offsetTracker.set(initialIndex, itemsLength - 1);
76
81
  }
82
+
83
+ /* #INCLUDE-IF: allowDismount */
84
+ while (originalPoolCount > itemsLength) {
85
+ component = pool.pop();
86
+ sharedPool.push(component);
87
+ component.dismount();
88
+ originalPoolCount--;
89
+ }
90
+ },
91
+ /* #INCLUDE-IF: allowDismount */ dismount: function () {
92
+ let component,
93
+ pool = this.p,
94
+ poolCount = pool.length,
95
+ sharedPool = this.s;
96
+ while (poolCount > 0) {
97
+ component = pool.pop();
98
+ sharedPool.push(component);
99
+ component.dismount();
100
+ poolCount--;
101
+ }
102
+ pool.length = 0;
77
103
  }
78
104
  };
package/lib/router.js CHANGED
@@ -22,8 +22,8 @@ export const route = (path, componentDef, converter, cleanup) =>
22
22
 
23
23
  export const Router = () => <div></div>;
24
24
 
25
- Router.methods = {
26
- render(props, ctrl) {
25
+ Object.assign(Router.prototype, {
26
+ render(props, /* #INCLUDE-IF: allowCtrl */ ctrl) {
27
27
  const defaultError = (error, router) => (router.el.innerHTML = error.message);
28
28
  this.error = props.error || defaultError;
29
29
  this.current = null;
@@ -33,7 +33,7 @@ Router.methods = {
33
33
  this.el.setAttribute(k, props.atts[k]);
34
34
  });
35
35
  }
36
- this.base.render.call(this, props, ctrl);
36
+ this.base.render.call(this, props, /* #INCLUDE-IF: allowCtrl */ ctrl);
37
37
  },
38
38
  async onHashChange() {
39
39
  const path = location.hash.slice(1) || "",
@@ -45,7 +45,10 @@ Router.methods = {
45
45
  while (i < len) {
46
46
  let route = routes[i];
47
47
  if ((routeData = route.match(path))) {
48
- const component = await route.getComponent(routeData, this.ctrl);
48
+ const component = await route.getComponent(
49
+ routeData,
50
+ /* #INCLUDE-IF: allowCtrl */ this.ctrl
51
+ );
49
52
  this.current && this.current.cleanup();
50
53
  this.mount(component);
51
54
  this.current = route;
@@ -62,7 +65,7 @@ Router.methods = {
62
65
  this.el.textContent = "";
63
66
  this.el.appendChild(component.el);
64
67
  }
65
- };
68
+ });
66
69
 
67
70
  export function Route(path, def, convert, cleanup) {
68
71
  this.chunks = path
@@ -96,12 +99,12 @@ Route.prototype = {
96
99
  }
97
100
  return { args, params: new URLSearchParams(query), url };
98
101
  },
99
- async getComponent(routeData, ctrl) {
102
+ async getComponent(routeData, /* #INCLUDE-IF: allowCtrl */ ctrl) {
100
103
  if (!this.component) {
101
104
  this.component = new this.def();
102
105
  }
103
106
  const props = await this._convert(routeData);
104
- this.component.render(props, ctrl);
107
+ this.component.render(props, /* #INCLUDE-IF: allowCtrl */ ctrl);
105
108
  return this.component;
106
109
  },
107
110
  /**
package/lib/stubs.js CHANGED
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * Gets a stub by name.
3
3
  */
4
- export const getStub = (component, name) => component.constructor.stubs[name];
4
+ export const getStub = (component, name) => component.constructor.stub[name];