From a43247093557d31a5589407b251a85b841f21217 Mon Sep 17 00:00:00 2001 From: Dylan Vorster Date: Tue, 27 Jun 2023 12:32:21 -0600 Subject: [PATCH] dispose callback, useful for react --- .gitignore | 3 ++- README.md | 6 ++++-- src/lib/client.ts | 4 ++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 9d8453c..f32d03f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ dist node_modules .DS_STORE -.vscode \ No newline at end of file +.vscode +.idea \ No newline at end of file diff --git a/README.md b/README.md index 90f0f8e..39031ed 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,13 @@ console.log(`The answer to life, the universe and everything is ${result}`); ```javascript // In your view's JS -component.html().on('sum', function(p1, p2) { +let dispose = component.html().on('sum', function(p1, p2) { return p1 + p2; }); -``` +// to stop listening +dispose(); +``` > *Note*: `.on` handlers can be initialized anywhere in your view's JS, as long as it happens before messages are posted to them. If you need the handler ready from the view's initialization, initialize them in the view's `init()` function. diff --git a/src/lib/client.ts b/src/lib/client.ts index 4a4cabe..e511ff0 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -33,7 +33,11 @@ export default class JourneyIFrameClient { return Promise.reject(e); } } + on(command: string, cb: (message) => any) { this.callbacks[command] = cb; + return () => { + delete this.callbacks[command]; + } } }