[ABANDONDED] Set of "apps" for the Flexor social network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. export interface Attachment {
  2. url: string
  3. text?: string
  4. cover?: string
  5. }
  6. export interface PostData {
  7. [key: string]: any
  8. }
  9. export interface Post {
  10. text?: string
  11. cover?: string
  12. attachments?: Attachment[]
  13. data?: PostData
  14. visible: boolean
  15. }
  16. export interface Theme {
  17. primary: string
  18. primaryAlternate: string
  19. secondary: string
  20. backgroundPrimary: string
  21. backgroundSecondary: string
  22. text: string
  23. red: string
  24. green: string
  25. blue: string
  26. }
  27. export interface MessageContent {
  28. [key: string]: any
  29. height?: number
  30. theme?: Theme
  31. colorScheme?: string
  32. parent?: Post
  33. }
  34. export interface IncomingMessageData {
  35. name: string
  36. content: MessageContent
  37. publicKey: string
  38. }
  39. export interface OutgoingMessageData {
  40. name: string
  41. content?: MessageContent
  42. error?: string
  43. settings?: object
  44. }
  45. interface Listener {
  46. resolve: (value?: MessageContent | PromiseLike<MessageContent>) => void
  47. reject: (reason: any) => void
  48. once: boolean
  49. }
  50. interface ListenerCollection {
  51. [name: string]: Listener
  52. }
  53. type PostOptions = Post
  54. export class Communicator {
  55. private origin = 'http://localhost:8080'
  56. private publicKey: string
  57. private listeners: ListenerCollection
  58. constructor(publicKey: string) {
  59. this.publicKey = publicKey
  60. this.listeners = {}
  61. window.addEventListener('message', (event: MessageEvent) => {
  62. if (event.origin !== this.origin) return
  63. try {
  64. const data = JSON.parse(event.data) as OutgoingMessageData
  65. this.emit(data)
  66. } catch (err) {
  67. console.error(err)
  68. return
  69. }
  70. }, false)
  71. }
  72. private emit(data: OutgoingMessageData) {
  73. const listener = this.listeners[data.name]
  74. if (listener) {
  75. if (data.content) listener.resolve(data.content)
  76. if (data.error) listener.reject(data.error)
  77. if (listener.once) delete this.listeners[data.name]
  78. }
  79. }
  80. private async postAndReceive(name: string, content?: MessageContent) {
  81. if (window.parent) {
  82. window.parent.postMessage(JSON.stringify({
  83. name,
  84. content,
  85. publicKey: this.publicKey,
  86. }), this.origin)
  87. return new Promise<MessageContent>((resolve, reject) => {
  88. this.listeners[name] = {
  89. resolve,
  90. reject,
  91. once: true,
  92. }
  93. })
  94. }
  95. }
  96. async init() {
  97. return this.postAndReceive('init')
  98. }
  99. async setHeight(height: number) {
  100. return this.postAndReceive('setHeight', { height })
  101. }
  102. async post(options: PostOptions) {
  103. return this.postAndReceive('post', options)
  104. }
  105. }