@esportsplus/reactivity 0.27.0 → 0.27.3

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.
@@ -0,0 +1,126 @@
1
+ import { defineProperty, isArray, isPromise } from '@esportsplus/utilities';
2
+ import { computed, dispose, effect, read, root, signal, write } from '~/system';
3
+ import { Computed, Signal } from '~/types';
4
+ import { COMPUTED, REACTIVE_ARRAY, REACTIVE_OBJECT, SIGNAL } from '~/constants';
5
+ import { ReactiveArray } from './array';
6
+
7
+
8
+ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
9
+ protected disposers: VoidFunction[] | null = null;
10
+
11
+
12
+ constructor(data: T | null) {
13
+ if (data == null) {
14
+ return;
15
+ }
16
+
17
+ for (let key in data) {
18
+ let value = data[key as keyof T],
19
+ type = typeof value;
20
+
21
+ if (type === 'function') {
22
+ let node = this[COMPUTED]( value as () => T[keyof T] );
23
+
24
+ defineProperty(this, key, {
25
+ enumerable: true,
26
+ get: () => read(node)
27
+ });
28
+
29
+ continue;
30
+ }
31
+
32
+ if (value == null || type !== 'object') {
33
+ // Skip isArray when possible
34
+ }
35
+ else if (isArray(value)) {
36
+ defineProperty(this, key, {
37
+ enumerable: true,
38
+ value: this[REACTIVE_ARRAY](value)
39
+ });
40
+
41
+ continue;
42
+ }
43
+
44
+ let node = signal(value);
45
+
46
+ defineProperty(this, key, {
47
+ enumerable: true,
48
+ get() {
49
+ return read(node);
50
+ },
51
+ set(v: typeof value) {
52
+ write(node, v);
53
+ }
54
+ });
55
+ }
56
+ }
57
+
58
+
59
+ protected [REACTIVE_ARRAY]<U>(value: U[]): ReactiveArray<U> {
60
+ let node = new ReactiveArray(...value);
61
+
62
+ (this.disposers ??= []).push( () => node.dispose() );
63
+
64
+ return node;
65
+ }
66
+
67
+ protected [COMPUTED]<T extends Computed<ReturnType<T>>['fn']>(value: T) {
68
+ return root(() => {
69
+ let node: Computed<ReturnType<T>> | Signal<ReturnType<T> | undefined> = computed(value);
70
+
71
+ if (isPromise(node.value)) {
72
+ let factory = node,
73
+ version = 0;
74
+
75
+ node = signal<ReturnType<T> | undefined>(undefined);
76
+
77
+ (this.disposers ??= []).push(
78
+ effect(() => {
79
+ let id = ++version;
80
+
81
+ (read(factory) as Promise<ReturnType<T>>).then((v) => {
82
+ if (id !== version) {
83
+ return;
84
+ }
85
+
86
+ write(node as Signal<typeof v>, v);
87
+ });
88
+ })
89
+ )
90
+ }
91
+ else {
92
+ (this.disposers ??= []).push(() => dispose(node as Computed<ReturnType<T>>));
93
+ }
94
+
95
+ return node;
96
+ });
97
+ }
98
+
99
+ protected [SIGNAL]<T>(value: T) {
100
+ return signal(value);
101
+ }
102
+
103
+
104
+ dispose() {
105
+ let disposers = this.disposers,
106
+ disposer;
107
+
108
+ if (!disposers) {
109
+ return;
110
+ }
111
+
112
+ while (disposer = disposers.pop()) {
113
+ disposer();
114
+ }
115
+ }
116
+ }
117
+
118
+ Object.defineProperty(ReactiveObject.prototype, REACTIVE_OBJECT, { value: true });
119
+
120
+
121
+ const isReactiveObject = (value: any): value is ReactiveObject<any> => {
122
+ return typeof value === 'object' && value !== null && value[REACTIVE_OBJECT] === true;
123
+ };
124
+
125
+
126
+ export { isReactiveObject, ReactiveObject };
package/src/types.ts CHANGED
@@ -43,9 +43,9 @@ type Reactive<T> = T extends (...args: unknown[]) => Promise<infer R>
43
43
  : T extends (...args: any[]) => infer R
44
44
  ? R & { readonly [READONLY]: true }
45
45
  : T extends (infer U)[]
46
- ? U[] & Pick<ReactiveArray<U>, 'clear' | 'on' | 'once'>
46
+ ? U[] & Pick<ReactiveArray<U>, 'clear' | 'dispose' | 'on' | 'once'>
47
47
  : T extends Record<PropertyKey, unknown>
48
- ? { [K in keyof T]: T[K] } & { dispose: VoidFunction }
48
+ ? { [K in keyof T]: Reactive<T[K]>; } & { dispose: VoidFunction }
49
49
  : T;
50
50
 
51
51
  type Signal<T> = {
package/test/debug.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { reactive } from '@esportsplus/reactivity';
2
+
3
+ let value = reactive({
4
+ hey: () => 'sadasd'
5
+ });
6
+
7
+ console.log(value.hey);
@@ -1,6 +1,6 @@
1
1
  import { resolve } from 'path';
2
2
  import { defineConfig } from 'vite';
3
- import reactivity from '../build/transformer/plugins/vite.js';
3
+ import reactivity from '../build/compiler/plugins/vite.js';
4
4
 
5
5
 
6
6
  export default defineConfig({
@@ -8,6 +8,7 @@ export default defineConfig({
8
8
  lib: {
9
9
  entry: {
10
10
  arrays: resolve(__dirname, 'arrays.ts'),
11
+ debug: resolve(__dirname, 'debug.ts'),
11
12
  effects: resolve(__dirname, 'effects.ts'),
12
13
  index: resolve(__dirname, 'index.ts'),
13
14
  nested: resolve(__dirname, 'nested.ts'),
@@ -1,4 +0,0 @@
1
- import { ts } from '@esportsplus/typescript';
2
- import type { Bindings } from '../types.js';
3
- declare const _default: (sourceFile: ts.SourceFile, bindings: Bindings, ns: string, checker?: ts.TypeChecker) => string;
4
- export default _default;
@@ -1,258 +0,0 @@
1
- import { ts } from '@esportsplus/typescript';
2
- import { ast, code as c, imports } from '@esportsplus/typescript/compiler';
3
- import { COMPILER_ENTRYPOINT, COMPILER_TYPES, PACKAGE } from '../constants.js';
4
- const COMPOUND_OPERATORS = new Map([
5
- [ts.SyntaxKind.AmpersandAmpersandEqualsToken, '&&'],
6
- [ts.SyntaxKind.AmpersandEqualsToken, '&'],
7
- [ts.SyntaxKind.AsteriskAsteriskEqualsToken, '**'],
8
- [ts.SyntaxKind.AsteriskEqualsToken, '*'],
9
- [ts.SyntaxKind.BarBarEqualsToken, '||'],
10
- [ts.SyntaxKind.BarEqualsToken, '|'],
11
- [ts.SyntaxKind.CaretEqualsToken, '^'],
12
- [ts.SyntaxKind.GreaterThanGreaterThanEqualsToken, '>>'],
13
- [ts.SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken, '>>>'],
14
- [ts.SyntaxKind.LessThanLessThanEqualsToken, '<<'],
15
- [ts.SyntaxKind.MinusEqualsToken, '-'],
16
- [ts.SyntaxKind.PercentEqualsToken, '%'],
17
- [ts.SyntaxKind.PlusEqualsToken, '+'],
18
- [ts.SyntaxKind.QuestionQuestionEqualsToken, '??'],
19
- [ts.SyntaxKind.SlashEqualsToken, '/']
20
- ]);
21
- function findBinding(bindings, name, node) {
22
- for (let i = 0, n = bindings.length; i < n; i++) {
23
- let b = bindings[i];
24
- if (b.name === name && isInScope(node, b)) {
25
- return b;
26
- }
27
- }
28
- return undefined;
29
- }
30
- function findEnclosingScope(node) {
31
- let current = node.parent;
32
- while (current) {
33
- if (ts.isBlock(current) ||
34
- ts.isSourceFile(current) ||
35
- ts.isFunctionDeclaration(current) ||
36
- ts.isFunctionExpression(current) ||
37
- ts.isArrowFunction(current) ||
38
- ts.isForStatement(current) ||
39
- ts.isForInStatement(current) ||
40
- ts.isForOfStatement(current)) {
41
- return current;
42
- }
43
- current = current.parent;
44
- }
45
- return node.getSourceFile();
46
- }
47
- function isInDeclarationInit(node) {
48
- let parent = node.parent;
49
- return ts.isVariableDeclaration(parent) && parent.initializer === node;
50
- }
51
- function isInScope(reference, binding) {
52
- let current = reference;
53
- while (current) {
54
- if (current === binding.scope) {
55
- return true;
56
- }
57
- current = current.parent;
58
- }
59
- return false;
60
- }
61
- function isReactiveCall(node, checker) {
62
- if (!ts.isIdentifier(node.expression)) {
63
- return false;
64
- }
65
- if (node.expression.text !== COMPILER_ENTRYPOINT) {
66
- return false;
67
- }
68
- return imports.isFromPackage(node.expression, PACKAGE, checker);
69
- }
70
- function isReactiveReassignment(node, checker) {
71
- let parent = node.parent;
72
- if (ts.isBinaryExpression(parent) &&
73
- parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
74
- parent.right === node &&
75
- ts.isCallExpression(node)) {
76
- return isReactiveCall(node, checker);
77
- }
78
- return false;
79
- }
80
- function isWriteContext(node) {
81
- let parent = node.parent;
82
- if (ts.isBinaryExpression(parent) && parent.left === node) {
83
- let op = parent.operatorToken.kind;
84
- if (op === ts.SyntaxKind.EqualsToken) {
85
- return 'simple';
86
- }
87
- if (COMPOUND_OPERATORS.has(op)) {
88
- return 'compound';
89
- }
90
- }
91
- if (ts.isPostfixUnaryExpression(parent) || ts.isPrefixUnaryExpression(parent)) {
92
- let op = parent.operator;
93
- if (op === ts.SyntaxKind.PlusPlusToken || op === ts.SyntaxKind.MinusMinusToken) {
94
- return 'increment';
95
- }
96
- }
97
- return false;
98
- }
99
- function visit(ctx, node) {
100
- if (ts.isCallExpression(node) &&
101
- node.arguments.length > 0 &&
102
- isReactiveCall(node, ctx.checker)) {
103
- let arg = node.arguments[0], classification = COMPILER_TYPES.Signal;
104
- if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg)) {
105
- classification = COMPILER_TYPES.Computed;
106
- }
107
- else if (ts.isObjectLiteralExpression(arg) || ts.isArrayLiteralExpression(arg)) {
108
- classification = null;
109
- }
110
- if (classification) {
111
- let varName = null;
112
- if (node.parent && ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) {
113
- varName = node.parent.name.text;
114
- }
115
- else if (node.parent &&
116
- ts.isBinaryExpression(node.parent) &&
117
- node.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
118
- ts.isIdentifier(node.parent.left)) {
119
- varName = node.parent.left.text;
120
- }
121
- if (varName) {
122
- let scope = findEnclosingScope(node);
123
- ctx.scopedBindings.push({ name: varName, scope, type: classification });
124
- ctx.bindings.set(varName, classification);
125
- }
126
- if (classification === COMPILER_TYPES.Computed) {
127
- let argStart = arg.getStart(ctx.sourceFile);
128
- ctx.computedArgRanges.push({ end: arg.end, start: argStart });
129
- let argCtx = {
130
- argStart,
131
- innerReplacements: [],
132
- ns: ctx.ns,
133
- scopedBindings: ctx.scopedBindings,
134
- sourceFile: ctx.sourceFile
135
- };
136
- visitArg(argCtx, arg);
137
- let argText = c.replace(arg.getText(ctx.sourceFile), argCtx.innerReplacements);
138
- ctx.replacements.push({
139
- end: node.end,
140
- newText: `${ctx.ns}.computed(${argText})`,
141
- start: node.pos
142
- });
143
- }
144
- else {
145
- ctx.replacements.push({
146
- end: node.end,
147
- newText: `${ctx.ns}.signal(${arg.getText(ctx.sourceFile)})`,
148
- start: node.pos
149
- });
150
- }
151
- }
152
- }
153
- if (ts.isIdentifier(node) && node.parent && !isInDeclarationInit(node.parent)) {
154
- if (ts.isPropertyAccessExpression(node.parent) && node.parent.name === node) {
155
- ts.forEachChild(node, n => visit(ctx, n));
156
- return;
157
- }
158
- let nodeStart = node.getStart(ctx.sourceFile);
159
- if (ast.inRange(ctx.computedArgRanges, nodeStart, node.end)) {
160
- ts.forEachChild(node, n => visit(ctx, n));
161
- return;
162
- }
163
- let binding = findBinding(ctx.scopedBindings, node.text, node), name = node.text;
164
- if (binding && node.parent) {
165
- if (!isReactiveReassignment(node.parent, ctx.checker) &&
166
- !(ts.isTypeOfExpression(node.parent) && node.parent.expression === node)) {
167
- let writeCtx = isWriteContext(node);
168
- if (writeCtx) {
169
- if (binding.type !== COMPILER_TYPES.Computed) {
170
- let parent = node.parent;
171
- if (writeCtx === 'simple' && ts.isBinaryExpression(parent)) {
172
- ctx.replacements.push({
173
- end: parent.end,
174
- newText: `${ctx.ns}.write(${name}, ${parent.right.getText(ctx.sourceFile)})`,
175
- start: parent.pos
176
- });
177
- }
178
- else if (writeCtx === 'compound' && ts.isBinaryExpression(parent)) {
179
- let op = COMPOUND_OPERATORS.get(parent.operatorToken.kind) ?? '+';
180
- ctx.replacements.push({
181
- end: parent.end,
182
- newText: `${ctx.ns}.write(${name}, ${name}.value ${op} ${parent.right.getText(ctx.sourceFile)})`,
183
- start: parent.pos
184
- });
185
- }
186
- else if (writeCtx === 'increment') {
187
- let delta = parent.operator === ts.SyntaxKind.PlusPlusToken ? '+ 1' : '- 1', isPrefix = ts.isPrefixUnaryExpression(parent);
188
- if (ts.isExpressionStatement(parent.parent)) {
189
- ctx.replacements.push({
190
- end: parent.end,
191
- newText: `${ctx.ns}.write(${name}, ${name}.value ${delta})`,
192
- start: parent.pos
193
- });
194
- }
195
- else if (isPrefix) {
196
- ctx.replacements.push({
197
- end: parent.end,
198
- newText: `(${ctx.ns}.write(${name}, ${name}.value ${delta}), ${name}.value)`,
199
- start: parent.pos
200
- });
201
- }
202
- else {
203
- let tmp = `_t${ctx.tmpCounter++}`;
204
- ctx.replacements.push({
205
- end: parent.end,
206
- newText: `((${tmp}) => (${ctx.ns}.write(${name}, ${tmp} ${delta}), ${tmp}))(${name}.value)`,
207
- start: parent.pos
208
- });
209
- }
210
- }
211
- }
212
- }
213
- else {
214
- ctx.replacements.push({
215
- end: node.end,
216
- newText: `${ctx.ns}.read(${name})`,
217
- start: node.pos
218
- });
219
- }
220
- }
221
- }
222
- }
223
- ts.forEachChild(node, n => visit(ctx, n));
224
- }
225
- function visitArg(ctx, node) {
226
- if (ts.isIdentifier(node) && node.parent) {
227
- if ((ts.isPropertyAccessExpression(node.parent) && node.parent.name === node) ||
228
- (ts.isCallExpression(node.parent) && node.parent.expression === node)) {
229
- ts.forEachChild(node, n => visitArg(ctx, n));
230
- return;
231
- }
232
- if (findBinding(ctx.scopedBindings, node.text, node)) {
233
- ctx.innerReplacements.push({
234
- end: node.end - ctx.argStart,
235
- newText: `${ctx.ns}.read(${node.text})`,
236
- start: node.getStart(ctx.sourceFile) - ctx.argStart
237
- });
238
- }
239
- }
240
- ts.forEachChild(node, n => visitArg(ctx, n));
241
- }
242
- export default (sourceFile, bindings, ns, checker) => {
243
- let code = sourceFile.getFullText(), ctx = {
244
- bindings,
245
- checker,
246
- computedArgRanges: [],
247
- ns,
248
- replacements: [],
249
- scopedBindings: [],
250
- sourceFile,
251
- tmpCounter: 0
252
- };
253
- visit(ctx, sourceFile);
254
- if (ctx.replacements.length === 0) {
255
- return code;
256
- }
257
- return c.replace(code, ctx.replacements);
258
- };