File: storeChild.js

package info (click to toggle)
lightbeam 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,344 kB
  • sloc: makefile: 18; sh: 6
file content (50 lines) | stat: -rw-r--r-- 893 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const storeChildObject = {
  callbacks: new Set(),

  init() {
    browser.runtime.onMessage.addListener((m) => {
      this.messageHandler(m);
    });
  },

  update(data) {
    this.callbacks.forEach((callback) => {
      callback(data);
    });
  },

  onUpdate(callback) {
    this.callbacks.add(callback);
  },

  messageHandler(m) {
    if (m.type !== 'storeChildCall') {
      return;
    }

    const args = m.args;
    return this.update(...args);
  },

  parentMessage(method, ...args) {
    return browser.runtime.sendMessage({
      type: 'storeCall',
      method,
      args
    });
  }
};

const storeChild = new Proxy(storeChildObject, {
  get(target, prop) {
    if (target[prop] === undefined) {
      return async function(...args)  {
        return await this.parentMessage(prop, ...args);
      };
    } else {
      return target[prop];
    }
  }
});

storeChild.init();