[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.

134 lines
3.0 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
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. settings?: InstallationSettings
  30. height?: number
  31. theme?: Theme
  32. colorScheme?: string
  33. parent?: Post
  34. }
  35. export interface IncomingMessageData {
  36. name: string
  37. content: MessageContent
  38. publicKey: string
  39. }
  40. export interface OutgoingMessageData {
  41. name: string
  42. content?: MessageContent
  43. error?: string
  44. settings?: object
  45. }
  46. export interface InstallationSettings {
  47. [key: string]: any
  48. }
  49. interface Listener {
  50. resolve: (value?: MessageContent | PromiseLike<MessageContent>) => void
  51. reject: (reason: any) => void
  52. once: boolean
  53. }
  54. interface ListenerCollection {
  55. [name: string]: Listener
  56. }
  57. type PostOptions = Post
  58. export class Communicator {
  59. private origin = 'http://localhost:8080'
  60. private publicKey: string
  61. private listeners: ListenerCollection
  62. constructor(publicKey: string) {
  63. this.publicKey = publicKey
  64. this.listeners = {}
  65. window.addEventListener('message', (event: MessageEvent) => {
  66. if (event.origin !== this.origin) return
  67. try {
  68. const data = JSON.parse(event.data) as OutgoingMessageData
  69. this.emit(data)
  70. } catch (err) {
  71. console.error(err)
  72. return
  73. }
  74. }, false)
  75. }
  76. private emit(data: OutgoingMessageData) {
  77. const listener = this.listeners[data.name]
  78. if (listener) {
  79. if (data.content) listener.resolve(data.content)
  80. if (data.error) listener.reject(data.error)
  81. if (listener.once) delete this.listeners[data.name]
  82. }
  83. }
  84. private async postAndReceive(name: string, content?: MessageContent) {
  85. if (window.parent) {
  86. window.parent.postMessage(JSON.stringify({
  87. name,
  88. content,
  89. publicKey: this.publicKey,
  90. }), this.origin)
  91. return new Promise<MessageContent>((resolve, reject) => {
  92. this.listeners[name] = {
  93. resolve,
  94. reject,
  95. once: true,
  96. }
  97. })
  98. }
  99. }
  100. async init() {
  101. return this.postAndReceive('init')
  102. }
  103. async setHeight(height: number) {
  104. return this.postAndReceive('setHeight', { height })
  105. }
  106. async saveSettings(settings: InstallationSettings) {
  107. return this.postAndReceive('saveSettings', { settings })
  108. }
  109. async post(options: PostOptions) {
  110. return this.postAndReceive('post', options)
  111. }
  112. }