@esmj/signals 0.0.2
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/LICENSE +8 -0
- package/README.md +3 -0
- package/dist/index.d.mts +266 -0
- package/dist/index.d.ts +266 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
|
|
2
|
+
Copyright 2024 Miroslav Jancarik jancarikmiroslav@gmail.com
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { Observable, Observer } from '@esmj/observable';
|
|
2
|
+
|
|
3
|
+
let context = null;
|
|
4
|
+
|
|
5
|
+
function untrack(callback) {
|
|
6
|
+
const prevContext = context;
|
|
7
|
+
context = null;
|
|
8
|
+
const result = callback();
|
|
9
|
+
context = prevContext;
|
|
10
|
+
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const INTERNAL_OBSERVABLE = Symbol('internal observable');
|
|
15
|
+
const ORIGINAL_NEXT = Symbol('original next');
|
|
16
|
+
|
|
17
|
+
class Watcher extends Observable {
|
|
18
|
+
#pendings = new Set();
|
|
19
|
+
#notify = null;
|
|
20
|
+
|
|
21
|
+
constructor(notify) {
|
|
22
|
+
super();
|
|
23
|
+
|
|
24
|
+
this.#notify = notify;
|
|
25
|
+
|
|
26
|
+
this.pipe((observable) => {
|
|
27
|
+
const originalSubscribe = observable.subscribe.bind(observable);
|
|
28
|
+
observable.subscribe = (observer) => {
|
|
29
|
+
originalSubscribe(observer);
|
|
30
|
+
this.#pendings.add(observer);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return observable;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
watch(signal) {
|
|
37
|
+
// Watch State
|
|
38
|
+
if (typeof signal.next !== 'function') {
|
|
39
|
+
signal.next = () => {
|
|
40
|
+
return untrack(() => {
|
|
41
|
+
this.#notify();
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
signal.next[ORIGINAL_NEXT] = undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// WATCH Computed
|
|
48
|
+
if (
|
|
49
|
+
signal instanceof Computed &&
|
|
50
|
+
signal.next[ORIGINAL_NEXT] === undefined
|
|
51
|
+
) {
|
|
52
|
+
const originalNext = signal.next.bind(signal);
|
|
53
|
+
signal.next = () => {
|
|
54
|
+
return untrack(() => {
|
|
55
|
+
originalNext();
|
|
56
|
+
|
|
57
|
+
this.#notify();
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
signal.next[ORIGINAL_NEXT] = originalNext;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return this.subscribe(signal);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getPending() {
|
|
67
|
+
return Array.from(this.#pendings);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
unwatch(signal) {
|
|
71
|
+
signal.next = signal.next[ORIGINAL_NEXT];
|
|
72
|
+
|
|
73
|
+
return this.unsubscribe(signal);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let w = null;
|
|
78
|
+
|
|
79
|
+
function createWatcher(notify) {
|
|
80
|
+
w = new Watcher(notify);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
createWatcher(() => {
|
|
84
|
+
// TODO performance improvement
|
|
85
|
+
setTimeout(() => {
|
|
86
|
+
getPending().forEach((pending) => {
|
|
87
|
+
untrack(() => {
|
|
88
|
+
pending.get();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}, 0);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
function getPending() {
|
|
95
|
+
return w.getPending();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function watch(signal) {
|
|
99
|
+
return w.watch(signal);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function unwatch(signal) {
|
|
103
|
+
return w.unwatch(signal);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
class Computed extends Observer {
|
|
107
|
+
#dirty = true;
|
|
108
|
+
#prevContext = null;
|
|
109
|
+
#signal = null;
|
|
110
|
+
#context = this.#createNewContext();
|
|
111
|
+
#callback = null;
|
|
112
|
+
#options = null;
|
|
113
|
+
|
|
114
|
+
constructor(callback, options) {
|
|
115
|
+
super();
|
|
116
|
+
|
|
117
|
+
this.#callback = callback;
|
|
118
|
+
this.#options = options;
|
|
119
|
+
|
|
120
|
+
this.get = this.get.bind(this);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
#clearContextDependencies() {
|
|
124
|
+
Array.from(this.#context.dependencies.values()).forEach(
|
|
125
|
+
({ unsubscribe }) => {
|
|
126
|
+
unsubscribe();
|
|
127
|
+
},
|
|
128
|
+
);
|
|
129
|
+
this.#context.dependencies.clear();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
#createNewContext() {
|
|
133
|
+
const context = {
|
|
134
|
+
dependencies: new Map(),
|
|
135
|
+
observer: this,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
return context;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#savePrevContext() {
|
|
142
|
+
this.#prevContext = context;
|
|
143
|
+
context = this.#context;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
#restorePrevContext() {
|
|
147
|
+
context = this.#prevContext;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
next() {
|
|
151
|
+
this.#dirty = true;
|
|
152
|
+
this.#signal[INTERNAL_OBSERVABLE].next();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
get() {
|
|
156
|
+
if (!this.#signal) {
|
|
157
|
+
this.#signal = createSignal(this.#run(), this.#options);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (this.#dirty) {
|
|
161
|
+
this.#run();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return this.#signal.get();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
#run() {
|
|
168
|
+
this.#dirty = false;
|
|
169
|
+
|
|
170
|
+
this.#clearContextDependencies();
|
|
171
|
+
this.#savePrevContext();
|
|
172
|
+
|
|
173
|
+
let result;
|
|
174
|
+
try {
|
|
175
|
+
result = this.#callback();
|
|
176
|
+
} catch (e) {
|
|
177
|
+
result = e;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
this.#restorePrevContext();
|
|
181
|
+
|
|
182
|
+
// todo test it
|
|
183
|
+
if (result instanceof Promise) {
|
|
184
|
+
result = result
|
|
185
|
+
.then((value) => {
|
|
186
|
+
return value;
|
|
187
|
+
})
|
|
188
|
+
.catch((e) => {
|
|
189
|
+
throw e;
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (this.#signal) {
|
|
194
|
+
this.#signal.set(result);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (result instanceof Error) {
|
|
198
|
+
throw result;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function computed(callback, options) {
|
|
206
|
+
const instance = new Computed(callback, options);
|
|
207
|
+
|
|
208
|
+
return instance;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function effect(callback) {
|
|
212
|
+
let destructor;
|
|
213
|
+
|
|
214
|
+
let c = computed(
|
|
215
|
+
() => {
|
|
216
|
+
destructor?.();
|
|
217
|
+
destructor = callback();
|
|
218
|
+
},
|
|
219
|
+
{ equals: () => false },
|
|
220
|
+
);
|
|
221
|
+
c.get();
|
|
222
|
+
|
|
223
|
+
watch(c);
|
|
224
|
+
return () => {
|
|
225
|
+
destructor?.();
|
|
226
|
+
unwatch(c);
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function createSignal(value, options = {}) {
|
|
231
|
+
const equals = options?.equals ?? Object.is;
|
|
232
|
+
|
|
233
|
+
const observable = new Observable();
|
|
234
|
+
function get() {
|
|
235
|
+
if (typeof context === 'object' && context !== null) {
|
|
236
|
+
context.dependencies.set(
|
|
237
|
+
observable,
|
|
238
|
+
observable.subscribe(context.observer),
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (value instanceof Error) {
|
|
243
|
+
throw value;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return value;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// TODO implement batch updates
|
|
250
|
+
function set(_value) {
|
|
251
|
+
if (!equals(value, _value)) {
|
|
252
|
+
value = _value;
|
|
253
|
+
|
|
254
|
+
observable.next();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return value;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return { get, set, [INTERNAL_OBSERVABLE]: observable };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// alias
|
|
264
|
+
const state = createSignal;
|
|
265
|
+
|
|
266
|
+
export { computed, createSignal, createWatcher, effect, getPending, state, untrack, unwatch, watch };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { Observable, Observer } from '@esmj/observable';
|
|
2
|
+
|
|
3
|
+
let context = null;
|
|
4
|
+
|
|
5
|
+
function untrack(callback) {
|
|
6
|
+
const prevContext = context;
|
|
7
|
+
context = null;
|
|
8
|
+
const result = callback();
|
|
9
|
+
context = prevContext;
|
|
10
|
+
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const INTERNAL_OBSERVABLE = Symbol('internal observable');
|
|
15
|
+
const ORIGINAL_NEXT = Symbol('original next');
|
|
16
|
+
|
|
17
|
+
class Watcher extends Observable {
|
|
18
|
+
#pendings = new Set();
|
|
19
|
+
#notify = null;
|
|
20
|
+
|
|
21
|
+
constructor(notify) {
|
|
22
|
+
super();
|
|
23
|
+
|
|
24
|
+
this.#notify = notify;
|
|
25
|
+
|
|
26
|
+
this.pipe((observable) => {
|
|
27
|
+
const originalSubscribe = observable.subscribe.bind(observable);
|
|
28
|
+
observable.subscribe = (observer) => {
|
|
29
|
+
originalSubscribe(observer);
|
|
30
|
+
this.#pendings.add(observer);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return observable;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
watch(signal) {
|
|
37
|
+
// Watch State
|
|
38
|
+
if (typeof signal.next !== 'function') {
|
|
39
|
+
signal.next = () => {
|
|
40
|
+
return untrack(() => {
|
|
41
|
+
this.#notify();
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
signal.next[ORIGINAL_NEXT] = undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// WATCH Computed
|
|
48
|
+
if (
|
|
49
|
+
signal instanceof Computed &&
|
|
50
|
+
signal.next[ORIGINAL_NEXT] === undefined
|
|
51
|
+
) {
|
|
52
|
+
const originalNext = signal.next.bind(signal);
|
|
53
|
+
signal.next = () => {
|
|
54
|
+
return untrack(() => {
|
|
55
|
+
originalNext();
|
|
56
|
+
|
|
57
|
+
this.#notify();
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
signal.next[ORIGINAL_NEXT] = originalNext;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return this.subscribe(signal);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getPending() {
|
|
67
|
+
return Array.from(this.#pendings);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
unwatch(signal) {
|
|
71
|
+
signal.next = signal.next[ORIGINAL_NEXT];
|
|
72
|
+
|
|
73
|
+
return this.unsubscribe(signal);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let w = null;
|
|
78
|
+
|
|
79
|
+
function createWatcher(notify) {
|
|
80
|
+
w = new Watcher(notify);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
createWatcher(() => {
|
|
84
|
+
// TODO performance improvement
|
|
85
|
+
setTimeout(() => {
|
|
86
|
+
getPending().forEach((pending) => {
|
|
87
|
+
untrack(() => {
|
|
88
|
+
pending.get();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}, 0);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
function getPending() {
|
|
95
|
+
return w.getPending();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function watch(signal) {
|
|
99
|
+
return w.watch(signal);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function unwatch(signal) {
|
|
103
|
+
return w.unwatch(signal);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
class Computed extends Observer {
|
|
107
|
+
#dirty = true;
|
|
108
|
+
#prevContext = null;
|
|
109
|
+
#signal = null;
|
|
110
|
+
#context = this.#createNewContext();
|
|
111
|
+
#callback = null;
|
|
112
|
+
#options = null;
|
|
113
|
+
|
|
114
|
+
constructor(callback, options) {
|
|
115
|
+
super();
|
|
116
|
+
|
|
117
|
+
this.#callback = callback;
|
|
118
|
+
this.#options = options;
|
|
119
|
+
|
|
120
|
+
this.get = this.get.bind(this);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
#clearContextDependencies() {
|
|
124
|
+
Array.from(this.#context.dependencies.values()).forEach(
|
|
125
|
+
({ unsubscribe }) => {
|
|
126
|
+
unsubscribe();
|
|
127
|
+
},
|
|
128
|
+
);
|
|
129
|
+
this.#context.dependencies.clear();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
#createNewContext() {
|
|
133
|
+
const context = {
|
|
134
|
+
dependencies: new Map(),
|
|
135
|
+
observer: this,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
return context;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#savePrevContext() {
|
|
142
|
+
this.#prevContext = context;
|
|
143
|
+
context = this.#context;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
#restorePrevContext() {
|
|
147
|
+
context = this.#prevContext;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
next() {
|
|
151
|
+
this.#dirty = true;
|
|
152
|
+
this.#signal[INTERNAL_OBSERVABLE].next();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
get() {
|
|
156
|
+
if (!this.#signal) {
|
|
157
|
+
this.#signal = createSignal(this.#run(), this.#options);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (this.#dirty) {
|
|
161
|
+
this.#run();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return this.#signal.get();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
#run() {
|
|
168
|
+
this.#dirty = false;
|
|
169
|
+
|
|
170
|
+
this.#clearContextDependencies();
|
|
171
|
+
this.#savePrevContext();
|
|
172
|
+
|
|
173
|
+
let result;
|
|
174
|
+
try {
|
|
175
|
+
result = this.#callback();
|
|
176
|
+
} catch (e) {
|
|
177
|
+
result = e;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
this.#restorePrevContext();
|
|
181
|
+
|
|
182
|
+
// todo test it
|
|
183
|
+
if (result instanceof Promise) {
|
|
184
|
+
result = result
|
|
185
|
+
.then((value) => {
|
|
186
|
+
return value;
|
|
187
|
+
})
|
|
188
|
+
.catch((e) => {
|
|
189
|
+
throw e;
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (this.#signal) {
|
|
194
|
+
this.#signal.set(result);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (result instanceof Error) {
|
|
198
|
+
throw result;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function computed(callback, options) {
|
|
206
|
+
const instance = new Computed(callback, options);
|
|
207
|
+
|
|
208
|
+
return instance;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function effect(callback) {
|
|
212
|
+
let destructor;
|
|
213
|
+
|
|
214
|
+
let c = computed(
|
|
215
|
+
() => {
|
|
216
|
+
destructor?.();
|
|
217
|
+
destructor = callback();
|
|
218
|
+
},
|
|
219
|
+
{ equals: () => false },
|
|
220
|
+
);
|
|
221
|
+
c.get();
|
|
222
|
+
|
|
223
|
+
watch(c);
|
|
224
|
+
return () => {
|
|
225
|
+
destructor?.();
|
|
226
|
+
unwatch(c);
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function createSignal(value, options = {}) {
|
|
231
|
+
const equals = options?.equals ?? Object.is;
|
|
232
|
+
|
|
233
|
+
const observable = new Observable();
|
|
234
|
+
function get() {
|
|
235
|
+
if (typeof context === 'object' && context !== null) {
|
|
236
|
+
context.dependencies.set(
|
|
237
|
+
observable,
|
|
238
|
+
observable.subscribe(context.observer),
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (value instanceof Error) {
|
|
243
|
+
throw value;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return value;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// TODO implement batch updates
|
|
250
|
+
function set(_value) {
|
|
251
|
+
if (!equals(value, _value)) {
|
|
252
|
+
value = _value;
|
|
253
|
+
|
|
254
|
+
observable.next();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return value;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return { get, set, [INTERNAL_OBSERVABLE]: observable };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// alias
|
|
264
|
+
const state = createSignal;
|
|
265
|
+
|
|
266
|
+
export { computed, createSignal, createWatcher, effect, getPending, state, untrack, unwatch, watch };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var observable=require('@esmj/observable');var r=null;function h(n){let t=r;r=null;let e=n();return r=t,e}var x=Symbol("internal observable"),c=Symbol("original next"),f=class extends observable.Observable{#t=new Set;#e=null;constructor(t){super(),this.#e=t,this.pipe(e=>{let s=e.subscribe.bind(e);return e.subscribe=i=>{s(i),this.#t.add(i);},e});}watch(t){if(typeof t.next!="function"&&(t.next=()=>h(()=>{this.#e();}),t.next[c]=void 0),t instanceof o&&t.next[c]===void 0){let e=t.next.bind(t);t.next=()=>h(()=>{e(),this.#e();}),t.next[c]=e;}return this.subscribe(t)}getPending(){return Array.from(this.#t)}unwatch(t){return t.next=t.next[c],this.unsubscribe(t)}},u=null;function w(n){u=new f(n);}w(()=>{setTimeout(()=>{y().forEach(n=>{h(()=>{n.get();});});},0);});function y(){return u.getPending()}function g(n){return u.watch(n)}function m(n){return u.unwatch(n)}var o=class extends observable.Observer{#t=!0;#e=null;#n=null;#r=this.#u();#s=null;#i=null;constructor(t,e){super(),this.#s=t,this.#i=e,this.get=this.get.bind(this);}#o(){Array.from(this.#r.dependencies.values()).forEach(({unsubscribe:t})=>{t();}),this.#r.dependencies.clear();}#u(){return {dependencies:new Map,observer:this}}#h(){this.#e=r,r=this.#r;}#f(){r=this.#e;}next(){this.#t=!0,this.#n[x].next();}get(){return this.#n||(this.#n=d(this.#c(),this.#i)),this.#t&&this.#c(),this.#n.get()}#c(){this.#t=!1,this.#o(),this.#h();let t;try{t=this.#s();}catch(e){t=e;}if(this.#f(),t instanceof Promise&&(t=t.then(e=>e).catch(e=>{throw e})),this.#n&&this.#n.set(t),t instanceof Error)throw t;return t}};function E(n,t){return new o(n,t)}function P(n){let t,e=E(()=>{t?.(),t=n();},{equals:()=>!1});return e.get(),g(e),()=>{t?.(),m(e);}}function d(n,t={}){let e=t?.equals??Object.is,s=new observable.Observable;function i(){if(typeof r=="object"&&r!==null&&r.dependencies.set(s,s.subscribe(r.observer)),n instanceof Error)throw n;return n}function b(l){return e(n,l)||(n=l,s.next()),n}return {get:i,set:b,[x]:s}}var S=d;exports.computed=E;exports.createSignal=d;exports.createWatcher=w;exports.effect=P;exports.getPending=y;exports.state=S;exports.untrack=h;exports.unwatch=m;exports.watch=g;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {Observable,Observer}from'@esmj/observable';var r=null;function h(n){let t=r;r=null;let e=n();return r=t,e}var x=Symbol("internal observable"),c=Symbol("original next"),f=class extends Observable{#t=new Set;#e=null;constructor(t){super(),this.#e=t,this.pipe(e=>{let s=e.subscribe.bind(e);return e.subscribe=i=>{s(i),this.#t.add(i);},e});}watch(t){if(typeof t.next!="function"&&(t.next=()=>h(()=>{this.#e();}),t.next[c]=void 0),t instanceof o&&t.next[c]===void 0){let e=t.next.bind(t);t.next=()=>h(()=>{e(),this.#e();}),t.next[c]=e;}return this.subscribe(t)}getPending(){return Array.from(this.#t)}unwatch(t){return t.next=t.next[c],this.unsubscribe(t)}},u=null;function w(n){u=new f(n);}w(()=>{setTimeout(()=>{y().forEach(n=>{h(()=>{n.get();});});},0);});function y(){return u.getPending()}function g(n){return u.watch(n)}function m(n){return u.unwatch(n)}var o=class extends Observer{#t=!0;#e=null;#n=null;#r=this.#u();#s=null;#i=null;constructor(t,e){super(),this.#s=t,this.#i=e,this.get=this.get.bind(this);}#o(){Array.from(this.#r.dependencies.values()).forEach(({unsubscribe:t})=>{t();}),this.#r.dependencies.clear();}#u(){return {dependencies:new Map,observer:this}}#h(){this.#e=r,r=this.#r;}#f(){r=this.#e;}next(){this.#t=!0,this.#n[x].next();}get(){return this.#n||(this.#n=d(this.#c(),this.#i)),this.#t&&this.#c(),this.#n.get()}#c(){this.#t=!1,this.#o(),this.#h();let t;try{t=this.#s();}catch(e){t=e;}if(this.#f(),t instanceof Promise&&(t=t.then(e=>e).catch(e=>{throw e})),this.#n&&this.#n.set(t),t instanceof Error)throw t;return t}};function E(n,t){return new o(n,t)}function P(n){let t,e=E(()=>{t?.(),t=n();},{equals:()=>!1});return e.get(),g(e),()=>{t?.(),m(e);}}function d(n,t={}){let e=t?.equals??Object.is,s=new Observable;function i(){if(typeof r=="object"&&r!==null&&r.dependencies.set(s,s.subscribe(r.observer)),n instanceof Error)throw n;return n}function b(l){return e(n,l)||(n=l,s.next()),n}return {get:i,set:b,[x]:s}}var S=d;export{E as computed,d as createSignal,w as createWatcher,P as effect,y as getPending,S as state,h as untrack,m as unwatch,g as watch};
|
package/package.json
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@esmj/signals",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Tiny reactive signals.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"signals",
|
|
7
|
+
"reactive",
|
|
8
|
+
"effect",
|
|
9
|
+
"computed"
|
|
10
|
+
],
|
|
11
|
+
"main": "dist/index",
|
|
12
|
+
"module": "dist/index",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"require": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"typings": "dist/index.d.ts",
|
|
21
|
+
"scripts": {
|
|
22
|
+
"lint:cmd": "eslint -c ./.eslintrc.js --ignore-path ./.prettierignore --no-eslintrc",
|
|
23
|
+
"lint": "npm run lint:cmd -- 'src/**/*.ts'",
|
|
24
|
+
"lint:fix": "npm run lint -- --fix",
|
|
25
|
+
"dev": "node_modules/.bin/tsup --dts --watch --onSuccess 'node ./dist/index.mjs'",
|
|
26
|
+
"test": "node --test",
|
|
27
|
+
"test:watch": "npm run test -- --watchAll",
|
|
28
|
+
"preversion": "npm test && npm run lint && npm run build",
|
|
29
|
+
"version": "npm run changelog && git add CHANGELOG.md",
|
|
30
|
+
"postversion": "git push && git push --tags",
|
|
31
|
+
"commit": "node_modules/.bin/git-cz",
|
|
32
|
+
"changelog": "node_modules/.bin/conventional-changelog -p angular -i CHANGELOG.md -s -r 1",
|
|
33
|
+
"build": "node_modules/.bin/tsup --dts",
|
|
34
|
+
"prepare": "husky install"
|
|
35
|
+
},
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/mjancarik/esmj-task.git"
|
|
39
|
+
},
|
|
40
|
+
"author": "Miroslav Jancarik",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/mjancarik/esmj-task/issues"
|
|
44
|
+
},
|
|
45
|
+
"lint-staged": {
|
|
46
|
+
"**/*.{ts,js,mjs}": "npm run lint:cmd"
|
|
47
|
+
},
|
|
48
|
+
"commitlint": {
|
|
49
|
+
"extends": [
|
|
50
|
+
"@commitlint/config-conventional"
|
|
51
|
+
]
|
|
52
|
+
},
|
|
53
|
+
"config": {
|
|
54
|
+
"commitizen": {
|
|
55
|
+
"path": "git-cz"
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"registry": "https://registry.npmjs.org/",
|
|
60
|
+
"access": "public"
|
|
61
|
+
},
|
|
62
|
+
"homepage": "https://github.com/mjancarik/esmj-task#readme",
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@commitlint/cli": "^19.5.0",
|
|
65
|
+
"@commitlint/config-conventional": "^19.5.0",
|
|
66
|
+
"@typescript-eslint/eslint-plugin": "^8.12.2",
|
|
67
|
+
"@typescript-eslint/parser": "^8.12.2",
|
|
68
|
+
"commitizen": "^4.3.1",
|
|
69
|
+
"conventional-changelog-cli": "^5.0.0",
|
|
70
|
+
"cz-conventional-changelog": "^3.3.0",
|
|
71
|
+
"eslint": "^8.48.0",
|
|
72
|
+
"eslint-config-prettier": "^9.1.0",
|
|
73
|
+
"eslint-plugin-jasmine": "^4.2.2",
|
|
74
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
75
|
+
"git-cz": "^4.9.0",
|
|
76
|
+
"husky": "^9.1.6",
|
|
77
|
+
"lint-staged": "^15.2.10",
|
|
78
|
+
"prettier": "^3.3.3",
|
|
79
|
+
"tsup": "^8.3.5"
|
|
80
|
+
},
|
|
81
|
+
"dependencies": {
|
|
82
|
+
"@esmj/observable": "^0.1.1"
|
|
83
|
+
}
|
|
84
|
+
}
|