informa 1.0.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.
Files changed (53) hide show
  1. package/.vscode/settings.json +3 -0
  2. package/README.md +238 -0
  3. package/dist/EnumerableWeakMap.d.ts +13 -0
  4. package/dist/EnumerableWeakMap.d.ts.map +1 -0
  5. package/dist/EnumerableWeakMap.js +56 -0
  6. package/dist/EnumerableWeakMap.js.map +1 -0
  7. package/dist/clone.d.ts +2 -0
  8. package/dist/clone.d.ts.map +1 -0
  9. package/dist/clone.js +30 -0
  10. package/dist/clone.js.map +1 -0
  11. package/dist/high.d.ts +60 -0
  12. package/dist/high.d.ts.map +1 -0
  13. package/dist/high.js +107 -0
  14. package/dist/high.js.map +1 -0
  15. package/dist/internals.d.ts +14 -0
  16. package/dist/internals.d.ts.map +1 -0
  17. package/dist/internals.js +24 -0
  18. package/dist/internals.js.map +1 -0
  19. package/dist/low.d.ts +57 -0
  20. package/dist/low.d.ts.map +1 -0
  21. package/dist/low.js +260 -0
  22. package/dist/low.js.map +1 -0
  23. package/dist/quirks/array.d.ts +21 -0
  24. package/dist/quirks/array.d.ts.map +1 -0
  25. package/dist/quirks/array.js +176 -0
  26. package/dist/quirks/array.js.map +1 -0
  27. package/dist/quirks/basestatified.d.ts +5 -0
  28. package/dist/quirks/basestatified.d.ts.map +1 -0
  29. package/dist/quirks/basestatified.js +117 -0
  30. package/dist/quirks/basestatified.js.map +1 -0
  31. package/dist/quirks/set.d.ts +12 -0
  32. package/dist/quirks/set.d.ts.map +1 -0
  33. package/dist/quirks/set.js +37 -0
  34. package/dist/quirks/set.js.map +1 -0
  35. package/dist/test.d.ts +2 -0
  36. package/dist/test.d.ts.map +1 -0
  37. package/dist/test.js +37 -0
  38. package/dist/test.js.map +1 -0
  39. package/dist/utils.d.ts +5 -0
  40. package/dist/utils.d.ts.map +1 -0
  41. package/dist/utils.js +23 -0
  42. package/dist/utils.js.map +1 -0
  43. package/package.json +28 -0
  44. package/src/EnumerableWeakMap.ts +63 -0
  45. package/src/high.ts +163 -0
  46. package/src/internals.ts +32 -0
  47. package/src/low.ts +411 -0
  48. package/src/quirks/array.ts +211 -0
  49. package/src/quirks/basestatified.ts +194 -0
  50. package/src/quirks/set.ts +48 -0
  51. package/src/test.ts +49 -0
  52. package/src/utils.ts +31 -0
  53. package/tsconfig.json +44 -0
@@ -0,0 +1,194 @@
1
+ import {
2
+ exitProxySymbol,
3
+ getMetadataOf,
4
+ globalStateMode,
5
+ setMetadataOf,
6
+ statifySealKey,
7
+ type Statify,
8
+ } from "../internals.js";
9
+ import {
10
+ emitDescendantPathEvents,
11
+ extract,
12
+ hook,
13
+ StateMetadata,
14
+ unhook,
15
+ type ExitProxyValue,
16
+ type StatifiableObj,
17
+ } from "../low.js";
18
+
19
+ type ClassType<T extends any[] = any[], U = object> = new (...args: T) => U;
20
+ const classMapMemo = new WeakMap<ClassType, ClassType>();
21
+ //
22
+ const blanketProtoMemo = new WeakMap<object, object>();
23
+ const blanketProtoSet = new WeakSet<object>();
24
+
25
+ function getBlanketPrototype(actualProto: object) {
26
+ const maybeMemoed = blanketProtoMemo.get(actualProto);
27
+ if (maybeMemoed) return maybeMemoed;
28
+
29
+ const storage = Object.create(actualProto) as {
30
+ [statifySealKey]: true;
31
+ [exitProxySymbol]: ExitProxyValue;
32
+ };
33
+ // storage[statifySealKey] = true;
34
+
35
+ const blanket = new Proxy(storage, {
36
+ get(target, prop, recv) {
37
+ if (prop === statifySealKey) return true;
38
+
39
+ if (globalStateMode === "extract-proxy-path") {
40
+ if (prop === exitProxySymbol) {
41
+ return { path: [], stateRoot: recv as Statify<StatifiableObj> };
42
+ }
43
+
44
+ return extract(undefined, recv as Statify<StatifiableObj>, [prop]);
45
+ }
46
+
47
+ return Reflect.get(target, prop, recv);
48
+ },
49
+
50
+ set(target, prop, newVal, recv) {
51
+ const stateMetadata = getMetadataOf(recv as Statify<StatifiableObj>);
52
+
53
+ const had = Reflect.has(recv, prop);
54
+ const oldVal = Reflect.get(recv, prop) as unknown;
55
+
56
+ const result = Reflect.set(target, prop, newVal, recv);
57
+
58
+ try {
59
+ if (result) {
60
+ if (
61
+ typeof newVal === "object"
62
+ && newVal != null
63
+ && (newVal as any)[statifySealKey]
64
+ ) {
65
+ hook(
66
+ stateMetadata,
67
+ prop,
68
+ getMetadataOf(newVal as Statify<StatifiableObj>),
69
+ );
70
+ }
71
+
72
+ if (had) {
73
+ if (
74
+ oldVal !== newVal
75
+ && typeof oldVal === "object"
76
+ && oldVal != null
77
+ && (oldVal as any)[statifySealKey]
78
+ ) {
79
+ unhook(
80
+ stateMetadata,
81
+ prop,
82
+ getMetadataOf(oldVal as Statify<StatifiableObj>),
83
+ );
84
+ }
85
+
86
+ stateMetadata.emit("replaceProp", newVal, prop);
87
+ }
88
+
89
+ stateMetadata.emit("setProp", newVal, prop);
90
+
91
+ const maybeEventEmitterAtPath = stateMetadata.eventEmitterAtPathMaybe([prop]);
92
+ if (maybeEventEmitterAtPath) {
93
+ if (had) {
94
+ maybeEventEmitterAtPath.emit("replace", newVal);
95
+ }
96
+
97
+ maybeEventEmitterAtPath.emit("set", newVal);
98
+
99
+ emitDescendantPathEvents(
100
+ stateMetadata,
101
+ [prop],
102
+ newVal,
103
+ had,
104
+ );
105
+ }
106
+ }
107
+ } finally {
108
+ return result;
109
+ }
110
+ },
111
+
112
+ has(target, prop) {
113
+ if (globalStateMode === "extract-proxy-path" && prop === exitProxySymbol) {
114
+ return true;
115
+ }
116
+
117
+ return Reflect.has(target, prop);
118
+ },
119
+ });
120
+
121
+ blanketProtoMemo.set(actualProto, blanket);
122
+ blanketProtoSet.add(blanket);
123
+
124
+ return blanket;
125
+ }
126
+ //
127
+
128
+ export function MakeStatified<
129
+ V extends ClassType<T, U>,
130
+ T extends any[],
131
+ U extends object,
132
+ >(
133
+ OriginalClass: V,
134
+ ): V & ClassType<T, Record<string | symbol, any>> {
135
+ const maybeMemoed = classMapMemo.get(OriginalClass);
136
+ if (maybeMemoed) return maybeMemoed as V;
137
+
138
+ const Statified = function (...args: T) {
139
+ const ctor = new.target ?? Statified;
140
+
141
+ const inst = Reflect.construct(OriginalClass, args, ctor) as Statify<U>;
142
+
143
+ setMetadataOf(inst, new StateMetadata());
144
+
145
+ const actualProto = ctor.prototype;
146
+ const finalProto = blanketProtoSet.has(actualProto)
147
+ ? actualProto
148
+ : getBlanketPrototype(actualProto);
149
+
150
+ Reflect.setPrototypeOf(inst, finalProto);
151
+
152
+ return inst;
153
+ } as unknown as (new (...args: T) => Statify<U>); // TODO: solve
154
+
155
+ Statified.prototype = new Proxy(
156
+ {
157
+ [statifySealKey]: true,
158
+ } as { [statifySealKey]: true; [exitProxySymbol]: ExitProxyValue; },
159
+ {
160
+ get(target, prop, recv) {
161
+ if (prop === statifySealKey) return true;
162
+
163
+ const result = Reflect.get(target, prop, recv);
164
+
165
+ if (globalStateMode === "extract-proxy-path") {
166
+ if (prop === exitProxySymbol) {
167
+ return { path: [], stateRoot: recv };
168
+ }
169
+
170
+ return extract(result, recv, [prop])
171
+ }
172
+
173
+ return result;
174
+ },
175
+
176
+ has(target, prop) {
177
+ if (globalStateMode === "extract-proxy-path") {
178
+ if (prop === exitProxySymbol) {
179
+ return true;
180
+ }
181
+ }
182
+
183
+ return Reflect.has(target, prop);
184
+ },
185
+ }
186
+ );
187
+ Reflect.setPrototypeOf(Statified.prototype, OriginalClass.prototype);
188
+
189
+ classMapMemo.set(OriginalClass, Statified);
190
+
191
+ return Statified as V; // & ClassType<T, Record<string | symbol, any>>;
192
+ }
193
+
194
+ export const BaseStatified = MakeStatified(Object);
@@ -0,0 +1,48 @@
1
+ import { exitProxySymbol, setMetadataOf, statifySealKey, type Statify } from "../internals.js";
2
+ import { StateMetadata, type ExitProxyValue, type StatifiableProp } from "../low.js";
3
+
4
+ export class StatifiedSet<T extends StatifiableProp> extends Set<T> implements Statify<Set<T>> {
5
+ [statifySealKey]: true = true;
6
+ declare [exitProxySymbol]: ExitProxyValue;
7
+
8
+ #metadata: StateMetadata;
9
+
10
+ constructor(args?: Iterable<T> | null) {
11
+ super(args);
12
+
13
+ this.#metadata = setMetadataOf(this, new StateMetadata());
14
+ }
15
+
16
+ add(value: T): this {
17
+ if (!super.has(value)) {
18
+ super.add(value);
19
+ this.#metadata.emit("addItem", value);
20
+ this.#metadata.emit("sizeChanged");
21
+ }
22
+
23
+ return this;
24
+ }
25
+
26
+ clear(): void {
27
+ const oldValues = [...this.values()];
28
+
29
+ super.clear();
30
+
31
+ for (const value of oldValues) {
32
+ this.#metadata.emit("deleteItem", value);
33
+ }
34
+ if (oldValues.length > 0) {
35
+ this.#metadata.emit("sizeChanged");
36
+ }
37
+ }
38
+
39
+ delete(value: T): boolean {
40
+ if (super.delete(value)) {
41
+ this.#metadata.emit("deleteItem", value);
42
+ this.#metadata.emit("sizeChanged");
43
+ return true;
44
+ }
45
+
46
+ return false;
47
+ }
48
+ }
package/src/test.ts ADDED
@@ -0,0 +1,49 @@
1
+ import $ from "./high.js";
2
+ import { BaseStatified } from "./quirks/basestatified.js";
3
+ import { StatifiedSet } from "./quirks/set.js";
4
+
5
+ const stateful = $.state<{ a: ({ d: number })[], b?: { c?: { d?: Set<number> } } }>({ a: [] });
6
+
7
+ $.onSpliceInElement(() => stateful.a, (v) => {
8
+ console.log("pushed", v);
9
+
10
+ $.onSet(() => v.d, (newValue) => {
11
+ console.log("New value is", newValue);
12
+ });
13
+ });
14
+
15
+ $.onSet(() => stateful.b?.c?.d, (newValue) => {
16
+ console.log("New value of .b.c.d is", newValue);
17
+ });
18
+
19
+ const b = $.state({ d: 3 });
20
+ stateful.a.push(b);
21
+
22
+ b.d = 10;
23
+
24
+ const asdf = $.state({ d: new Set<number>() });
25
+ const asdf2 = $.state({ c: asdf });
26
+
27
+ // const aassddff = $.state(new Set());
28
+ stateful.b = asdf2;
29
+
30
+ class Wayland extends BaseStatified {
31
+ displays = new StatifiedSet();
32
+
33
+ #state = 0;
34
+ get state() { return this.#state; }
35
+ set state(v: number) {
36
+ console.log("set", v)
37
+ this.#state = v;
38
+ super.state = v;
39
+ }
40
+
41
+ constructor() {
42
+ super();
43
+ }
44
+ }
45
+
46
+ const w = new Wayland();
47
+ $.onSet(() => w.state, (v) => console.log("asdf awawa!!", v));
48
+
49
+ w.state = 5;
package/src/utils.ts ADDED
@@ -0,0 +1,31 @@
1
+ export function ofWhichIsProto<T extends object, U extends symbol>(target: object, expect: Map<T, U>): U | undefined {
2
+ let current = Reflect.getPrototypeOf(target);
3
+
4
+ while (current) {
5
+ const correspondingSymbol = expect.get(current as T);
6
+ if (correspondingSymbol) {
7
+ return correspondingSymbol;
8
+ }
9
+
10
+ current = Reflect.getPrototypeOf(current);
11
+ }
12
+
13
+ return undefined;
14
+ }
15
+
16
+ export const isSetProto = Symbol();
17
+ export const supportedQuirks = new Map([
18
+ [Set.prototype, isSetProto]
19
+ ] as const);
20
+
21
+
22
+ export function isListInGrid<T>(
23
+ grid: T[][],
24
+ list: T[],
25
+ ) {
26
+ return grid.some((row) => {
27
+ if (row.length !== list.length) return false;
28
+
29
+ return row.every((v, i) => v === list[i]);
30
+ });
31
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ // Visit https://aka.ms/tsconfig to read more about this file
3
+ "compilerOptions": {
4
+ // File Layout
5
+ "rootDir": "./src",
6
+ "outDir": "./dist",
7
+
8
+ // Environment Settings
9
+ // See also https://aka.ms/tsconfig/module
10
+ "module": "nodenext",
11
+ "target": "esnext",
12
+ // "types": [],
13
+ // For nodejs:
14
+ "lib": ["esnext"],
15
+ "types": ["node"],
16
+ // and npm install -D @types/node
17
+
18
+ // Other Outputs
19
+ "sourceMap": true,
20
+ "declaration": true,
21
+ "declarationMap": true,
22
+
23
+ // Stricter Typechecking Options
24
+ "noUncheckedIndexedAccess": true,
25
+ "exactOptionalPropertyTypes": true,
26
+
27
+ // Style Options
28
+ // "noImplicitReturns": true,
29
+ // "noImplicitOverride": true,
30
+ // "noUnusedLocals": true,
31
+ // "noUnusedParameters": true,
32
+ // "noFallthroughCasesInSwitch": true,
33
+ // "noPropertyAccessFromIndexSignature": true,
34
+
35
+ // Recommended Options
36
+ "strict": true,
37
+ "jsx": "react-jsx",
38
+ "verbatimModuleSyntax": true,
39
+ "isolatedModules": true,
40
+ "noUncheckedSideEffectImports": true,
41
+ "moduleDetection": "force",
42
+ "skipLibCheck": true,
43
+ }
44
+ }