{"id":13765155,"url":"https://github.com/okotoki/figma-messenger","last_synced_at":"2025-04-15T01:21:51.222Z","repository":{"id":36680695,"uuid":"229476034","full_name":"okotoki/figma-messenger","owner":"okotoki","description":"Type-safe communication for good 🧐.","archived":false,"fork":false,"pushed_at":"2024-10-11T08:07:02.000Z","size":1761,"stargazers_count":29,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-11T20:14:58.075Z","etag":null,"topics":["emitter","figma","figma-plugins","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/okotoki.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-21T19:52:49.000Z","updated_at":"2024-06-02T12:01:31.000Z","dependencies_parsed_at":"2023-12-21T20:32:07.974Z","dependency_job_id":"e4ffb8e3-cdfc-4a3c-a543-93c7ddd38d71","html_url":"https://github.com/okotoki/figma-messenger","commit_stats":{"total_commits":16,"total_committers":2,"mean_commits":8.0,"dds":0.0625,"last_synced_commit":"fac7ef1bcba6d2859613679e9f2579fd3a5159eb"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okotoki%2Ffigma-messenger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okotoki%2Ffigma-messenger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okotoki%2Ffigma-messenger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okotoki%2Ffigma-messenger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/okotoki","download_url":"https://codeload.github.com/okotoki/figma-messenger/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986502,"owners_count":21194052,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["emitter","figma","figma-plugins","typescript"],"created_at":"2024-08-03T16:00:34.718Z","updated_at":"2025-04-15T01:21:51.206Z","avatar_url":"https://github.com/okotoki.png","language":"TypeScript","funding_links":[],"categories":["Plugins development","插件开发","TypeScript"],"sub_categories":[],"readme":"# \u003cimg src=\"media/header.svg\" width=\"800\" align=\"center\" alt=\"Type-safe Iframe ↔ Main Thread communication for good 🧐\"/\u003e\n\n\n## Usage\n\nInstallation\n```sh\nnpm i figma-messenger\n# or using Yarn\nyarn add figma-messenger\n```\n\nQuick usage example\n```typescript\n// Shared code between main thread and iframe\n// shared.ts\ninterface IframeToMain {\n  setVersion(name: string, value: number): void\n}\n\ninterface MainToIframe {\n  heyIframe(data: any): void\n}\n\n// main.ts\nimport { createMainThreadMessenger } from 'figma-messenger'\n\nconst mainMessenger = createMainThreadMessenger\u003cMainToIframe, IframeToMain\u003e()\n\n// All good\nmainMessenger.send('heyIframe', { any: 'data'})\n\n// Error. Argument of type \"unknownMessage\" is not assignable to parameter of type \"heyIframe\".\nmainMessenger.send('unknownMessage')\n// Error. Expected 2 arguments, but got 1.\nmainMessenger.send('heyIframe')\n\nmainMessenger.on('setVersion', (name, value) =\u003e {\n  console.log('setVersion', name, value)\n})\n\n// Remove all listeners\nmainMessenger.off('setVersion')\n\n// iframe.ts\nimport { createIframeMessenger, createMainThreadMessenger } from 'figma-messenger'\n\nconst iframeMessenger = createIframeMessenger\u003cIframeToMain, MainToIframe\u003e()\n\n// All good\niframeMessenger.send('setVersion', 'initial', 1)\n\n// Error. Expected 3 arguments, but got 2.\niframeMessenger.send('setVersion', 'initial')\n\niframeMessenger.on('heyIframe', sel =\u003e console.log(sel))\n\n// Remove all listeners\niframeMessenger.off('heyIframe')\n```\n\nSee more comprehensive live figma plugin example at [examples/figma-plugin](examples/figma-plugin).  \nFiles `shared/types.ts`, `app.tsx` and `main/index.ts`\n\n\n## Api\n\n### createIframeMessenger\u003cMessagesToSend, MessagesToListen\u003e(name?: string) / createMainThreadMessenger\u003cMessagesToSend, MessagesToListen\u003e(name?: string)\n\nCreates a messenger instance for Iframe and Main Thread sides respectively.  \nOptional `name` argument. If not set, messenger will be global. Otherwise only will receive events from the messenger with the same name.  \nAlso, takes 2 type arguments:  \n`MessagesToSend` – messages to send signature  \n`MessagesToListen` – messages to receive signature\n\nExample:\n```typescript\n// Messages sent from Iframe side, received on Main Thread side\ninterface IframeToMain {\n  setVersion(name: string, value: number): void\n}\n\n// Messages sent from Main Thread side, received on Iframe side\ninterface MainToIframe {\n  heyIframe(data: any): void\n}\n\n/**\n * somewhere in iframe code: \n */\n\n// global messenger\nconst iframeMessenger = createIframeMessenger\u003cIframeToMain, MainToIframe\u003e()\n// named messenger. Will communicate only to messenger with the same name on main thread.\nconst namedIframeMessenger = createIframeMessenger\u003cIframeToMain, MainToIframe\u003e('SPECIAL')\n\n\n/**\n * somewhere in main thread code:\n */\n\n// global messenger\nconst mainThreadMessenger = createMainThreadMessenger\u003cMainToIframe, IframeToMain\u003e()\n\n// named messenger. Will communicate only to messenger 'SPECIAL' on iframe side.\nconst namedMainThreadMessenger = createMainThreadMessenger\u003cIframeToMain, MainToIframe\u003e('SPECIAL')\n```\n\nSingle global listener under the hood makes it possible to create multiple instances, which won't conflict, but would handle messages with same name.\n```typescript\nconst m1 = createIframeMessenger()\nconst m2 = createIframeMessenger()\nconst m3 = createIframeMessenger('SPECIAL')\n\n\n// When fired globally, \"msg\" message would be received by m1 and m2, but not by m3.\nm1.on('msg', callback1) // receives global message\nm2.on('msg', callback2) // receives global message\n\nm3.on('msg', callback3) // only will receive from messenger named 'SPECIAL' on main thread side\n```\n\n### .on(message: string, listener: (...arg: any[]) =\u003e void): void\nAdd listener for the message from opposite side.  \nCallbacks can take no or multiple arguments.\n```typescript\nmessenger.on('aMessage', handleMessage)\nmessenger.on('someMessage', (data) =\u003e doSomething(data))\nmessenger.on('otherMessage', (arg1, arg2) =\u003e hello(arg1, arg2))\nmessenger.on('noArgsMessage', () =\u003e world())\n```\n\n### .off(message: string, , listener: (...arg: any[]) =\u003e void): void\nRemove one or all listeners for the message.\n```typescript\n// remove particular listener\nmessenger.off('aMessage', handleMessage)\n\n// remove all listeners\nmessenger.on('someMessage')\n```\n\n### .send(message: string, ...data?: any[]): void\nSend a message to an opposite side.\n```typescript\n// send message with one data item\nmessenger.on('someMessage', data)\n// with multiple data items\nmessenger.on('otherMessage', arg1, arg2)\n// or no data at all\nmessenger.send('noArgsMessage')\n```\n\n## License\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fokotoki%2Ffigma-messenger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fokotoki%2Ffigma-messenger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fokotoki%2Ffigma-messenger/lists"}