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