@joist/observable 2.0.0-next.1637881209.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 +120 -0
- package/package.json +38 -0
- package/target/build/lib/observable.d.ts +25 -0
- package/target/build/lib/observable.js +73 -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,120 @@
|
|
|
1
|
+
# Di
|
|
2
|
+
|
|
3
|
+
Dependency Injection in ~800 bytes. Can be used with and without decorators.
|
|
4
|
+
|
|
5
|
+
#### Installation:
|
|
6
|
+
|
|
7
|
+
```BASH
|
|
8
|
+
npm i @joist/di
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
#### Example:
|
|
12
|
+
|
|
13
|
+
```TS
|
|
14
|
+
import { Injector } from '@joist/di';
|
|
15
|
+
|
|
16
|
+
class FooService {
|
|
17
|
+
sayHello() {
|
|
18
|
+
return 'Hello From FooService';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class BarService {
|
|
23
|
+
static deps = [FooService];
|
|
24
|
+
|
|
25
|
+
constructor(private foo: FooService) {}
|
|
26
|
+
|
|
27
|
+
sayHello() {
|
|
28
|
+
return 'Hello From BarService and ' + this.foo.sayHello();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const app = new Injector();
|
|
33
|
+
|
|
34
|
+
app.get(BarService).sayHello(); // Hello from BarService and Hello from FooService
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```TS
|
|
38
|
+
import { Injector } from '@joist/di';
|
|
39
|
+
import { inject } from '@joist/di/decorators';
|
|
40
|
+
|
|
41
|
+
class FooService {
|
|
42
|
+
sayHello() {
|
|
43
|
+
return 'Hello From FooService';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class BarService {
|
|
48
|
+
constructor(@inject(FooService) private foo: FooService) {}
|
|
49
|
+
|
|
50
|
+
sayHello() {
|
|
51
|
+
return 'Hello From BarService and ' + this.foo.sayHello();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const app = new Injector();
|
|
56
|
+
|
|
57
|
+
app.get(BarService).sayHello(); // Hello from BarService and Hello from FooService
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
#### Override A Service:
|
|
61
|
+
|
|
62
|
+
```TS
|
|
63
|
+
import { Injector } from '@joist/di';
|
|
64
|
+
import { inject } from '@joist/di/decorators';
|
|
65
|
+
|
|
66
|
+
class FooService {
|
|
67
|
+
sayHello() {
|
|
68
|
+
return 'Hello From FooService';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
class BarService {
|
|
73
|
+
constructor(@inject(FooService) private foo: FooService) {}
|
|
74
|
+
|
|
75
|
+
sayHello() {
|
|
76
|
+
return 'Hello From BarService and ' + this.foo.sayHello();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Override FooService with an alternate implementation
|
|
81
|
+
const app = new Injector({
|
|
82
|
+
providers: [
|
|
83
|
+
{
|
|
84
|
+
provide: FooService,
|
|
85
|
+
use: class extends FooService {
|
|
86
|
+
sayHello() {
|
|
87
|
+
return 'IT HAS BEEN OVERRIDEN'
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
]
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
app.get(BarService).sayHello(); // Hello from BarService and IT HAS BEEN OVERRIDEN
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### Root Service
|
|
98
|
+
|
|
99
|
+
If you have nested injectors and you still want singleton instances mark your service as shown or decorate with `@service()`
|
|
100
|
+
|
|
101
|
+
```TS
|
|
102
|
+
class FooService {
|
|
103
|
+
static providedInRoot = true;
|
|
104
|
+
|
|
105
|
+
sayHello() {
|
|
106
|
+
return 'Hello From FooService';
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
```TS
|
|
112
|
+
import { service } from '@joist/di/decorators';
|
|
113
|
+
|
|
114
|
+
@service()
|
|
115
|
+
class FooService {
|
|
116
|
+
sayHello() {
|
|
117
|
+
return 'Hello From FooService';
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@joist/observable",
|
|
3
|
+
"version": "2.0.0-next.1637881209.0+eb19bc3",
|
|
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": "eb19bc349db140ab2afc163af33403135d4dda0d"
|
|
38
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare class PropChange<T = any> {
|
|
2
|
+
key: string | symbol;
|
|
3
|
+
newValue: T;
|
|
4
|
+
oldValue?: T | undefined;
|
|
5
|
+
constructor(key: string | symbol, newValue: T, oldValue?: T | undefined);
|
|
6
|
+
}
|
|
7
|
+
export declare type PropChanges = Record<string | symbol, PropChange>;
|
|
8
|
+
export interface OnChange {
|
|
9
|
+
onChange(changes: PropChanges): void;
|
|
10
|
+
}
|
|
11
|
+
export declare function readPropertyDefs(c: any): Record<string | symbol, {}>;
|
|
12
|
+
export interface ObservableBase {
|
|
13
|
+
propChanges: PropChanges;
|
|
14
|
+
propChange: Promise<void> | null;
|
|
15
|
+
definePropChange(propChange: PropChange): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export declare function observe(): (target: any, key: string) => void;
|
|
18
|
+
export declare function observable(): <T extends new (...args: any[]) => OnChange>(CustomElement: T) => {
|
|
19
|
+
new (...args: any[]): {
|
|
20
|
+
propChanges: PropChanges;
|
|
21
|
+
propChange: Promise<void> | null;
|
|
22
|
+
onChange(e: PropChanges): void;
|
|
23
|
+
definePropChange(propChange: PropChange): Promise<void>;
|
|
24
|
+
};
|
|
25
|
+
} & T;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export class PropChange {
|
|
2
|
+
key;
|
|
3
|
+
newValue;
|
|
4
|
+
oldValue;
|
|
5
|
+
constructor(key, newValue, oldValue) {
|
|
6
|
+
this.key = key;
|
|
7
|
+
this.newValue = newValue;
|
|
8
|
+
this.oldValue = oldValue;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function readPropertyDefs(c) {
|
|
12
|
+
return c.properties || c.prototype.properties || {};
|
|
13
|
+
}
|
|
14
|
+
const PROPERTY_KEY = 'properties';
|
|
15
|
+
export function observe() {
|
|
16
|
+
return function (target, key) {
|
|
17
|
+
target[PROPERTY_KEY] = target[PROPERTY_KEY] || {};
|
|
18
|
+
target[PROPERTY_KEY][key] = {};
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export function observable() {
|
|
22
|
+
return (CustomElement) => {
|
|
23
|
+
const defs = readPropertyDefs(CustomElement);
|
|
24
|
+
const props = {};
|
|
25
|
+
for (let def in defs) {
|
|
26
|
+
const privateKey = createPrivateKey(def);
|
|
27
|
+
props[def] = {
|
|
28
|
+
set(val) {
|
|
29
|
+
const prevVal = Reflect.get(this, privateKey);
|
|
30
|
+
this.definePropChange(new PropChange(def, val, prevVal));
|
|
31
|
+
Reflect.set(this, privateKey, val);
|
|
32
|
+
},
|
|
33
|
+
get() {
|
|
34
|
+
return Reflect.get(this, privateKey);
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return class ObservableElement extends CustomElement {
|
|
39
|
+
propChanges = {};
|
|
40
|
+
propChange = null;
|
|
41
|
+
constructor(...args) {
|
|
42
|
+
super(...args);
|
|
43
|
+
for (let def in defs) {
|
|
44
|
+
Reflect.set(this, createPrivateKey(def), Reflect.get(this, def));
|
|
45
|
+
Object.defineProperties(this, props);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
onChange(e) {
|
|
49
|
+
if (!!super.onChange) {
|
|
50
|
+
super.onChange(e);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
definePropChange(propChange) {
|
|
54
|
+
this.propChanges[propChange.key] = propChange;
|
|
55
|
+
if (!this.propChange) {
|
|
56
|
+
// If there is no previous change defined set it up
|
|
57
|
+
this.propChange = Promise.resolve().then(() => {
|
|
58
|
+
// run onPropChanges here. This makes sure we capture all changes
|
|
59
|
+
this.onChange(this.propChanges);
|
|
60
|
+
// reset for next time
|
|
61
|
+
this.propChanges = {};
|
|
62
|
+
this.propChange = null;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return this.propChange;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function createPrivateKey(key) {
|
|
71
|
+
return `__${key}`;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=observable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observable.js","sourceRoot":"","sources":["../../../lib/observable.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,UAAU;IACF;IAA6B;IAAoB;IAApE,YAAmB,GAAoB,EAAS,QAAW,EAAS,QAAY;QAA7D,QAAG,GAAH,GAAG,CAAiB;QAAS,aAAQ,GAAR,QA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@
|
|
|
1
|
+
{"version":3,"file":"lib.js","sourceRoot":"","sources":["../../lib.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAY,OAAO,EAAe,MAAM,kBAAkB,CAAC"}
|