Skip to content

Commit

Permalink
fix and execute Upgrade_20240327_NamespaceInClients
Browse files Browse the repository at this point in the history
  • Loading branch information
olmobrutall committed Mar 26, 2024
1 parent 03a9375 commit 2cf26f9
Show file tree
Hide file tree
Showing 260 changed files with 9,023 additions and 8,814 deletions.
4 changes: 2 additions & 2 deletions Extensions/Signum.Alerts/AlertDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { Entity, getToString, is, JavascriptMessage, liteKey, parseLite, toLite
import { Toast } from 'react-bootstrap'
import { DateTime } from 'luxon'
import { useAPIWithReload, useForceUpdate, useThrottle, useUpdatedRef } from '@framework/Hooks';
import * as AuthClient from '../Signum.Authorization/AuthClient'
import { AuthClient } from '../Signum.Authorization/AuthClient'
import { Navigator } from '@framework/Navigator'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import * as AlertsClient from './AlertsClient'
import { AlertsClient } from './AlertsClient'
import "./AlertDropdown.css"
import { Link } from 'react-router-dom';
import { classes, Dic } from '@framework/Globals'
Expand Down
407 changes: 205 additions & 202 deletions Extensions/Signum.Alerts/AlertsClient.tsx

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Extensions/Signum.Alerts/Templates/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react'
import { AutoLine, EntityLine, EntityCombo, FormGroup, TextBoxLine } from '@framework/Lines'
import { TypeContext } from '@framework/TypeContext'
import { AlertEntity, AlertMessage } from '../Signum.Alerts'
import * as AlertsClient from '../AlertsClient'
import { AlertsClient } from '../AlertsClient'
import { useForceUpdate } from '@framework/Hooks';

export default function Alert(p: { ctx: TypeContext<AlertEntity> }) {
Expand Down
Loading

1 comment on commit 2cf26f9

@olmobrutall
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Navigator, Finder, Operations, and *Client modules get a namespace

Signum uses static classes in C# a lot, and in the client side the same pattern was implemented using naked .ts/*.tsx files that just exported a bunch of functions and types.

This approach has two problems:

  • One is that writing Operations or AuthClient doesn't automatically include @framework/Operations or @extensions/Signum.Authorization/AuthClient on the top of the file.

image

  • Another one, maybe true maybe just superstition, is that so many global export pollute the auto-completion making it slower.

image

The solution is to add a big namespace to each of this files, like export namespace Navigator {...} or export namespace AuthClient {...} and put all the code inside....

All?? Really? .... ok not really. There are four cases:

  1. Exported functions and variables (in lowercase) inside the namespace.
  2. API or Options namespaces, also inside the namespace.
  3. Classes and interfaces that are considered internal, only for consumption of components or code in the module itself (like DTOs) also inside the namespace.
  4. Classes and interfaces that are used everywhere, like EntitySettings, EntityOperationSettings ChartRow etc.. go outside the namespace to avoid having to write new Navigator.EntitySettings, new Operations.EntityOperationSettings and new ChartClient.ChartRow.

How to upgrade

  • Run all the upgrades Upgrade_20240304_Navigator, Upgrade_20240306_Finder, Upgrade_20240307_Constructor, Upgrade_20240307_Operations , Upgrade_20240312_TypeScript_and_nugets, Upgrade_20240326_TypeScript_and_nugets2
  • Run the upgrade Upgrade_20240327_NamespaceInClients. This upgrade this two things:
    1. Wrap all the code inside of a SomeClient.ts(x) file in a export namespace SomeClient
    2. Update the imports
OLD: import * as SomeClient from ".../SomeClient"
NEW: import { SomeClient } from ".../SomeClient"

OLD: import { someFunction, SomeClass } as SomeClient from ".../SomeClient"
NEW: import { SomeClient, SomeClass } from ".../SomeClient"

REPLACE
OLD: someFunction
NEW SomeClient.someFunction

Note how SomeClass will break because the imports have not been updated but the class is now inside the namespace! This error messages are you TO-DO list to decide what to do with this class:

  • If SomeClass is considered internal then update the components code to use SomeClient.SomeClass instead, this is the common cases when outside framework.
  • If SomeClass is a popular API (case 4), extract SomeClass outside of the namespace SomeClient to be able to import it easily.

Conclusion

I hope this change will pay of with a better IDE performance and auto-completion experience when writing TypeScript code.

Please sign in to comment.