deep signals improvements

refactor
Laurin Weger 1 day ago
parent b4bcbecaaf
commit e979f0233a
No known key found for this signature in database
GPG Key ID: 9B372BB0B792770F
  1. 8
      sdk/js/alien-deepsignals/README.md
  2. 8
      sdk/js/alien-deepsignals/src/deepSignal.ts

@ -53,7 +53,7 @@ console.log(state.$count!()); // read via signal function
```ts
type DeepSignalOptions = {
idGenerator?: () => string | number; // Custom ID generator function
idGenerator?: (pathToObject: (string | number)[]) => string | number; // Custom ID generator function
addIdToObjects?: boolean; // Automatically add @id to plain objects
};
```
@ -84,7 +84,7 @@ When `addIdToObjects: true`, plain objects automatically receive a readonly, enu
const state = deepSignal(
{ data: {} },
{
idGenerator: () => `urn:uuid:${crypto.randomUUID()}`,
idGenerator: (_path) => `urn:uuid:${crypto.randomUUID()}`,
addIdToObjects: true
}
);
@ -191,7 +191,7 @@ import { addWithId, setSetEntrySyntheticId } from "@ng-org/alien-deepsignals";
const state = deepSignal(
{ items: new Set() },
{
idGenerator: () => `urn:uuid:${crypto.randomUUID()}`,
idGenerator: (_path) => `urn:uuid:${crypto.randomUUID()}`,
addIdToObjects: true
}
);
@ -250,7 +250,7 @@ state.s.add({ data: "test" });
const state = deepSignal(
{ users: new Set() },
{
idGenerator: () => `urn:user:${crypto.randomUUID()}`,
idGenerator: (path) => `urn:user:${path.join("-")}:${crypto.randomUUID()}`,
addIdToObjects: true
}
);

@ -59,7 +59,7 @@ export type DeepPatchSubscriber = (patches: DeepPatch[]) => void;
/** Options for configuring deepSignal behavior. */
export interface DeepSignalOptions {
/** Custom function to generate synthetic IDs for objects without @id. */
idGenerator?: () => string | number;
idGenerator?: (pathToObject: (string | number)[]) => string | number;
/** If true, add @id property to all objects in the tree. */
addIdToObjects?: boolean;
}
@ -156,7 +156,7 @@ function queueDeepPatches(
) {
let syntheticId: string | number;
if (options.idGenerator) {
syntheticId = options.idGenerator();
syntheticId = options.idGenerator(basePath);
} else {
syntheticId = assignBlankNodeId(val);
}
@ -516,6 +516,7 @@ function getFromSet(
};
// Pre-pass to ensure any existing non-proxied object entries are proxied (enables deep patches after iteration)
if (meta) raw.forEach(ensureEntryProxy);
if (key === "add" || key === "delete" || key === "clear") {
const fn: Function = (raw as any)[key];
return function (this: any, ...args: any[]) {
@ -545,7 +546,8 @@ function getFromSet(
) {
let syntheticId: string | number;
if (metaNow.options.idGenerator) {
syntheticId = metaNow.options.idGenerator();
syntheticId =
metaNow.options.idGenerator(containerPath);
} else {
syntheticId = assignBlankNodeId(entry);
}

Loading…
Cancel
Save