# `duplicateNodes`

> Contract 2.0.0 · package sha256 `a857eca95b35921109faf997094bb4192f12527d9d0fcd4be7e97f06425bad6e` · generated from `configurator-runtime.d.ts` by `services/onsite-scripter/scripts/build-contract-docs.mjs`; edit the source, not this file.

Function · `import { duplicateNodes } from '@holostep-configurators/runtime-client';` · stable · since 2.0.0 · section 22. Pure helpers (root of the package; run in Node and the fake)

Copies nodes with new ids. Within the copies it remaps each reference whose target is copied:
`defaultNodeId`, every `globalMaterialList[].globalMaterial` (a ModelNode's and a GroupNode's
`childDefaults`), and both ends of a connection inside the copy; a reference to a node outside
the copy and an empty selection stay as given, and a connection that crosses the copy's
boundary is not copied (spec section 6.9, ID3). Child nodes are copied too. Each
copy's `_id` is its new `nodeId`, as 1.4 wrote a re-minted node, never its source's (the
backend's `_id` names one stored node); every remaining field, `plainGlbUrl` and
`plainOptGlbUrl` included, is copied as given. So copy from your own document: the runtime's model carries no
plain twin, and the backend re-grafts a twin by `_id` only, so a copy saved from it has none.
Sync, pure. Returns the copies, their connections and the old → new id map.

## Declaration

```ts
export declare function duplicateNodes(nodes: readonly DeepReadonly<ConfiguratorNode>[], options: {
  reserved: Iterable<string>;
  connections?: readonly Connection[];
  mint?: () => string;
}): {
  nodes: ConfiguratorNode[];
  connections: Connection[];
  remap: ReadonlyMap<string, string>;
};
```

## Example

Imports: values (`has`, `validate`, the `Holo*Error` classes, the pure helpers) from `@holostep-configurators/runtime-client`, types from `@holostep-configurators/runtime-client/contract`, `/math` and `/testing` names from those entries; `boot` from the runtime module the root's `loadRuntimeAtFloor` resolves to. A type your page defines is your app's: declare it there, never import it from the package.

Compiled under the contract's strict settings (`examples/declaration/22-duplicateNodes.ts`):

```ts
import { duplicateNodes } from '@holostep-configurators/runtime-client';
import type { ConfiguratorNode, DeepReadonly, IConfigurator, SceneModel, Session } from '@holostep-configurators/runtime-client/contract';
declare const session: Session;
declare const signal: AbortSignal;
// An app whose scene model is its source of truth. A store app duplicates in its store and lets
// its one sync write the model (spec section 11.2).
// your own document, this session's edits merged in by `nodeId` (spec section 8.5)
declare const doc: DeepReadonly<IConfigurator>;
declare function allNodeIds(model: DeepReadonly<SceneModel>): string[];
// a tree walk: nested nodes too
declare function findNode(model: DeepReadonly<SceneModel>, nodeId: string): DeepReadonly<ConfiguratorNode> | undefined;
// the same tree with `nodes` inserted right after `nodeId`, at its depth
declare function insertAfter(
  root: DeepReadonly<SceneModel>['root'], nodeId: string, nodes: readonly ConfiguratorNode[],
): ConfiguratorNode[];
export async function duplicate(nodeId: string): Promise<void> {
  const model = session.getSceneModel();
  // From your document: plain twins are input-only, so `model` has none to copy.
  const original = findNode(doc, nodeId);
  if (!model || !original) return;
  // The runtime's model carries no connections: read them from the assembly.
  const connections = session.assembly.connections();
  const copy = duplicateNodes([original], { reserved: allNodeIds(model), connections });
  // Add `copy.nodes` to your document too, so your next save carries their twins.
  await session.setSceneModel(
    {
      ...model,
      root: insertAfter(model.root, nodeId, copy.nodes),
      // no connectionsBasis: connections built in this session need no basis check
      connections: [...connections, ...copy.connections],
    },
    { undo: 'Duplicate', actor: 'shopper' },
  );
}
```

## Generic-API test (spec 3.1)

| Rule | Result |
| --- | --- |
| 1. Neutral name | pass: `duplicateNodes` names no product |
| 2. A precedent | pass: [DOM Document.getElementById (unique ids)](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById) (row `X3`) |
| 3. Two apps, or labs | pass: 4 apps (ledger: app-04c2774145, app-37356bf09f, app-4762caacdc, app-f9a32142f1) |
| 4. The cheaper fix first | decided per need in Appendix B: rows `X3` at tier stable (a member only where no rule, data, asset or app code serves the need) |
