@joist/observable 2.0.0-alpha.0 → 2.0.0-alpha.1

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
@@ -1,12 +1,12 @@
1
1
  # Observable
2
2
 
3
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
4
+ Decorating 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
5
 
6
6
  #### Installation:
7
7
 
8
8
  ```BASH
9
- npm i @joist/observable
9
+ npm i @joist/observable@alpha
10
10
  ```
11
11
 
12
12
  #### Example:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@joist/observable",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.1",
4
4
  "main": "./target/build/lib.js",
5
5
  "module": "./target/build/lib.js",
6
6
  "exports": {
@@ -12,19 +12,20 @@
12
12
  "target/build"
13
13
  ],
14
14
  "sideEffects": false,
15
- "description": "Create objects with observable properties",
15
+ "description": "Dependency Injection in ~800 bytes",
16
16
  "repository": {
17
17
  "type": "git",
18
- "url": "git+https://github.com/joist-framework/joist.git"
18
+ "url": "git+https://github.com/deebloo/joist.git"
19
19
  },
20
20
  "keywords": [
21
21
  "TypeScript",
22
- "Observable"
22
+ "DI",
23
+ "Dependency Injection"
23
24
  ],
24
25
  "author": "deebloo",
25
26
  "license": "MIT",
26
27
  "bugs": {
27
- "url": "https://github.com/joist-framework/joist/issues"
28
+ "url": "https://github.com/deebloo/joist/issues"
28
29
  },
29
30
  "publishConfig": {
30
31
  "access": "public"
@@ -33,5 +34,5 @@
33
34
  "test": "tsc -p tsconfig.test.json && wtr --config ../../wtr.config.mjs --port 8002",
34
35
  "build": "tsc -p tsconfig.build.json"
35
36
  },
36
- "gitHead": "8078ed9c1005afd279b1ccb2c7b7856dd563bec7"
37
+ "gitHead": "a1e00a87cdc22383fdaab2ad49b645eed1e9e370"
37
38
  }
@@ -13,15 +13,18 @@ export interface ObservableBase {
13
13
  propChanges: Changes;
14
14
  propChange: Promise<void> | null;
15
15
  initializedChanges: Set<string | symbol>;
16
+ onChange?: (changes: Changes) => void;
16
17
  definePropChange(key: string | symbol, propChange: Change): Promise<void>;
17
18
  }
18
19
  export declare function observe(target: any, key: string): void;
19
- export declare function observable<T extends new (...args: any[]) => any>(CustomElement: T): {
20
+ export declare function observable<T extends new (...args: any[]) => any>(Base: T): {
20
21
  new (...args: any[]): {
21
22
  [x: string]: any;
22
23
  propChanges: Changes;
23
24
  propChange: Promise<void> | null;
24
25
  initializedChanges: Set<string | symbol>;
25
- definePropChange(key: string | symbol, propChange: Change): Promise<void>;
26
+ definePropChange: typeof definePropChange;
26
27
  };
27
28
  } & T;
29
+ declare function definePropChange(this: ObservableBase, key: string | symbol, propChange: Change): Promise<void>;
30
+ export {};
@@ -1,7 +1,4 @@
1
1
  export class Change {
2
- value;
3
- previousValue;
4
- firstChange;
5
2
  constructor(value, previousValue, firstChange) {
6
3
  this.value = value;
7
4
  this.previousValue = previousValue;
@@ -16,44 +13,21 @@ export function observe(target, key) {
16
13
  target[PROPERTY_KEY] = target[PROPERTY_KEY] || {};
17
14
  target[PROPERTY_KEY][key] = {};
18
15
  }
19
- export function observable(CustomElement) {
20
- const defs = readPropertyDefs(CustomElement);
16
+ export function observable(Base) {
17
+ const defs = readPropertyDefs(Base);
21
18
  const props = createPropertyDescripors(defs);
22
- return class ObservableElement extends CustomElement {
23
- propChanges = {};
24
- propChange = null;
25
- initializedChanges = new Set();
19
+ return class Observable extends Base {
26
20
  constructor(...args) {
27
21
  super(...args);
22
+ this.propChanges = {};
23
+ this.propChange = null;
24
+ this.initializedChanges = new Set();
25
+ this.definePropChange = definePropChange;
28
26
  for (let def in defs) {
29
27
  Reflect.set(this, createPrivateKey(def), Reflect.get(this, def));
30
28
  }
31
29
  Object.defineProperties(this, props);
32
30
  }
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
31
  };
58
32
  }
59
33
  function createPrivateKey(key) {
@@ -66,8 +40,10 @@ function createPropertyDescripors(defs) {
66
40
  props[def] = {
67
41
  set(val) {
68
42
  const prevVal = Reflect.get(this, privateKey);
69
- this.definePropChange(def, new Change(val, prevVal, true));
70
- Reflect.set(this, privateKey, val);
43
+ if (prevVal !== val) {
44
+ this.definePropChange(def, new Change(val, prevVal, true));
45
+ }
46
+ return Reflect.set(this, privateKey, val);
71
47
  },
72
48
  get() {
73
49
  return Reflect.get(this, privateKey);
@@ -76,4 +52,28 @@ function createPropertyDescripors(defs) {
76
52
  }
77
53
  return props;
78
54
  }
55
+ function definePropChange(key, propChange) {
56
+ if (!this.propChanges[key]) {
57
+ this.propChanges[key] = propChange;
58
+ }
59
+ this.propChanges[key].value = propChange.value;
60
+ if (!this.propChange) {
61
+ // If there is no previous change defined set it up
62
+ this.propChange = Promise.resolve().then(() => {
63
+ // run onPropChanges here. This makes sure we capture all changes
64
+ // keep track of whether or not this is the first time a given property has changes
65
+ for (let change in this.propChanges) {
66
+ this.propChanges[change].firstChange = !this.initializedChanges.has(change);
67
+ this.initializedChanges.add(change);
68
+ }
69
+ if (this.onChange) {
70
+ this.onChange(this.propChanges);
71
+ }
72
+ // reset for next time
73
+ this.propChanges = {};
74
+ this.propChange = null;
75
+ });
76
+ }
77
+ return this.propChange;
78
+ }
79
79
  //# sourceMappingURL=observable.js.map
@@ -1 +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,CAAC,aAAa,CAAC,CAAC;IAC7C,MAAM,KAAK,GAAG,wBAAwB,CAAC,IAAI,CAAC,CAAC;IAE7C,OAAO,MAAM,iBAAkB,SAAQ,aAAa;QAClD,WAAW,GAAY,EAAE,CAAC;QAC1B,UAAU,GAAyB,IAAI,CAAC;QACxC,kBAAkB,GAAG,IAAI,GAAG,EAAmB,CAAC;QAEhD,YAAY,GAAG,IAAW;YACxB,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;YAEf,KAAK,IAAI,GAAG,IAAI,IAAI,EAAE;gBACpB,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,gBAAgB,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,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