Deprecated API

Frame description

In order to maintain the abandoned API while making it more friendly and convenient, it will be implemented through three functions:

  • markAsWarning embeds a warning in the attribute on the given object, and the attribute needs to exist on the given object.

  • removeProperty redefines the removed property on the given object, and embeds an error message. The property should not exist on the given object.

  • replaceProperty redefines the removed property on the given object, and embeds a warning and calls the new property. Incompatible parameters need to be adapted. The given object should not have the property.

Function signature

  1. interface IRemoveItem {
  2. /** The name of the obsolete attribute */
  3. name: string;
  4. /** Number of warnings */
  5. logTimes?: number;
  6. /** Additional suggestions */
  7. suggest?: string;
  8. }
  9. interface IMarkItem {
  10. /** The name of the obsolete attribute */
  11. name: string;
  12. /** Number of warnings */
  13. logTimes?: number;
  14. /** Additional suggestions */
  15. suggest?: string;
  16. }
  17. interface IReplacement {
  18. /** The name of the obsolete attribute */
  19. name: string;
  20. /** Number of warnings */
  21. logTimes?: number;
  22. /** Additional suggestions */
  23. suggest?: string;
  24. /** The object of the discarded attribute */
  25. target?: object;
  26. /** The name of the object of the discarded attribute */
  27. targetName?: string;
  28. /** Custom replacement attribute (function) */
  29. customFunction?: Function;
  30. /** Setter of custom replacement properties */
  31. customSetter?: (v: any) => void;
  32. /** Getter for custom replacement attributes */
  33. customGetter?: () => any;
  34. }
  35. export let removeProperty: (owner: object, ownerName: string, properties: IRemoveItem[]) => void;
  36. export let markAsWarning: (owner: object, ownerName: string, properties: IMarkItem[]) => void;
  37. export let replaceProperty: (owner: object, ownerName: string, properties: IReplacement[]) => void;
  38. /** This function is used to set the global default information output times */
  39. export function setDefaultLogTimes (times: number): void;

Terms of Use

According to the module division, each module maintains an obsolete file. In order to facilitate maintenance, the name is unified as deprecated.ts and placed in the directory of the corresponding module, and the file needs to be import in the index.ts file of the corresponding module, such as import'./deprecated'.

Note: the deprecated.ts file in the cocos\utils directory is a declaration and implementation file.

Usage example

  1. // For APIs that are not compatible with replacement parameters, adapt them through appropriate custom functions
  2. replaceProperty(AnimationComponent.prototype, 'AnimationComponent.prototype', [
  3. {
  4. name: 'removeClip',
  5. newName: 'removeState',
  6. customFunction: function (...args: any) {
  7. const arg0 = args[0] as AnimationClip;
  8. return AnimationComponent.prototype.removeState.call(this, arg0.name);
  9. }
  10. }
  11. ]);
  12. replaceProperty(vmath, 'vmath', [
  13. {
  14. name: 'vec2',
  15. newName: 'Vec2',
  16. target: math,
  17. targetName: 'math',
  18. 'logTimes': 1
  19. },
  20. {
  21. name: 'EPSILON',
  22. target: math,
  23. targetName: 'math',
  24. 'logTimes': 2
  25. }
  26. ]);
  27. removeProperty(vmath, 'vmath', [
  28. {
  29. 'name': 'random',
  30. 'suggest': 'use Math.random.'
  31. }
  32. ]);
  33. markAsWarning(math, 'math', [
  34. {
  35. 'name': 'toRadian'
  36. }
  37. ]);

Usage Notes

  • The operation targets are all objects. If you want to modify the member functions of the class, please pass in target.prototype.

  • replaceProperty does not pass in newName or newTarget, which means it is consistent with name or target.

  • If you want to control the number of times, it is best to call setDefaultLogTimes before use, because other modules may change the default times.