ctx-core 4.14.1 → 4.16.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.
@@ -0,0 +1,35 @@
1
+ import type { Be, be_config_T, Ctx } from '../be_/index.js'
2
+ import type { rmemo_val_T, sig_T } from '../rmemo/index.js'
3
+ export declare function be_lock_memosig_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_lock_memosig_triple_T<val_T, _sig_T, ctx_T>
8
+ export declare function be_lock_memosig_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:(ctx:Ctx, memosig:_sig_T)=>val_T,
14
+ ...subscriber_a_THEN_config:
15
+ |[...((ctx:Ctx, sig:_sig_T)=>unknown)[]]
16
+ |[...((ctx:Ctx, sig:_sig_T)=>unknown)[], config:be_config_T]
17
+ ):be_lock_memosig_triple_T<val_T, _sig_T, ctx_T>
18
+ export declare function globalThis__be_lock_memosig_triple_<
19
+ val_T,
20
+ _sig_T extends sig_T<val_T> = sig_T<val_T>,
21
+ ctx_T extends Ctx = Ctx
22
+ >(
23
+ rmemo__new:(ctx:Ctx, memosig:_sig_T)=>val_T,
24
+ ...subscriber_a_THEN_config:
25
+ |[...((ctx:Ctx, sig:_sig_T)=>unknown)[], config:be_config_T&{ id:string }]
26
+ ):be_lock_memosig_triple_T<val_T, _sig_T, ctx_T>
27
+ export type be_lock_memosig_triple_T<
28
+ val_T,
29
+ _sig_T extends sig_T<val_T> = sig_T<val_T>,
30
+ ctx_T extends Ctx = Ctx
31
+ > = [
32
+ Be<_sig_T>,
33
+ (ctx:ctx_T)=>val_T,
34
+ (ctx:ctx_T, val:rmemo_val_T<_sig_T>)=>void
35
+ ]
@@ -0,0 +1,38 @@
1
+ /// <reference types="../be_/index.d.ts" />
2
+ /// <reference types="../be_sig_triple/index.d.ts" />
3
+ /// <reference types="../rmemo/index.d.ts" />
4
+ /// <reference types="./index.d.ts" />
5
+ import { be_ } from '../be_/index.js'
6
+ import { lock_memosig_ } from '../rmemo/index.js'
7
+ /**
8
+ * @param {Be<sig_T>|be__val__new_T<unknown>}be_OR_val__new
9
+ * @param {rmemo_subscriber_T[]|[...rmemo_subscriber_T[], be_config_T]}subscriber_a_THEN_config
10
+ * @returns {be_sig_triple_T}
11
+ * @private
12
+ */
13
+ export function be_lock_memosig_triple_(
14
+ be_OR_val__new,
15
+ ...subscriber_a_THEN_config
16
+ ) {
17
+ let config =
18
+ typeof subscriber_a_THEN_config[subscriber_a_THEN_config.length - 1] === 'object'
19
+ ? subscriber_a_THEN_config.pop()
20
+ : 0
21
+ /** @type {Be<sig_T>} */
22
+ let be =
23
+ be_OR_val__new.is_be
24
+ ? be_OR_val__new
25
+ : be_(ctx=>
26
+ lock_memosig_(
27
+ memo=>be_OR_val__new(ctx, memo),
28
+ ...subscriber_a_THEN_config.map(subscriber=>
29
+ sig=>subscriber(ctx, sig))),
30
+ config)
31
+ return [
32
+ be,
33
+ ctx=>be(ctx)(),
34
+ (ctx, val)=>{
35
+ be(ctx)._ = val
36
+ },
37
+ ]
38
+ }
@@ -0,0 +1,144 @@
1
+ import { test } from 'uvu'
2
+ import { equal } from 'uvu/assert'
3
+ import { be_ } from '../be_/index.js'
4
+ import { be_sig_triple_ } from '../be_sig_triple/index.js'
5
+ import { ctx__new } from '../ctx/index.js'
6
+ import { memosig_, type sig_T } from '../rmemo/index.js'
7
+ import { be_lock_memosig_triple_ } from './index.js'
8
+ test('be_lock_memosig_triple_', ()=>{
9
+ const [
10
+ ,
11
+ base_,
12
+ base__set,
13
+ ] = be_sig_triple_(()=>1)
14
+ const [
15
+ foobar$_,
16
+ foobar_,
17
+ foobar__set,
18
+ ] = be_lock_memosig_triple_<number, sig_T<number>&{
19
+ count:number
20
+ }>((_ctx, foobar$)=>{
21
+ equal(_ctx, ctx)
22
+ foobar$.count = (foobar$.count ||= 0) + 1
23
+ return base_(ctx) + 1
24
+ })
25
+ const ctx = ctx__new()
26
+ equal(foobar$_(ctx).count, undefined)
27
+ equal(foobar$_(ctx)._, 2)
28
+ equal(foobar_(ctx), 2)
29
+ equal(foobar$_(ctx).count, 1)
30
+ foobar__set(ctx, 5)
31
+ equal(foobar$_(ctx)._, 5)
32
+ equal(foobar_(ctx), 5)
33
+ equal(foobar$_(ctx).count, 1)
34
+ base__set(ctx, 2)
35
+ equal(foobar$_(ctx)._, 5)
36
+ equal(foobar_(ctx), 5)
37
+ equal(foobar$_(ctx).count, 1)
38
+ })
39
+ test('be_lock_memosig_triple_|+id|+is_source_', ()=>{
40
+ const ctx = ctx__new()
41
+ let subscriber_count = 0
42
+ const [
43
+ ,
44
+ base_,
45
+ base__set,
46
+ ] = be_sig_triple_(
47
+ ()=>1,
48
+ { is_source_: map_ctx=>map_ctx === ctx })
49
+ const [
50
+ ,
51
+ subscriber_dep_,
52
+ subscriber_dep__set
53
+ ] = be_sig_triple_(()=>1,
54
+ { is_source_: map_ctx=>map_ctx === ctx })
55
+ const [
56
+ foobar$_,
57
+ foobar_,
58
+ foobar__set,
59
+ ] = be_lock_memosig_triple_(
60
+ ctx=>
61
+ base_(ctx) + 1,
62
+ (ctx, foobar$)=>{
63
+ subscriber_count++
64
+ subscriber_dep__set(ctx, subscriber_count + foobar$())
65
+ },
66
+ { id: 'foobar', is_source_: map_ctx=>map_ctx === ctx })
67
+ equal(subscriber_count, 0)
68
+ equal(foobar$_([ctx__new(), ctx])._, 2)
69
+ equal(foobar_([ctx__new(), ctx]), 2)
70
+ equal(foobar$_(ctx)._, 2)
71
+ equal(foobar_(ctx), 2)
72
+ equal(subscriber_count, 1)
73
+ equal((ctx.get('foobar') as sig_T<number>)._, 2)
74
+ equal(subscriber_dep_(ctx), 3)
75
+ foobar__set([ctx__new(), ctx], 5)
76
+ equal(foobar$_([ctx__new(), ctx])._, 5)
77
+ equal(foobar_([ctx__new(), ctx]), 5)
78
+ equal(foobar$_(ctx)._, 5)
79
+ equal(foobar_(ctx), 5)
80
+ equal((ctx.get('foobar') as sig_T<number>)._, 5)
81
+ equal(subscriber_count, 2)
82
+ equal(subscriber_dep_(ctx), 7)
83
+ base__set(ctx, 2)
84
+ equal(foobar$_([ctx__new(), ctx])._, 5)
85
+ equal(foobar_([ctx__new(), ctx]), 5)
86
+ equal(foobar$_(ctx)._, 5)
87
+ equal(foobar_(ctx), 5)
88
+ equal(subscriber_count, 2)
89
+ equal((ctx.get('foobar') as sig_T<number>)._, 5)
90
+ equal(subscriber_dep_(ctx), 7)
91
+ })
92
+ test('be_lock_memosig_triple_|+be', ()=>{
93
+ const ctx = ctx__new()
94
+ let subscriber_count = 0
95
+ const [
96
+ ,
97
+ base_,
98
+ base__set,
99
+ ] = be_sig_triple_(()=>1,
100
+ { is_source_: map_ctx=>map_ctx === ctx })
101
+ const [
102
+ foobar$_,
103
+ foobar_,
104
+ foobar__set,
105
+ ] = be_lock_memosig_triple_<number, custom_sig_T>(
106
+ be_(ctx=>{
107
+ const foobar$ =
108
+ memosig_(()=>base_(ctx) + 1,
109
+ foobar$=>{
110
+ foobar$()
111
+ subscriber_count++
112
+ }) as custom_sig_T
113
+ foobar$.custom = 'custom-val'
114
+ return foobar$
115
+ }, { id: 'foobar', is_source_: map_ctx=>map_ctx === ctx }))
116
+ equal(subscriber_count, 0)
117
+ equal(foobar$_([ctx__new(), ctx])._, 2)
118
+ equal(foobar_([ctx__new(), ctx]), 2)
119
+ equal(foobar$_(ctx)._, 2)
120
+ equal(foobar_(ctx), 2)
121
+ equal(subscriber_count, 1)
122
+ equal((ctx.get('foobar') as sig_T<number>)._, 2)
123
+ equal(foobar$_(ctx).custom, 'custom-val')
124
+ foobar__set(ctx, 5)
125
+ equal(foobar$_([ctx__new(), ctx])._, 5)
126
+ equal(foobar_([ctx__new(), ctx]), 5)
127
+ equal(foobar$_(ctx)._, 5)
128
+ equal(foobar_(ctx), 5)
129
+ equal(subscriber_count, 2)
130
+ equal((ctx.get('foobar') as sig_T<number>)._, 5)
131
+ equal(foobar$_(ctx).custom, 'custom-val')
132
+ base__set([ctx__new(), ctx], 2)
133
+ equal(foobar$_([ctx__new(), ctx])._, 3)
134
+ equal(foobar_([ctx__new(), ctx]), 3)
135
+ equal(foobar$_(ctx)._, 3)
136
+ equal(foobar_(ctx), 3)
137
+ equal((ctx.get('foobar') as sig_T<number>)._, 3)
138
+ equal(foobar$_(ctx).custom, 'custom-val')
139
+ equal(subscriber_count, 3)
140
+ })
141
+ test.run()
142
+ type custom_sig_T = sig_T<number>&{
143
+ custom:string
144
+ }
@@ -3,19 +3,4 @@
3
3
  /// <reference types="../rmemo/index.d.ts" />
4
4
  /// <reference types="./index.d.ts" />
5
5
  import { be_memo_pair_ } from '../be_memo_pair/index.js'
6
- /**
7
- * @param {Be<sig_T>|be__val__new_T<unknown>}be_OR_val__new
8
- * @param {rmemo_subscriber_T[]|[...rmemo_subscriber_T[], be_config_T]}subscriber_a_THEN_config
9
- * @returns {be_memosig_triple_T}
10
- * @private
11
- */
12
- export function be_memosig_triple_(...arg_a) {
13
- /** @type {be_sig_triple_T} */
14
- let memo_pair = be_memo_pair_(...arg_a)
15
- return [
16
- ...memo_pair,
17
- (ctx, val)=>{
18
- memo_pair[0](ctx)._ = val
19
- },
20
- ]
21
- }
6
+ export { be_memo_pair_ as be_memosig_triple_ }
@@ -1,8 +1,7 @@
1
1
  /// <reference types="../be_/index.d.ts" />
2
2
  /// <reference types="../rmemo/index.d.ts" />
3
3
  /// <reference types="./index.d.ts" />
4
- import { be_, globalThis__be_ } from '../be_/index.js'
5
- import { ctx_, ctx__new } from '../ctx'
4
+ import { be_ } from '../be_/index.js'
6
5
  import { sig_ } from '../rmemo/index.js'
7
6
  /**
8
7
  * @param {Be<sig_T>|be__val__new_T<unknown>}be_OR_val__new
package/all/index.d.ts CHANGED
@@ -35,6 +35,7 @@ export * from './base64__buffer/index.js'
35
35
  export * from './batch_queue/index.js'
36
36
  export * from './be/index.js'
37
37
  export * from './be_/index.js'
38
+ export * from './be_lock_memosig_triple/index.js'
38
39
  export * from './be_memo_pair/index.js'
39
40
  export * from './be_sig_triple/index.js'
40
41
  export * from './binary_sort_rank/index.js'
package/all/index.js CHANGED
@@ -35,6 +35,7 @@ export * from './base64__buffer/index.js'
35
35
  export * from './batch_queue/index.js'
36
36
  export * from './be/index.js'
37
37
  export * from './be_/index.js'
38
+ export * from './be_lock_memosig_triple/index.js'
38
39
  export * from './be_memo_pair/index.js'
39
40
  export * from './be_sig_triple/index.js'
40
41
  export * from './binary_sort_rank/index.js'
@@ -1,2 +1,4 @@
1
- export declare function pathname_():string
1
+ export declare function pathname_(
2
+ url?:string
3
+ ):string|undefined
2
4
  export { pathname_ as _pathname, }
@@ -1,8 +1,11 @@
1
+ /// <reference lib="dom" />
1
2
  /**
2
- * @returns {string}
3
+ * @returns {string|undefined}
4
+ * @private true
3
5
  */
4
- export function pathname_() {
5
- const pathname = typeof window === 'object' && window.location.pathname || ''
6
- return pathname
6
+ export function pathname_(
7
+ url = typeof window === 'object' ? window.location.href : undefined
8
+ ) {
9
+ return url != null ? new URL(url).pathname : undefined
7
10
  }
8
11
  export { pathname_ as _pathname, }
@@ -1,16 +1,19 @@
1
1
  export declare function memo_<val_T>(
2
- def:rmemo_def_T<val_T>,
3
- ...subscriber_a:rmemo_subscriber_T<val_T>[]
2
+ def:memo_def_T<val_T>,
3
+ ...subscriber_a:memo_subscriber_T<val_T>[]
4
4
  ):memo_T<val_T>
5
5
  export declare function sig_<val_T>(
6
6
  init_val:val_T,
7
- ...subscriber_a:rmemo_subscriber_T<val_T>[]
7
+ ...subscriber_a:memo_subscriber_T<val_T>[]
8
8
  ):sig_T<val_T>
9
9
  export declare function memosig_<val_T>(
10
- def:rmemo_def_T<val_T>,
11
- ...subscriber_a:rmemo_subscriber_T<val_T>[]
10
+ def:memo_def_T<val_T>,
11
+ ...subscriber_a:memo_subscriber_T<val_T>[]
12
+ ):sig_T<val_T>
13
+ export declare function lock_memosig_<val_T>(
14
+ def:memo_def_T<val_T>,
15
+ ...subscriber_a:memo_subscriber_T<val_T>[]
12
16
  ):sig_T<val_T>
13
- // TODO: lock_memosig_
14
17
  export declare function on(rmemo:rmemo_T<unknown>):void
15
18
  export declare function off(rmemo:rmemo_T<unknown>):void
16
19
  export type rmemo_T<val_T> = memo_T<val_T>|sig_T<val_T>
@@ -29,5 +32,5 @@ export type sig_T<val_T> = (()=>val_T)&{
29
32
  export type rmemo_val_T<sig_T> = sig_T extends { ():infer val_T }
30
33
  ? val_T
31
34
  : unknown
32
- export type rmemo_def_T<val_T> = (sig:sig_T<val_T>)=>val_T
33
- export type rmemo_subscriber_T<val_T> = (sig:sig_T<val_T>)=>unknown
35
+ export type memo_def_T<val_T> = (sig:sig_T<val_T>)=>val_T
36
+ export type memo_subscriber_T<val_T> = (sig:sig_T<val_T>)=>unknown
@@ -4,12 +4,12 @@ let cur_memo
4
4
  /** @type {Set<()=>unknown>} */
5
5
  let queue = new Set
6
6
  /**
7
- * @param {rmemo_def_T}rmemo_def
7
+ * @param {rmemo_def_T}memo_def
8
8
  * @param {rmemo_subscriber_T<unknown>[]}subscriber_a
9
9
  * @returns {memo_T}
10
10
  * @private
11
11
  */
12
- export function memo_(rmemo_def, ...subscriber_a) {
12
+ export function memo_(memo_def, ...subscriber_a) {
13
13
  let memo = ()=>{
14
14
  if (!('val' in memo)) {
15
15
  memo.f()
@@ -66,7 +66,7 @@ export function memo_(rmemo_def, ...subscriber_a) {
66
66
  cur_memo = memo
67
67
  memo.f.s = []
68
68
  try {
69
- memo._ = rmemo_def(memo)
69
+ memo._ = memo_def(memo)
70
70
  } catch (err) {
71
71
  console.error(err)
72
72
  }
@@ -79,6 +79,27 @@ export function memo_(rmemo_def, ...subscriber_a) {
79
79
  return memo
80
80
  }
81
81
  export { memo_ as memosig_ }
82
+ /**
83
+ * @param {rmemo_def_T}memo_def
84
+ * @param {rmemo_subscriber_T<unknown>[]}subscriber_a
85
+ * @returns {sig_T}
86
+ * @private
87
+ */
88
+ export function lock_memosig_(memo_def, ...subscriber_a) {
89
+ return new Proxy(
90
+ /** @type {sig_T} */memo_(memo=>
91
+ memo.c ? memo._ : memo_def(memo),
92
+ ...subscriber_a),
93
+ {
94
+ set(memo, prop, val) {
95
+ if (prop === '_') {
96
+ memo.c = 1
97
+ memo._ = val
98
+ }
99
+ return 1
100
+ }
101
+ })
102
+ }
82
103
  /**
83
104
  * @param {unknown}init_val
84
105
  * @param {rmemo_subscriber_T[]}subscriber_a
@@ -111,6 +132,7 @@ export function on(rmemo) {
111
132
  export function off(rmemo) {
112
133
  if (rmemo.r) {
113
134
  rmemo.r.d ||= rmemo.r.deref
114
- rmemo.r.deref = ()=>{}
135
+ rmemo.r.deref = ()=>{
136
+ }
115
137
  }
116
138
  }
@@ -4,7 +4,7 @@ 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 { memo_, type memo_T, memosig_, off, on, sig_ } from './index.js'
7
+ import { lock_memosig_, memo_, type memo_T, memosig_, off, on, sig_ } from './index.js'
8
8
  test('memo_|static value', ()=>{
9
9
  let count = 0
10
10
  const memo = memo_(()=>{
@@ -113,6 +113,24 @@ test('memosig_', ()=>{
113
113
  equal(selected_index$(), 3)
114
114
  equal(selected_item$(), 'Item 4')
115
115
  })
116
+ test('lock_memosig_', ()=>{
117
+ const num_items$ = sig_(0)
118
+ const items$ = memo_(()=>[...Array(num_items$()).keys()].map(i=>`Item ${i + 1}`))
119
+ // TODO: Jetbrains or Typescript type inference is wrong without generic
120
+ const selected_index$ = lock_memosig_<number>(()=>(items$(), 0))
121
+ const selected_item$ = memo_(()=>items$()[selected_index$()])
122
+ num_items$._ = 3
123
+ equal(num_items$(), 3)
124
+ equal(items$().join(','), 'Item 1,Item 2,Item 3')
125
+ equal(selected_index$(), 0)
126
+ equal(selected_item$(), 'Item 1')
127
+ selected_index$._ = 2
128
+ equal(selected_index$(), 2)
129
+ equal(selected_item$(), 'Item 3')
130
+ num_items$._ = 5
131
+ equal(num_items$(), 5)
132
+ equal(selected_index$(), 2)
133
+ })
116
134
  test('memo_|error|case 1', ()=>{
117
135
  const r$ = memo_(()=>{
118
136
  throw new Error('error case')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ctx-core",
3
- "version": "4.14.1",
3
+ "version": "4.16.0",
4
4
  "description": "ctx-core core library",
5
5
  "keywords": [
6
6
  "ctx-core",
@@ -156,7 +156,7 @@
156
156
  "import": {
157
157
  "./rmemo": "{ sig_, memo_ }"
158
158
  },
159
- "limit": "355 B"
159
+ "limit": "356 B"
160
160
  },
161
161
  {
162
162
  "name": "memo_ sig_ be_ ctx_",
@@ -170,7 +170,7 @@
170
170
  "import": {
171
171
  "./rmemo": "{ sig_, memo_, be_, ctx_, be_memo_pair_, be_sig_triple_ }"
172
172
  },
173
- "limit": "591 B"
173
+ "limit": "588 B"
174
174
  }
175
175
  ],
176
176
  "scripts": {
package/rmemo/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from '../all/be_/index.js'
2
+ export * from '../all/be_lock_memosig_triple/index.js'
2
3
  export * from '../all/be_memo_pair/index.js'
3
4
  export * from '../all/be_memosig_triple/index.js'
4
5
  export * from '../all/be_sig_triple/index.js'
package/rmemo/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from '../all/be_/index.js'
2
+ export * from '../all/be_lock_memosig_triple/index.js'
2
3
  export * from '../all/be_memo_pair/index.js'
3
4
  export * from '../all/be_memosig_triple/index.js'
4
5
  export * from '../all/be_sig_triple/index.js'