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