Skip to content

Dispose callback, useful for react #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist
node_modules
.DS_STORE
.vscode
.vscode
.idea
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
}