@joist/observable 2.0.0-alpha.16 → 2.0.0-alpha.19
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@joist/observable",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.19",
|
|
4
4
|
"main": "./target/build/lib.js",
|
|
5
5
|
"module": "./target/build/lib.js",
|
|
6
6
|
"exports": {
|
|
@@ -12,15 +12,16 @@
|
|
|
12
12
|
"target/build"
|
|
13
13
|
],
|
|
14
14
|
"sideEffects": false,
|
|
15
|
-
"description": "
|
|
15
|
+
"description": "Monitor and respond to object changes",
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
18
18
|
"url": "git+https://github.com/deebloo/joist.git"
|
|
19
19
|
},
|
|
20
20
|
"keywords": [
|
|
21
21
|
"TypeScript",
|
|
22
|
-
"
|
|
23
|
-
"
|
|
22
|
+
"Observable",
|
|
23
|
+
"WebComponents",
|
|
24
|
+
"Reactive"
|
|
24
25
|
],
|
|
25
26
|
"author": "deebloo",
|
|
26
27
|
"license": "MIT",
|
|
@@ -34,5 +35,5 @@
|
|
|
34
35
|
"test": "tsc -p tsconfig.test.json && wtr --config ../../wtr.config.mjs --port 8002",
|
|
35
36
|
"build": "tsc -p tsconfig.build.json"
|
|
36
37
|
},
|
|
37
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "c83cae0c4733a4c5d79d52b683ec49001bd2b645"
|
|
38
39
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export interface AttributeParser<T> {
|
|
2
2
|
read(val: string): T;
|
|
3
3
|
write(val: T): string;
|
|
4
|
+
mapTo: string;
|
|
4
5
|
}
|
|
5
6
|
export declare type AttributeParsers = Record<string, AttributeParser<unknown>>;
|
|
6
|
-
export declare function defaultParser(): AttributeParser<boolean | string>;
|
|
7
|
+
export declare function defaultParser(mapTo: string): AttributeParser<boolean | string>;
|
|
8
|
+
export declare function propNameToAttrName(prop: string): string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export function defaultParser() {
|
|
1
|
+
export function defaultParser(mapTo) {
|
|
2
2
|
return {
|
|
3
3
|
read(val) {
|
|
4
4
|
// if a boolean assume such
|
|
@@ -8,5 +8,12 @@ export function defaultParser() {
|
|
|
8
8
|
return val;
|
|
9
9
|
},
|
|
10
10
|
write: String,
|
|
11
|
+
mapTo,
|
|
11
12
|
};
|
|
12
13
|
}
|
|
14
|
+
export function propNameToAttrName(prop) {
|
|
15
|
+
return prop
|
|
16
|
+
.split(/(?=[A-Z])/)
|
|
17
|
+
.join('-')
|
|
18
|
+
.toLowerCase();
|
|
19
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AttributeParser } from './attribute-parsers';
|
|
2
2
|
export declare function getObservableAttributes(c: typeof HTMLElement): Array<string>;
|
|
3
3
|
export declare function getAttributeParsers<T extends typeof HTMLElement>(c: T): Record<string, AttributeParser<unknown>>;
|
|
4
|
-
export declare function attr<T
|
|
4
|
+
export declare function attr<T>(p: Partial<AttributeParser<T>>): <E extends HTMLElement>(t: E, key: string) => void;
|
|
5
5
|
export declare function attr<T extends HTMLElement>(t: T, key: string): void;
|
|
@@ -1,39 +1,42 @@
|
|
|
1
|
-
import { defaultParser } from './attribute-parsers';
|
|
1
|
+
import { defaultParser, propNameToAttrName, } from './attribute-parsers';
|
|
2
2
|
export function getObservableAttributes(c) {
|
|
3
|
-
|
|
3
|
+
const attrs = Reflect.get(c, 'observedAttributes') || [];
|
|
4
|
+
return attrs.map(propNameToAttrName);
|
|
4
5
|
}
|
|
5
6
|
export function getAttributeParsers(c) {
|
|
6
7
|
const parsers = Reflect.get(c, 'attributeParsers') || {};
|
|
7
8
|
return parsers;
|
|
8
9
|
}
|
|
9
10
|
export function attr(targetOrParser, key) {
|
|
10
|
-
if (targetOrParser instanceof HTMLElement) {
|
|
11
|
-
|
|
11
|
+
if (targetOrParser instanceof HTMLElement && typeof key === 'string') {
|
|
12
|
+
const attrName = propNameToAttrName(key);
|
|
13
|
+
return defineAttribute(targetOrParser, attrName, key);
|
|
12
14
|
}
|
|
13
15
|
return (target, key) => {
|
|
14
16
|
const parser = targetOrParser;
|
|
15
|
-
|
|
17
|
+
const attrName = propNameToAttrName(key);
|
|
18
|
+
defineAttribute(target, attrName, key);
|
|
16
19
|
const attributeParsers = Reflect.get(target.constructor, 'attributeParsers');
|
|
17
|
-
attributeParsers[
|
|
18
|
-
attributeParsers[
|
|
20
|
+
attributeParsers[attrName].read = parser.read || attributeParsers[attrName].read;
|
|
21
|
+
attributeParsers[attrName].write = parser.write || attributeParsers[attrName].write;
|
|
19
22
|
Reflect.set(target.constructor, 'attributeParsers', attributeParsers);
|
|
20
23
|
return void 0;
|
|
21
24
|
};
|
|
22
25
|
}
|
|
23
|
-
function defineAttribute(target,
|
|
26
|
+
function defineAttribute(target, attrName, propName) {
|
|
24
27
|
const observedAttributes = Reflect.get(target.constructor, 'observedAttributes');
|
|
25
28
|
if (observedAttributes) {
|
|
26
|
-
observedAttributes.push(
|
|
29
|
+
observedAttributes.push(attrName);
|
|
27
30
|
}
|
|
28
31
|
else {
|
|
29
|
-
Reflect.set(target.constructor, 'observedAttributes', [
|
|
32
|
+
Reflect.set(target.constructor, 'observedAttributes', [attrName]);
|
|
30
33
|
}
|
|
31
34
|
const attributeParsers = Reflect.get(target.constructor, 'attributeParsers');
|
|
32
35
|
if (attributeParsers) {
|
|
33
|
-
attributeParsers[
|
|
36
|
+
attributeParsers[attrName] = defaultParser(propName);
|
|
34
37
|
}
|
|
35
38
|
else {
|
|
36
|
-
Reflect.set(target.constructor, 'attributeParsers', { [
|
|
39
|
+
Reflect.set(target.constructor, 'attributeParsers', { [attrName]: defaultParser(propName) });
|
|
37
40
|
}
|
|
38
41
|
return void 0;
|
|
39
42
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getAttributeParsers, getObservableAttributes } from './attribute';
|
|
2
|
+
import { propNameToAttrName } from './attribute-parsers';
|
|
2
3
|
export class Change {
|
|
3
4
|
constructor(value, previousValue, firstChange) {
|
|
4
5
|
this.value = value;
|
|
@@ -34,10 +35,11 @@ export function observable(Base) {
|
|
|
34
35
|
connectedCallback() {
|
|
35
36
|
for (let i = 0; i < attributes.length; i++) {
|
|
36
37
|
const key = attributes[i];
|
|
38
|
+
const { write, mapTo } = parsers[key];
|
|
37
39
|
if (this.getAttribute(key) === null) {
|
|
38
|
-
const propVal = Reflect.get(this,
|
|
40
|
+
const propVal = Reflect.get(this, mapTo);
|
|
39
41
|
if (propVal !== undefined && propVal !== null && propVal !== '') {
|
|
40
|
-
this.setAttribute(key,
|
|
42
|
+
this.setAttribute(key, write(propVal));
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
}
|
|
@@ -46,7 +48,8 @@ export function observable(Base) {
|
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
50
|
attributeChangedCallback(name, oldVal, newVal) {
|
|
49
|
-
|
|
51
|
+
const { read, mapTo } = parsers[name];
|
|
52
|
+
Reflect.set(this, mapTo, read(newVal));
|
|
50
53
|
if (super.attributeChangedCallback) {
|
|
51
54
|
super.attributeChangedCallback(name, oldVal, newVal);
|
|
52
55
|
}
|
|
@@ -56,8 +59,9 @@ export function observable(Base) {
|
|
|
56
59
|
for (let change in changes) {
|
|
57
60
|
if (attributes.includes(change)) {
|
|
58
61
|
const value = parsers[change].write(changes[change].value);
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
const attrName = propNameToAttrName(change);
|
|
63
|
+
if (value !== this.getAttribute(attrName)) {
|
|
64
|
+
this.setAttribute(attrName, value);
|
|
61
65
|
}
|
|
62
66
|
}
|
|
63
67
|
}
|