ctx-core 3.0.0 → 3.2.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/be_/index.js +44 -68
- package/all/rmemo/index.d.ts +13 -6
- package/all/rmemo/index.test.ts +8 -0
- package/package.json +3 -3
package/all/be_/index.js
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
import { globalThis__prop__ensure } from '../globalThis__prop__ensure/index.js'
|
|
2
|
-
|
|
3
|
-
let proto_ = Object.getPrototypeOf
|
|
4
|
-
let string_proto = proto_('')
|
|
5
|
-
let _null = null
|
|
6
|
-
let pending_sym = Symbol.for('pending')
|
|
7
|
-
let be_M_is_source_ = globalThis__prop__ensure(
|
|
8
|
-
Symbol.for('be_M_is_source_'),
|
|
9
|
-
()=>new WeakMap())
|
|
2
|
+
let be_M_is_source_ = globalThis.be_M_is_source_ ||= new WeakMap()
|
|
10
3
|
/** @typedef {import('./index.d.ts').Be}Be */
|
|
11
4
|
/** @typedef {import('./index.d.ts').Ctx}Ctx */
|
|
12
5
|
/** @typedef {import('./index.d.ts').MapCtx}MapCtx */
|
|
@@ -51,69 +44,60 @@ export function globalThis__be_(
|
|
|
51
44
|
export function be_(
|
|
52
45
|
id_OR_val__new,
|
|
53
46
|
val__new_OR_be__params,
|
|
54
|
-
be__params
|
|
47
|
+
be__params
|
|
55
48
|
) {
|
|
56
49
|
/** @type {string} */
|
|
57
|
-
let id
|
|
58
|
-
proto_(id_OR_val__new) === string_proto
|
|
59
|
-
? id_OR_val__new
|
|
60
|
-
: _null
|
|
50
|
+
let id
|
|
61
51
|
/** @type {be__val__T} */
|
|
62
|
-
let val__new
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
be__params
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
let be = (argv__ctx, params
|
|
52
|
+
let val__new
|
|
53
|
+
/** @type {is_source__T} */
|
|
54
|
+
let is_source_
|
|
55
|
+
/** @type {expired__T} */
|
|
56
|
+
let expired_
|
|
57
|
+
if (typeof id_OR_val__new === 'string') {
|
|
58
|
+
val__new = val__new_OR_be__params
|
|
59
|
+
id = id_OR_val__new
|
|
60
|
+
} else {
|
|
61
|
+
val__new = id_OR_val__new
|
|
62
|
+
be__params = val__new_OR_be__params
|
|
63
|
+
}
|
|
64
|
+
if (be__params) {
|
|
65
|
+
is_source_ = be__params.is_source_
|
|
66
|
+
expired_ = be__params.expired_
|
|
67
|
+
}
|
|
68
|
+
let be = (argv__ctx, params)=>{
|
|
79
69
|
if (!argv__ctx) {
|
|
80
|
-
throw new Error(`be
|
|
70
|
+
throw new Error(`be: no Ctx`)
|
|
81
71
|
}
|
|
82
72
|
let saved__val = be__val_(be, argv__ctx)
|
|
83
73
|
if (
|
|
84
|
-
|
|
85
|
-
&&
|
|
86
|
-
&&
|
|
74
|
+
!params?.force
|
|
75
|
+
&& be__has_(be, argv__ctx)
|
|
76
|
+
&& !expired_?.(argv__ctx)
|
|
87
77
|
) {
|
|
88
78
|
return saved__val
|
|
89
79
|
}
|
|
90
80
|
let ctx = source__map_ctx_(argv__ctx, is_source_)
|
|
91
81
|
if (!ctx) {
|
|
92
82
|
throw new Error(
|
|
93
|
-
`be: ${
|
|
94
|
-
String(id)
|
|
95
|
-
}: is_source_ must be true for at least one Ctx`)
|
|
83
|
+
`be: ${String(id)}: no is_source_ returns true`)
|
|
96
84
|
}
|
|
97
|
-
let pending = ctx.get(
|
|
85
|
+
let pending = ctx.get(Symbol.for('pending'))
|
|
98
86
|
if (!pending) {
|
|
99
|
-
pending = new Map
|
|
100
|
-
ctx.set(
|
|
87
|
+
pending = new Map
|
|
88
|
+
ctx.set(Symbol.for('pending'), pending)
|
|
101
89
|
}
|
|
102
90
|
if (pending.get(be)) {
|
|
103
|
-
let pending_value_a = []
|
|
104
|
-
for (let value of pending.values()) {
|
|
105
|
-
pending_value_a.push(value)
|
|
106
|
-
}
|
|
107
91
|
throw new Error(
|
|
108
92
|
`be_: ${
|
|
109
93
|
String(id)
|
|
110
|
-
}: circular
|
|
111
|
-
typeof pending_value === '
|
|
112
|
-
?
|
|
113
|
-
:
|
|
94
|
+
}: circular:\n${pending.values().map(pending_value=>
|
|
95
|
+
typeof pending_value === 'string'
|
|
96
|
+
? pending_value
|
|
97
|
+
: 'Function').join('\n')}`)
|
|
114
98
|
}
|
|
115
99
|
pending.set(be, id || be)
|
|
116
|
-
let val = val__new(argv__ctx, be, params)
|
|
100
|
+
let val = val__new ? val__new(argv__ctx, be, params) : null
|
|
117
101
|
ctx.set(be, val)
|
|
118
102
|
if (id) {
|
|
119
103
|
ctx.set(id, val)
|
|
@@ -162,7 +146,6 @@ export function ctx__set(
|
|
|
162
146
|
val,
|
|
163
147
|
is_source_
|
|
164
148
|
) {
|
|
165
|
-
if (!is_source_) is_source_ = ()=>true
|
|
166
149
|
let source__map_ctx = source__map_ctx_(ctx, is_source_)
|
|
167
150
|
if (source__map_ctx) {
|
|
168
151
|
source__map_ctx.set(be_OR_id, val)
|
|
@@ -189,25 +172,18 @@ export function ctx__delete(
|
|
|
189
172
|
ctx,
|
|
190
173
|
be_OR_id,
|
|
191
174
|
) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
if (isArray(ctx)) {
|
|
196
|
-
for (let i = 0; i < ctx.length; i++) {
|
|
197
|
-
if (is_source_(ctx[i], ctx)) {
|
|
198
|
-
ctx__delete(ctx[i], be_OR_id)
|
|
199
|
-
}
|
|
175
|
+
if (Array.isArray(ctx)) {
|
|
176
|
+
for (let _ctx of ctx) {
|
|
177
|
+
ctx__delete(_ctx, be_OR_id)
|
|
200
178
|
}
|
|
201
179
|
} else {
|
|
202
180
|
/** @type {MapCtx} */
|
|
203
181
|
let map_ctx = /** @type {any} */ctx
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
map_ctx.delete(id)
|
|
208
|
-
}
|
|
209
|
-
map_ctx.delete(be_OR_id)
|
|
182
|
+
let { id } = be_OR_id
|
|
183
|
+
if (id) {
|
|
184
|
+
map_ctx.delete(id)
|
|
210
185
|
}
|
|
186
|
+
map_ctx.delete(be_OR_id)
|
|
211
187
|
}
|
|
212
188
|
}
|
|
213
189
|
/**
|
|
@@ -217,7 +193,7 @@ export function ctx__delete(
|
|
|
217
193
|
* @private
|
|
218
194
|
*/
|
|
219
195
|
export function be__has_(be_or_id, argv__ctx) {
|
|
220
|
-
return
|
|
196
|
+
return Boolean(be__has__ctx_(be_or_id, argv__ctx))
|
|
221
197
|
}
|
|
222
198
|
/**
|
|
223
199
|
* @param {Be|string}be_or_id
|
|
@@ -226,7 +202,7 @@ export function be__has_(be_or_id, argv__ctx) {
|
|
|
226
202
|
* @private
|
|
227
203
|
*/
|
|
228
204
|
export function be__has__ctx_(be_or_id, argv__ctx) {
|
|
229
|
-
if (isArray(argv__ctx)) {
|
|
205
|
+
if (Array.isArray(argv__ctx)) {
|
|
230
206
|
for (let i = 0; i < argv__ctx.length; i++) {
|
|
231
207
|
const be__has__ctx = be__has__ctx_(be_or_id, argv__ctx[i])
|
|
232
208
|
if (be__has__ctx) return be__has__ctx
|
|
@@ -243,7 +219,7 @@ export function be__has__ctx_(be_or_id, argv__ctx) {
|
|
|
243
219
|
* @private
|
|
244
220
|
*/
|
|
245
221
|
export function be__val_(be_or_id, argv__ctx) {
|
|
246
|
-
if (isArray(argv__ctx)) {
|
|
222
|
+
if (Array.isArray(argv__ctx)) {
|
|
247
223
|
for (let i = 0; i < argv__ctx.length; i++) {
|
|
248
224
|
let ctx = argv__ctx[i]
|
|
249
225
|
const be__has__ctx = be__has__ctx_(be_or_id, ctx)
|
|
@@ -260,7 +236,7 @@ export function be__val_(be_or_id, argv__ctx) {
|
|
|
260
236
|
* @private
|
|
261
237
|
*/
|
|
262
238
|
export function source__map_ctx_(ctx, is_source_) {
|
|
263
|
-
if (isArray(ctx)) {
|
|
239
|
+
if (Array.isArray(ctx)) {
|
|
264
240
|
for (let i = 0; i < ctx.length; i++) {
|
|
265
241
|
let i_ctx = ctx[i]
|
|
266
242
|
let source__map_ctx = source__map_ctx_(i_ctx, is_source_)
|
package/all/rmemo/index.d.ts
CHANGED
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
export declare function rmemo_<val_T>(
|
|
2
2
|
def:rmemo_def_T<val_T>,
|
|
3
3
|
...subscriber_a:rmemo_subscriber_T<val_T>[]
|
|
4
|
-
):
|
|
4
|
+
):read_rmemo_T<val_T>
|
|
5
5
|
export declare function rsig_<val_T>(
|
|
6
6
|
init_val:val_T,
|
|
7
7
|
...subscriber_a:rmemo_subscriber_T<val_T>[]
|
|
8
|
-
):
|
|
9
|
-
export type rmemo_T<val_T> =
|
|
10
|
-
export type
|
|
11
|
-
export type
|
|
8
|
+
):readwrite_rmemo_T<val_T>
|
|
9
|
+
export type rmemo_T<val_T> = readwrite_rmemo_T<val_T>|read_rmemo_T<val_T>
|
|
10
|
+
export type readwrite_rmemo_T<val_T> = ((val?:val_T)=>val_T)&rmemo_o_T<val_T>
|
|
11
|
+
export type read_rmemo_T<val_T> = (()=>val_T)&read_rmemo_o_T<val_T>
|
|
12
|
+
export type rmemo_def_T<val_T> = (rmemo:readwrite_rmemo_T<val_T>)=>val_T
|
|
13
|
+
export type rmemo_subscriber_T<val_T> = (rmemo:readwrite_rmemo_T<val_T>)=>unknown
|
|
12
14
|
export type rmemo_o_T<val_T> = {
|
|
13
15
|
_:val_T
|
|
14
|
-
|
|
16
|
+
go():unknown
|
|
17
|
+
onset(val:val_T):unknown
|
|
18
|
+
refresh():val_T
|
|
19
|
+
}&read_rmemo_o_T<val_T>
|
|
20
|
+
export type read_rmemo_o_T<val_T> = {
|
|
21
|
+
readonly _:val_T
|
|
15
22
|
go():unknown
|
|
16
23
|
onset(val:val_T):unknown
|
|
17
24
|
refresh():val_T
|
package/all/rmemo/index.test.ts
CHANGED
|
@@ -37,21 +37,29 @@ test('rsig_|async subsubscriber|case 1', async ()=>{
|
|
|
37
37
|
let user0 = { id: 'id-0' }
|
|
38
38
|
let user1 = { id: 'id-1' }
|
|
39
39
|
let id$ = rsig_('id-0')
|
|
40
|
+
let call_count = 0
|
|
40
41
|
let user$ = rsig_<{ id:string }|null>(
|
|
41
42
|
null,
|
|
42
43
|
async (_user$)=>{
|
|
44
|
+
call_count++
|
|
43
45
|
id$()
|
|
44
46
|
let user:{ id:string } = await new Promise(_resolve=>resolve = _resolve)
|
|
45
47
|
_user$(user)
|
|
46
48
|
})
|
|
49
|
+
// equal(call_count, 0)
|
|
47
50
|
equal(user$(), null)
|
|
51
|
+
equal(call_count, 1)
|
|
48
52
|
resolve!(user0)
|
|
49
53
|
await sleep(0)
|
|
54
|
+
equal(call_count, 1)
|
|
50
55
|
equal(user$(), user0)
|
|
56
|
+
equal(call_count, 1)
|
|
51
57
|
id$('id-1')
|
|
58
|
+
equal(call_count, 2)
|
|
52
59
|
equal(user$(), user0)
|
|
53
60
|
resolve!(user1)
|
|
54
61
|
await sleep(0)
|
|
62
|
+
equal(call_count, 2)
|
|
55
63
|
})
|
|
56
64
|
test('rsig_|async subsubscriber|case 2', async ()=>{
|
|
57
65
|
let a$ = rsig_(1)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ctx-core",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "ctx-core core library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ctx-core",
|
|
@@ -114,7 +114,7 @@
|
|
|
114
114
|
"sinon": "^17.0.1",
|
|
115
115
|
"size-limit": "^11.0.0",
|
|
116
116
|
"ts-node": "^10.9.1",
|
|
117
|
-
"tsx": "^4.1.
|
|
117
|
+
"tsx": "^4.1.3",
|
|
118
118
|
"typescript": "next",
|
|
119
119
|
"uvu": "^0.5.6"
|
|
120
120
|
},
|
|
@@ -129,7 +129,7 @@
|
|
|
129
129
|
"import": {
|
|
130
130
|
"./be": "{ be_ }"
|
|
131
131
|
},
|
|
132
|
-
"limit": "
|
|
132
|
+
"limit": "465 B"
|
|
133
133
|
},
|
|
134
134
|
{
|
|
135
135
|
"name": "rmemo",
|