WindowExt.ts 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. class WindowExt {
  23. /**
  24. * Used to send a notification message to the host
  25. * @param {string} messageType
  26. * @param {object} data
  27. * @return {Promise} will resolve when the host has handled the message
  28. */
  29. static atomicHostEvent(messageType: string, data?: {}): Promise<void> {
  30. let queryMessage: HostMessage;
  31. // if we have a data element, then we need to structure the message so that the host understands it
  32. // by adding the message to the object and then stringify-ing the whole thing
  33. if (data) {
  34. // stringify and reparse since we need to modify the data, but don't want to modify the passed in object
  35. queryMessage = JSON.parse(JSON.stringify(data));
  36. queryMessage.message = messageType;
  37. } else {
  38. queryMessage = {
  39. message: messageType
  40. };
  41. }
  42. return new Promise<void>(function(resolve, reject) {
  43. window.atomicQuery({
  44. request: JSON.stringify(queryMessage),
  45. persistent: false,
  46. onSuccess: (result?: string) => resolve(),
  47. onFailure: (error_code, error_message) => reject({ error_code: error_code, error_message: error_message })
  48. });
  49. });
  50. }
  51. /**
  52. * Used to send a request to the server. The server will send back the results in the promise
  53. * @param {string} messageType
  54. * @param {object} data
  55. * @return {Promise}
  56. */
  57. static atomicHostRequest<T>(messageType: string, data?: {}): Promise<T> {
  58. let queryMessage: HostMessage;
  59. // if we have a data element, then we need to structure the message so that the host understands it
  60. // by adding the message to the object and then stringify-ing the whole thing
  61. if (data) {
  62. // stringify and reparse since we need to modify the data, but don't want to modify the passed in object
  63. queryMessage = JSON.parse(JSON.stringify(data));
  64. queryMessage.message = messageType;
  65. } else {
  66. queryMessage = {
  67. message: messageType
  68. };
  69. }
  70. return new Promise<T>(function(resolve, reject) {
  71. window.atomicQuery({
  72. request: JSON.stringify(queryMessage),
  73. persistent: false,
  74. onSuccess: (s) => {
  75. // unwrap the message that was returned
  76. let o = JSON.parse(s) as WebView.WebMessageEventResponse<T>;
  77. resolve(o.response);
  78. },
  79. onFailure: (error_code, error_message) => reject({ error_code: error_code, error_message: error_message })
  80. });
  81. });
  82. }
  83. }
  84. // set up the window
  85. window.atomicQueryPromise = WindowExt.atomicHostEvent; // deprecated
  86. window.atomicHostEvent = WindowExt.atomicHostEvent;
  87. window.atomicHostRequest = WindowExt.atomicHostRequest;