wallace 0.0.7 → 0.1.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.
- package/lib/component.js +54 -15
- package/lib/index.js +13 -16
- package/lib/initCalls.js +44 -52
- package/lib/{pool.js → repeaters.js} +38 -43
- package/lib/types.d.ts +1635 -26
- package/lib/utils.js +30 -32
- package/package.json +4 -4
- package/lib/lookup.js +0 -62
package/lib/utils.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creates and mounts a component onto an element.
|
|
3
3
|
*
|
|
4
|
-
* @param {
|
|
5
|
-
* @param {
|
|
4
|
+
* @param {any} elementOrId Either a string representing an id, or an element.
|
|
5
|
+
* @param {callable} componentDefinition 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,
|
|
10
|
-
const component =
|
|
8
|
+
export function mount(elementOrId, componentDefinition, props, ctrl) {
|
|
9
|
+
const component = buildComponent(componentDefinition);
|
|
10
|
+
component.render(props, ctrl);
|
|
11
11
|
replaceNode(getElement(elementOrId), component.el);
|
|
12
12
|
return component;
|
|
13
13
|
}
|
|
@@ -23,38 +23,25 @@ export function getElement(elementOrId) {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* @param {class} cls The class of Component to create
|
|
29
|
-
* @param {object} parent The parent component (optional)
|
|
30
|
-
* @param {object} props The props to pass to the component (optional)
|
|
31
|
-
*/
|
|
32
|
-
export function createComponent(cls, parent, props) {
|
|
33
|
-
const component = buildComponent(cls, parent);
|
|
34
|
-
component.render(props);
|
|
35
|
-
return component;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Builds a component.
|
|
26
|
+
* Builds a component's initial DOM.
|
|
40
27
|
*/
|
|
41
|
-
export function buildComponent(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
28
|
+
export function buildComponent(componentDefinition) {
|
|
29
|
+
// TODO: add a dev warning here:
|
|
30
|
+
// if "componentDefinition is not a constructor" then we're probably missing a stub.
|
|
31
|
+
const component = new componentDefinition();
|
|
32
|
+
const proto = componentDefinition.prototype;
|
|
33
|
+
const dom = proto._n.cloneNode(true);
|
|
45
34
|
component.el = dom;
|
|
46
|
-
|
|
35
|
+
proto._b(component, dom);
|
|
47
36
|
return component;
|
|
48
37
|
}
|
|
49
38
|
|
|
50
39
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* @param {*} target - Any object, including arrays.
|
|
54
|
-
* @param {*} component - A component.
|
|
55
|
-
* @returns a Proxy object.
|
|
40
|
+
* See types for docs. Set grace to 0 for testing.
|
|
56
41
|
*/
|
|
57
|
-
export
|
|
42
|
+
export function watch(target, callback, grace) {
|
|
43
|
+
let active = false;
|
|
44
|
+
if (grace === undefined) grace = 100;
|
|
58
45
|
const handler = {
|
|
59
46
|
get(target, key) {
|
|
60
47
|
if (key == "isProxy") return true;
|
|
@@ -65,11 +52,22 @@ export const createProxy = (target, component) => {
|
|
|
65
52
|
target[key] = new Proxy(prop, handler);
|
|
66
53
|
return target[key];
|
|
67
54
|
},
|
|
55
|
+
|
|
68
56
|
set(target, key, value) {
|
|
69
57
|
target[key] = value;
|
|
70
|
-
|
|
58
|
+
if (grace) {
|
|
59
|
+
if (!active) {
|
|
60
|
+
setTimeout(() => {
|
|
61
|
+
active = false;
|
|
62
|
+
}, grace);
|
|
63
|
+
active = true;
|
|
64
|
+
callback();
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
callback();
|
|
68
|
+
}
|
|
71
69
|
return true;
|
|
72
70
|
},
|
|
73
71
|
};
|
|
74
72
|
return new Proxy(target, handler);
|
|
75
|
-
}
|
|
73
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wallace",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"author": "Andrew Buchan",
|
|
5
|
-
"description": "The framework that brings you
|
|
5
|
+
"description": "The framework that brings you FREEDOM!!",
|
|
6
6
|
"license": "ISC",
|
|
7
7
|
"main": "lib/index.js",
|
|
8
8
|
"files": [
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"test": "jest --clearCache && jest"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"babel-plugin-wallace": "^0.0
|
|
16
|
+
"babel-plugin-wallace": "^0.1.0",
|
|
17
17
|
"browserify": "^17.0.1"
|
|
18
18
|
},
|
|
19
|
-
"gitHead": "
|
|
19
|
+
"gitHead": "950a6c9e6a849afa97e3221a81887fb0267405f2"
|
|
20
20
|
}
|
package/lib/lookup.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
const ALWAYS_UPDATE = "__";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Used internally.
|
|
5
|
-
* An object which pools the results of lookup queries so we don't have to
|
|
6
|
-
* repeat them in the same component.
|
|
7
|
-
* The Lookup instance will be shared between instances of a component.
|
|
8
|
-
* Must call reset() on every update.
|
|
9
|
-
*/
|
|
10
|
-
export function Lookup(callbacks) {
|
|
11
|
-
this.callbacks = callbacks;
|
|
12
|
-
this.run = {};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
Lookup.prototype = {
|
|
16
|
-
/**
|
|
17
|
-
* Lookup a watched value during update. Returns an object with {o, n, c}
|
|
18
|
-
* (oldValue, newValue, changed).
|
|
19
|
-
* You must resetLookups before calling get during an update.
|
|
20
|
-
* The point is to pool the result so it doesn't have to be repeated.
|
|
21
|
-
*/
|
|
22
|
-
get: function (component, props, key) {
|
|
23
|
-
const run = this.run;
|
|
24
|
-
if (run[key] === undefined) {
|
|
25
|
-
// Verbose but efficient way as it avoids lookups?
|
|
26
|
-
// Or is this harmful to performance because we're just reading values more than calling functions?
|
|
27
|
-
let o = component._p[key];
|
|
28
|
-
// TODO: is this checking for watchOnce?
|
|
29
|
-
o = o === undefined ? "" : o;
|
|
30
|
-
const n = this.callbacks[key](props, component);
|
|
31
|
-
const c = n !== o;
|
|
32
|
-
component._p[key] = n;
|
|
33
|
-
const rtn = { n, o, c };
|
|
34
|
-
run[key] = rtn;
|
|
35
|
-
return rtn;
|
|
36
|
-
}
|
|
37
|
-
return run[key];
|
|
38
|
-
},
|
|
39
|
-
reset: function () {
|
|
40
|
-
this.run = {};
|
|
41
|
-
},
|
|
42
|
-
applyCallbacks: function (component, props, element, callbacks) {
|
|
43
|
-
for (let key in callbacks) {
|
|
44
|
-
let callback = callbacks[key];
|
|
45
|
-
if (key === ALWAYS_UPDATE) {
|
|
46
|
-
callback.call(component, element, component.props, component);
|
|
47
|
-
} else {
|
|
48
|
-
const result = this.get(component, props, key);
|
|
49
|
-
if (result.c) {
|
|
50
|
-
callback.call(
|
|
51
|
-
component,
|
|
52
|
-
result.n,
|
|
53
|
-
result.o,
|
|
54
|
-
element,
|
|
55
|
-
props,
|
|
56
|
-
component,
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
};
|