Skip to content

Commit 661cf04

Browse files
committed
feat(di): make the contract name optional, split out LegacyContract
@contract() declares a token resolved by the class alone. @LegacyContract ({ name }) is the form that also answers to a string name, for callers that resolve by string and for extensions carrying their own copy of a contract. @contract({ name }) keeps working as a deprecated overload, since it was published. Every existing contract keeps its name and moves to @LegacyContract, with no change in behaviour.
1 parent 5a9a505 commit 661cf04

24 files changed

Lines changed: 139 additions & 52 deletions

‎lib/common/contracts/command-registry.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Contract } from "../di/contract";
1+
import { LegacyContract } from "../di/contract";
22
import { InjectionToken } from "../di/injection-token";
33
import type { ICommand } from "../definitions/commands";
44

@@ -84,7 +84,7 @@ export type DeferredCommandResult =
8484
* facade later is a provider swap for this token, not a consumer migration.
8585
* Members slated for replacement keep their deprecation markers.
8686
*/
87-
@Contract({ name: "commandRegistry" })
87+
@LegacyContract({ name: "commandRegistry" })
8888
export abstract class CommandRegistry {
8989
/**
9090
* @deprecated Path-based command registration; use registerDeferredCommand,

‎lib/common/contracts/commands-service.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Contract } from "../di/contract";
1+
import { LegacyContract } from "../di/contract";
22
import type { Injector } from "../di/injector";
33
import type { CommandReference } from "../define-command";
44

@@ -17,7 +17,7 @@ export interface CommandDispatchOptions {
1717
* key shortcut or a plugin uses to run or consult another command. The command
1818
* line's own entry points into the dispatcher are not part of it.
1919
*/
20-
@Contract({ name: "commandsService" })
20+
@LegacyContract({ name: "commandsService" })
2121
export abstract class CommandsService {
2222
/**
2323
* Whether the command running now was dispatched in process rather than by

‎lib/common/contracts/key-shortcuts.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Contract } from "../di/contract";
1+
import { LegacyContract } from "../di/contract";
22
import type { Injector } from "../di/injector";
33

44
/**
@@ -53,7 +53,7 @@ export interface KeyShortcutRegistration {
5353
* batch attach itself registered, so entries a lifecycle registered on its own
5454
* survive until that lifecycle disposes them.
5555
*/
56-
@Contract({ name: "keyShortcutRegistry" })
56+
@LegacyContract({ name: "keyShortcutRegistry" })
5757
export abstract class KeyShortcutRegistry {
5858
/** Later registrations shadow earlier ones per key; disposing restores what was shadowed. */
5959
abstract add(...shortcuts: KeyShortcut[]): KeyShortcutRegistration;

‎lib/common/contracts/module-registry.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Contract } from "../di/contract";
1+
import { LegacyContract } from "../di/contract";
22

33
/**
44
* @deprecated The lazy module-loader face of the injector facade — the
55
* require-time path map. Wholly replaced by provideLazy(); this contract
66
* exists so its remaining consumers are typed against exactly what they use
77
* until the bootstrap migrates.
88
*/
9-
@Contract({ name: "moduleRegistry" })
9+
@LegacyContract({ name: "moduleRegistry" })
1010
export abstract class ModuleRegistry {
1111
abstract require(names: string | string[], file: string): void;
1212
abstract overrideAlreadyRequiredModule: boolean;

‎lib/common/contracts/public-api-builder.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Contract } from "../di/contract";
1+
import { LegacyContract } from "../di/contract";
22

33
/**
44
* The public-API-builder face of the injector facade: the machinery behind
55
* `require('nativescript')`. Bound by the compatibility constraints of the
66
* published library surface; do not add new entries through it.
77
*/
8-
@Contract({ name: "publicApiBuilder" })
8+
@LegacyContract({ name: "publicApiBuilder" })
99
export abstract class PublicApiBuilder {
1010
abstract requirePublic(names: string | string[], file: string): void;
1111
abstract requirePublicClass(names: string | string[], file: string): void;

‎lib/common/di/contract.ts‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
export const CONTRACT_NAME = Symbol.for("nativescript:di:contractName");
99

10-
export interface IContractOptions {
10+
export interface ILegacyContractOptions {
1111
/**
1212
* Canonical token name, without the `$` prefix. Must be an explicit string
1313
* literal — never derive it from `class.name`, which changes under
@@ -16,6 +16,9 @@ export interface IContractOptions {
1616
name: string;
1717
}
1818

19+
/** @deprecated Use {@link ILegacyContractOptions}. */
20+
export type IContractOptions = ILegacyContractOptions;
21+
1922
// Per module instance on purpose: a duplicated CLI copy in an extensions tree
2023
// carries its own registry, so contracts redeclared by another copy never
2124
// false-positive here.
@@ -29,7 +32,7 @@ function describeOwner(owner: object): string {
2932
}
3033

3134
/**
32-
* Claims a token name for `owner`. Both `@Contract` and `InjectionToken` mint
35+
* Claims a token name for `owner`. Both `@LegacyContract` and `InjectionToken` mint
3336
* here so the two kinds share one namespace: a contract and a token that claim
3437
* the same name would be two tokens silently aliasing one registration.
3538
*/
@@ -45,12 +48,31 @@ export function mintTokenName(name: string, owner: object): void {
4548
}
4649

4750
/**
48-
* Marks an abstract class as a DI token. The decorated class resolves by
49-
* object identity first and by its name on a miss, so duplicated copies of a
50-
* contract remain interchangeable across node_modules trees.
51+
* Marks an abstract class as a DI token, resolved by object identity alone: it
52+
* has no string spelling, and another copy of the class is a different token.
5153
*/
54+
export function Contract(): (target: Function) => void;
55+
/** @deprecated Use {@link LegacyContract} for a contract that needs a name. */
56+
export function Contract(
57+
options: ILegacyContractOptions,
58+
): (target: Function) => void;
5259
export function Contract(
53-
options: IContractOptions,
60+
options?: ILegacyContractOptions,
61+
): (target: Function) => void {
62+
return options && options.name !== undefined
63+
? LegacyContract(options)
64+
: () => {};
65+
}
66+
67+
/**
68+
* A contract that also answers to a string name, for compatibility: with
69+
* callers that resolve the service by string (hooks, `$`-prefixed parameters,
70+
* string registrations), or with extensions whose own copy of the contract has
71+
* to reach the running CLI's provider. Identity is tried first, the name on a
72+
* miss.
73+
*/
74+
export function LegacyContract(
75+
options: ILegacyContractOptions,
5476
): (target: Function) => void {
5577
const { name } = options;
5678
return (target: Function): void => {

‎lib/common/di/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ export { inject, getCurrentInjector, runInInjectionContext } from "./inject";
44
export { forwardRef, resolveForwardRef } from "./forward-ref";
55
export {
66
Contract,
7+
LegacyContract,
78
getContractName,
89
CONTRACT_NAME,
910
clearMintedContractNames,
1011
} from "./contract";
11-
export type { IContractOptions } from "./contract";
12+
export type { IContractOptions, ILegacyContractOptions } from "./contract";
1213
export {
1314
InjectionToken,
1415
getInjectionTokenName,

‎lib/contracts/child-process.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Contract } from "../common/di/contract";
1+
import { LegacyContract } from "../common/di/contract";
22
import type * as child_process from "child_process";
33
import type {
44
IExecOptions,
@@ -9,7 +9,7 @@ import type {
99
/**
1010
* Promise-based wrapper around Node's `child_process` module.
1111
*/
12-
@Contract({ name: "childProcess" })
12+
@LegacyContract({ name: "childProcess" })
1313
export abstract class ChildProcess {
1414
abstract exec(
1515
command: string,

‎lib/contracts/devices-service.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Contract } from "../common/di/contract";
1+
import { LegacyContract } from "../common/di/contract";
22
import type { IAppInstalledInfo } from "../common/declarations";
33

44
/**
@@ -12,7 +12,7 @@ export interface DevicesService extends NodeJS.EventEmitter {}
1212
/**
1313
* Discovers connected devices and emulators and executes actions against them.
1414
*/
15-
@Contract({ name: "devicesService" })
15+
@LegacyContract({ name: "devicesService" })
1616
export abstract class DevicesService {
1717
/** The platform the service has been initialized for. */
1818
abstract platform: string;

‎lib/contracts/doctor-service.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Contract } from "../common/di/contract";
1+
import { LegacyContract } from "../common/di/contract";
22
import type { ISpawnResult } from "../common/declarations";
33
import type { IOptions } from "../declarations";
44

55
/**
66
* Verifies the host OS configuration — the code behind `ns doctor`.
77
*/
8-
@Contract({ name: "doctorService" })
8+
@LegacyContract({ name: "doctorService" })
99
export abstract class DoctorService {
1010
/**
1111
* Verifies the host OS configuration and prints warnings to the users.

0 commit comments

Comments
 (0)