@joist/observable 2.0.0-canary.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/LICENSE +21 -0
- package/README.md +73 -0
- package/package.json +38 -0
- package/target/build/lib/observable.d.ts +27 -0
- package/target/build/lib/observable.js +79 -0
- package/target/build/lib/observable.js.map +1 -0
- package/target/build/lib.d.ts +1 -0
- package/target/build/lib.js +2 -0
- package/target/build/lib.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019-2020 Danny Blue
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Observable
|
|
2
|
+
|
|
3
|
+
Observable in Joist means something slightly different then in something like RxJs.
|
|
4
|
+
Marking a class with `@observable()` means that instances of that class will BE observable. This means you can watch for changes on select properties.s
|
|
5
|
+
|
|
6
|
+
#### Installation:
|
|
7
|
+
|
|
8
|
+
```BASH
|
|
9
|
+
npm i @joist/observable
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
#### Example:
|
|
13
|
+
|
|
14
|
+
```TS
|
|
15
|
+
import { observable, observer, OnChange, Changes } from '@joist/observable';
|
|
16
|
+
|
|
17
|
+
@observable
|
|
18
|
+
class State implements OnChange {
|
|
19
|
+
// Changes to these will trigger callback
|
|
20
|
+
@observe todos: string[] = [];
|
|
21
|
+
@observe userName?: string;
|
|
22
|
+
|
|
23
|
+
// changes to this will not
|
|
24
|
+
someValue: boolean = false;
|
|
25
|
+
|
|
26
|
+
onChange(changes: Changes) {
|
|
27
|
+
console.log(changes);
|
|
28
|
+
// { todos: { value: ['Build Shit'], previousValue: [] }, userName: { value: 'Danny Blue', previousValue: undefined } }
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const state = new State();
|
|
33
|
+
|
|
34
|
+
state.todos = [...state.todos, 'Build Shit'];
|
|
35
|
+
state.userName = 'Danny Blue'
|
|
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
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@joist/observable",
|
|
3
|
+
"version": "2.0.0-canary.0+d60b309",
|
|
4
|
+
"main": "./target/build/lib.js",
|
|
5
|
+
"module": "./target/build/lib.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./target/build/lib.js"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"target/build"
|
|
13
|
+
],
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"description": "Dependency Injection in ~800 bytes",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/deebloo/joist.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"TypeScript",
|
|
22
|
+
"DI",
|
|
23
|
+
"Dependency Injection"
|
|
24
|
+
],
|
|
25
|
+
"author": "deebloo",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/deebloo/joist/issues"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"test": "tsc -p tsconfig.test.json && wtr --config ../../wtr.config.mjs --port 8002",
|
|
35
|
+
"build": "tsc -p tsconfig.build.json"
|
|
36
|
+
},
|
|
37
|
+
"gitHead": "d60b309e8217c15aa200955d6cf0921f0c5a2cc3"
|
|
38
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare class Change<T = any> {
|
|
2
|
+
value: T;
|
|
3
|
+
previousValue: T | undefined;
|
|
4
|
+
firstChange: boolean;
|
|
5
|
+
constructor(value: T, previousValue: T | undefined, firstChange: boolean);
|
|
6
|
+
}
|
|
7
|
+
export declare type Changes = Record<string | symbol, Change>;
|
|
8
|
+
export interface OnChange {
|
|
9
|
+
onChange(changes: Changes): void;
|
|
10
|
+
}
|
|
11
|
+
export declare function readPropertyDefs(c: any): Record<string | symbol, {}>;
|
|
12
|
+
export interface ObservableBase {
|
|
13
|
+
propChanges: Changes;
|
|
14
|
+
propChange: Promise<void> | null;
|
|
15
|
+
initializedChanges: Set<string | symbol>;
|
|
16
|
+
definePropChange(key: string | symbol, propChange: Change): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export declare function observe(target: any, key: string): void;
|
|
19
|
+
export declare function observable<T extends new (...args: any[]) => any>(CustomElement: T): {
|
|
20
|
+
new (...args: any[]): {
|
|
21
|
+
[x: string]: any;
|
|
22
|
+
propChanges: Changes;
|
|
23
|
+
propChange: Promise<void> | null;
|
|
24
|
+
initializedChanges: Set<string | symbol>;
|
|
25
|
+
definePropChange(key: string | symbol, propChange: Change): Promise<void>;
|
|
26
|
+
};
|
|
27
|
+
} & T;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export class Change {
|
|
2
|
+
value;
|
|
3
|
+
previousValue;
|
|
4
|
+
firstChange;
|
|
5
|
+
constructor(value, previousValue, firstChange) {
|
|
6
|
+
this.value = value;
|
|
7
|
+
this.previousValue = previousValue;
|
|
8
|
+
this.firstChange = firstChange;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function readPropertyDefs(c) {
|
|
12
|
+
return c.properties || c.prototype.properties || {};
|
|
13
|
+
}
|
|
14
|
+
const PROPERTY_KEY = 'properties';
|
|
15
|
+
export function observe(target, key) {
|
|
16
|
+
target[PROPERTY_KEY] = target[PROPERTY_KEY] || {};
|
|
17
|
+
target[PROPERTY_KEY][key] = {};
|
|
18
|
+
}
|
|
19
|
+
export function observable(CustomElement) {
|
|
20
|
+
const defs = readPropertyDefs(CustomElement);
|
|
21
|
+
const props = createPropertyDescripors(defs);
|
|
22
|
+
return class ObservableElement extends CustomElement {
|
|
23
|
+
propChanges = {};
|
|
24
|
+
propChange = null;
|
|
25
|
+
initializedChanges = new Set();
|
|
26
|
+
constructor(...args) {
|
|
27
|
+
super(...args);
|
|
28
|
+
for (let def in defs) {
|
|
29
|
+
Reflect.set(this, createPrivateKey(def), Reflect.get(this, def));
|
|
30
|
+
}
|
|
31
|
+
Object.defineProperties(this, props);
|
|
32
|
+
}
|
|
33
|
+
definePropChange(key, propChange) {
|
|
34
|
+
if (!this.propChanges[key]) {
|
|
35
|
+
this.propChanges[key] = propChange;
|
|
36
|
+
}
|
|
37
|
+
this.propChanges[key].value = propChange.value;
|
|
38
|
+
if (!this.propChange) {
|
|
39
|
+
// If there is no previous change defined set it up
|
|
40
|
+
this.propChange = Promise.resolve().then(() => {
|
|
41
|
+
// run onPropChanges here. This makes sure we capture all changes
|
|
42
|
+
// keep track of whether or not this is the first time a given property has changes
|
|
43
|
+
for (let change in this.propChanges) {
|
|
44
|
+
this.propChanges[change].firstChange = !this.initializedChanges.has(change);
|
|
45
|
+
this.initializedChanges.add(change);
|
|
46
|
+
}
|
|
47
|
+
if (this.onChange) {
|
|
48
|
+
this.onChange(this.propChanges);
|
|
49
|
+
}
|
|
50
|
+
// reset for next time
|
|
51
|
+
this.propChanges = {};
|
|
52
|
+
this.propChange = null;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return this.propChange;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function createPrivateKey(key) {
|
|
60
|
+
return `__${key}`;
|
|
61
|
+
}
|
|
62
|
+
function createPropertyDescripors(defs) {
|
|
63
|
+
const props = {};
|
|
64
|
+
for (let def in defs) {
|
|
65
|
+
const privateKey = createPrivateKey(def);
|
|
66
|
+
props[def] = {
|
|
67
|
+
set(val) {
|
|
68
|
+
const prevVal = Reflect.get(this, privateKey);
|
|
69
|
+
this.definePropChange(def, new Change(val, prevVal, true));
|
|
70
|
+
Reflect.set(this, privateKey, val);
|
|
71
|
+
},
|
|
72
|
+
get() {
|
|
73
|
+
return Reflect.get(this, privateKey);
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return props;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=observable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,CAAC,MAAW,EAAE,GAAW;IAC9C,MAAM,CAAC,YAAY,CAAC,GAAG,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;IAClD,MAAM,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;AACjC,CAAC;AAED,MAAM,UAAU,UAAU,CAAwC,aAAgB;IAChF,MAAM,IAAI,GAAG,gBAAgB,CAA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;oBAED,IAAI,IAAI,CAAC,QAAQ,EAAE;wBACjB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;qBACjC;oBAED,sBAAsB;oBACtB,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;oBACtB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;gBACzB,CAAC,CAAC,CAAC;aACJ;YAED,OAAO,IAAI,CAAC,UAAU,CAAC;QACzB,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAS,gBAAgB,CAAC,GAAW;IACnC,OAAO,KAAK,GAAG,EAAE,CAAC;AACpB,CAAC;AAED,SAAS,wBAAwB,CAC/B,IAAiC;IAEjC,MAAM,KAAK,GAAuC,EAAE,CAAC;IAErD,KAAK,IAAI,GAAG,IAAI,IAAI,EAAE;QACpB,MAAM,UAAU,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC;QAEzC,KAAK,CAAC,GAAG,CAAC,GAAG;YACX,GAAG,CAAuB,GAAG;gBAC3B,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;gBAE9C,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,IAAI,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;gBAE3D,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,EAAE,GAAG,CAAC,CAAC;YACrC,CAAC;YACD,GAAG;gBACD,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;YACvC,CAAC;SACF,CAAC;KACH;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { observable, Change, OnChange, observe, Changes } from './lib/observable';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lib.js","sourceRoot":"","sources":["../../lib.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAY,OAAO,EAAW,MAAM,kBAAkB,CAAC"}
|