1
0

eval.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {
  2. DioxusChannel,
  3. Channel,
  4. WeakDioxusChannel,
  5. } from "../../../document/src/ts/eval";
  6. window.__nextChannelId = 0;
  7. window.__channels = [];
  8. export class WebDioxusChannel extends DioxusChannel {
  9. js_to_rust: Channel;
  10. rust_to_js: Channel;
  11. owner: any;
  12. id: number;
  13. constructor(owner: any) {
  14. super();
  15. this.owner = owner;
  16. this.js_to_rust = new Channel();
  17. this.rust_to_js = new Channel();
  18. this.id = window.__nextChannelId;
  19. window.__channels[this.id] = this;
  20. window.__nextChannelId += 1;
  21. }
  22. // Return a weak reference to this channel
  23. weak(): WeakDioxusChannel {
  24. return new WeakDioxusChannel(this);
  25. }
  26. // Receive message from Rust
  27. async recv() {
  28. return await this.rust_to_js.recv();
  29. }
  30. // Send message to rust.
  31. send(data: any) {
  32. this.js_to_rust.send(data);
  33. }
  34. // Send data from rust to javascript
  35. rustSend(data: any) {
  36. this.rust_to_js.send(data);
  37. }
  38. // Receive data sent from javascript in rust
  39. async rustRecv(): Promise<any> {
  40. return await this.js_to_rust.recv();
  41. }
  42. // Close the channel, dropping it.
  43. close(): void {
  44. window.__channels[this.id] = null;
  45. }
  46. }