Advertisement
Guest User

g

a guest
Apr 19th, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import Toybox.Lang;
  2.  
  3. (:background) function isBackground() as Boolean {
  4. // return evcc.isBackground;
  5. return true; // for testing purposes
  6. }
  7.  
  8. class A_FG extends A_BG {
  9. function initialize() {
  10. A_BG.initialize();
  11. }
  12.  
  13. function notNeededInBackground() as Void {
  14. // [NOTE 1]
  15. // this assumes that B_BG has an empty implementation
  16. // of notNeededInBackground(), for convenience
  17. _b.notNeededInBackground();
  18. // if not, use the following code instead:
  19. // (_b as B_FG).notNeededInBackground();
  20. }
  21. }
  22.  
  23. (:background) class A_BG {
  24. protected var _b as B_BG;
  25.  
  26. function initialize() {
  27. // some complicated logic
  28. _b = B_BG.create();
  29. // some complicated logic
  30. }
  31.  
  32. function neededInBackground() as Void {
  33. _b.neededInBackground();
  34. }
  35. }
  36.  
  37. //! Do not directly call `new B_FG()`; use B_BG.create() instead
  38. class B_FG extends B_BG {
  39. // would've been nice if this could've been protected
  40. function initialize(_doNotCallDirectly as Number, _seriouslyDontDoIt as Symbol) {
  41. B_BG.initialize(42, :initialize);
  42. }
  43.  
  44. function notNeededInBackground() as Void {
  45. // do stuff
  46. // ...
  47. }
  48. }
  49.  
  50.  
  51. //! Do not directly call `new B_BG()`; use B_BG.create() instead
  52. (:background) class B_BG {
  53. // would've been nice if this could've been protected
  54. function initialize(_doNotCallDirectly as Number, _seriouslyDontDoIt as Symbol) {
  55. // ...
  56. }
  57.  
  58. (:typecheck(disableBackgroundCheck))
  59. static public function create() as B_BG {
  60. if (isBackground()) {
  61. return new B_BG(42, :initialize);
  62. } else {
  63. return new B_FG(42, :initialize);
  64. }
  65. }
  66.  
  67. function neededInBackground() as Void {
  68. // do stuff
  69. // ...
  70. }
  71.  
  72. // empty stub function
  73. // See [NOTE 1] above
  74. function notNeededInBackground() as Void {
  75. // do nothing
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement