Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dxu committed Nov 26, 2022
1 parent facc265 commit 1ffe956
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 118 deletions.
24 changes: 12 additions & 12 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"index.js": {
"bundled": 4095,
"minified": 1253,
"gzipped": 586,
"bundled": 28663,
"minified": 13201,
"gzipped": 3879,
"treeshaked": {
"rollup": {
"code": 1108,
"import_statements": 57
"code": 10165,
"import_statements": 0
},
"webpack": {
"code": 2107
"code": 11507
}
}
},
"index.cjs.js": {
"bundled": 4402,
"minified": 1424,
"gzipped": 628
"bundled": 29043,
"minified": 13364,
"gzipped": 3900
},
"index.iife.js": {
"bundled": 4752,
"minified": 1329,
"gzipped": 602
"bundled": 30740,
"minified": 11795,
"gzipped": 3651
}
}
109 changes: 6 additions & 103 deletions src/Compound.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
import { BehaviorSubject } from "rxjs";
import { Atom } from "./Atom";
import { IsntObject, KeyOfType } from "./types";

// type Tree<T extends InType> = {
// [key in keyof T]: Atom<T[key]> | Compound<T[key]>;
// };

type InType = { [key: string]: any };

function existsInObject(
obj: object,
k: string | number | symbol
): k is keyof typeof obj {
return k in obj;
}

class _Compound<T extends InType> {
// _atoms: {
// [key in keyof T as T[key] extends object ? never : key]: Atom<T[key]>;
// } = {};

// _compounds: {
// // [key in keyof T]: key extends object ? Compound<T[key]> : never;
// [key in keyof T as T[key] extends object ? key : never]: Compound<T[key]>;
// } = {};
type BaseObjectType = { [key: string]: any };

class _Compound<T extends BaseObjectType> {
constructor(base: T) {
const newAttributes: { [k in keyof T]?: Atom<T[k]> | Compound<T[k]> } = {
..._Compound.prototype,
};
// Object.assign(this, base);
// for every single key, traverse and create a tree of atoms
// for every key, traverse and create a tree of Atoms for all primitive values, and Compounds for all object values
(Object.keys(base) as (keyof typeof base)[]).map((key) => {
const value = base[key];
if (
Expand All @@ -40,97 +17,23 @@ class _Compound<T extends InType> {
!((value as object) instanceof Function)
) {
newAttributes[key] = new Compound<typeof value>(value);

// this[key] = new Compound<typeof value>(value);
// value extends
// this._compounds[key] = new Compound<typeof value>(value);
} else {
newAttributes[key] = new Atom<typeof value>(value);
}
});

Object.assign(this, newAttributes);

// for (const key in base) {
// const value = base[key];
// // if it's an object, create a new compound
// if (
// typeof value === "object" &&
// !Array.isArray(value) &&
// value !== null &&
// !((value as object) instanceof Function)
// ) {
// // value extends
// this._compounds[key] = new Compound<typeof value>(value);
// }
// }
}

// get(key: keyof Compound<T>["_atoms"] | keyof typeof this._compounds) {
// const atoms = this._atoms;
// if (key in this._atoms) {
// return this._atoms[key];
// } else {
// return this._compounds[key];
// }
// }

// push<K extends Extract<keyof T, KeyOfType<T, IsntObject<T>>>, V extends T[K]>(
// key: K,
// value: V
// ) {
// const atomOrCompound = this._tree[key];
// if (atomOrCompound instanceof Atom) {
// atomOrCompound.push(value);
// } else {
// atomOrCompound.push(value);
// }
// }
}

type Compound<T extends InType> = _Compound<T> & {
// Primary export to support extending _Compound with attribute selectors
type Compound<T extends BaseObjectType> = _Compound<T> & {
[k in keyof T]: T[k] extends object
? T[k] extends Function
? Atom<T[k]>
: Compound<T[k]>
: Atom<T[k]>;
};

export const Compound: new <T extends InType>(data: T) => Compound<T> =
export const Compound: new <T extends BaseObjectType>(data: T) => Compound<T> =
_Compound as any;

// export class Compound<T> implements T {
// constructor(private _value: T) {}

// get() {}

// set() {}
// }
//

const comp2 = new Compound({ a: 10, b: 10, c: { c1: 10 }, d: () => 4 });

const a2 = comp2.a;
const c2 = comp2.c;
const d2 = comp2.d;

const a2Val = a2.getValue();
console.log(a2);

// const comp = Compounds.create<{
// a: number;
// b: number;
// c: { c1: number };
// }>({ a: 10, b: 10, c: { c1: 10 } });

// const a = comp.a;
// const c = comp.c;

// const d = comp._atoms;
// const e = comp._compounds;

// d.a.get("c1");

// e.c.get("c1");

// comp.push("a", 11);
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// export type KeyOfType<O, T> = {
// [K in keyof O]: O[K] extends T ? K : never;
// }[keyof O];
// Helper types if needed

export type KeyOfType<T, V> = {
[K in keyof T]: T[K] extends V ? K : never;
Expand Down

0 comments on commit 1ffe956

Please sign in to comment.