wallace 0.0.5 → 0.0.6

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,5 +1,3 @@
1
- const noop = function () {};
2
-
3
1
  /**
4
2
  * The base component.
5
3
  */
@@ -16,9 +14,6 @@ export function Component(parent) {
16
14
 
17
15
  var proto = Component.prototype;
18
16
 
19
- proto.onUpdate = noop;
20
- proto.afterUpdate = noop;
21
-
22
17
  Object.defineProperty(proto, "hidden", {
23
18
  set: function (value) {
24
19
  this.el.hidden = value;
@@ -28,24 +23,16 @@ Object.defineProperty(proto, "hidden", {
28
23
  /**
29
24
  * Sets the props and updates the component.
30
25
  */
31
- proto.setProps = function (props) {
26
+ proto.render = function (props) {
32
27
  this.props = props;
33
28
  this.update();
34
29
  };
35
30
 
36
31
  /**
37
- * Updates the component.
38
- */
39
- proto.update = function () {
40
- this.onUpdate();
41
- this.updateSelf();
42
- this.afterUpdate();
43
- };
44
-
45
- /**
32
+ * Updates the DOM.
46
33
  * Loops over watches, skipping n watches if elements are hidden.
47
34
  */
48
- proto.updateSelf = function () {
35
+ proto.update = function () {
49
36
  let i = 0,
50
37
  watch,
51
38
  element,
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createComponent, mount } from "./utils";
1
+ import { createComponent, createProxy, mount } from "./utils";
2
2
  import { Component } from "./component";
3
3
  import { KeyedPool, SequentialPool } from "./pool";
4
4
  import {
@@ -18,6 +18,7 @@ export {
18
18
  extendComponent,
19
19
  Component,
20
20
  createComponent,
21
+ createProxy,
21
22
  defineComponent,
22
23
  extendPrototype,
23
24
  findElement,
package/lib/lookup.js CHANGED
@@ -1,4 +1,5 @@
1
- const ALWAYS_UPDATE = "*";
1
+ const ALWAYS_UPDATE = "__";
2
+
2
3
  /**
3
4
  * Used internally.
4
5
  * An object which pools the results of lookup queries so we don't have to
package/lib/pool.js CHANGED
@@ -7,7 +7,7 @@ const getComponent = (pool, componentClass, key, item, parent) => {
7
7
  let component;
8
8
  if (pool.hasOwnProperty(key)) {
9
9
  component = pool[key];
10
- component.setProps(item);
10
+ component.render(item);
11
11
  } else {
12
12
  component = createComponent(componentClass, parent, item);
13
13
  pool[key] = component;
@@ -140,7 +140,7 @@ SequentialPool.prototype.patch = function (e, items, parent) {
140
140
  item = items[i];
141
141
  if (i < poolCount) {
142
142
  component = pool[i];
143
- component.setProps(item);
143
+ component.render(item);
144
144
  } else {
145
145
  component = createComponent(componentClass, parent, item);
146
146
  pool.push(component);
package/lib/types.d.ts CHANGED
@@ -16,9 +16,9 @@ declare module "wallace" {
16
16
 
17
17
  export type Accepts<Type> = ComponentFunction<Type>;
18
18
 
19
- export interface Component {
19
+ export interface Component<T> {
20
20
  update(): void;
21
- setProps(props: any): void;
21
+ render(props: T): void;
22
22
  el: HTMLElement;
23
23
  }
24
24
 
@@ -27,7 +27,9 @@ declare module "wallace" {
27
27
  element: string | HTMLElement,
28
28
  component: Accepts<T>,
29
29
  props?: T,
30
- ): Component;
30
+ ): Component<T>;
31
+
32
+ export function createProxy<T>(obj: T, component: Component<T>): T;
31
33
 
32
34
  export function extendPrototype<T>(
33
35
  base: Accepts<T>,
package/lib/utils.js CHANGED
@@ -31,8 +31,7 @@ export function getElement(elementOrId) {
31
31
  */
32
32
  export function createComponent(cls, parent, props) {
33
33
  const component = buildComponent(cls, parent);
34
- component.props = props;
35
- component.update();
34
+ component.render(props);
36
35
  return component;
37
36
  }
38
37
 
@@ -47,3 +46,30 @@ export function buildComponent(cls, parent) {
47
46
  component._b(component, dom);
48
47
  return component;
49
48
  }
49
+
50
+ /**
51
+ * Wraps target in a Proxy which calls component.update() whenever it is modified.
52
+ *
53
+ * @param {*} target - Any object, including arrays.
54
+ * @param {*} component - A component.
55
+ * @returns a Proxy object.
56
+ */
57
+ export const createProxy = (target, component) => {
58
+ const handler = {
59
+ get(target, key) {
60
+ if (key == "isProxy") return true;
61
+ const prop = target[key];
62
+ if (typeof prop == "undefined") return;
63
+ // set value as proxy if object
64
+ if (!prop.isProxy && typeof prop === "object")
65
+ target[key] = new Proxy(prop, handler);
66
+ return target[key];
67
+ },
68
+ set(target, key, value) {
69
+ target[key] = value;
70
+ component.update();
71
+ return true;
72
+ },
73
+ };
74
+ return new Proxy(target, handler);
75
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wallace",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "author": "Andrew Buchan",
5
5
  "description": "The framework that brings you glee.",
6
6
  "license": "ISC",
@@ -10,13 +10,11 @@
10
10
  ],
11
11
  "types": "./lib/types.d.ts",
12
12
  "scripts": {
13
- "test": "jest --clearCache && jest",
14
- "check": "babel $@",
15
- "rebuild-plugin": "cd ../babel-plugin-wallace && npx tsc"
13
+ "test": "jest --clearCache && jest"
16
14
  },
17
15
  "dependencies": {
18
- "babel-plugin-wallace": "^0.0.5",
16
+ "babel-plugin-wallace": "^0.0.6",
19
17
  "browserify": "^17.0.1"
20
18
  },
21
- "gitHead": "3acf6a601862c29c011f5745322d0c3853561d75"
19
+ "gitHead": "3742fbcfdc21146c90252c2436fde91634fafa9e"
22
20
  }