wallace 0.0.5 → 0.0.7
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 +75 -34
- package/lib/index.js +4 -3
- package/lib/initCalls.js +7 -11
- package/lib/lookup.js +2 -1
- package/lib/pool.js +2 -2
- package/lib/types.d.ts +5 -3
- package/lib/utils.js +28 -2
- package/package.json +4 -6
package/lib/component.js
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
|
-
const noop = function () {};
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* The base component.
|
|
5
3
|
*/
|
|
6
4
|
export function Component(parent) {
|
|
7
|
-
this.parent = parent; // The parent component
|
|
5
|
+
this.parent = parent; // The parent component. TODO: is this needed?
|
|
8
6
|
this.props = undefined; // The props passed to the component. May be changed.
|
|
9
|
-
this.el = null; // the element - will be set during build
|
|
10
|
-
this.ref = {}; // user set references to elements or components
|
|
7
|
+
this.el = null; // the element - will be set during build.
|
|
8
|
+
this.ref = {}; // user set references to elements or components.
|
|
11
9
|
// Internal state objects
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
10
|
+
this._e = {}; // The dynamic elements in the DOM.
|
|
11
|
+
this._p = {}; // The previous values for watches to compare against.
|
|
12
|
+
this._s = []; // A stash for misc objects.
|
|
15
13
|
}
|
|
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,50 +23,96 @@ Object.defineProperty(proto, "hidden", {
|
|
|
28
23
|
/**
|
|
29
24
|
* Sets the props and updates the component.
|
|
30
25
|
*/
|
|
31
|
-
proto.
|
|
26
|
+
proto.render = function (props) {
|
|
32
27
|
this.props = props;
|
|
33
28
|
this.update();
|
|
34
29
|
};
|
|
35
30
|
|
|
36
31
|
/**
|
|
37
|
-
* Updates the
|
|
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.
|
|
35
|
+
proto.update = function () {
|
|
49
36
|
let i = 0,
|
|
50
37
|
watch,
|
|
51
38
|
element,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
39
|
+
parent,
|
|
40
|
+
displayToggle,
|
|
41
|
+
detacher,
|
|
42
|
+
lookupReturn,
|
|
43
|
+
lookupTrue,
|
|
44
|
+
shouldBeVisible,
|
|
45
|
+
visibilityChanged,
|
|
46
|
+
detachedElements,
|
|
47
|
+
detachedElement,
|
|
48
|
+
index,
|
|
49
|
+
adjustedIndex,
|
|
50
|
+
thisElement;
|
|
55
51
|
|
|
56
|
-
const watches = this.
|
|
52
|
+
const watches = this._w;
|
|
57
53
|
const lookup = this._l;
|
|
58
54
|
const props = this.props;
|
|
59
55
|
lookup.reset();
|
|
60
56
|
const il = watches.length;
|
|
57
|
+
/*
|
|
58
|
+
Watches is an array of objects with keys:
|
|
59
|
+
e: the element reference (string)
|
|
60
|
+
c: the callbacks (object)
|
|
61
|
+
?v: visibility toggle (object)
|
|
62
|
+
|
|
63
|
+
The display toggle has keys:
|
|
64
|
+
q: the query key in lookup
|
|
65
|
+
s: the number of watches to skip as their node is underneath
|
|
66
|
+
r: reversed
|
|
67
|
+
?d: detacher
|
|
68
|
+
|
|
69
|
+
The detacher has keys:
|
|
70
|
+
i: the initial element index
|
|
71
|
+
s: the stash key of the detacher (plain object)
|
|
72
|
+
e: the parent element key
|
|
73
|
+
*/
|
|
61
74
|
while (i < il) {
|
|
62
75
|
watch = watches[i];
|
|
63
|
-
element = this._e[watch.
|
|
64
|
-
|
|
76
|
+
element = this._e[watch.e];
|
|
77
|
+
displayToggle = watch.v;
|
|
65
78
|
i++;
|
|
66
79
|
shouldBeVisible = true;
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
80
|
+
if (displayToggle) {
|
|
81
|
+
lookupReturn = lookup.get(this, props, displayToggle.q);
|
|
82
|
+
lookupTrue = !!lookupReturn.n;
|
|
83
|
+
shouldBeVisible = displayToggle.r ? lookupTrue : !lookupTrue;
|
|
84
|
+
visibilityChanged = lookupTrue != !!lookupReturn.o;
|
|
85
|
+
detacher = displayToggle.d;
|
|
86
|
+
if (detacher) {
|
|
87
|
+
index = detacher.i;
|
|
88
|
+
parent = this._e[detacher.e];
|
|
89
|
+
detachedElements = this._s[detacher.s];
|
|
90
|
+
detachedElement = detachedElements[index];
|
|
91
|
+
if (shouldBeVisible && detachedElement) {
|
|
92
|
+
adjustedIndex =
|
|
93
|
+
index -
|
|
94
|
+
Object.keys(detachedElements).filter(function (k) {
|
|
95
|
+
return k < index && detachedElements[k];
|
|
96
|
+
}).length;
|
|
97
|
+
parent.insertBefore(
|
|
98
|
+
detachedElement,
|
|
99
|
+
parent.childNodes[adjustedIndex],
|
|
100
|
+
);
|
|
101
|
+
detachedElements[index] = null;
|
|
102
|
+
} else if (!shouldBeVisible && !detachedElement) {
|
|
103
|
+
thisElement = this._e[watch.e];
|
|
104
|
+
parent.removeChild(thisElement);
|
|
105
|
+
detachedElements[index] = thisElement;
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
element.hidden = !shouldBeVisible;
|
|
109
|
+
}
|
|
110
|
+
if (!shouldBeVisible) {
|
|
111
|
+
i += displayToggle.s;
|
|
112
|
+
}
|
|
72
113
|
}
|
|
73
114
|
if (shouldBeVisible) {
|
|
74
|
-
lookup.applyCallbacks(this, props, element, watch.
|
|
115
|
+
lookup.applyCallbacks(this, props, element, watch.c);
|
|
75
116
|
}
|
|
76
117
|
}
|
|
77
118
|
};
|
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 {
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
nestComponent,
|
|
11
11
|
defineComponent,
|
|
12
12
|
extendPrototype,
|
|
13
|
-
|
|
13
|
+
stashMisc,
|
|
14
14
|
saveRef,
|
|
15
15
|
} from "./initCalls";
|
|
16
16
|
|
|
@@ -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,
|
|
@@ -27,7 +28,7 @@ export {
|
|
|
27
28
|
mount,
|
|
28
29
|
nestComponent,
|
|
29
30
|
onEvent,
|
|
30
|
-
|
|
31
|
+
stashMisc,
|
|
31
32
|
saveRef,
|
|
32
33
|
SequentialPool,
|
|
33
34
|
};
|
package/lib/initCalls.js
CHANGED
|
@@ -28,7 +28,7 @@ export const nestComponent = (rootElement, path, cls, parent) => {
|
|
|
28
28
|
};
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
* Saves a reference to element or nested component.
|
|
31
|
+
* Saves a reference to element or nested component. Returns the element.
|
|
32
32
|
*/
|
|
33
33
|
export const saveRef = (element, component, name) => {
|
|
34
34
|
component.ref[name] = element;
|
|
@@ -36,10 +36,11 @@ export const saveRef = (element, component, name) => {
|
|
|
36
36
|
};
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
39
|
+
* Stash something on the component. Returns the element.
|
|
40
|
+
* The generated code is expected to keep track of the position.
|
|
40
41
|
*/
|
|
41
|
-
export const
|
|
42
|
-
component.
|
|
42
|
+
export const stashMisc = (element, component, object) => {
|
|
43
|
+
component._s.push(object);
|
|
43
44
|
return element;
|
|
44
45
|
};
|
|
45
46
|
|
|
@@ -82,13 +83,8 @@ export function extendComponent(
|
|
|
82
83
|
lookups,
|
|
83
84
|
buildFunction,
|
|
84
85
|
) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
sq: arr[1], // The shield query key
|
|
88
|
-
rv: arr[2], // whether shieldQuery should be flipped
|
|
89
|
-
sc: arr[3], // The number of items to shield
|
|
90
|
-
cb: arr[4], // The callbacks - object
|
|
91
|
-
}));
|
|
86
|
+
//Ensure these do not clash with fields on the component itself.
|
|
87
|
+
prototype._w = watches;
|
|
92
88
|
prototype._l = new Lookup(lookups);
|
|
93
89
|
prototype._b = buildFunction;
|
|
94
90
|
prototype._n = makeEl(html);
|
package/lib/lookup.js
CHANGED
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.
|
|
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.
|
|
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
|
-
|
|
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
|
|
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.
|
|
3
|
+
"version": "0.0.7",
|
|
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.
|
|
16
|
+
"babel-plugin-wallace": "^0.0.7",
|
|
19
17
|
"browserify": "^17.0.1"
|
|
20
18
|
},
|
|
21
|
-
"gitHead": "
|
|
19
|
+
"gitHead": "625563b34b61857f1180da42d4b1f7bc229440c9"
|
|
22
20
|
}
|