signals.ts — astro Source File
Architecture documentation for signals.ts, a typescript file in the astro codebase. 2 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 4b26ab17_02c6_6dd0_7b9f_13b28a462719["signals.ts"] bf82cf79_dc97_8b42_cd75_dd4f0216baa0["./context.js"] 4b26ab17_02c6_6dd0_7b9f_13b28a462719 --> bf82cf79_dc97_8b42_cd75_dd4f0216baa0 88d5831c_10b7_7e11_b115_8c6e411804a3["./types.js"] 4b26ab17_02c6_6dd0_7b9f_13b28a462719 --> 88d5831c_10b7_7e11_b115_8c6e411804a3 style 4b26ab17_02c6_6dd0_7b9f_13b28a462719 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import type { Context } from './context.js';
import { incrementId } from './context.js';
import type {
ArrayObjectMapping,
AstroPreactAttrs,
PropNameToSignalMap,
SignalLike,
Signals,
SignalToKeyOrIndexMap,
} from './types.js';
function isSignal(x: any): x is SignalLike {
return x != null && typeof x === 'object' && typeof x.peek === 'function' && 'value' in x;
}
export function restoreSignalsOnProps(ctx: Context, props: Record<string, any>) {
// Restore signal props that were mutated for serialization
let propMap: PropNameToSignalMap;
if (ctx.propsToSignals.has(props)) {
propMap = ctx.propsToSignals.get(props)!;
} else {
propMap = new Map();
ctx.propsToSignals.set(props, propMap);
}
for (const [key, signal] of propMap) {
props[key] = signal;
}
return propMap;
}
export function serializeSignals(
ctx: Context,
props: Record<string, any>,
attrs: AstroPreactAttrs,
map: PropNameToSignalMap,
) {
// Check for signals
const signals: Signals = {};
for (const [key, value] of Object.entries(props)) {
const isPropArray = Array.isArray(value);
// `typeof null` is 'object' in JS, so we need to check for `null` specifically
const isPropObject =
!isSignal(value) && typeof props[key] === 'object' && props[key] !== null && !isPropArray;
if (isPropObject || isPropArray) {
const values = isPropObject ? Object.keys(props[key]) : value;
values.forEach((valueKey: number | string, valueIndex: number) => {
const signal = isPropObject ? props[key][valueKey] : valueKey;
if (isSignal(signal)) {
const keyOrIndex = isPropObject ? valueKey.toString() : valueIndex;
props[key] = isPropObject
? Object.assign({}, props[key], { [keyOrIndex]: signal.peek() })
: props[key].map((v: SignalLike, i: number) =>
i === valueIndex ? [signal.peek(), i] : v,
);
const currentMap = (map.get(key) || []) as SignalToKeyOrIndexMap;
map.set(key, [...currentMap, [signal, keyOrIndex]]);
const currentSignals = (signals[key] || []) as ArrayObjectMapping;
signals[key] = [...currentSignals, [getSignalId(ctx, signal), keyOrIndex]];
}
});
} else if (isSignal(value)) {
// Set the value to the current signal value
// This mutates the props on purpose, so that it will be serialized correct.
props[key] = value.peek();
map.set(key, value);
signals[key] = getSignalId(ctx, value);
}
}
if (Object.keys(signals).length) {
attrs['data-preact-signals'] = JSON.stringify(signals);
}
}
function getSignalId(ctx: Context, item: SignalLike) {
let id = ctx.signals.get(item);
if (!id) {
id = incrementId(ctx);
ctx.signals.set(item, id);
}
return id;
}
Domain
Subdomains
Dependencies
- ./context.js
- ./types.js
Source
Frequently Asked Questions
What does signals.ts do?
signals.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RoutingSystem subdomain.
What functions are defined in signals.ts?
signals.ts defines 4 function(s): getSignalId, isSignal, restoreSignalsOnProps, serializeSignals.
What does signals.ts depend on?
signals.ts imports 2 module(s): ./context.js, ./types.js.
Where is signals.ts in the architecture?
signals.ts is located at packages/integrations/preact/src/signals.ts (domain: CoreAstro, subdomain: RoutingSystem, directory: packages/integrations/preact/src).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free