ctx-core 4.12.0 → 4.13.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.
@@ -58,7 +58,7 @@ export declare type Be<
58
58
  ctx_T extends Ctx = Ctx
59
59
  > = ((ctx:ctx_T)=>val_T)&{
60
60
  is_be: true
61
- id?:string
61
+ id:string|Be<val_T, ctx_T>
62
62
  }
63
63
  export type be_config_T = {
64
64
  id?:string
package/all/be_/index.js CHANGED
@@ -1,4 +1,3 @@
1
- import { globalThis__prop__ensure } from '../globalThis__prop__ensure/index.js'
2
1
  /** @typedef {import('./index.d.ts').Be} */
3
2
  /** @typedef {import('./index.d.ts').be_} */
4
3
  /** @typedef {import('./index.d.ts').be_config_T} */
@@ -6,22 +5,6 @@ import { globalThis__prop__ensure } from '../globalThis__prop__ensure/index.js'
6
5
  /** @typedef {import('./index.d.ts').MapCtx} */
7
6
  /** @typedef {import('./index.d.ts').be__val__new_T} */
8
7
  /** @typedef {import('./index.d.ts').is_source__T} */
9
- /**
10
- * Auto-memoization function for the Ctx.
11
- * Memoized on globalThis to allow packages being loaded multiple times, which can happen during bundling.
12
- *
13
- * Returns a function to ensure that a member id_OR_val_ is defined on a ctx object,
14
- * otherwise it caches & uses the return value of val__new.
15
- * @param {string|symbol}id
16
- * @param {be__val__new_T}val__new
17
- * @param {be_config_T}config
18
- * @returns {Be}
19
- * @private
20
- */
21
- export function globalThis__be_(val__new, config) {
22
- return globalThis__prop__ensure(config.id, ()=>
23
- be_(val__new, config))
24
- }
25
8
  /**
26
9
  * Auto-memoization function for the Ctx.
27
10
  *
@@ -34,30 +17,42 @@ export function globalThis__be_(val__new, config) {
34
17
  */
35
18
  export function be_(val__new, config) {
36
19
  let be = argv__ctx=>{
37
- let flat__argv__ctx = Array.isArray(argv__ctx) && argv__ctx.flat(Infinity)
20
+ let flat__argv__ctx = argv__ctx.flat && argv__ctx.flat(Infinity)
38
21
  let be__ctx =
39
- flat__argv__ctx
40
- ? flat__argv__ctx.find(ctx=>ctx.has(be))
41
- : argv__ctx.has(be) && argv__ctx
22
+ flat__argv__ctx
23
+ ? flat__argv__ctx.find(ctx=>ctx.has(be.id))
24
+ : argv__ctx.has(be.id) && argv__ctx
42
25
  if (!be__ctx) {
43
26
  be__ctx =
44
27
  flat__argv__ctx
45
28
  ? flat__argv__ctx.find(ctx=>!be.is_source_ || be.is_source_(ctx, argv__ctx))
46
29
  : (!be.is_source_ || be.is_source_(argv__ctx, argv__ctx)) && argv__ctx
47
30
  // circular dependency state
48
- // if val__new calls this be before returning, Symbol('cir') will be the value of this be
31
+ // if val__new calls this be before returning, 'cir' will be the value of this be
49
32
  // 'cir' is used instead of 'circular' to reduce the payload by a few bytes
50
- be__ctx.set(be, Symbol('cir'))
51
- be__ctx.set(be, val__new(argv__ctx, be))
52
- if (be.id) be__ctx.set(be.id, be__ctx.get(be))
33
+ be__ctx.set(be.id, 'cir')
34
+ be__ctx.set(be.id, val__new(argv__ctx, be))
53
35
  }
54
- return be__ctx.get(be)
36
+ return be__ctx.get(be.id)
55
37
  }
56
- be.id = config?.id
38
+ be.id = config?.id ?? be
57
39
  be.is_source_ = config?.is_source_
58
40
  be.is_be = true
59
41
  return be
60
42
  }
43
+ /**
44
+ * Auto-memoization function for the Ctx.
45
+ * Memoized on globalThis to allow packages being loaded multiple times, which can happen during bundling.
46
+ *
47
+ * Returns a function to ensure that a member id_OR_val_ is defined on a ctx object,
48
+ * otherwise it caches & uses the return value of val__new.
49
+ * @param {string|symbol}id
50
+ * @param {be__val__new_T}val__new
51
+ * @param {be_config_T}config
52
+ * @returns {Be}
53
+ * @private
54
+ */
55
+ export { be_ as globalThis__be_ }
61
56
  /**
62
57
  * @param {Ctx}ctx
63
58
  * @param {Be|string|symbol}be_OR_id
@@ -74,11 +69,7 @@ export function ctx__set(
74
69
  ) {
75
70
  let source__map_ctx = source__map_ctx_(ctx, is_source_)
76
71
  if (source__map_ctx) {
77
- source__map_ctx.set(be_OR_id, val)
78
- let { id } = be_OR_id
79
- if (id) {
80
- source__map_ctx.set(id, val)
81
- }
72
+ source__map_ctx.set(be_OR_id.id ?? be_OR_id, val)
82
73
  }
83
74
  }
84
75
  /**
@@ -94,13 +85,7 @@ export function ctx__delete(
94
85
  ctx__delete(_ctx, be_OR_id)
95
86
  }
96
87
  } else {
97
- /** @type {MapCtx} */
98
- let map_ctx = /** @type {any} */ctx
99
- let { id } = be_OR_id
100
- if (id) {
101
- map_ctx.delete(id)
102
- }
103
- map_ctx.delete(be_OR_id)
88
+ ctx.delete(be_OR_id.id ?? be_OR_id)
104
89
  }
105
90
  }
106
91
  /**
@@ -121,11 +106,11 @@ export function be__has_(be_or_id, argv__ctx) {
121
106
  export function be__ctx_(be_or_id, argv__ctx) {
122
107
  if (Array.isArray(argv__ctx)) {
123
108
  for (let ctx of argv__ctx) {
124
- let be__ctx = be__ctx_(be_or_id, ctx)
109
+ let be__ctx = be__ctx_(be_or_id.id ?? be_or_id, ctx)
125
110
  if (be__ctx) return be__ctx
126
111
  }
127
112
  } else {
128
- if (argv__ctx.has(be_or_id)) return argv__ctx
113
+ if (argv__ctx.has(be_or_id.id ?? be_or_id)) return argv__ctx
129
114
  }
130
115
  }
131
116
  export { be__ctx_ as be__has__ctx_ }
@@ -137,7 +122,7 @@ export { be__ctx_ as be__has__ctx_ }
137
122
  */
138
123
  export function be__val_(be_or_id, argv__ctx) {
139
124
  let be__ctx = be__ctx_(be_or_id, argv__ctx)
140
- if (be__ctx) return be__ctx.get(be_or_id)
125
+ if (be__ctx) return be__ctx.get(be_or_id.id ?? be_or_id)
141
126
  }
142
127
  /**
143
128
  * @param {Ctx}argv__ctx
@@ -151,7 +136,9 @@ export function source__map_ctx_(argv__ctx, is_source_) {
151
136
  let source__map_ctx = source__map_ctx_(ctx, is_source_)
152
137
  if (source__map_ctx) return source__map_ctx
153
138
  }
154
- } else if (!is_source_ || is_source_(/** @type {MapCtx} */argv__ctx, argv__ctx)) {
155
- return argv__ctx
139
+ } else {
140
+ if (!is_source_ || is_source_(argv__ctx, argv__ctx)) {
141
+ return argv__ctx
142
+ }
156
143
  }
157
144
  }
@@ -18,26 +18,23 @@ test.after(()=>{
18
18
  })
19
19
  test('globalThis__be_', ()=>{
20
20
  const ctx = ctx__new()
21
- equal(root_be_(), undefined)
22
21
  const be = globalThis__be_(()=>1, { id: 'root_be' })
23
- equal(root_be_(), be)
24
- equal(root_be_()!(ctx), 1)
25
22
  equal(be(ctx), 1)
26
23
  const be2 = globalThis__be_(()=>1, { id: 'root_be' })
27
- equal(be2, be)
28
- equal(root_be_(), be2)
29
- equal(root_be_()!(ctx), 1)
30
24
  equal(be2(ctx), 1)
31
- function root_be_() {
32
- return (globalThis as { root_be?:Be<unknown> }).root_be
33
- }
34
25
  })
35
26
  test('be_|Map', ()=>{
36
27
  const ctx = ctx__new()
37
28
  let incrementer_num = 0
38
29
  const incrementer = ()=>++incrementer_num
39
- const root_ = be_(()=>incrementer(), { id: 'root_' })
40
- const child_ = be_(ctx=>root_(ctx) + incrementer(), { id: 'child_' })
30
+ const root_ = be_(()=>{
31
+ console.debug('root_|debug|1')
32
+ return incrementer()
33
+ }, { id: 'root_' })
34
+ const child_ = be_(ctx=>{
35
+ console.debug('child_|debug|1')
36
+ return root_(ctx) + incrementer()
37
+ }, { id: 'child_' })
41
38
  const child1_ = be_(ctx=>root_(ctx) + child_(ctx), { id: 'child1_' })
42
39
  equal(root_(ctx), 1)
43
40
  equal(ctx.get('root_'), 1)
@@ -53,12 +50,12 @@ test('be_|simple array', ()=>{
53
50
  const root_ = be_(()=>1, { id: 'root_' })
54
51
  equal(root_(ctx1), 1)
55
52
  equal(root_(ctx), 1)
56
- equal(ctx0.has(root_), false)
57
- equal(ctx1.has(root_), true)
53
+ equal(ctx0.has(root_.id), false)
54
+ equal(ctx1.has(root_.id), true)
58
55
  const child_ = be_(ctx=>root_(ctx) + 1, { id: 'child_' })
59
56
  equal(child_(ctx), 2)
60
- equal(ctx0.has(child_), true)
61
- equal(ctx1.has(child_), false)
57
+ equal(ctx0.has(child_.id), true)
58
+ equal(ctx1.has(child_.id), false)
62
59
  })
63
60
  test('be_|nested array', ()=>{
64
61
  const ctx0 = ctx__new()
@@ -69,16 +66,16 @@ test('be_|nested array', ()=>{
69
66
  const root_ = be_(()=>1, { id: 'root_' })
70
67
  equal(root_(ctx3), 1)
71
68
  equal(root_(ctx), 1)
72
- equal(ctx0.has(root_), false)
73
- equal(ctx1.has(root_), false)
74
- equal(ctx2.has(root_), false)
75
- equal(ctx3.has(root_), true)
69
+ equal(ctx0.has(root_.id), false)
70
+ equal(ctx1.has(root_.id), false)
71
+ equal(ctx2.has(root_.id), false)
72
+ equal(ctx3.has(root_.id), true)
76
73
  const child_ = be_(ctx=>root_(ctx) + 1, { id: 'child_' })
77
74
  equal(child_(ctx), 2)
78
- equal(ctx0.has(child_), true)
79
- equal(ctx1.has(child_), false)
80
- equal(ctx2.has(child_), false)
81
- equal(ctx3.has(child_), false)
75
+ equal(ctx0.has(child_.id), true)
76
+ equal(ctx1.has(child_.id), false)
77
+ equal(ctx2.has(child_.id), false)
78
+ equal(ctx3.has(child_.id), false)
82
79
  })
83
80
  test('be_|is_source_', ()=>{
84
81
  const ctx0 = ctx__new()
@@ -95,8 +92,8 @@ test('be_|is_source_', ()=>{
95
92
  })
96
93
  equal(root_(ctx), 1)
97
94
  equal(be__ctx_a, [[ctx0, ctx1]])
98
- equal(ctx0.has(root_), false)
99
- equal(ctx1.has(root_), true)
95
+ equal(ctx0.has(root_.id), false)
96
+ equal(ctx1.has(root_.id), true)
100
97
  })
101
98
  test('be_|Ctx generic type', ()=>{
102
99
  const valid_ctx = ctx__new() as test_ctx_T
@@ -116,8 +113,8 @@ test('be_|circular dependency', ()=>{
116
113
  const be0:Be<symbol> = be_(ctx=>be1(ctx))
117
114
  const be1:Be<symbol> = be_(ctx=>be0(ctx))
118
115
  const ctx = ctx__new()
119
- equal(String(be0(ctx)), 'Symbol(cir)')
120
- equal(String(be1(ctx)), 'Symbol(cir)')
116
+ equal(be0(ctx), 'cir')
117
+ equal(be1(ctx), 'cir')
121
118
  })
122
119
  test('ctx__set', ()=>{
123
120
  const ctx0 = ctx__new()
@@ -152,46 +149,46 @@ test('ctx__delete|be', ()=>{
152
149
  const ctx0 = ctx__new()
153
150
  const num_ = be_(()=>1)
154
151
  ctx__delete(ctx0, num_)
155
- equal(ctx0.has(num_), false)
152
+ equal(ctx0.has(num_.id), false)
156
153
  num_(ctx0)
157
- equal(ctx0.has(num_), true)
154
+ equal(ctx0.has(num_.id), true)
158
155
  ctx__delete(ctx0, num_)
159
- equal(ctx0.has(num_), false)
156
+ equal(ctx0.has(num_.id), false)
160
157
  num_(ctx0)
161
- equal(ctx0.has(num_), true)
158
+ equal(ctx0.has(num_.id), true)
162
159
  ctx__delete(ctx0, num_)
163
- equal(ctx0.has(num_), false)
160
+ equal(ctx0.has(num_.id), false)
164
161
  const ctx1 = ctx__new()
165
162
  ctx1.set('ctx1', true)
166
163
  const nested__ctx = [ctx0, ctx1]
167
164
  num_(ctx0)
168
165
  num_(ctx1)
169
- equal(ctx0.has(num_), true)
170
- equal(ctx1.has(num_), true)
166
+ equal(ctx0.has(num_.id), true)
167
+ equal(ctx1.has(num_.id), true)
171
168
  ctx__delete(nested__ctx, num_)
172
- equal(ctx0.has(num_), false)
173
- equal(ctx1.has(num_), false)
169
+ equal(ctx0.has(num_.id), false)
170
+ equal(ctx1.has(num_.id), false)
174
171
  num_(ctx0)
175
172
  num_(ctx1)
176
- equal(ctx0.has(num_), true)
177
- equal(ctx1.has(num_), true)
173
+ equal(ctx0.has(num_.id), true)
174
+ equal(ctx1.has(num_.id), true)
178
175
  ctx__delete(nested__ctx, num_)
179
- equal(ctx0.has(num_), false)
180
- equal(ctx1.has(num_), false)
176
+ equal(ctx0.has(num_.id), false)
177
+ equal(ctx1.has(num_.id), false)
181
178
  const is_source__num_ =
182
179
  be_(()=>1, { is_source_: ctx=>!!ctx.get('ctx1') })
183
180
  is_source__num_(nested__ctx)
184
- equal(ctx0.has(is_source__num_), false)
185
- equal(ctx1.has(is_source__num_), true)
181
+ equal(ctx0.has(is_source__num_.id), false)
182
+ equal(ctx1.has(is_source__num_.id), true)
186
183
  ctx__delete(nested__ctx, is_source__num_)
187
- equal(ctx0.has(is_source__num_), false)
188
- equal(ctx1.has(is_source__num_), false)
184
+ equal(ctx0.has(is_source__num_.id), false)
185
+ equal(ctx1.has(is_source__num_.id), false)
189
186
  is_source__num_(nested__ctx)
190
- equal(ctx0.has(is_source__num_), false)
191
- equal(ctx1.has(is_source__num_), true)
187
+ equal(ctx0.has(is_source__num_.id), false)
188
+ equal(ctx1.has(is_source__num_.id), true)
192
189
  ctx__delete(nested__ctx, is_source__num_)
193
- equal(ctx0.has(is_source__num_), false)
194
- equal(ctx1.has(is_source__num_), false)
190
+ equal(ctx0.has(is_source__num_.id), false)
191
+ equal(ctx1.has(is_source__num_.id), false)
195
192
  })
196
193
  test('be__has_', ()=>{
197
194
  const ctx0 = ctx__new()
@@ -223,7 +220,7 @@ test('be__val_', ()=>{
223
220
  const ctx = ctx__new()
224
221
  const val_ = be_<boolean>(()=>true, { id: 'val_' })
225
222
  equal(val_(ctx), true)
226
- equal(ctx.get(val_), true)
223
+ equal(ctx.get(val_.id), true)
227
224
  equal(be__val_(val_, ctx), true)
228
225
  ctx__set(ctx, val_, false)
229
226
  equal(val_(ctx), false)
@@ -232,5 +229,5 @@ test('be__val_', ()=>{
232
229
  test.run()
233
230
  declare const test_ctx_sym:unique symbol
234
231
  type test_ctx_T = Ctx&{
235
- [test_ctx_sym]:any
232
+ [test_ctx_sym]:unknown
236
233
  }
@@ -15,6 +15,15 @@ export declare function be_memo_pair_<
15
15
  |[...((ctx:Ctx, memosig:sig_T<val_T>)=>unknown)[]]
16
16
  |[...((ctx:Ctx, memosig:sig_T<val_T>)=>unknown)[], config:be_config_T]
17
17
  ):be_memo_pair_T<val_T, _memo_T, ctx_T>
18
+ export declare function globalThis__be_memo_pair_<
19
+ val_T,
20
+ _memo_T extends memo_T<val_T> = memo_T<val_T>,
21
+ ctx_T extends Ctx = Ctx
22
+ >(
23
+ rmemo__new:(ctx:Ctx, memo:_memo_T&{ _:val_T })=>val_T,
24
+ ...subscriber_a_THEN_config:
25
+ |[...((ctx:Ctx, memosig:sig_T<val_T>)=>unknown)[], config:be_config_T&{ id:string }]
26
+ ):be_memo_pair_T<val_T, _memo_T, ctx_T>
18
27
  export type be_memo_pair_T<
19
28
  val_T,
20
29
  _memo_T extends memo_T<val_T> = memo_T<val_T>,
@@ -15,6 +15,15 @@ export declare function be_memosig_triple_<
15
15
  |[...((ctx:Ctx, sig:_sig_T)=>unknown)[]]
16
16
  |[...((ctx:Ctx, sig:_sig_T)=>unknown)[], config:be_config_T]
17
17
  ):be_memosig_triple_T<val_T, _sig_T, ctx_T>
18
+ export declare function globalThis__be_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_memosig_triple_T<val_T, _sig_T, ctx_T>
18
27
  export type be_memosig_triple_T<
19
28
  val_T,
20
29
  _sig_T extends sig_T<val_T> = sig_T<val_T>,
@@ -1,7 +1,8 @@
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_ } from '../be_/index.js'
4
+ import { be_, globalThis__be_ } from '../be_/index.js'
5
+ import { ctx_, ctx__new } from '../ctx'
5
6
  import { sig_ } from '../rmemo/index.js'
6
7
  /**
7
8
  * @param {Be<sig_T>|be__val__new_T<unknown>}be_OR_val__new
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ctx-core",
3
- "version": "4.12.0",
3
+ "version": "4.13.0",
4
4
  "description": "ctx-core core library",
5
5
  "keywords": [
6
6
  "ctx-core",
@@ -135,14 +135,14 @@
135
135
  "import": {
136
136
  "./be": "{ be_ }"
137
137
  },
138
- "limit": "162 B"
138
+ "limit": "147 B"
139
139
  },
140
140
  {
141
141
  "name": "be_ ctx_",
142
142
  "import": {
143
143
  "./be": "{ be_, ctx_ }"
144
144
  },
145
- "limit": "187 B"
145
+ "limit": "173 B"
146
146
  },
147
147
  {
148
148
  "name": "memo_",
@@ -163,14 +163,14 @@
163
163
  "import": {
164
164
  "./rmemo": "{ sig_, memo_, be_, ctx_ }"
165
165
  },
166
- "limit": "506 B"
166
+ "limit": "494 B"
167
167
  },
168
168
  {
169
169
  "name": "memo_ sig_ be_ ctx_ be_memo_pair_ be_sig_triple_",
170
170
  "import": {
171
171
  "./rmemo": "{ sig_, memo_, be_, ctx_, be_memo_pair_, be_sig_triple_ }"
172
172
  },
173
- "limit": "600 B"
173
+ "limit": "591 B"
174
174
  }
175
175
  ],
176
176
  "scripts": {