@joist/observable 2.0.0-next.2 → 2.0.0-next.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/README.md
CHANGED
|
@@ -16,12 +16,16 @@ import { observable, observer, OnChange, Changes } from '@joist/observable';
|
|
|
16
16
|
|
|
17
17
|
@observable()
|
|
18
18
|
class State implements OnChange {
|
|
19
|
+
// Changes to these will trigger callback
|
|
19
20
|
@observe() todos: string[] = [];
|
|
20
21
|
@observe() userName?: string;
|
|
21
22
|
|
|
23
|
+
// changes to this will not
|
|
24
|
+
someValue: boolean = false;
|
|
25
|
+
|
|
22
26
|
onChange(changes: Changes) {
|
|
23
27
|
console.log(changes);
|
|
24
|
-
// { todos: { value: ['Build Shit'] }, userName: 'Danny Blue' }
|
|
28
|
+
// { todos: { value: ['Build Shit'], previousValue: [] }, userName: { value: 'Danny Blue', previousValue: undefined } }
|
|
25
29
|
}
|
|
26
30
|
}
|
|
27
31
|
|
|
@@ -30,3 +34,40 @@ const state = new State();
|
|
|
30
34
|
state.todos = [...state.todos, 'Build Shit'];
|
|
31
35
|
state.userName = 'Danny Blue'
|
|
32
36
|
```
|
|
37
|
+
|
|
38
|
+
#### Event target example:
|
|
39
|
+
|
|
40
|
+
If you want to externally monitor your class for changes you can extend event target and dispatch events. (available in both node and the browser)
|
|
41
|
+
|
|
42
|
+
```TS
|
|
43
|
+
import { observable, observer, OnChange, Changes } from '@joist/observable';
|
|
44
|
+
|
|
45
|
+
class StateChangeEvent extends Event {
|
|
46
|
+
consetructor(public changes: Changes) {
|
|
47
|
+
super('statechange')
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@observable()
|
|
52
|
+
class State extends EventTarget implements OnChange {
|
|
53
|
+
// Changes to these will trigger callback
|
|
54
|
+
@observe() todos: string[] = [];
|
|
55
|
+
@observe() userName?: string;
|
|
56
|
+
|
|
57
|
+
// changes to this will not
|
|
58
|
+
someValue: boolean = false;
|
|
59
|
+
|
|
60
|
+
onChange(changes: Changes) {
|
|
61
|
+
this.dispatchEvent(new StateChangeEvent());
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const state = new State();
|
|
66
|
+
|
|
67
|
+
state.addEventListener('statechange', (e) => {
|
|
68
|
+
console.log(e.changes);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
state.todos = [...state.todos, 'Build Shit'];
|
|
72
|
+
state.userName = 'Danny Blue'
|
|
73
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@joist/observable",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.6",
|
|
4
4
|
"main": "./target/build/lib.js",
|
|
5
5
|
"module": "./target/build/lib.js",
|
|
6
6
|
"exports": {
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"test": "tsc -p tsconfig.test.json && wtr --config ../../wtr.config.mjs --port 8002",
|
|
35
35
|
"build": "tsc -p tsconfig.build.json"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "c99017a6f57235948d3753db16743ee23118c91a"
|
|
38
38
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export declare class Change<T = any> {
|
|
2
2
|
value: T;
|
|
3
|
-
previousValue
|
|
4
|
-
|
|
3
|
+
previousValue: T | undefined;
|
|
4
|
+
firstChange: boolean;
|
|
5
|
+
constructor(value: T, previousValue: T | undefined, firstChange: boolean);
|
|
5
6
|
}
|
|
6
7
|
export declare type Changes = Record<string | symbol, Change>;
|
|
7
8
|
export interface OnChange {
|
|
@@ -11,6 +12,7 @@ export declare function readPropertyDefs(c: any): Record<string | symbol, {}>;
|
|
|
11
12
|
export interface ObservableBase {
|
|
12
13
|
propChanges: Changes;
|
|
13
14
|
propChange: Promise<void> | null;
|
|
15
|
+
initializedChanges: Set<string | symbol>;
|
|
14
16
|
definePropChange(key: string | symbol, propChange: Change): Promise<void>;
|
|
15
17
|
}
|
|
16
18
|
export declare function observe(): (target: any, key: string) => void;
|
|
@@ -19,6 +21,7 @@ export declare function observable(): <T extends new (...args: any[]) => any>(Cu
|
|
|
19
21
|
[x: string]: any;
|
|
20
22
|
propChanges: Changes;
|
|
21
23
|
propChange: Promise<void> | null;
|
|
24
|
+
initializedChanges: Set<string | symbol>;
|
|
22
25
|
definePropChange(key: string | symbol, propChange: Change): Promise<void>;
|
|
23
26
|
};
|
|
24
27
|
} & T;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export class Change {
|
|
2
2
|
value;
|
|
3
3
|
previousValue;
|
|
4
|
-
|
|
4
|
+
firstChange;
|
|
5
|
+
constructor(value, previousValue, firstChange) {
|
|
5
6
|
this.value = value;
|
|
6
7
|
this.previousValue = previousValue;
|
|
8
|
+
this.firstChange = firstChange;
|
|
7
9
|
}
|
|
8
10
|
}
|
|
9
11
|
export function readPropertyDefs(c) {
|
|
@@ -23,19 +25,28 @@ export function observable() {
|
|
|
23
25
|
return class ObservableElement extends CustomElement {
|
|
24
26
|
propChanges = {};
|
|
25
27
|
propChange = null;
|
|
28
|
+
initializedChanges = new Set();
|
|
26
29
|
constructor(...args) {
|
|
27
30
|
super(...args);
|
|
28
31
|
for (let def in defs) {
|
|
29
32
|
Reflect.set(this, createPrivateKey(def), Reflect.get(this, def));
|
|
30
|
-
Object.defineProperties(this, props);
|
|
31
33
|
}
|
|
34
|
+
Object.defineProperties(this, props);
|
|
32
35
|
}
|
|
33
36
|
definePropChange(key, propChange) {
|
|
34
|
-
this.propChanges[key]
|
|
37
|
+
if (!this.propChanges[key]) {
|
|
38
|
+
this.propChanges[key] = propChange;
|
|
39
|
+
}
|
|
40
|
+
this.propChanges[key].value = propChange.value;
|
|
35
41
|
if (!this.propChange) {
|
|
36
42
|
// If there is no previous change defined set it up
|
|
37
43
|
this.propChange = Promise.resolve().then(() => {
|
|
38
44
|
// run onPropChanges here. This makes sure we capture all changes
|
|
45
|
+
// keep track of whether or not this is the first time a given property has changes
|
|
46
|
+
for (let change in this.propChanges) {
|
|
47
|
+
this.propChanges[change].firstChange = !this.initializedChanges.has(change);
|
|
48
|
+
this.initializedChanges.add(change);
|
|
49
|
+
}
|
|
39
50
|
if (this.onChange) {
|
|
40
51
|
this.onChange(this.propChanges);
|
|
41
52
|
}
|
|
@@ -59,7 +70,7 @@ function createPropertyDescripors(defs) {
|
|
|
59
70
|
props[def] = {
|
|
60
71
|
set(val) {
|
|
61
72
|
const prevVal = Reflect.get(this, privateKey);
|
|
62
|
-
this.definePropChange(def, new Change(val, prevVal));
|
|
73
|
+
this.definePropChange(def, new Change(val, prevVal, true));
|
|
63
74
|
Reflect.set(this, privateKey, val);
|
|
64
75
|
},
|
|
65
76
|
get() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"observable.js","sourceRoot":"","sources":["../../../lib/observable.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,MAAM;IACE;IAAiB;
|
|
1
|
+
{"version":3,"file":"observable.js","sourceRoot":"","sources":["../../../lib/observable.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,MAAM;IACE;IAAiB;IAAqC;IAAzE,YAAmB,KAAQ,EAAS,aAA4B,EAAS,WAAoB;QAA1E,UAAK,GAAL,KAAK,CAAG;QAAS,kBAAa,GAAb,aAAa,CAAe;QAAS,gBAAW,GAAX,WAAW,CAAS;IAAG,CAAC;CAClG;AAQD,MAAM,UAAU,gBAAgB,CAAC,CAAM;IACrC,OAAO,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,SAAS,CAAC,UAAU,IAAI,EAAE,CAAC;AACtD,CAAC;AASD,MAAM,YAAY,GAAG,YAAY,CAAC;AAElC,MAAM,UAAU,OAAO;IACrB,OAAO,UAAU,MAAW,EAAE,GAAW;QACvC,MAAM,CAAC,YAAY,CAAC,GAAG,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;QAClD,MAAM,CAA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