sygnal 7.0.0 → 7.1.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.
@@ -3059,7 +3059,7 @@ function withUses(def, state) {
3059
3059
  * state reads from the cache ('success' with the cached data), else 'loading' ('idle' for a
3060
3060
  * falsy request), as the client's first paint does with the same (hydrated) cache
3061
3061
  */
3062
- function withResources(def, state) {
3062
+ function withResources(def, state, ctx) {
3063
3063
  state = withUses(def, state);
3064
3064
  const res = def && def.resources;
3065
3065
  if (!res || !state || typeof state !== 'object' || Array.isArray(state))
@@ -3070,7 +3070,7 @@ function withResources(def, state) {
3070
3070
  continue;
3071
3071
  let q;
3072
3072
  try {
3073
- q = typeof res[n] === 'function' ? res[n](state) : res[n];
3073
+ q = typeof res[n] === 'function' ? res[n](state, ctx) : res[n];
3074
3074
  }
3075
3075
  catch (_) { }
3076
3076
  const E = q && ssrCache ? ssrCache.get(ssrCache.key(q, 1)) : undefined;
@@ -3117,15 +3117,33 @@ function viewFailed(def, err, name) {
3117
3117
  }
3118
3118
  return vnode;
3119
3119
  }
3120
- function collectHead(def, state) {
3120
+ function collectHead(def, state, ctx) {
3121
3121
  const h = def && def.head;
3122
3122
  if (!heads || !h)
3123
3123
  return;
3124
3124
  try {
3125
- heads.push(typeof h === 'function' ? h(state) : h);
3125
+ heads.push(typeof h === 'function' ? h(state, ctx) : h);
3126
3126
  }
3127
3127
  catch (_) { }
3128
3128
  }
3129
+ /**
3130
+ * PLAN-13 F-1: the context a component sees (its parent's, plus its own entries computed on
3131
+ * `state`). The statics get it as on the client; for `resources` it is computed on the state
3132
+ * before the resources are filled in (a context entry can't read the component's own resource)
3133
+ */
3134
+ function contextOf(def, state, parent) {
3135
+ const own = def.context || {}, out = { ...parent };
3136
+ for (const key of Object.keys(own)) {
3137
+ const f = own[key];
3138
+ if (typeof f === 'function' && state != null) {
3139
+ try {
3140
+ out[key] = f(state);
3141
+ }
3142
+ catch (_) { }
3143
+ }
3144
+ }
3145
+ return out;
3146
+ }
3129
3147
  /**
3130
3148
  * Render a Sygnal component to an HTML string.
3131
3149
  *
@@ -3166,20 +3184,10 @@ function renderRoot(componentDef, options) {
3166
3184
  const uid = (options.uid || 'u').replace(/[^\w-]+/g, '_');
3167
3185
  const ownState = state !== undefined ? state : componentDef.initialState;
3168
3186
  // 5-5: the view sees its resources; the hydration script keeps the state as it was given
3169
- const resolvedState = withResources(componentDef, ownState);
3170
- collectHead(componentDef, resolvedState);
3187
+ const resolvedState = withResources(componentDef, ownState, contextOf(componentDef, ownState, context));
3171
3188
  // Build context: merge parent context with component's own context definitions
3172
- const componentContext = componentDef.context || {};
3173
- const mergedContext = { ...context };
3174
- for (const key of Object.keys(componentContext)) {
3175
- const contextFn = componentContext[key];
3176
- if (typeof contextFn === 'function' && resolvedState != null) {
3177
- try {
3178
- mergedContext[key] = contextFn(resolvedState);
3179
- }
3180
- catch (_) { }
3181
- }
3182
- }
3189
+ const mergedContext = contextOf(componentDef, resolvedState, context);
3190
+ collectHead(componentDef, resolvedState, mergedContext);
3183
3191
  // Call the view function
3184
3192
  let vnode;
3185
3193
  try {
@@ -3390,19 +3398,9 @@ function renderSubComponent(vnode, context, parentState, uid) {
3390
3398
  // State passed directly
3391
3399
  childState = stateProp;
3392
3400
  }
3393
- childState = withResources(componentDef, childState);
3401
+ childState = withResources(componentDef, childState, contextOf(componentDef, childState, context));
3394
3402
  // Build child context
3395
- const childContext = { ...context };
3396
- const componentContext = componentDef.context || {};
3397
- for (const key of Object.keys(componentContext)) {
3398
- const contextFn = componentContext[key];
3399
- if (typeof contextFn === 'function' && childState != null) {
3400
- try {
3401
- childContext[key] = contextFn(childState);
3402
- }
3403
- catch (_) { }
3404
- }
3405
- }
3403
+ const childContext = contextOf(componentDef, childState, context);
3406
3404
  // Extract slots from children
3407
3405
  const slots = {};
3408
3406
  const defaultSlotChildren = [];
@@ -3424,7 +3422,7 @@ function renderSubComponent(vnode, context, parentState, uid) {
3424
3422
  slots.default = [];
3425
3423
  slots.default.push(...defaultSlotChildren);
3426
3424
  }
3427
- collectHead(componentDef, childState);
3425
+ collectHead(componentDef, childState, childContext);
3428
3426
  // Call the view function
3429
3427
  let result;
3430
3428
  try {
@@ -3641,19 +3639,10 @@ function renderSwitchable(vnode, context, parentState, uid) {
3641
3639
  * Internal helper: render a component def to a VNode (not HTML string).
3642
3640
  */
3643
3641
  function renderToStringInternal(componentDef, state, context, uid) {
3644
- const resolvedState = withResources(componentDef, state !== undefined ? state : componentDef.initialState);
3645
- const componentContext = componentDef.context || {};
3646
- const mergedContext = { ...context };
3647
- for (const key of Object.keys(componentContext)) {
3648
- const contextFn = componentContext[key];
3649
- if (typeof contextFn === 'function' && resolvedState != null) {
3650
- try {
3651
- mergedContext[key] = contextFn(resolvedState);
3652
- }
3653
- catch (_) { }
3654
- }
3655
- }
3656
- collectHead(componentDef, resolvedState);
3642
+ const own = state !== undefined ? state : componentDef.initialState;
3643
+ const resolvedState = withResources(componentDef, own, contextOf(componentDef, own, context));
3644
+ const mergedContext = contextOf(componentDef, resolvedState, context);
3645
+ collectHead(componentDef, resolvedState, mergedContext);
3657
3646
  let vnode;
3658
3647
  try {
3659
3648
  vnode = componentDef({