ctx-core 4.5.0 → 4.6.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.
- package/all/rmemo/index.d.ts +6 -6
- package/all/rmemo/index.js +32 -35
- package/all/rmemo/index.test.ts +33 -21
- package/package.json +5 -5
package/all/rmemo/index.d.ts
CHANGED
|
@@ -14,17 +14,17 @@ export type rmemo_T<val_T> = memo_T<val_T>|sig_T<val_T>
|
|
|
14
14
|
export type memo_T<val_T> = (()=>val_T)&{
|
|
15
15
|
readonly _:val_T
|
|
16
16
|
readonly val:val_T
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
r?:WeakRef<()=>val_T>
|
|
18
|
+
memor:WeakRef<()=>val_T>[]
|
|
19
19
|
}
|
|
20
20
|
export type sig_T<val_T> = (()=>val_T)&{
|
|
21
21
|
_:val_T
|
|
22
22
|
readonly val:val_T
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
r?:WeakRef<()=>val_T>
|
|
24
|
+
memor:WeakRef<()=>val_T>[]
|
|
25
25
|
}
|
|
26
26
|
export type rmemo_val_T<sig_T> = sig_T extends { ():infer val_T }
|
|
27
27
|
? val_T
|
|
28
28
|
: unknown
|
|
29
|
-
export type rmemo_def_T<val_T> = (
|
|
30
|
-
export type rmemo_subscriber_T<val_T> = (
|
|
29
|
+
export type rmemo_def_T<val_T> = (sig:sig_T<val_T>)=>val_T
|
|
30
|
+
export type rmemo_subscriber_T<val_T> = (sig:sig_T<val_T>)=>unknown
|
package/all/rmemo/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/** @typedef {import('./index.d.ts').rmemo_subscriber_T} */
|
|
4
4
|
/** @typedef {import('./index.d.ts').sig_T} */
|
|
5
5
|
/** @type {WeakRef<memo_T>} */
|
|
6
|
-
let
|
|
6
|
+
let cur_memo
|
|
7
7
|
/** @type {Set<()=>unknown>} */
|
|
8
8
|
let queue = new Set
|
|
9
9
|
/**
|
|
@@ -13,41 +13,41 @@ let queue = new Set
|
|
|
13
13
|
* @private
|
|
14
14
|
*/
|
|
15
15
|
export function memo_(rmemo_def, ...subscriber_a) {
|
|
16
|
-
let
|
|
17
|
-
let rmrs
|
|
16
|
+
let memor
|
|
18
17
|
let memo = ()=>{
|
|
19
18
|
if (!('val' in memo)) {
|
|
20
|
-
|
|
19
|
+
memo.f()
|
|
21
20
|
}
|
|
22
|
-
if (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
~cur_rmr_refresh.S.indexOf(memo) || cur_rmr_refresh.S.push(memo)
|
|
21
|
+
if (cur_memo) {
|
|
22
|
+
if (!~memor.indexOf(cur_memo.r ||= new WeakRef(cur_memo.f))) memor.push(cur_memo.r)
|
|
23
|
+
if (cur_memo.f.l < memo.f.l + 1) cur_memo.f.l = memo.f.l + 1
|
|
24
|
+
// conditional in r calls this r_memo
|
|
25
|
+
cur_memo.f.s.push(memo)
|
|
26
|
+
// prevent this rmemo from GC while cur_memo is still active
|
|
27
|
+
if (!~cur_memo.f.S.indexOf(memo)) cur_memo.f.S.push(memo)
|
|
30
28
|
}
|
|
31
29
|
return memo.val
|
|
32
30
|
}
|
|
33
31
|
Object.defineProperty(memo, '_', {
|
|
34
32
|
get: memo,
|
|
35
33
|
set: val=>{
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
let i = memo.val
|
|
35
|
+
memo.val = val
|
|
36
|
+
if (i !== val) {
|
|
37
|
+
// val is available for other purposes
|
|
38
38
|
let run_queue = !queue.size
|
|
39
|
-
|
|
40
|
-
for (let
|
|
41
|
-
val =
|
|
39
|
+
i = 0
|
|
40
|
+
for (let r of memor) {
|
|
41
|
+
val = r.deref() // val is no longer used...saving bytes
|
|
42
42
|
if (!val) {
|
|
43
|
-
|
|
44
|
-
} else if (~val.s.indexOf(memo)) { // if conditional
|
|
43
|
+
memor.splice(i, 1)
|
|
44
|
+
} else if (~val.s.indexOf(memo)) { // if conditional r refresh calls this r_memo, add to queue
|
|
45
45
|
queue.add(val)
|
|
46
46
|
}
|
|
47
47
|
i++
|
|
48
48
|
}
|
|
49
49
|
// add reference to subscribers to prevent GC
|
|
50
|
-
memo.
|
|
50
|
+
memo.b ||=
|
|
51
51
|
subscriber_a.map(subscriber=>
|
|
52
52
|
memo_(subscriber$=>(
|
|
53
53
|
subscriber(memo),
|
|
@@ -68,24 +68,21 @@ export function memo_(rmemo_def, ...subscriber_a) {
|
|
|
68
68
|
}
|
|
69
69
|
},
|
|
70
70
|
})
|
|
71
|
-
|
|
72
|
-
let
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
memo.f = ()=>{
|
|
72
|
+
let prev_memo = cur_memo
|
|
73
|
+
cur_memo = memo
|
|
74
|
+
memo.f.s = []
|
|
75
75
|
try {
|
|
76
76
|
memo._ = rmemo_def(memo)
|
|
77
77
|
} catch (err) {
|
|
78
78
|
console.error(err)
|
|
79
79
|
}
|
|
80
|
-
|
|
80
|
+
cur_memo = prev_memo // finally is not necessary...catch does not throw
|
|
81
81
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
memo.
|
|
86
|
-
memo.rmr = new WeakRef(refresh)
|
|
87
|
-
refresh.s = []
|
|
88
|
-
refresh.S = []
|
|
82
|
+
memo.f.l = 0
|
|
83
|
+
memo.f.s = []
|
|
84
|
+
memo.f.S = []
|
|
85
|
+
memo.memor = memor = []
|
|
89
86
|
return memo
|
|
90
87
|
}
|
|
91
88
|
export { memo_ as memosig_ }
|
|
@@ -96,9 +93,9 @@ export { memo_ as memosig_ }
|
|
|
96
93
|
* @private
|
|
97
94
|
*/
|
|
98
95
|
export function sig_(init_val, ...subscriber_a) {
|
|
99
|
-
return memo_(
|
|
100
|
-
'val' in
|
|
101
|
-
?
|
|
96
|
+
return memo_(sig=>
|
|
97
|
+
'val' in sig
|
|
98
|
+
? sig.val
|
|
102
99
|
: init_val,
|
|
103
100
|
...subscriber_a)
|
|
104
101
|
}
|
package/all/rmemo/index.test.ts
CHANGED
|
@@ -7,28 +7,28 @@ import { sleep } from '../sleep/index.js'
|
|
|
7
7
|
import { memo_, type memo_T, memosig_, sig_ } from './index.js'
|
|
8
8
|
test('memo_|static value', ()=>{
|
|
9
9
|
let count = 0
|
|
10
|
-
const
|
|
10
|
+
const memo = memo_(()=>{
|
|
11
11
|
count++
|
|
12
12
|
return 'rmemo-value'
|
|
13
13
|
})
|
|
14
14
|
equal(count, 0)
|
|
15
|
-
equal(
|
|
15
|
+
equal(memo(), 'rmemo-value')
|
|
16
16
|
equal(count, 1)
|
|
17
|
-
equal(
|
|
17
|
+
equal(memo(), 'rmemo-value')
|
|
18
18
|
equal(count, 1)
|
|
19
19
|
})
|
|
20
20
|
test('memo_|def function|rmemo argument', ()=>{
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
`${_rmemo$.custom}-${
|
|
24
|
-
|
|
25
|
-
equal(
|
|
26
|
-
|
|
27
|
-
equal(
|
|
28
|
-
|
|
29
|
-
equal(
|
|
21
|
+
const sig = sig_('val0')
|
|
22
|
+
const memo:memo_T<string>&{ custom?:string } = memo_<string>((_rmemo$:memo_T<string>&{ custom?:string })=>
|
|
23
|
+
`${_rmemo$.custom}-${sig()}`)
|
|
24
|
+
memo.custom = 'custom_val0'
|
|
25
|
+
equal(memo(), 'custom_val0-val0')
|
|
26
|
+
memo.custom = 'custom_val1'
|
|
27
|
+
equal(memo(), 'custom_val0-val0')
|
|
28
|
+
sig._ = 'val1'
|
|
29
|
+
equal(memo(), 'custom_val1-val1')
|
|
30
30
|
})
|
|
31
|
-
test('
|
|
31
|
+
test('memo_|side effect', ()=>{
|
|
32
32
|
const history:string[] = []
|
|
33
33
|
const s = sig_('This')
|
|
34
34
|
memo_(()=>history.push(s()))()
|
|
@@ -38,6 +38,18 @@ test('r_memo_|side effect', ()=>{
|
|
|
38
38
|
s._ = 'test'
|
|
39
39
|
equal(history, ['This', 'is', 'a', 'test'])
|
|
40
40
|
})
|
|
41
|
+
test('memo_|undefined', ()=>{
|
|
42
|
+
let count = 0
|
|
43
|
+
const memo = memo_(()=>{
|
|
44
|
+
count++
|
|
45
|
+
return undefined
|
|
46
|
+
})
|
|
47
|
+
equal(count, 0)
|
|
48
|
+
equal(memo(), undefined)
|
|
49
|
+
equal(count, 1)
|
|
50
|
+
equal(memo(), undefined)
|
|
51
|
+
equal(count, 1)
|
|
52
|
+
})
|
|
41
53
|
test('memo_|conditional', ()=>{
|
|
42
54
|
const cond$ = sig_(true)
|
|
43
55
|
const a$ = sig_(1)
|
|
@@ -125,16 +137,16 @@ test('memo_|error|case 2', ()=>{
|
|
|
125
137
|
equal(r3(), 9)
|
|
126
138
|
})
|
|
127
139
|
test('sig_', ()=>{
|
|
128
|
-
const
|
|
129
|
-
equal(
|
|
130
|
-
|
|
131
|
-
equal(
|
|
140
|
+
const sig = sig_('val0')
|
|
141
|
+
equal(sig(), 'val0')
|
|
142
|
+
sig._ = 'val1'
|
|
143
|
+
equal(sig(), 'val1')
|
|
132
144
|
})
|
|
133
145
|
test('sig_|undefined', ()=>{
|
|
134
|
-
const
|
|
135
|
-
const
|
|
136
|
-
equal(
|
|
137
|
-
equal(
|
|
146
|
+
const sig = sig_(undefined)
|
|
147
|
+
const memo = memo_(()=>sig())
|
|
148
|
+
equal(sig(), undefined)
|
|
149
|
+
equal(memo(), undefined)
|
|
138
150
|
})
|
|
139
151
|
test('sig_|async subsubscriber|case 1', async ()=>{
|
|
140
152
|
let resolve:(user:{ id:string })=>void
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ctx-core",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.0",
|
|
4
4
|
"description": "ctx-core core library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ctx-core",
|
|
@@ -150,28 +150,28 @@
|
|
|
150
150
|
"import": {
|
|
151
151
|
"./rmemo": "{ memo_ }"
|
|
152
152
|
},
|
|
153
|
-
"limit": "
|
|
153
|
+
"limit": "344 B"
|
|
154
154
|
},
|
|
155
155
|
{
|
|
156
156
|
"name": "memo_ sig_",
|
|
157
157
|
"import": {
|
|
158
158
|
"./rmemo": "{ sig_, memo_ }"
|
|
159
159
|
},
|
|
160
|
-
"limit": "
|
|
160
|
+
"limit": "359 B"
|
|
161
161
|
},
|
|
162
162
|
{
|
|
163
163
|
"name": "memo_ sig_ be_ ctx_",
|
|
164
164
|
"import": {
|
|
165
165
|
"./rmemo": "{ sig_, memo_, be_, ctx_ }"
|
|
166
166
|
},
|
|
167
|
-
"limit": "
|
|
167
|
+
"limit": "519 B"
|
|
168
168
|
},
|
|
169
169
|
{
|
|
170
170
|
"name": "memo_ sig_ be_ ctx_ be_memo_pair_ be_sig_triple_",
|
|
171
171
|
"import": {
|
|
172
172
|
"./rmemo": "{ sig_, memo_, be_, ctx_, be_memo_pair_, be_sig_triple_ }"
|
|
173
173
|
},
|
|
174
|
-
"limit": "
|
|
174
|
+
"limit": "602 B"
|
|
175
175
|
}
|
|
176
176
|
],
|
|
177
177
|
"scripts": {
|