Platform-Node.st 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. Smalltalk createPackage: 'Platform-Node'!
  2. (Smalltalk packageAt: 'Platform-Node' ifAbsent: [ self error: 'Package not created: Platform-Node' ]) imports: {'amber/core/Platform-Services'}!
  3. Object subclass: #NodePlatform
  4. slots: {}
  5. package: 'Platform-Node'!
  6. !NodePlatform commentStamp!
  7. I am `Platform` service implementation for node-like environment.!
  8. !NodePlatform methodsFor: 'accessing'!
  9. globals
  10. ^ global
  11. ! !
  12. !NodePlatform methodsFor: 'initialization'!
  13. initialize
  14. process
  15. on: 'uncaughtException'
  16. do: [ :err | ErrorHandler handleError: err. process exit: 1 ];
  17. on: 'unhandledRejection'
  18. do: [ :err | ErrorHandler handleError: err. process exit: 2 ]
  19. ! !
  20. !NodePlatform methodsFor: 'public API'!
  21. fetch: aStringOrObject
  22. ^ self globals at: #fetch
  23. ifPresent: [ :fetch | fetch value: aStringOrObject ]
  24. ifAbsent: [ Promise signal: 'fetch not available.' ]
  25. !
  26. fetchUrl: aString options: anObject
  27. ^ self globals at: #fetch
  28. ifPresent: [ :fetch | fetch value: aString value: anObject ]
  29. ifAbsent: [ Promise signal: 'fetch not available.' ]
  30. !
  31. newXhr
  32. XMLHttpRequest
  33. ifNotNil: [ ^ XMLHttpRequest new ]
  34. ifNil: [ self error: 'XMLHttpRequest not available.' ]
  35. ! !
  36. !NodePlatform class methodsFor: 'testing'!
  37. initialize
  38. self isFeasible ifTrue: [ Platform registerIfNone: self new ]
  39. !
  40. isFeasible
  41. <inlineJS: 'return typeof process !!== "undefined" && process && process.versions && process.versions.node !!= null'>
  42. ! !