|
| 1 | +# Giving a service a contract |
| 2 | + |
| 3 | +Read the "Tokens", "Internal contracts" and "Registering" |
| 4 | +sections of `dependency-injection.md` first. The worked example is |
| 5 | +`lib/contracts/internal/os-info.ts` with `lib/common/os-info.ts`. |
| 6 | + |
| 7 | +## 1. Survey |
| 8 | + |
| 9 | +For a service registered as `thing`: |
| 10 | + |
| 11 | +```sh |
| 12 | +rg -n '"\$?thing"|\$thing\b' lib test # registration, lookups, stubs |
| 13 | +rg -n 'interface IThing\b' lib # the declared shape |
| 14 | +rg -n '\bThing\b' lib test # imports, subclasses, new Thing() |
| 15 | +``` |
| 16 | + |
| 17 | +List the open pull requests that touch a file: |
| 18 | + |
| 19 | +```sh |
| 20 | +R=NativeScript/nativescript-cli |
| 21 | +for n in $(gh pr list -R $R --state open --limit 200 --json number -q '.[].number'); do |
| 22 | + gh api --paginate "repos/$R/pulls/$n/files" -q ".[].filename" | sed "s|^|$n |" |
| 23 | +done | grep -F lib/services/thing.ts |
| 24 | +``` |
| 25 | + |
| 26 | +If one changes the service's shape, pick another service or ask. |
| 27 | + |
| 28 | +## 2. Classify |
| 29 | + |
| 30 | +| Kind | What to do | |
| 31 | +|---|---| |
| 32 | +| Class with one interface | this recipe | |
| 33 | +| Interface shared by several registrations (package managers, platform services, log filters) | one token class per registration, merged with the interface: `@LegacyContract({ name }) export abstract class X {}` plus `export interface X extends IShared {}`; leave the interface alone | |
| 34 | +| Value that is not a class (a module, a function) | `InjectionToken`, not a contract | |
| 35 | +| `shared: false` registration | keep it transient: `{ provide: Token, useClass: Impl, shared: false }` | |
| 36 | +| Class that does not satisfy its interface | stop and report; fix conformance in its own change | |
| 37 | +| Nothing uses it | stop and report; hooks can still resolve it by name | |
| 38 | + |
| 39 | +## 3. Choose the decorator and the place |
| 40 | + |
| 41 | +- **`@LegacyContract({ name })`** when the service already has a string name. Hooks |
| 42 | + and `$` parameters resolve it, so the name stays exactly as registered. |
| 43 | +- **`@Contract()`** when it never had one, and for new services. Without a name it |
| 44 | + cannot be resolved by string, or through an extension's own copy of the CLI. |
| 45 | +- **`lib/contracts/internal/`** is the default; the skill says what separates it from a |
| 46 | + public contract. A test fails if a class in it is exported from |
| 47 | + `nativescript/contracts`. |
| 48 | + |
| 49 | +A contract file imports only the decorator and types. It must stay free of side |
| 50 | +effects, so it never imports `lib/common/yok` or an implementation. |
| 51 | + |
| 52 | +## 4. Edit |
| 53 | + |
| 54 | +1. Declare the contract with the members of the interface, moving their JSDoc. |
| 55 | + Correct JSDoc that does not describe the member. For an inherited interface |
| 56 | + such as `EventEmitter`, merge it: |
| 57 | + `export interface Thing extends NodeJS.EventEmitter {}` next to the class. |
| 58 | +2. Make the interface an alias: |
| 59 | + `/** @deprecated Kept so existing annotations compile; use the {@link Thing} contract. */` |
| 60 | + `interface IThing extends Thing {}`, with an `import type` at the top of the |
| 61 | + declaration file. A `.d.ts` without any `import` or `export` is ambient, and |
| 62 | + the import turns it into a module: wrap **everything** in it in |
| 63 | + `declare global { … }`, as `lib/definitions/prompter.d.ts` does. |
| 64 | +3. Rename the class to `ThingImpl implements Thing` and fix imports of the class, |
| 65 | + tests included. A class with `@performanceLog` methods keeps its name: the |
| 66 | + decorator reports it. The old class export disappears for deep `require()`s; |
| 67 | + say so in the pull request. |
| 68 | +4. Register it: |
| 69 | + - no `$` constructor parameters left: `injector.register(provide(Thing, ThingImpl))` |
| 70 | + - any left: keep `injector.register("thing", ThingImpl)`. With `provide()` they |
| 71 | + are **silently `undefined`**, because the class is built with no arguments. |
| 72 | +5. Leave the `injector.require("thing", "./path")` line in the bootstrap. Never |
| 73 | + import an implementation there: modules must stay lazy. A service with no |
| 74 | + string name is registered there with |
| 75 | + `injector.register(provideLazy(Thing, () => require("./path").ThingImpl))`, |
| 76 | + and its module registers nothing. |
| 77 | +6. Leave the service's own `$` parameters, and its consumers, unless you are |
| 78 | + changing them anyway; then see |
| 79 | + [moving a class to tokens](moving-a-class-to-tokens.md). Both spellings reach the same instance, so |
| 80 | + consumers can move before or after. |
| 81 | + |
| 82 | +Keep `@exported` and `@hook` usages and their names unchanged. |
| 83 | + |
| 84 | +## 5. Verify |
| 85 | + |
| 86 | +```sh |
| 87 | +npx tsc --noEmit -p tsconfig.json |
| 88 | +npx tsc -p tsconfig.release.json --noEmit |
| 89 | +npm run build && npx vitest run |
| 90 | +``` |
| 91 | + |
| 92 | +Add a test to `test/contracts.ts`: for a service registered with `provide()`, that the |
| 93 | +class, the name and the `$`-spelling resolve to one instance, as `OsInfo` does. With |
| 94 | +a by-name registration that needs stubs, assert `getContractName(Thing)` instead. |
| 95 | + |
| 96 | +Check the real registration, and that it is still lazy (both lines must print as |
| 97 | +shown): |
| 98 | + |
| 99 | +```sh |
| 100 | +node -e ' |
| 101 | +require("./dist/lib/bootstrap"); |
| 102 | +const { injector } = require("./dist/lib/common/yok"); |
| 103 | +const { Thing } = require("./dist/lib/contracts/internal/thing"); |
| 104 | +const loaded = () => Object.keys(require.cache).some((k) => k.endsWith("services/thing.js")); |
| 105 | +console.log(loaded()); // false |
| 106 | +const a = injector.get(Thing); |
| 107 | +console.log(loaded(), a === injector.resolve("$thing")); // true true |
| 108 | +process.exit();' |
| 109 | +``` |
| 110 | + |
| 111 | +Report the decorator and folder you chose and why, the consumers left on strings, |
| 112 | +and any open pull request that overlaps. |
0 commit comments