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

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.4",
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": "313ce28f4c1a4a634849ec271376e9c9589c045d"
37
38
  }
@@ -8,20 +8,23 @@ export declare type Changes = Record<string | symbol, Change>;
8
8
  export interface OnChange {
9
9
  onChange(changes: Changes): void;
10
10
  }
11
- export declare function readPropertyDefs(c: any): Record<string | symbol, {}>;
11
+ export declare function readPropertyDefs(c: any): Array<string | symbol>;
12
12
  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,79 +1,83 @@
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;
8
5
  this.firstChange = firstChange;
9
6
  }
10
7
  }
8
+ const PROPERTY_KEY = 'props';
11
9
  export function readPropertyDefs(c) {
12
- return c.properties || c.prototype.properties || {};
10
+ return c[PROPERTY_KEY] || {};
13
11
  }
14
- const PROPERTY_KEY = 'properties';
15
12
  export function observe(target, key) {
16
- target[PROPERTY_KEY] = target[PROPERTY_KEY] || {};
17
- target[PROPERTY_KEY][key] = {};
13
+ target.constructor[PROPERTY_KEY] = target.constructor[PROPERTY_KEY] || [];
14
+ target.constructor[PROPERTY_KEY].push(key);
18
15
  }
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();
16
+ export function observable(Base) {
17
+ const props = readPropertyDefs(Base);
18
+ const descriptors = createPropertyDescripors(props);
19
+ return class Observable extends Base {
26
20
  constructor(...args) {
27
21
  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;
22
+ this.propChanges = {};
23
+ this.propChange = null;
24
+ this.initializedChanges = new Set();
25
+ this.definePropChange = definePropChange;
26
+ initObservbale.call(this, descriptors);
56
27
  }
57
28
  };
58
29
  }
30
+ function initObservbale(descriptors) {
31
+ for (let prop in descriptors) {
32
+ Reflect.set(this, createPrivateKey(prop), Reflect.get(this, prop));
33
+ }
34
+ Object.defineProperties(this, descriptors);
35
+ }
59
36
  function createPrivateKey(key) {
60
- return `__${key}`;
37
+ return `__${key.toString()}`;
61
38
  }
62
- function createPropertyDescripors(defs) {
63
- const props = {};
64
- for (let def in defs) {
65
- const privateKey = createPrivateKey(def);
66
- props[def] = {
39
+ function createPropertyDescripors(props) {
40
+ const descriptors = {};
41
+ for (let i = 0; i < props.length; i++) {
42
+ const prop = props[i];
43
+ const privateKey = createPrivateKey(prop);
44
+ descriptors[prop] = {
67
45
  set(val) {
68
46
  const prevVal = Reflect.get(this, privateKey);
69
- this.definePropChange(def, new Change(val, prevVal, true));
70
- Reflect.set(this, privateKey, val);
47
+ if (prevVal !== val) {
48
+ this.definePropChange(prop, new Change(val, prevVal, true));
49
+ }
50
+ return Reflect.set(this, privateKey, val);
71
51
  },
72
52
  get() {
73
53
  return Reflect.get(this, privateKey);
74
54
  },
75
55
  };
76
56
  }
77
- return props;
57
+ return descriptors;
58
+ }
59
+ function definePropChange(key, propChange) {
60
+ if (!this.propChanges[key]) {
61
+ this.propChanges[key] = propChange;
62
+ }
63
+ this.propChanges[key].value = propChange.value;
64
+ if (!this.propChange) {
65
+ // If there is no previous change defined set it up
66
+ this.propChange = Promise.resolve().then(() => {
67
+ // run onPropChanges here. This makes sure we capture all changes
68
+ // keep track of whether or not this is the first time a given property has changes
69
+ for (let change in this.propChanges) {
70
+ this.propChanges[change].firstChange = !this.initializedChanges.has(change);
71
+ this.initializedChanges.add(change);
72
+ }
73
+ if (this.onChange) {
74
+ this.onChange(this.propChanges);
75
+ }
76
+ // reset for next time
77
+ this.propChanges = {};
78
+ this.propChange = null;
79
+ });
80
+ }
81
+ return this.propChange;
78
82
  }
79
83
  //# 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,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