deep signals improvements

feat/orm-diffs
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 ```ts
type DeepSignalOptions = { 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 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( const state = deepSignal(
{ data: {} }, { data: {} },
{ {
idGenerator: () => `urn:uuid:${crypto.randomUUID()}`, idGenerator: (_path) => `urn:uuid:${crypto.randomUUID()}`,
addIdToObjects: true addIdToObjects: true
} }
); );
@ -191,7 +191,7 @@ import { addWithId, setSetEntrySyntheticId } from "@ng-org/alien-deepsignals";
const state = deepSignal( const state = deepSignal(
{ items: new Set() }, { items: new Set() },
{ {
idGenerator: () => `urn:uuid:${crypto.randomUUID()}`, idGenerator: (_path) => `urn:uuid:${crypto.randomUUID()}`,
addIdToObjects: true addIdToObjects: true
} }
); );
@ -250,7 +250,7 @@ state.s.add({ data: "test" });
const state = deepSignal( const state = deepSignal(
{ users: new Set() }, { users: new Set() },
{ {
idGenerator: () => `urn:user:${crypto.randomUUID()}`, idGenerator: (path) => `urn:user:${path.join("-")}:${crypto.randomUUID()}`,
addIdToObjects: true addIdToObjects: true
} }
); );

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

Loading…
Cancel
Save