gm-values.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import bridge from './bridge';
  2. import store from './store';
  3. // Nested objects: scriptId -> keyName -> listenerId -> GMValueChangeListener
  4. export const changeHooks = createNullObj();
  5. const dataDecoders = {
  6. __proto__: null,
  7. o: jsonParse,
  8. n: val => +val,
  9. b: val => val === 'true',
  10. };
  11. bridge.addHandlers({
  12. UpdatedValues(updates) {
  13. objectKeys(updates)::forEach(id => {
  14. const oldData = store.values[id];
  15. if (oldData) {
  16. const update = updates[id];
  17. const keyHooks = changeHooks[id];
  18. if (keyHooks) changedRemotely(keyHooks, oldData, update);
  19. else applyPartialUpdate(oldData, update);
  20. }
  21. });
  22. },
  23. });
  24. export function loadValues(id) {
  25. return store.values[id];
  26. }
  27. /**
  28. * @param {number} id
  29. * @param {string} key
  30. * @param {?} val
  31. * @param {?string} raw
  32. * @param {?string} oldRaw
  33. * @param {GMContext} context
  34. * @return {void|Promise<void>}
  35. */
  36. export function dumpValue(id, key, val, raw, oldRaw, context) {
  37. let res;
  38. if (raw !== oldRaw) {
  39. res = bridge[context.async ? 'send' : 'post']('UpdateValue', { id, key, raw }, context);
  40. const hooks = changeHooks[id]?.[key];
  41. if (hooks) notifyChange(hooks, key, val, raw, oldRaw);
  42. } else if (context.async) {
  43. res = promiseResolve();
  44. }
  45. return res;
  46. }
  47. export function decodeValue(raw) {
  48. const type = raw[0];
  49. const handle = dataDecoders[type];
  50. let val = raw::slice(1);
  51. try {
  52. if (handle) val = handle(val);
  53. } catch (e) {
  54. if (process.env.DEBUG) log('warn', ['GM_getValue'], e);
  55. }
  56. return val;
  57. }
  58. function applyPartialUpdate(data, update) {
  59. objectKeys(update)::forEach(key => {
  60. const val = update[key];
  61. if (val) data[key] = val;
  62. else delete data[key];
  63. });
  64. }
  65. function changedRemotely(keyHooks, data, update) {
  66. objectKeys(update)::forEach(key => {
  67. const raw = update[key] || undefined; // partial `update` currently uses null for deleted values
  68. const oldRaw = data[key];
  69. if (oldRaw !== raw) {
  70. if (raw) data[key] = raw; else delete data[key];
  71. const hooks = keyHooks[key];
  72. if (hooks) notifyChange(hooks, key, undefined, raw, oldRaw, true);
  73. }
  74. });
  75. }
  76. function notifyChange(hooks, key, val, raw, oldRaw, remote = false) {
  77. // converting `null` from messaging to `undefined` to match the documentation and TM
  78. const oldVal = (oldRaw || undefined) && decodeValue(oldRaw);
  79. const newVal = val === undefined && raw ? decodeValue(raw) : val;
  80. objectValues(hooks)::forEach(fn => {
  81. try {
  82. fn(key, oldVal, newVal, remote);
  83. } catch (e) {
  84. log('error', ['GM_addValueChangeListener', 'callback'], e);
  85. }
  86. });
  87. }