ctx-core 4.3.1 → 4.5.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_memo_pair/index.d.ts +25 -0
- package/all/{be_r_rmemo_pair → be_memo_pair}/index.js +5 -5
- package/all/{be_r_rmemo_pair → be_memo_pair}/index.test.ts +20 -20
- package/all/be_sig_triple/index.d.ts +26 -0
- package/all/{be_rw_rmemo_triple → be_sig_triple}/index.js +8 -8
- package/all/{be_rw_rmemo_triple → be_sig_triple}/index.test.ts +14 -14
- package/all/index.d.ts +3 -2
- package/all/index.js +3 -2
- package/all/primitive/index.d.ts +1 -1
- package/all/rmemo/index.d.ts +20 -18
- package/all/rmemo/index.js +82 -58
- package/all/rmemo/index.test.ts +236 -134
- package/package.json +16 -16
- package/rmemo/index.d.ts +3 -2
- package/rmemo/index.js +2 -2
- package/all/be_r_rmemo_pair/index.d.ts +0 -25
- package/all/be_rw_rmemo_triple/index.d.ts +0 -26
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Be, be__val__new_T, be_config_T, Ctx } from '../be_/index.js'
|
|
2
|
+
import type { memo_T, rmemo_def_T } from '../rmemo/index.js'
|
|
3
|
+
export declare function be_memo_pair_<
|
|
4
|
+
val_T,
|
|
5
|
+
_memo_T extends memo_T<val_T> = memo_T<val_T>,
|
|
6
|
+
ctx_T extends Ctx = Ctx
|
|
7
|
+
>(be: Be<_memo_T, ctx_T>):be_memo_pair_T<val_T, _memo_T, ctx_T>
|
|
8
|
+
export declare function be_memo_pair_<
|
|
9
|
+
val_T,
|
|
10
|
+
_memo_T extends memo_T<val_T> = memo_T<val_T>,
|
|
11
|
+
ctx_T extends Ctx = Ctx
|
|
12
|
+
>(
|
|
13
|
+
rmemo__new:be__val__new_T<val_T>,
|
|
14
|
+
...subscriber_a_THEN_config:
|
|
15
|
+
|[...rmemo_def_T<val_T>[]]
|
|
16
|
+
|[...rmemo_def_T<val_T>[], config:be_config_T]
|
|
17
|
+
):be_memo_pair_T<val_T, _memo_T, ctx_T>
|
|
18
|
+
export type be_memo_pair_T<
|
|
19
|
+
val_T,
|
|
20
|
+
_memo_T extends memo_T<val_T> = memo_T<val_T>,
|
|
21
|
+
ctx_T extends Ctx = Ctx
|
|
22
|
+
> = [
|
|
23
|
+
Be<_memo_T, ctx_T>,
|
|
24
|
+
(ctx:ctx_T)=>val_T
|
|
25
|
+
]
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { be_ } from '../be_/index.js'
|
|
2
|
-
import {
|
|
2
|
+
import { memo_ } from '../rmemo/index.js'
|
|
3
3
|
/** @typedef {import('../be_/index.d.ts').Be} */
|
|
4
4
|
/** @typedef {import('../be_/index.d.ts').be__val__new_T} */
|
|
5
5
|
/** @typedef {import('../be_/index.d.ts').be_config_T} */
|
|
6
6
|
/** @typedef {import('../rmemo/index.d.ts').rmemo_subscriber_T} */
|
|
7
|
-
/** @typedef {import('./index.d.ts').
|
|
7
|
+
/** @typedef {import('./index.d.ts').be_memo_pair_T} */
|
|
8
8
|
/**
|
|
9
9
|
* @param {Be|be__val__new_T<unknown>}be_OR_val__new
|
|
10
10
|
* @param {rmemo_subscriber_T[]|[...rmemo_subscriber_T[], be_config_T]}subscriber_a_THEN_config
|
|
11
|
-
* @returns {
|
|
11
|
+
* @returns {be_memo_pair_T}
|
|
12
12
|
* @private
|
|
13
13
|
*/
|
|
14
|
-
export function
|
|
14
|
+
export function be_memo_pair_(
|
|
15
15
|
be_OR_val__new,
|
|
16
16
|
...subscriber_a_THEN_config
|
|
17
17
|
) {
|
|
@@ -24,7 +24,7 @@ export function be_r_rmemo_pair_(
|
|
|
24
24
|
be_OR_val__new.is_be
|
|
25
25
|
? be_OR_val__new
|
|
26
26
|
: be_(ctx=>
|
|
27
|
-
|
|
27
|
+
memo_(()=>be_OR_val__new(ctx), ...subscriber_a_THEN_config),
|
|
28
28
|
config)
|
|
29
29
|
return [
|
|
30
30
|
be,
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import { test } from 'uvu'
|
|
2
2
|
import { equal } from 'uvu/assert'
|
|
3
3
|
import { be_ } from '../be_/index.js'
|
|
4
|
-
import {
|
|
4
|
+
import { be_sig_triple_ } from '../be_sig_triple/index.js'
|
|
5
5
|
import { ctx__new } from '../ctx/index.js'
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
test('
|
|
6
|
+
import type { memo_T, sig_T } from '../rmemo/index.js'
|
|
7
|
+
import { memo_ } from '../rmemo/index.js'
|
|
8
|
+
import { be_memo_pair_ } from './index.js'
|
|
9
|
+
test('be_memo_pair_', ()=>{
|
|
10
10
|
const [
|
|
11
11
|
,
|
|
12
12
|
base_,
|
|
13
13
|
base__set,
|
|
14
|
-
] =
|
|
14
|
+
] = be_sig_triple_(()=>1)
|
|
15
15
|
const [
|
|
16
16
|
foobar$_,
|
|
17
17
|
foobar_,
|
|
18
|
-
] =
|
|
18
|
+
] = be_memo_pair_(ctx=>base_(ctx) + 1)
|
|
19
19
|
const ctx = ctx__new()
|
|
20
20
|
equal(foobar$_(ctx)._, 2)
|
|
21
21
|
equal(foobar_(ctx), 2)
|
|
@@ -23,19 +23,19 @@ test('be_r_rmemo_pair_', ()=>{
|
|
|
23
23
|
equal(foobar$_(ctx)._, 3)
|
|
24
24
|
equal(foobar_(ctx), 3)
|
|
25
25
|
})
|
|
26
|
-
test('
|
|
26
|
+
test('be_memo_pair_|+id|+is_source_|+oninit|+subscriber_a', ()=>{
|
|
27
27
|
const ctx = ctx__new()
|
|
28
28
|
let subscriber_count = 0
|
|
29
29
|
const [
|
|
30
30
|
,
|
|
31
31
|
base_,
|
|
32
32
|
base__set,
|
|
33
|
-
] =
|
|
33
|
+
] = be_sig_triple_(()=>1,
|
|
34
34
|
{ is_source_: map_ctx=>map_ctx === ctx })
|
|
35
35
|
const [
|
|
36
36
|
foobar$_,
|
|
37
37
|
foobar_,
|
|
38
|
-
] =
|
|
38
|
+
] = be_memo_pair_(ctx=>base_(ctx) + 1,
|
|
39
39
|
()=>subscriber_count++,
|
|
40
40
|
{
|
|
41
41
|
id: 'foobar',
|
|
@@ -46,34 +46,34 @@ test('be_r_rmemo_pair_|+id|+is_source_|+oninit|+subscriber_a', ()=>{
|
|
|
46
46
|
equal(foobar_([ctx__new(), ctx]), 2)
|
|
47
47
|
equal(foobar$_(ctx)._, 2)
|
|
48
48
|
equal(foobar_(ctx), 2)
|
|
49
|
-
equal((ctx.get('foobar') as
|
|
49
|
+
equal((ctx.get('foobar') as memo_T<number>)._, 2)
|
|
50
50
|
equal(subscriber_count, 1)
|
|
51
51
|
base__set(ctx, 2)
|
|
52
52
|
equal(foobar$_([ctx__new(), ctx])._, 3)
|
|
53
53
|
equal(foobar_([ctx__new(), ctx]), 3)
|
|
54
54
|
equal(foobar$_(ctx)._, 3)
|
|
55
55
|
equal(foobar_(ctx), 3)
|
|
56
|
-
equal((ctx.get('foobar') as
|
|
56
|
+
equal((ctx.get('foobar') as memo_T<number>)._, 3)
|
|
57
57
|
equal(subscriber_count, 1)
|
|
58
58
|
})
|
|
59
|
-
test('
|
|
59
|
+
test('be_memo_pair_|be', ()=>{
|
|
60
60
|
const ctx = ctx__new()
|
|
61
61
|
let subscriber_count = 0
|
|
62
62
|
const [
|
|
63
63
|
,
|
|
64
64
|
base_,
|
|
65
65
|
base__set,
|
|
66
|
-
] =
|
|
66
|
+
] = be_sig_triple_(()=>1,
|
|
67
67
|
{ is_source_: map_ctx=>map_ctx === ctx })
|
|
68
68
|
const [
|
|
69
69
|
foobar$_,
|
|
70
70
|
foobar_,
|
|
71
|
-
] =
|
|
71
|
+
] = be_memo_pair_<number, custom_memo_T>(
|
|
72
72
|
be_(_ctx=>{
|
|
73
|
-
const foobar$ =
|
|
73
|
+
const foobar$ = memo_(
|
|
74
74
|
()=>base_(ctx) + 1,
|
|
75
75
|
()=>subscriber_count++
|
|
76
|
-
) as
|
|
76
|
+
) as custom_memo_T
|
|
77
77
|
if (Array.isArray(_ctx)) equal(_ctx[1], ctx)
|
|
78
78
|
else equal(_ctx, ctx)
|
|
79
79
|
foobar$.custom = 'custom-val'
|
|
@@ -87,7 +87,7 @@ test('be_r_rmemo_pair_|be', ()=>{
|
|
|
87
87
|
equal(foobar_([ctx__new(), ctx]), 2)
|
|
88
88
|
equal(foobar$_(ctx)._, 2)
|
|
89
89
|
equal(foobar_(ctx), 2)
|
|
90
|
-
equal((ctx.get('foobar') as
|
|
90
|
+
equal((ctx.get('foobar') as memo_T<number>)._, 2)
|
|
91
91
|
equal(foobar$_(ctx).custom, 'custom-val')
|
|
92
92
|
equal(subscriber_count, 1)
|
|
93
93
|
base__set(ctx, 2)
|
|
@@ -95,9 +95,9 @@ test('be_r_rmemo_pair_|be', ()=>{
|
|
|
95
95
|
equal(foobar_([ctx__new(), ctx]), 3)
|
|
96
96
|
equal(foobar$_(ctx)._, 3)
|
|
97
97
|
equal(foobar_(ctx), 3)
|
|
98
|
-
equal((ctx.get('foobar') as
|
|
98
|
+
equal((ctx.get('foobar') as memo_T<number>)._, 3)
|
|
99
99
|
equal(foobar$_(ctx).custom, 'custom-val')
|
|
100
100
|
equal(subscriber_count, 1)
|
|
101
101
|
})
|
|
102
102
|
test.run()
|
|
103
|
-
type
|
|
103
|
+
type custom_memo_T = sig_T<number>&{ custom:string }
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Be, be__val__new_T, be_config_T, Ctx } from '../be_/index.js'
|
|
2
|
+
import type { sig_T, rmemo_def_T } from '../rmemo/index.js'
|
|
3
|
+
export declare function be_sig_triple_<
|
|
4
|
+
val_T,
|
|
5
|
+
_sig_T extends sig_T<val_T> = sig_T<val_T>,
|
|
6
|
+
ctx_T extends Ctx = Ctx
|
|
7
|
+
>(be: Be<_sig_T, ctx_T>):be_sig_triple_T<val_T, _sig_T, ctx_T>
|
|
8
|
+
export declare function be_sig_triple_<
|
|
9
|
+
val_T,
|
|
10
|
+
_sig_T extends sig_T<val_T> = sig_T<val_T>,
|
|
11
|
+
ctx_T extends Ctx = Ctx
|
|
12
|
+
>(
|
|
13
|
+
rmemo__new:be__val__new_T<val_T>,
|
|
14
|
+
...subscriber_a_THEN_config:
|
|
15
|
+
|[...rmemo_def_T<val_T>[]]
|
|
16
|
+
|[...rmemo_def_T<val_T>[], config:be_config_T]
|
|
17
|
+
):be_sig_triple_T<val_T, _sig_T, ctx_T>
|
|
18
|
+
export type be_sig_triple_T<
|
|
19
|
+
val_T,
|
|
20
|
+
_sig_T extends sig_T<val_T> = sig_T<val_T>,
|
|
21
|
+
ctx_T extends Ctx = Ctx
|
|
22
|
+
> = [
|
|
23
|
+
Be<_sig_T>,
|
|
24
|
+
(ctx:ctx_T)=>val_T,
|
|
25
|
+
(ctx:ctx_T, val:val_T)=>void
|
|
26
|
+
]
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { be_ } from '../be_/index.js'
|
|
2
|
-
import {
|
|
2
|
+
import { sig_ } from '../rmemo/index.js'
|
|
3
3
|
/** @typedef {import('../be_/index.d.ts').Be} */
|
|
4
4
|
/** @typedef {import('../be_/index.d.ts').be__val__new_T} */
|
|
5
5
|
/** @typedef {import('../be_/index.d.ts').be_config_T} */
|
|
6
|
-
/** @typedef {import('../rmemo/index.d.ts').
|
|
6
|
+
/** @typedef {import('../rmemo/index.d.ts').sig_T} */
|
|
7
7
|
/** @typedef {import('../rmemo/index.d.ts').rmemo_subscriber_T} */
|
|
8
|
-
/** @typedef {import('./index.d.ts').
|
|
8
|
+
/** @typedef {import('./index.d.ts').be_sig_triple_T} */
|
|
9
9
|
/**
|
|
10
|
-
* @param {Be<
|
|
10
|
+
* @param {Be<sig_T>|be__val__new_T<unknown>}be_OR_val__new
|
|
11
11
|
* @param {rmemo_subscriber_T[]|[...rmemo_subscriber_T[], be_config_T]}subscriber_a_THEN_config
|
|
12
|
-
* @returns {
|
|
12
|
+
* @returns {be_sig_triple_T}
|
|
13
13
|
* @private
|
|
14
14
|
*/
|
|
15
|
-
export function
|
|
15
|
+
export function be_sig_triple_(
|
|
16
16
|
be_OR_val__new,
|
|
17
17
|
...subscriber_a_THEN_config
|
|
18
18
|
) {
|
|
@@ -20,11 +20,11 @@ export function be_rw_rmemo_triple_(
|
|
|
20
20
|
typeof subscriber_a_THEN_config[subscriber_a_THEN_config.length - 1] === 'object'
|
|
21
21
|
? subscriber_a_THEN_config.pop()
|
|
22
22
|
: undefined
|
|
23
|
-
/** @type {Be<
|
|
23
|
+
/** @type {Be<sig_T>} */
|
|
24
24
|
let be =
|
|
25
25
|
be_OR_val__new.is_be
|
|
26
26
|
? be_OR_val__new
|
|
27
|
-
: be_(ctx=>
|
|
27
|
+
: be_(ctx=>sig_(be_OR_val__new(ctx), ...subscriber_a_THEN_config),
|
|
28
28
|
config)
|
|
29
29
|
return [
|
|
30
30
|
be,
|
|
@@ -2,14 +2,14 @@ import { test } from 'uvu'
|
|
|
2
2
|
import { equal } from 'uvu/assert'
|
|
3
3
|
import { be_ } from '../be_/index.js'
|
|
4
4
|
import { ctx__new } from '../ctx/index.js'
|
|
5
|
-
import { type
|
|
6
|
-
import {
|
|
7
|
-
test('
|
|
5
|
+
import { type sig_T, sig_ } from '../rmemo/index.js'
|
|
6
|
+
import { be_sig_triple_ } from './index.js'
|
|
7
|
+
test('be_sig_triple_', ()=>{
|
|
8
8
|
const [
|
|
9
9
|
foobar$_,
|
|
10
10
|
foobar_,
|
|
11
11
|
foobar__set,
|
|
12
|
-
] =
|
|
12
|
+
] = be_sig_triple_(()=>1)
|
|
13
13
|
const ctx = ctx__new()
|
|
14
14
|
equal(foobar$_(ctx)._, 1)
|
|
15
15
|
equal(foobar_(ctx), 1)
|
|
@@ -17,14 +17,14 @@ test('be_rw_rmemo_triple_', ()=>{
|
|
|
17
17
|
equal(foobar$_(ctx)._, 2)
|
|
18
18
|
equal(foobar_(ctx), 2)
|
|
19
19
|
})
|
|
20
|
-
test('
|
|
20
|
+
test('be_sig_triple_|+id|+is_source_', ()=>{
|
|
21
21
|
const ctx = ctx__new()
|
|
22
22
|
let subscriber_count = 0
|
|
23
23
|
const [
|
|
24
24
|
foobar$_,
|
|
25
25
|
foobar_,
|
|
26
26
|
foobar__set,
|
|
27
|
-
] =
|
|
27
|
+
] = be_sig_triple_(
|
|
28
28
|
()=>1,
|
|
29
29
|
()=>subscriber_count++,
|
|
30
30
|
{ id: 'foobar', is_source_: map_ctx=>map_ctx === ctx },)
|
|
@@ -34,26 +34,26 @@ test('be_rw_rmemo_triple_|+id|+is_source_', ()=>{
|
|
|
34
34
|
equal(foobar$_(ctx)._, 1)
|
|
35
35
|
equal(foobar_(ctx), 1)
|
|
36
36
|
equal(subscriber_count, 1)
|
|
37
|
-
equal((ctx.get('foobar') as
|
|
37
|
+
equal((ctx.get('foobar') as sig_T<number>)._, 1)
|
|
38
38
|
foobar__set([ctx__new(), ctx], 2)
|
|
39
39
|
equal(foobar$_([ctx__new(), ctx])._, 2)
|
|
40
40
|
equal(foobar_([ctx__new(), ctx]), 2)
|
|
41
41
|
equal(foobar$_(ctx)._, 2)
|
|
42
42
|
equal(foobar_(ctx), 2)
|
|
43
|
-
equal((ctx.get('foobar') as
|
|
43
|
+
equal((ctx.get('foobar') as sig_T<number>)._, 2)
|
|
44
44
|
equal(subscriber_count, 1)
|
|
45
45
|
})
|
|
46
|
-
test('
|
|
46
|
+
test('be_sig_triple_|+be', ()=>{
|
|
47
47
|
const ctx = ctx__new()
|
|
48
48
|
let subscriber_count = 0
|
|
49
49
|
const [
|
|
50
50
|
foobar$_,
|
|
51
51
|
foobar_,
|
|
52
52
|
foobar__set,
|
|
53
|
-
] =
|
|
53
|
+
] = be_sig_triple_<number, custom_sig_T>(
|
|
54
54
|
be_(()=>{
|
|
55
55
|
const foobar$ =
|
|
56
|
-
|
|
56
|
+
sig_(1, ()=>subscriber_count++) as custom_sig_T
|
|
57
57
|
foobar$.custom = 'custom-val'
|
|
58
58
|
return foobar$
|
|
59
59
|
}, { id: 'foobar', is_source_: map_ctx=>map_ctx === ctx }))
|
|
@@ -63,16 +63,16 @@ test('be_rw_rmemo_triple_|+be', ()=>{
|
|
|
63
63
|
equal(foobar$_(ctx)._, 1)
|
|
64
64
|
equal(foobar_(ctx), 1)
|
|
65
65
|
equal(subscriber_count, 1)
|
|
66
|
-
equal((ctx.get('foobar') as
|
|
66
|
+
equal((ctx.get('foobar') as sig_T<number>)._, 1)
|
|
67
67
|
equal(foobar$_(ctx).custom, 'custom-val')
|
|
68
68
|
foobar__set([ctx__new(), ctx], 2)
|
|
69
69
|
equal(foobar$_([ctx__new(), ctx])._, 2)
|
|
70
70
|
equal(foobar_([ctx__new(), ctx]), 2)
|
|
71
71
|
equal(foobar$_(ctx)._, 2)
|
|
72
72
|
equal(foobar_(ctx), 2)
|
|
73
|
-
equal((ctx.get('foobar') as
|
|
73
|
+
equal((ctx.get('foobar') as sig_T<number>)._, 2)
|
|
74
74
|
equal(foobar$_(ctx).custom, 'custom-val')
|
|
75
75
|
equal(subscriber_count, 1)
|
|
76
76
|
})
|
|
77
77
|
test.run()
|
|
78
|
-
type
|
|
78
|
+
type custom_sig_T = sig_T<number>&{ custom:string }
|
package/all/index.d.ts
CHANGED
|
@@ -44,8 +44,8 @@ export * from './base64__buffer/index.js'
|
|
|
44
44
|
export * from './batch_queue/index.js'
|
|
45
45
|
export * from './be/index.js'
|
|
46
46
|
export * from './be_/index.js'
|
|
47
|
-
export * from './
|
|
48
|
-
export * from './
|
|
47
|
+
export * from './be_memo_pair/index.js'
|
|
48
|
+
export * from './be_sig_triple/index.js'
|
|
49
49
|
export * from './binary_sort_rank/index.js'
|
|
50
50
|
export * from './bind_apply/index.js'
|
|
51
51
|
export * from './bind_call/index.js'
|
|
@@ -309,6 +309,7 @@ export * from './rank/index.js'
|
|
|
309
309
|
export * from './rate_limit/index.js'
|
|
310
310
|
export * from './reduce/index.js'
|
|
311
311
|
export * from './reject/index.js'
|
|
312
|
+
export * from './rmemo/index.js'
|
|
312
313
|
export * from './remove/index.js'
|
|
313
314
|
export * from './remove_idx/index.js'
|
|
314
315
|
export * from './resolver_curry/index.js'
|
package/all/index.js
CHANGED
|
@@ -44,8 +44,8 @@ export * from './base64__buffer/index.js'
|
|
|
44
44
|
export * from './batch_queue/index.js'
|
|
45
45
|
export * from './be/index.js'
|
|
46
46
|
export * from './be_/index.js'
|
|
47
|
-
export * from './
|
|
48
|
-
export * from './
|
|
47
|
+
export * from './be_memo_pair/index.js'
|
|
48
|
+
export * from './be_sig_triple/index.js'
|
|
49
49
|
export * from './binary_sort_rank/index.js'
|
|
50
50
|
export * from './bind_apply/index.js'
|
|
51
51
|
export * from './bind_call/index.js'
|
|
@@ -309,6 +309,7 @@ export * from './rank/index.js'
|
|
|
309
309
|
export * from './rate_limit/index.js'
|
|
310
310
|
export * from './reduce/index.js'
|
|
311
311
|
export * from './reject/index.js'
|
|
312
|
+
export * from './rmemo/index.js'
|
|
312
313
|
export * from './remove/index.js'
|
|
313
314
|
export * from './remove_idx/index.js'
|
|
314
315
|
export * from './resolver_curry/index.js'
|
package/all/primitive/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { nullish } from '../nullish/index.js'
|
|
2
|
-
export type primitive_T = boolean|number|string|nullish
|
|
2
|
+
export type primitive_T = boolean|number|bigint|string|nullish
|
package/all/rmemo/index.d.ts
CHANGED
|
@@ -1,28 +1,30 @@
|
|
|
1
|
-
export declare function
|
|
1
|
+
export declare function memo_<val_T>(
|
|
2
2
|
def:rmemo_def_T<val_T>,
|
|
3
3
|
...subscriber_a:rmemo_subscriber_T<val_T>[]
|
|
4
|
-
):
|
|
5
|
-
export declare function
|
|
4
|
+
):memo_T<val_T>
|
|
5
|
+
export declare function sig_<val_T>(
|
|
6
6
|
init_val:val_T,
|
|
7
7
|
...subscriber_a:rmemo_subscriber_T<val_T>[]
|
|
8
|
-
):
|
|
9
|
-
export
|
|
8
|
+
):sig_T<val_T>
|
|
9
|
+
export declare function memosig_<val_T>(
|
|
10
|
+
def:rmemo_def_T<val_T>,
|
|
11
|
+
...subscriber_a:rmemo_subscriber_T<val_T>[]
|
|
12
|
+
):sig_T<val_T>
|
|
13
|
+
export type rmemo_T<val_T> = memo_T<val_T>|sig_T<val_T>
|
|
14
|
+
export type memo_T<val_T> = (()=>val_T)&{
|
|
10
15
|
readonly _:val_T
|
|
11
|
-
val:val_T
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
readonly val:val_T
|
|
17
|
+
rmr:WeakRef<()=>val_T>
|
|
18
|
+
rmrs:WeakRef<()=>val_T>[]
|
|
14
19
|
}
|
|
15
|
-
export type
|
|
20
|
+
export type sig_T<val_T> = (()=>val_T)&{
|
|
16
21
|
_:val_T
|
|
17
|
-
val:val_T
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
go():r_rmemo_T<val_T>
|
|
22
|
+
readonly val:val_T
|
|
23
|
+
rmr:WeakRef<()=>val_T>
|
|
24
|
+
rmrs:WeakRef<()=>val_T>[]
|
|
21
25
|
}
|
|
22
|
-
export type rmemo_val_T<
|
|
23
|
-
_:infer val_T
|
|
24
|
-
}
|
|
26
|
+
export type rmemo_val_T<sig_T> = sig_T extends { ():infer val_T }
|
|
25
27
|
? val_T
|
|
26
28
|
: unknown
|
|
27
|
-
export type rmemo_def_T<val_T> = (rw_rmemo:
|
|
28
|
-
export type rmemo_subscriber_T<val_T> = (rw_rmemo:
|
|
29
|
+
export type rmemo_def_T<val_T> = (rw_rmemo:sig_T<val_T>)=>val_T
|
|
30
|
+
export type rmemo_subscriber_T<val_T> = (rw_rmemo:sig_T<val_T>)=>unknown
|
package/all/rmemo/index.js
CHANGED
|
@@ -1,80 +1,104 @@
|
|
|
1
1
|
/** @typedef {import('./index.d.ts').rmemo_def_T} */
|
|
2
|
-
/** @typedef {import('./index.d.ts').
|
|
2
|
+
/** @typedef {import('./index.d.ts').memo_T} */
|
|
3
3
|
/** @typedef {import('./index.d.ts').rmemo_subscriber_T} */
|
|
4
|
-
/** @
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
/** @typedef {import('./index.d.ts').sig_T} */
|
|
5
|
+
/** @type {WeakRef<memo_T>} */
|
|
6
|
+
let cur_rmr
|
|
7
|
+
/** @type {Set<()=>unknown>} */
|
|
8
|
+
let queue = new Set
|
|
8
9
|
/**
|
|
9
10
|
* @param {rmemo_def_T}rmemo_def
|
|
10
11
|
* @param {rmemo_subscriber_T<unknown>[]}subscriber_a
|
|
11
|
-
* @returns {
|
|
12
|
+
* @returns {memo_T}
|
|
12
13
|
* @private
|
|
13
14
|
*/
|
|
14
|
-
export function
|
|
15
|
-
let
|
|
16
|
-
let
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
let
|
|
39
|
-
for (let
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
15
|
+
export function memo_(rmemo_def, ...subscriber_a) {
|
|
16
|
+
let refresh
|
|
17
|
+
let rmrs
|
|
18
|
+
let memo = ()=>{
|
|
19
|
+
if (!('val' in memo)) {
|
|
20
|
+
refresh()
|
|
21
|
+
}
|
|
22
|
+
if (cur_rmr) {
|
|
23
|
+
let cur_rmr_refresh = cur_rmr.deref()
|
|
24
|
+
~rmrs.indexOf(cur_rmr) || rmrs.push(cur_rmr)
|
|
25
|
+
cur_rmr_refresh.l < refresh.l + 1 && (cur_rmr_refresh.l = refresh.l + 1)
|
|
26
|
+
// conditional in rmr calls this r_memo
|
|
27
|
+
cur_rmr_refresh.s.push(memo)
|
|
28
|
+
// prevent this rmemo from GC while cur_rmr is still active
|
|
29
|
+
~cur_rmr_refresh.S.indexOf(memo) || cur_rmr_refresh.S.push(memo)
|
|
30
|
+
}
|
|
31
|
+
return memo.val
|
|
32
|
+
}
|
|
33
|
+
Object.defineProperty(memo, '_', {
|
|
34
|
+
get: memo,
|
|
35
|
+
set: val=>{
|
|
36
|
+
if (memo.val !== val) {
|
|
37
|
+
memo.val = val // val is available for other purposes
|
|
38
|
+
let run_queue = !queue.size
|
|
39
|
+
let i = 0
|
|
40
|
+
for (let rmr of rmrs) {
|
|
41
|
+
val = rmr.deref() // val is no longer used...saving bytes
|
|
42
|
+
if (!val) {
|
|
43
|
+
rmrs.splice(i, 1)
|
|
44
|
+
} else if (~val.s.indexOf(memo)) { // if conditional rmr refresh calls this r_memo, add to queue
|
|
45
|
+
queue.add(val)
|
|
46
|
+
}
|
|
47
|
+
i++
|
|
46
48
|
}
|
|
49
|
+
// add reference to subscribers to prevent GC
|
|
50
|
+
memo._s ||=
|
|
51
|
+
subscriber_a.map(subscriber=>
|
|
52
|
+
memo_(subscriber$=>(
|
|
53
|
+
subscriber(memo),
|
|
54
|
+
subscriber$
|
|
55
|
+
))())
|
|
47
56
|
if (run_queue) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
cur_refresh_loop:for (let cur_refresh of queue) {
|
|
58
|
+
queue.delete(cur_refresh)
|
|
59
|
+
for (let queue_refresh of queue) {
|
|
60
|
+
if (cur_refresh.l > queue_refresh.l) {
|
|
61
|
+
queue.add(cur_refresh)
|
|
62
|
+
continue cur_refresh_loop
|
|
63
|
+
}
|
|
54
64
|
}
|
|
65
|
+
cur_refresh()
|
|
55
66
|
}
|
|
56
67
|
}
|
|
57
68
|
}
|
|
58
69
|
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
70
|
+
})
|
|
71
|
+
refresh = ()=>{
|
|
72
|
+
let prev_rmr = cur_rmr
|
|
73
|
+
cur_rmr = memo.rmr
|
|
74
|
+
refresh.s = []
|
|
75
|
+
try {
|
|
76
|
+
memo._ = rmemo_def(memo)
|
|
77
|
+
} catch (err) {
|
|
78
|
+
console.error(err)
|
|
79
|
+
}
|
|
80
|
+
cur_rmr = prev_rmr // finally is not necessary...catch does not throw
|
|
63
81
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
82
|
+
refresh.l = 0
|
|
83
|
+
// rmrs = new Set
|
|
84
|
+
// memo.rmrs is kept for GC testing/debugging purposes...small size increase
|
|
85
|
+
memo.rmrs = rmrs = []
|
|
86
|
+
memo.rmr = new WeakRef(refresh)
|
|
87
|
+
refresh.s = []
|
|
88
|
+
refresh.S = []
|
|
89
|
+
return memo
|
|
68
90
|
}
|
|
91
|
+
export { memo_ as memosig_ }
|
|
69
92
|
/**
|
|
70
93
|
* @param {unknown}init_val
|
|
71
94
|
* @param {rmemo_subscriber_T[]}subscriber_a
|
|
72
|
-
* @returns {
|
|
95
|
+
* @returns {sig_T}
|
|
73
96
|
* @private
|
|
74
97
|
*/
|
|
75
|
-
export function
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
98
|
+
export function sig_(init_val, ...subscriber_a) {
|
|
99
|
+
return memo_(rw_rmemo=>
|
|
100
|
+
'val' in rw_rmemo
|
|
101
|
+
? rw_rmemo.val
|
|
102
|
+
: init_val,
|
|
103
|
+
...subscriber_a)
|
|
80
104
|
}
|
package/all/rmemo/index.test.ts
CHANGED
|
@@ -1,85 +1,189 @@
|
|
|
1
|
+
// Reference: https://github.com/nanostores/nanostores/blob/main/computed/index.test.ts
|
|
2
|
+
// Reference: https://github.com/vanjs-org/van/blob/main/test/van.test.ts
|
|
1
3
|
import { deepStrictEqual } from 'node:assert'
|
|
2
4
|
import { test } from 'uvu'
|
|
3
5
|
import { equal } from 'uvu/assert'
|
|
4
6
|
import { sleep } from '../sleep/index.js'
|
|
5
|
-
import {
|
|
6
|
-
test('
|
|
7
|
+
import { memo_, type memo_T, memosig_, sig_ } from './index.js'
|
|
8
|
+
test('memo_|static value', ()=>{
|
|
7
9
|
let count = 0
|
|
8
|
-
const r_rmemo =
|
|
10
|
+
const r_rmemo = memo_(()=>{
|
|
9
11
|
count++
|
|
10
12
|
return 'rmemo-value'
|
|
11
13
|
})
|
|
12
14
|
equal(count, 0)
|
|
13
|
-
equal(r_rmemo
|
|
15
|
+
equal(r_rmemo(), 'rmemo-value')
|
|
14
16
|
equal(count, 1)
|
|
15
|
-
equal(r_rmemo
|
|
17
|
+
equal(r_rmemo(), 'rmemo-value')
|
|
16
18
|
equal(count, 1)
|
|
17
19
|
})
|
|
18
|
-
test('
|
|
19
|
-
const rw_rmemo =
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
equal(rw_rmemo._, 'val1')
|
|
23
|
-
})
|
|
24
|
-
test('r_rmemo_|def function|rmemo$ argument', ()=>{
|
|
25
|
-
const rw_rmemo = rw_rmemo_('val0')
|
|
26
|
-
const r_rmemo:r_rmemo_T<string>&{custom?:string} = r_rmemo_<string>((_rmemo$:r_rmemo_T<string>&{custom?:string})=>
|
|
27
|
-
`${_rmemo$.custom}-${rw_rmemo._}`)
|
|
20
|
+
test('memo_|def function|rmemo argument', ()=>{
|
|
21
|
+
const rw_rmemo = sig_('val0')
|
|
22
|
+
const r_rmemo:memo_T<string>&{ custom?:string } = memo_<string>((_rmemo$:memo_T<string>&{ custom?:string })=>
|
|
23
|
+
`${_rmemo$.custom}-${rw_rmemo()}`)
|
|
28
24
|
r_rmemo.custom = 'custom_val0'
|
|
29
|
-
equal(r_rmemo
|
|
25
|
+
equal(r_rmemo(), 'custom_val0-val0')
|
|
30
26
|
r_rmemo.custom = 'custom_val1'
|
|
31
|
-
equal(r_rmemo
|
|
27
|
+
equal(r_rmemo(), 'custom_val0-val0')
|
|
32
28
|
rw_rmemo._ = 'val1'
|
|
33
|
-
equal(r_rmemo
|
|
29
|
+
equal(r_rmemo(), 'custom_val1-val1')
|
|
30
|
+
})
|
|
31
|
+
test('r_memo_|side effect', ()=>{
|
|
32
|
+
const history:string[] = []
|
|
33
|
+
const s = sig_('This')
|
|
34
|
+
memo_(()=>history.push(s()))()
|
|
35
|
+
s._ = 'is'
|
|
36
|
+
s._ = 'a'
|
|
37
|
+
s._ = 'test'
|
|
38
|
+
s._ = 'test'
|
|
39
|
+
equal(history, ['This', 'is', 'a', 'test'])
|
|
40
|
+
})
|
|
41
|
+
test('memo_|conditional', ()=>{
|
|
42
|
+
const cond$ = sig_(true)
|
|
43
|
+
const a$ = sig_(1)
|
|
44
|
+
const b$ = sig_(2)
|
|
45
|
+
const c$ = sig_(3)
|
|
46
|
+
const d$ = sig_(4)
|
|
47
|
+
let trigger_count = 0
|
|
48
|
+
const sum$ = memo_(()=>(++trigger_count, cond$() ? a$() + b$() : c$() + d$()))
|
|
49
|
+
equal(sum$(), 3)
|
|
50
|
+
equal(trigger_count, 1)
|
|
51
|
+
a$._ = 11
|
|
52
|
+
equal(sum$(), 13)
|
|
53
|
+
equal(trigger_count, 2)
|
|
54
|
+
b$._ = 12
|
|
55
|
+
equal(sum$(), 23)
|
|
56
|
+
equal(trigger_count, 3)
|
|
57
|
+
// Changing c$ or d$ won't triggered the effect as they're not its current dependencies
|
|
58
|
+
c$._ = 13
|
|
59
|
+
equal(sum$(), 23)
|
|
60
|
+
equal(trigger_count, 3)
|
|
61
|
+
d$._ = 14
|
|
62
|
+
equal(sum$(), 23)
|
|
63
|
+
equal(trigger_count, 3)
|
|
64
|
+
cond$._ = false
|
|
65
|
+
equal(sum$(), 27)
|
|
66
|
+
equal(trigger_count, 4)
|
|
67
|
+
c$._ = 23
|
|
68
|
+
equal(sum$(), 37)
|
|
69
|
+
equal(trigger_count, 5)
|
|
70
|
+
d$._ = 24
|
|
71
|
+
equal(sum$(), 47)
|
|
72
|
+
equal(trigger_count, 6)
|
|
73
|
+
// Changing a$ or b$ won't triggered the effect as they're not its current dependencies
|
|
74
|
+
a$._ = 21
|
|
75
|
+
equal(sum$(), 47)
|
|
76
|
+
equal(trigger_count, 6)
|
|
77
|
+
b$._ = 22
|
|
78
|
+
equal(sum$(), 47)
|
|
79
|
+
equal(trigger_count, 6)
|
|
80
|
+
})
|
|
81
|
+
test('memosig_', ()=>{
|
|
82
|
+
const num_items$ = sig_(0)
|
|
83
|
+
const items$ = memo_(()=>[...Array(num_items$()).keys()].map(i=>`Item ${i + 1}`))
|
|
84
|
+
// TODO: Jetbrains or Typescript type inference is wrong without generic
|
|
85
|
+
const selected_index$ = memosig_<number>(()=>(items$(), 0))
|
|
86
|
+
const selected_item$ = memo_(()=>items$()[selected_index$()])
|
|
87
|
+
num_items$._ = 3
|
|
88
|
+
equal(num_items$(), 3)
|
|
89
|
+
equal(items$().join(','), 'Item 1,Item 2,Item 3')
|
|
90
|
+
equal(selected_index$(), 0)
|
|
91
|
+
equal(selected_item$(), 'Item 1')
|
|
92
|
+
selected_index$._ = 2
|
|
93
|
+
equal(selected_index$(), 2)
|
|
94
|
+
equal(selected_item$(), 'Item 3')
|
|
95
|
+
num_items$._ = 5
|
|
96
|
+
equal(num_items$(), 5)
|
|
97
|
+
equal(items$().join(','), 'Item 1,Item 2,Item 3,Item 4,Item 5')
|
|
98
|
+
equal(selected_index$(), 0)
|
|
99
|
+
equal(selected_item$(), 'Item 1')
|
|
100
|
+
selected_index$._ = 3
|
|
101
|
+
equal(selected_index$(), 3)
|
|
102
|
+
equal(selected_item$(), 'Item 4')
|
|
103
|
+
})
|
|
104
|
+
test('memo_|error|case 1', ()=>{
|
|
105
|
+
const r$ = memo_(()=>{
|
|
106
|
+
throw new Error('error case')
|
|
107
|
+
})
|
|
108
|
+
equal(r$(), undefined)
|
|
34
109
|
})
|
|
35
|
-
test('
|
|
110
|
+
test('memo_|error|case 2', ()=>{
|
|
111
|
+
const rw0 = sig_(1)
|
|
112
|
+
const r1 = memo_(()=>rw0() * 2)
|
|
113
|
+
const r2 = memo_(()=>{
|
|
114
|
+
if (rw0() > 1) throw new Error()
|
|
115
|
+
return rw0()
|
|
116
|
+
})
|
|
117
|
+
const r3 = memo_(()=>rw0() * rw0())
|
|
118
|
+
equal(r1(), 2)
|
|
119
|
+
equal(r2(), 1)
|
|
120
|
+
equal(r3(), 1)
|
|
121
|
+
rw0._ = 3
|
|
122
|
+
equal(r1(), 6)
|
|
123
|
+
// r2() keeps it's old value of 1 due to error
|
|
124
|
+
equal(r2(), 1)
|
|
125
|
+
equal(r3(), 9)
|
|
126
|
+
})
|
|
127
|
+
test('sig_', ()=>{
|
|
128
|
+
const rw_rmemo = sig_('val0')
|
|
129
|
+
equal(rw_rmemo(), 'val0')
|
|
130
|
+
rw_rmemo._ = 'val1'
|
|
131
|
+
equal(rw_rmemo(), 'val1')
|
|
132
|
+
})
|
|
133
|
+
test('sig_|undefined', ()=>{
|
|
134
|
+
const rw_rmemo = sig_(undefined)
|
|
135
|
+
const r_rmemo = memo_(()=>rw_rmemo())
|
|
136
|
+
equal(rw_rmemo(), undefined)
|
|
137
|
+
equal(r_rmemo(), undefined)
|
|
138
|
+
})
|
|
139
|
+
test('sig_|async subsubscriber|case 1', async ()=>{
|
|
36
140
|
let resolve:(user:{ id:string })=>void
|
|
37
141
|
const user0 = { id: 'id-0' }
|
|
38
142
|
const user1 = { id: 'id-1' }
|
|
39
|
-
const id$ =
|
|
143
|
+
const id$ = sig_('id-0')
|
|
40
144
|
let count = 0
|
|
41
|
-
const user$ =
|
|
145
|
+
const user$ = sig_<{ id:string }|null>(
|
|
42
146
|
null,
|
|
43
147
|
async (_user$)=>{
|
|
44
148
|
count++
|
|
45
|
-
id
|
|
149
|
+
id$()
|
|
46
150
|
const user:{ id:string } = await new Promise(_resolve=>resolve = _resolve)
|
|
47
|
-
_user$._ =
|
|
151
|
+
_user$._ = user
|
|
48
152
|
})
|
|
49
153
|
equal(count, 0)
|
|
50
|
-
equal(user
|
|
154
|
+
equal(user$(), null)
|
|
51
155
|
equal(count, 1)
|
|
52
156
|
resolve!(user0)
|
|
53
157
|
await sleep(0)
|
|
54
158
|
equal(count, 1)
|
|
55
|
-
equal(user
|
|
159
|
+
equal(user$(), user0)
|
|
56
160
|
equal(count, 1)
|
|
57
161
|
id$._ = 'id-1'
|
|
58
162
|
equal(count, 2)
|
|
59
|
-
equal(user
|
|
163
|
+
equal(user$(), user0)
|
|
60
164
|
resolve!(user1)
|
|
61
165
|
await sleep(0)
|
|
62
166
|
equal(count, 2)
|
|
63
167
|
})
|
|
64
|
-
test('
|
|
65
|
-
const a$ =
|
|
66
|
-
const b$ =
|
|
168
|
+
test('sig_|async subsubscriber|case 2', async ()=>{
|
|
169
|
+
const a$ = sig_(1)
|
|
170
|
+
const b$ = sig_(2)
|
|
67
171
|
const sleepCycles = 5
|
|
68
172
|
const taskArgumentsCalls:number[][] = []
|
|
69
|
-
const sum$ =
|
|
173
|
+
const sum$ = sig_<null|number>(null,
|
|
70
174
|
async sum$=>{
|
|
71
|
-
taskArgumentsCalls.push([a
|
|
175
|
+
taskArgumentsCalls.push([a$(), b$()])
|
|
72
176
|
for (let i = 0; i < sleepCycles; i++) {
|
|
73
177
|
await Promise.resolve()
|
|
74
178
|
}
|
|
75
|
-
sum$._ = a
|
|
179
|
+
sum$._ = a$() + b$()
|
|
76
180
|
})
|
|
77
|
-
equal(sum
|
|
181
|
+
equal(sum$(), null)
|
|
78
182
|
deepStrictEqual(taskArgumentsCalls, [[1, 2]])
|
|
79
183
|
a$._ = 10
|
|
80
184
|
b$._ = 20
|
|
81
185
|
for (let i = 0; i < sleepCycles; i++) {
|
|
82
|
-
equal(sum
|
|
186
|
+
equal(sum$(), null)
|
|
83
187
|
await Promise.resolve()
|
|
84
188
|
deepStrictEqual(taskArgumentsCalls, [
|
|
85
189
|
[1, 2],
|
|
@@ -87,100 +191,100 @@ test('rw_rmemo_|async subsubscriber|case 2', async ()=>{
|
|
|
87
191
|
[10, 20]
|
|
88
192
|
])
|
|
89
193
|
}
|
|
90
|
-
equal(sum
|
|
194
|
+
equal(sum$(), 30)
|
|
91
195
|
deepStrictEqual(taskArgumentsCalls, [
|
|
92
196
|
[1, 2],
|
|
93
197
|
[10, 2],
|
|
94
198
|
[10, 20]
|
|
95
199
|
])
|
|
96
200
|
})
|
|
97
|
-
test('
|
|
98
|
-
const base$ =
|
|
99
|
-
const dep0$ =
|
|
100
|
-
const dep1$ =
|
|
101
|
-
const dep2$ =
|
|
102
|
-
const dep3$ =
|
|
103
|
-
const dep4$ =
|
|
104
|
-
equal(dep4
|
|
105
|
-
equal(dep3
|
|
106
|
-
equal(dep2
|
|
107
|
-
equal(dep1
|
|
108
|
-
equal(dep0
|
|
109
|
-
equal(base
|
|
201
|
+
test('memo_+sig_|simple graph', ()=>{
|
|
202
|
+
const base$ = sig_('base0')
|
|
203
|
+
const dep0$ = memo_(()=>base$() + '-dep0')
|
|
204
|
+
const dep1$ = memo_(()=>dep0$() + '-dep1')
|
|
205
|
+
const dep2$ = memo_(()=>dep1$() + '-dep2')
|
|
206
|
+
const dep3$ = memo_(()=>dep2$() + '-dep3')
|
|
207
|
+
const dep4$ = memo_(()=>dep3$() + '-dep4')
|
|
208
|
+
equal(dep4$(), 'base0-dep0-dep1-dep2-dep3-dep4')
|
|
209
|
+
equal(dep3$(), 'base0-dep0-dep1-dep2-dep3')
|
|
210
|
+
equal(dep2$(), 'base0-dep0-dep1-dep2')
|
|
211
|
+
equal(dep1$(), 'base0-dep0-dep1')
|
|
212
|
+
equal(dep0$(), 'base0-dep0')
|
|
213
|
+
equal(base$(), 'base0')
|
|
110
214
|
base$._ = 'base1'
|
|
111
|
-
equal(base
|
|
112
|
-
equal(dep0
|
|
113
|
-
equal(dep1
|
|
114
|
-
equal(dep2
|
|
115
|
-
equal(dep3
|
|
116
|
-
equal(dep4
|
|
215
|
+
equal(base$(), 'base1')
|
|
216
|
+
equal(dep0$(), 'base1-dep0')
|
|
217
|
+
equal(dep1$(), 'base1-dep0-dep1')
|
|
218
|
+
equal(dep2$(), 'base1-dep0-dep1-dep2')
|
|
219
|
+
equal(dep3$(), 'base1-dep0-dep1-dep2-dep3')
|
|
220
|
+
equal(dep4$(), 'base1-dep0-dep1-dep2-dep3-dep4')
|
|
117
221
|
})
|
|
118
222
|
test('prevents diamond dependency problem 1', ()=>{
|
|
119
|
-
const store$ =
|
|
223
|
+
const store$ = sig_(0)
|
|
120
224
|
const values:string[] = []
|
|
121
|
-
const a$ =
|
|
122
|
-
const b$ =
|
|
123
|
-
const c$ =
|
|
124
|
-
const d$ =
|
|
125
|
-
|
|
225
|
+
const a$ = memo_(()=>`a${store$()}`)
|
|
226
|
+
const b$ = memo_(()=>a$().replace('a', 'b'))
|
|
227
|
+
const c$ = memo_(()=>a$().replace('a', 'c'))
|
|
228
|
+
const d$ = memo_(()=>a$().replace('a', 'd'))
|
|
229
|
+
memo_(()=>`${b$()}${c$()}${d$()}`,
|
|
126
230
|
combined$=>
|
|
127
|
-
values.push(combined
|
|
128
|
-
)
|
|
231
|
+
values.push(combined$())
|
|
232
|
+
)()
|
|
129
233
|
deepStrictEqual(values, ['b0c0d0'])
|
|
130
234
|
store$._ = 1
|
|
131
235
|
store$._ = 2
|
|
132
236
|
deepStrictEqual(values, ['b0c0d0', 'b1c1d1', 'b2c2d2'])
|
|
133
237
|
})
|
|
134
238
|
test('prevents diamond dependency problem 2', ()=>{
|
|
135
|
-
const store$ =
|
|
239
|
+
const store$ = sig_(0)
|
|
136
240
|
const values:string[] = []
|
|
137
|
-
const a$ =
|
|
138
|
-
const b$ =
|
|
139
|
-
const c$ =
|
|
140
|
-
const d$ =
|
|
141
|
-
const e$ =
|
|
142
|
-
|
|
143
|
-
()=>[a
|
|
144
|
-
$=>values.push(
|
|
145
|
-
)
|
|
241
|
+
const a$ = memo_(()=>`a${store$()}`)
|
|
242
|
+
const b$ = memo_(()=>a$().replace('a', 'b'))
|
|
243
|
+
const c$ = memo_(()=>b$().replace('b', 'c'))
|
|
244
|
+
const d$ = memo_(()=>c$().replace('c', 'd'))
|
|
245
|
+
const e$ = memo_(()=>d$().replace('d', 'e'))
|
|
246
|
+
memo_<string>(
|
|
247
|
+
()=>[a$(), e$()].join(''),
|
|
248
|
+
$=>values.push($())
|
|
249
|
+
)()
|
|
146
250
|
deepStrictEqual(values, ['a0e0'])
|
|
147
251
|
store$._ = 1
|
|
148
252
|
deepStrictEqual(values, ['a0e0', 'a1e1'])
|
|
149
253
|
})
|
|
150
254
|
test('prevents diamond dependency problem 3', ()=>{
|
|
151
|
-
const store$ =
|
|
255
|
+
const store$ = sig_(0)
|
|
152
256
|
const values:string[] = []
|
|
153
|
-
const a$ =
|
|
154
|
-
const b$ =
|
|
155
|
-
const c$ =
|
|
156
|
-
const d$ =
|
|
157
|
-
|
|
158
|
-
()=>`${a
|
|
159
|
-
combined$=>values.push(combined
|
|
160
|
-
)
|
|
257
|
+
const a$ = memo_(()=>`a${store$()}`)
|
|
258
|
+
const b$ = memo_(()=>a$().replace('a', 'b'))
|
|
259
|
+
const c$ = memo_(()=>b$().replace('b', 'c'))
|
|
260
|
+
const d$ = memo_(()=>c$().replace('c', 'd'))
|
|
261
|
+
memo_<string>(
|
|
262
|
+
()=>`${a$()}${b$()}${c$()}${d$()}`,
|
|
263
|
+
combined$=>values.push(combined$())
|
|
264
|
+
)()
|
|
161
265
|
deepStrictEqual(values, ['a0b0c0d0'])
|
|
162
266
|
store$._ = 1
|
|
163
267
|
deepStrictEqual(values, ['a0b0c0d0', 'a1b1c1d1'])
|
|
164
268
|
})
|
|
165
269
|
test('autosubscribe: prevents diamond dependency problem 4 (complex)', ()=>{
|
|
166
|
-
const store1$ =
|
|
167
|
-
const store2$ =
|
|
270
|
+
const store1$ = sig_(0)
|
|
271
|
+
const store2$ = sig_(0)
|
|
168
272
|
const values:string[] = []
|
|
169
|
-
const a$ =
|
|
170
|
-
const b$ =
|
|
171
|
-
const c$ =
|
|
172
|
-
const d$ =
|
|
173
|
-
const e$ =
|
|
174
|
-
const f$ =
|
|
175
|
-
const g$ =
|
|
176
|
-
|
|
177
|
-
()=>e
|
|
178
|
-
combined1$=>values.push(combined1
|
|
179
|
-
)
|
|
180
|
-
|
|
181
|
-
()=>[e
|
|
182
|
-
combined2$=>values.push(combined2
|
|
183
|
-
)
|
|
273
|
+
const a$ = memo_(()=>`a${store1$()}`)
|
|
274
|
+
const b$ = memo_(()=>`b${store2$()}`)
|
|
275
|
+
const c$ = memo_(()=>`c${a$()}${b$()}`)
|
|
276
|
+
const d$ = memo_(()=>`d${a$()}`)
|
|
277
|
+
const e$ = memo_(()=>`e${c$()}${d$()}`)
|
|
278
|
+
const f$ = memo_(()=>`f${e$()}`)
|
|
279
|
+
const g$ = memo_(()=>`g${f$()}`)
|
|
280
|
+
memo_(
|
|
281
|
+
()=>e$(),
|
|
282
|
+
combined1$=>values.push(combined1$())
|
|
283
|
+
)()
|
|
284
|
+
memo_(
|
|
285
|
+
()=>[e$(), g$()].join(''),
|
|
286
|
+
combined2$=>values.push(combined2$())
|
|
287
|
+
)()
|
|
184
288
|
deepStrictEqual(values, ['eca0b0da0', 'eca0b0da0gfeca0b0da0'])
|
|
185
289
|
store1$._ = 1
|
|
186
290
|
store2$._ = 2
|
|
@@ -195,66 +299,64 @@ test('autosubscribe: prevents diamond dependency problem 4 (complex)', ()=>{
|
|
|
195
299
|
})
|
|
196
300
|
test('prevents diamond dependency problem 5', ()=>{
|
|
197
301
|
let events = ''
|
|
198
|
-
const firstName$ =
|
|
199
|
-
const lastName$ =
|
|
200
|
-
const fullName$ =
|
|
302
|
+
const firstName$ = sig_('John')
|
|
303
|
+
const lastName$ = sig_('Doe')
|
|
304
|
+
const fullName$ = memo_(()=>{
|
|
201
305
|
events += 'full '
|
|
202
|
-
return `${firstName
|
|
306
|
+
return `${firstName$()} ${lastName$()}`
|
|
203
307
|
})
|
|
204
|
-
const isFirstShort$ =
|
|
308
|
+
const isFirstShort$ = memo_(()=>{
|
|
205
309
|
events += 'short '
|
|
206
|
-
return firstName
|
|
310
|
+
return firstName$().length < 10
|
|
311
|
+
})
|
|
312
|
+
const displayName$ = memo_(()=>{
|
|
313
|
+
events += 'display '
|
|
314
|
+
return isFirstShort$() ? fullName$() : firstName$()
|
|
207
315
|
})
|
|
208
|
-
const displayName$ = r_rmemo_(
|
|
209
|
-
()=>{
|
|
210
|
-
events += 'display '
|
|
211
|
-
return isFirstShort$._ ? fullName$._ : firstName$._
|
|
212
|
-
}
|
|
213
|
-
)
|
|
214
316
|
equal(events, '')
|
|
215
|
-
equal(displayName
|
|
317
|
+
equal(displayName$(), 'John Doe')
|
|
216
318
|
equal(events, 'display short full ')
|
|
217
319
|
firstName$._ = 'Benedict'
|
|
218
|
-
equal(displayName
|
|
320
|
+
equal(displayName$(), 'Benedict Doe')
|
|
219
321
|
equal(events, 'display short full short full display ')
|
|
220
322
|
firstName$._ = 'Montgomery'
|
|
221
|
-
equal(displayName
|
|
323
|
+
equal(displayName$(), 'Montgomery')
|
|
222
324
|
equal(events, 'display short full short full display short full display ')
|
|
223
325
|
})
|
|
224
326
|
test('prevents diamond dependency problem 6', ()=>{
|
|
225
|
-
const store1$ =
|
|
226
|
-
const store2$ =
|
|
327
|
+
const store1$ = sig_(0)
|
|
328
|
+
const store2$ = sig_(0)
|
|
227
329
|
const values:string[] = []
|
|
228
|
-
const a$ =
|
|
229
|
-
const b$ =
|
|
230
|
-
const c$ =
|
|
231
|
-
|
|
232
|
-
()=>`${a
|
|
233
|
-
combined$=>values.push(combined
|
|
234
|
-
)
|
|
330
|
+
const a$ = memo_(()=>`a${store1$()}`)
|
|
331
|
+
const b$ = memo_(()=>`b${store2$()}`)
|
|
332
|
+
const c$ = memo_(()=>b$().replace('b', 'c'))
|
|
333
|
+
memo_(
|
|
334
|
+
()=>`${a$()}${c$()}`,
|
|
335
|
+
combined$=>values.push(combined$())
|
|
336
|
+
)()
|
|
235
337
|
deepStrictEqual(values, ['a0c0'])
|
|
236
338
|
store1$._ = 1
|
|
237
339
|
deepStrictEqual(values, ['a0c0', 'a1c0'])
|
|
238
340
|
})
|
|
239
341
|
test('prevents dependency listeners from being out of order', ()=>{
|
|
240
|
-
const base$ =
|
|
241
|
-
const a$ =
|
|
242
|
-
return `${base
|
|
342
|
+
const base$ = sig_(0)
|
|
343
|
+
const a$ = memo_(()=>{
|
|
344
|
+
return `${base$()}a`
|
|
243
345
|
})
|
|
244
346
|
const values:string[] = []
|
|
245
|
-
const b$ =
|
|
246
|
-
return `${a
|
|
247
|
-
}, b$=>values.push(b
|
|
248
|
-
equal(b
|
|
347
|
+
const b$ = memo_(()=>{
|
|
348
|
+
return `${a$()}b`
|
|
349
|
+
}, b$=>values.push(b$()))
|
|
350
|
+
equal(b$(), '0ab')
|
|
249
351
|
deepStrictEqual(values, ['0ab'])
|
|
250
|
-
equal(a
|
|
352
|
+
equal(a$(), '0a')
|
|
251
353
|
base$._ = 1
|
|
252
354
|
deepStrictEqual(values, ['0ab', '1ab'])
|
|
253
355
|
})
|
|
254
356
|
test('computes initial value when argument is undefined', ()=>{
|
|
255
|
-
const one$ =
|
|
256
|
-
const two$ =
|
|
257
|
-
equal(one
|
|
258
|
-
equal(two
|
|
357
|
+
const one$ = sig_<string|undefined>(undefined)
|
|
358
|
+
const two$ = memo_(()=>!!one$())
|
|
359
|
+
equal(one$(), undefined)
|
|
360
|
+
equal(two$(), false)
|
|
259
361
|
})
|
|
260
362
|
test.run()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ctx-core",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.5.0",
|
|
4
4
|
"description": "ctx-core core library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ctx-core",
|
|
@@ -105,16 +105,16 @@
|
|
|
105
105
|
"./package.json": "./package.json"
|
|
106
106
|
},
|
|
107
107
|
"devDependencies": {
|
|
108
|
-
"@arethetypeswrong/cli": "^0.13.
|
|
108
|
+
"@arethetypeswrong/cli": "^0.13.2",
|
|
109
109
|
"@size-limit/preset-small-lib": "^11.0.0",
|
|
110
|
-
"@types/node": "^20.
|
|
110
|
+
"@types/node": "^20.10.0",
|
|
111
111
|
"@types/sinon": "^17.0.2",
|
|
112
112
|
"c8": "^8.0.1",
|
|
113
113
|
"check-dts": "^0.7.2",
|
|
114
114
|
"sinon": "^17.0.1",
|
|
115
115
|
"size-limit": "^11.0.0",
|
|
116
116
|
"ts-node": "^10.9.1",
|
|
117
|
-
"tsx": "^4.
|
|
117
|
+
"tsx": "^4.5.0",
|
|
118
118
|
"typescript": "next",
|
|
119
119
|
"uvu": "^0.5.6"
|
|
120
120
|
},
|
|
@@ -146,32 +146,32 @@
|
|
|
146
146
|
"limit": "187 B"
|
|
147
147
|
},
|
|
148
148
|
{
|
|
149
|
-
"name": "
|
|
149
|
+
"name": "memo_",
|
|
150
150
|
"import": {
|
|
151
|
-
"./rmemo": "{
|
|
151
|
+
"./rmemo": "{ memo_ }"
|
|
152
152
|
},
|
|
153
|
-
"limit": "
|
|
153
|
+
"limit": "343 B"
|
|
154
154
|
},
|
|
155
155
|
{
|
|
156
|
-
"name": "
|
|
156
|
+
"name": "memo_ sig_",
|
|
157
157
|
"import": {
|
|
158
|
-
"./rmemo": "{
|
|
158
|
+
"./rmemo": "{ sig_, memo_ }"
|
|
159
159
|
},
|
|
160
|
-
"limit": "
|
|
160
|
+
"limit": "366 B"
|
|
161
161
|
},
|
|
162
162
|
{
|
|
163
|
-
"name": "
|
|
163
|
+
"name": "memo_ sig_ be_ ctx_",
|
|
164
164
|
"import": {
|
|
165
|
-
"./rmemo": "{
|
|
165
|
+
"./rmemo": "{ sig_, memo_, be_, ctx_ }"
|
|
166
166
|
},
|
|
167
|
-
"limit": "
|
|
167
|
+
"limit": "522 B"
|
|
168
168
|
},
|
|
169
169
|
{
|
|
170
|
-
"name": "
|
|
170
|
+
"name": "memo_ sig_ be_ ctx_ be_memo_pair_ be_sig_triple_",
|
|
171
171
|
"import": {
|
|
172
|
-
"./rmemo": "{
|
|
172
|
+
"./rmemo": "{ sig_, memo_, be_, ctx_, be_memo_pair_, be_sig_triple_ }"
|
|
173
173
|
},
|
|
174
|
-
"limit": "
|
|
174
|
+
"limit": "609 B"
|
|
175
175
|
}
|
|
176
176
|
],
|
|
177
177
|
"scripts": {
|
package/rmemo/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
export { sig__set_ as set_ } from '../all/rmemo/index.js'
|
|
1
2
|
export * from '../all/be_/index.js'
|
|
2
|
-
export * from '../all/
|
|
3
|
-
export * from '../all/
|
|
3
|
+
export * from '../all/be_memo_pair/index.js'
|
|
4
|
+
export * from '../all/be_sig_triple/index.js'
|
|
4
5
|
export * from '../all/ctx/index.js'
|
|
5
6
|
export * from '../all/rmemo/index.js'
|
package/rmemo/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from '../all/be_/index.js'
|
|
2
|
-
export * from '../all/
|
|
3
|
-
export * from '../all/
|
|
2
|
+
export * from '../all/be_memo_pair/index.js'
|
|
3
|
+
export * from '../all/be_sig_triple/index.js'
|
|
4
4
|
export * from '../all/ctx/index.js'
|
|
5
5
|
export * from '../all/rmemo/index.js'
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { Be, be__val__new_T, be_config_T, Ctx } from '../be_/index.js'
|
|
2
|
-
import type { r_rmemo_T, rmemo_def_T } from '../rmemo/index.js'
|
|
3
|
-
export declare function be_r_rmemo_pair_<
|
|
4
|
-
val_T,
|
|
5
|
-
_r_rmemo_T extends r_rmemo_T<val_T> = r_rmemo_T<val_T>,
|
|
6
|
-
ctx_T extends Ctx = Ctx
|
|
7
|
-
>(be: Be<_r_rmemo_T, ctx_T>):be_r_rmemo_pair_T<val_T, _r_rmemo_T, ctx_T>
|
|
8
|
-
export declare function be_r_rmemo_pair_<
|
|
9
|
-
val_T,
|
|
10
|
-
_r_rmemo_T extends r_rmemo_T<val_T> = r_rmemo_T<val_T>,
|
|
11
|
-
ctx_T extends Ctx = Ctx
|
|
12
|
-
>(
|
|
13
|
-
rmemo__new:be__val__new_T<val_T>,
|
|
14
|
-
...subscriber_a_THEN_config:
|
|
15
|
-
|[...rmemo_def_T<val_T>[]]
|
|
16
|
-
|[...rmemo_def_T<val_T>[], config:be_config_T]
|
|
17
|
-
):be_r_rmemo_pair_T<val_T, _r_rmemo_T, ctx_T>
|
|
18
|
-
export type be_r_rmemo_pair_T<
|
|
19
|
-
val_T,
|
|
20
|
-
_r_rmemo_T extends r_rmemo_T<val_T> = r_rmemo_T<val_T>,
|
|
21
|
-
ctx_T extends Ctx = Ctx
|
|
22
|
-
> = [
|
|
23
|
-
Be<_r_rmemo_T, ctx_T>,
|
|
24
|
-
(ctx:ctx_T)=>val_T
|
|
25
|
-
]
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { Be, be__val__new_T, be_config_T, Ctx } from '../be_/index.js'
|
|
2
|
-
import type { rw_rmemo_T, rmemo_def_T } from '../rmemo/index.js'
|
|
3
|
-
export declare function be_rw_rmemo_triple_<
|
|
4
|
-
val_T,
|
|
5
|
-
_rw_rmemo_T extends rw_rmemo_T<val_T> = rw_rmemo_T<val_T>,
|
|
6
|
-
ctx_T extends Ctx = Ctx
|
|
7
|
-
>(be: Be<_rw_rmemo_T, ctx_T>):be_rw_rmemo_triple_T<val_T, _rw_rmemo_T, ctx_T>
|
|
8
|
-
export declare function be_rw_rmemo_triple_<
|
|
9
|
-
val_T,
|
|
10
|
-
_rw_rmemo_T extends rw_rmemo_T<val_T> = rw_rmemo_T<val_T>,
|
|
11
|
-
ctx_T extends Ctx = Ctx
|
|
12
|
-
>(
|
|
13
|
-
rmemo__new:be__val__new_T<val_T>,
|
|
14
|
-
...subscriber_a_THEN_config:
|
|
15
|
-
|[...rmemo_def_T<val_T>[]]
|
|
16
|
-
|[...rmemo_def_T<val_T>[], config:be_config_T]
|
|
17
|
-
):be_rw_rmemo_triple_T<val_T, _rw_rmemo_T, ctx_T>
|
|
18
|
-
export type be_rw_rmemo_triple_T<
|
|
19
|
-
val_T,
|
|
20
|
-
_rw_rmemo_T extends rw_rmemo_T<val_T> = rw_rmemo_T<val_T>,
|
|
21
|
-
ctx_T extends Ctx = Ctx
|
|
22
|
-
> = [
|
|
23
|
-
Be<_rw_rmemo_T>,
|
|
24
|
-
(ctx:ctx_T)=>val_T,
|
|
25
|
-
(ctx:ctx_T, val:val_T)=>void
|
|
26
|
-
]
|