wallace 0.0.7 → 0.0.8

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.
package/lib/component.js CHANGED
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * The base component.
3
3
  */
4
- export function Component(parent) {
5
- this.parent = parent; // The parent component. TODO: is this needed?
4
+ export function Component() {
5
+ this.ctrl = undefined; // The controller.
6
6
  this.props = undefined; // The props passed to the component. May be changed.
7
7
  this.el = null; // the element - will be set during build.
8
8
  this.ref = {}; // user set references to elements or components.
@@ -21,10 +21,11 @@ Object.defineProperty(proto, "hidden", {
21
21
  });
22
22
 
23
23
  /**
24
- * Sets the props and updates the component.
24
+ * The render function that gets called by parent components.
25
25
  */
26
- proto.render = function (props) {
26
+ proto.render = function (props, ctrl) {
27
27
  this.props = props;
28
+ this.ctrl = ctrl;
28
29
  this.update();
29
30
  };
30
31
 
package/lib/index.js CHANGED
@@ -1,11 +1,11 @@
1
- import { createComponent, createProxy, mount } from "./utils";
1
+ import { createComponent, watch, mount } from "./utils";
2
2
  import { Component } from "./component";
3
- import { KeyedPool, SequentialPool } from "./pool";
3
+ import { KeyedRepeater, SequentialRepeater } from "./repeaters";
4
4
  import {
5
5
  extendComponent,
6
6
  findElement,
7
- getKeyedPool,
8
- getSequentialPool,
7
+ getKeyedRepeater,
8
+ getSequentialRepeater,
9
9
  onEvent,
10
10
  nestComponent,
11
11
  defineComponent,
@@ -18,17 +18,17 @@ export {
18
18
  extendComponent,
19
19
  Component,
20
20
  createComponent,
21
- createProxy,
21
+ watch,
22
22
  defineComponent,
23
23
  extendPrototype,
24
24
  findElement,
25
- getKeyedPool,
26
- getSequentialPool,
27
- KeyedPool,
25
+ getKeyedRepeater,
26
+ getSequentialRepeater,
27
+ KeyedRepeater,
28
28
  mount,
29
29
  nestComponent,
30
30
  onEvent,
31
31
  stashMisc,
32
32
  saveRef,
33
- SequentialPool,
33
+ SequentialRepeater,
34
34
  };
package/lib/initCalls.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Component } from "./component";
2
2
  import { Lookup } from "./lookup";
3
3
  import { buildComponent, replaceNode } from "./utils";
4
- import { KeyedPool, SequentialPool } from "./pool";
4
+ import { KeyedRepeater, SequentialRepeater } from "./repeaters";
5
5
  const throwAway = document.createElement("template");
6
6
 
7
7
  /**
@@ -20,9 +20,9 @@ export const findElement = (rootElement, path) => {
20
20
  return path.reduce((acc, index) => acc.childNodes[index], rootElement);
21
21
  };
22
22
 
23
- export const nestComponent = (rootElement, path, cls, parent) => {
23
+ export const nestComponent = (rootElement, path, cls) => {
24
24
  const el = findElement(rootElement, path),
25
- child = buildComponent(cls, parent);
25
+ child = buildComponent(cls);
26
26
  replaceNode(el, child.el);
27
27
  return child;
28
28
  };
@@ -49,15 +49,12 @@ export const onEvent = (element, eventName, callback) => {
49
49
  return element;
50
50
  };
51
51
 
52
- /**
53
- * Creates a pool.
54
- */
55
- export const getKeyedPool = (cls, keyFn) => {
56
- return new KeyedPool(cls, keyFn);
52
+ export const getKeyedRepeater = (cls, keyFn) => {
53
+ return new KeyedRepeater(cls, keyFn);
57
54
  };
58
55
 
59
- export const getSequentialPool = (cls) => {
60
- return new SequentialPool(cls);
56
+ export const getSequentialRepeater = (cls) => {
57
+ return new SequentialRepeater(cls);
61
58
  };
62
59
 
63
60
  export function defineComponent(
@@ -91,8 +88,8 @@ export function extendComponent(
91
88
  }
92
89
 
93
90
  export function extendPrototype(base, prototypeExtras) {
94
- const Constructor = function (parent) {
95
- base.call(this, parent);
91
+ const Constructor = function () {
92
+ base.call(this);
96
93
  };
97
94
  Constructor.prototype = Object.create(base && base.prototype, {
98
95
  constructor: {
@@ -3,17 +3,17 @@ import { createComponent } from "./utils";
3
3
  /*
4
4
  * Gets a component from the pool.
5
5
  */
6
- const getComponent = (pool, componentClass, key, item, parent) => {
6
+ function getComponent(pool, componentDefinition, ctrl, key, props) {
7
7
  let component;
8
8
  if (pool.hasOwnProperty(key)) {
9
9
  component = pool[key];
10
- component.render(item);
10
+ component.render(props, ctrl);
11
11
  } else {
12
- component = createComponent(componentClass, parent, item);
12
+ component = createComponent(componentDefinition, props, ctrl);
13
13
  pool[key] = component;
14
14
  }
15
15
  return component;
16
- };
16
+ }
17
17
 
18
18
  /**
19
19
  * Trims the unwanted child elements from the end.
@@ -44,29 +44,27 @@ function pull(arr, item, to) {
44
44
  }
45
45
 
46
46
  /**
47
- * Pools same type components, retrieving by key.
48
- * Must not be shared.
47
+ * Repeats nested components, reusing items based on key.
49
48
  *
50
- * @param {class} componentClass - The class of Component to create.
51
- * @param {function} keyFn - A function which obtains the key to pool by
49
+ * @param {function} componentDefinition - The ComponentDefinition to create.
50
+ * @param {function} keyFn - A function which obtains the key.
52
51
  */
53
- export function KeyedPool(componentClass, keyFn) {
54
- this._v = componentClass;
52
+ export function KeyedRepeater(componentDefinition, keyFn) {
53
+ this._v = componentDefinition;
55
54
  this._f = keyFn;
56
55
  this._k = []; // keys
57
56
  this._p = {}; // pool of component instances
58
57
  }
59
- const proto = KeyedPool.prototype;
58
+ const proto = KeyedRepeater.prototype;
60
59
 
61
60
  /**
62
61
  * Retrieves a single component. Though not used in wallace itself, it may
63
62
  * be used elsewhere, such as in the router.
64
63
  *
65
64
  * @param {Object} item - The item which will be passed as props.
66
- * @param {Component} parent - The parent component.
67
65
  */
68
- proto.getOne = function (item, parent) {
69
- return getComponent(this._p, this._v, this._f(item), item, parent);
66
+ proto.getOne = function (item, ctrl) {
67
+ return getComponent(this._p, this._v, ctrl, this._f(item), item);
70
68
  };
71
69
 
72
70
  /**
@@ -75,13 +73,12 @@ proto.getOne = function (item, parent) {
75
73
  *
76
74
  * @param {DOMElement} e - The DOM element to patch.
77
75
  * @param {Array} items - Array of items which will be passed as props.
78
- * @param {Component} parent - The parent component.
79
76
  */
80
- proto.patch = function (e, items, parent) {
77
+ proto.patch = function (e, items, ctrl) {
81
78
  // Attempt to speed up by reducing lookups. Does this even do anything?
82
79
  // Does webpack undo this/do it for for me? Does the engine?
83
80
  const pool = this._p;
84
- const componentClass = this._v;
81
+ const componentDefinition = this._v;
85
82
  const keyFn = this._f;
86
83
  const childNodes = e.childNodes;
87
84
  const itemsLength = items.length;
@@ -94,7 +91,7 @@ proto.patch = function (e, items, parent) {
94
91
  for (let i = 0; i < itemsLength; i++) {
95
92
  item = items[i];
96
93
  key = keyFn(item);
97
- component = getComponent(pool, componentClass, key, item, parent);
94
+ component = getComponent(pool, componentDefinition, ctrl, key, item);
98
95
  newKeys.push(key);
99
96
  if (i > childElementCount) {
100
97
  e.appendChild(component.el);
@@ -108,12 +105,12 @@ proto.patch = function (e, items, parent) {
108
105
  };
109
106
 
110
107
  /**
111
- * Pools same type components, retrieving by sequence.
108
+ * Repeats nested components, yielding from its pool sequentially.
112
109
  *
113
- * @param {class} componentClass - The class of Component to create.
110
+ * @param {componentDefinition} componentDefinition - The class ComponentDefinition to create.
114
111
  */
115
- export function SequentialPool(componentClass) {
116
- this._v = componentClass;
112
+ export function SequentialRepeater(componentDefinition) {
113
+ this._v = componentDefinition;
117
114
  this._p = []; // pool of component instances
118
115
  this._c = 0; // Child element count
119
116
  }
@@ -124,11 +121,11 @@ export function SequentialPool(componentClass) {
124
121
  *
125
122
  * @param {DOMElement} e - The DOM element to patch.
126
123
  * @param {Array} items - Array of items which will be passed as props.
127
- * @param {Component} parent - The parent component.
124
+ * @param {any} ctrl - The parent item's controller.
128
125
  */
129
- SequentialPool.prototype.patch = function (e, items, parent) {
126
+ SequentialRepeater.prototype.patch = function (e, items, ctrl) {
130
127
  const pool = this._p;
131
- const componentClass = this._v;
128
+ const componentDefinition = this._v;
132
129
  const childNodes = e.childNodes;
133
130
  const itemsLength = items.length;
134
131
  let item,
@@ -140,9 +137,9 @@ SequentialPool.prototype.patch = function (e, items, parent) {
140
137
  item = items[i];
141
138
  if (i < poolCount) {
142
139
  component = pool[i];
143
- component.render(item);
140
+ component.render(item, ctrl);
144
141
  } else {
145
- component = createComponent(componentClass, parent, item);
142
+ component = createComponent(componentDefinition, item, ctrl);
146
143
  pool.push(component);
147
144
  poolCount++;
148
145
  }
package/lib/types.d.ts CHANGED
@@ -27,9 +27,10 @@ declare module "wallace" {
27
27
  element: string | HTMLElement,
28
28
  component: Accepts<T>,
29
29
  props?: T,
30
+ ctrl?: any,
30
31
  ): Component<T>;
31
32
 
32
- export function createProxy<T>(obj: T, component: Component<T>): T;
33
+ export function watch<T>(obj: T, callback: CallableFunction): T;
33
34
 
34
35
  export function extendPrototype<T>(
35
36
  base: Accepts<T>,
package/lib/utils.js CHANGED
@@ -4,10 +4,9 @@
4
4
  * @param {unsure} elementOrId Either a string representing an id, or an element.
5
5
  * @param {class} cls The class of Component to create
6
6
  * @param {object} props The props to pass to the component (optional)
7
- * @param {object} parent The parent component (optional)
8
7
  */
9
- export function mount(elementOrId, cls, props, parent) {
10
- const component = createComponent(cls, parent, props);
8
+ export function mount(elementOrId, cls, props, ctrl) {
9
+ const component = createComponent(cls, props, ctrl);
11
10
  replaceNode(getElement(elementOrId), component.el);
12
11
  return component;
13
12
  }
@@ -26,35 +25,34 @@ export function getElement(elementOrId) {
26
25
  * Creates a component and initialises it.
27
26
  *
28
27
  * @param {class} cls The class of Component to create
29
- * @param {object} parent The parent component (optional)
30
28
  * @param {object} props The props to pass to the component (optional)
31
29
  */
32
- export function createComponent(cls, parent, props) {
33
- const component = buildComponent(cls, parent);
34
- component.render(props);
30
+ export function createComponent(cls, props, ctrl) {
31
+ const component = buildComponent(cls);
32
+ component.render(props, ctrl);
35
33
  return component;
36
34
  }
37
35
 
38
36
  /**
39
- * Builds a component.
37
+ * Builds a component's initial DOM.
40
38
  */
41
- export function buildComponent(cls, parent) {
42
- const component = new cls(parent);
43
- const prototype = cls.prototype;
44
- const dom = prototype._n.cloneNode(true);
39
+ export function buildComponent(cls) {
40
+ const component = new cls();
41
+ const proto = cls.prototype;
42
+ const dom = proto._n.cloneNode(true);
45
43
  component.el = dom;
46
- component._b(component, dom);
44
+ proto._b(component, dom);
47
45
  return component;
48
46
  }
49
47
 
50
48
  /**
51
- * Wraps target in a Proxy which calls component.update() whenever it is modified.
49
+ * Wraps target in a Proxy which calls a function whenever it is modified.
52
50
  *
53
51
  * @param {*} target - Any object, including arrays.
54
- * @param {*} component - A component.
55
- * @returns a Proxy object.
52
+ * @param {*} callback - A callback function.
53
+ * @returns a Proxy of the object.
56
54
  */
57
- export const createProxy = (target, component) => {
55
+ export function watch(target, callback) {
58
56
  const handler = {
59
57
  get(target, key) {
60
58
  if (key == "isProxy") return true;
@@ -67,9 +65,9 @@ export const createProxy = (target, component) => {
67
65
  },
68
66
  set(target, key, value) {
69
67
  target[key] = value;
70
- component.update();
68
+ callback();
71
69
  return true;
72
70
  },
73
71
  };
74
72
  return new Proxy(target, handler);
75
- };
73
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wallace",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "author": "Andrew Buchan",
5
5
  "description": "The framework that brings you glee.",
6
6
  "license": "ISC",
@@ -13,8 +13,8 @@
13
13
  "test": "jest --clearCache && jest"
14
14
  },
15
15
  "dependencies": {
16
- "babel-plugin-wallace": "^0.0.7",
16
+ "babel-plugin-wallace": "^0.0.8",
17
17
  "browserify": "^17.0.1"
18
18
  },
19
- "gitHead": "625563b34b61857f1180da42d4b1f7bc229440c9"
19
+ "gitHead": "7548afec467b0e4f6e433a401cca08acb3dc7bc7"
20
20
  }