metaProxy.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import isEmpty from 'lodash/isEmpty';
  2. import isNull from 'lodash/isNull';
  3. import {Meta} from 'app/types';
  4. const GET_META = Symbol('GET_META');
  5. const IS_PROXY = Symbol('IS_PROXY');
  6. function isAnnotated(meta) {
  7. if (isEmpty(meta)) {
  8. return false;
  9. }
  10. return !isEmpty(meta.rem) || !isEmpty(meta.err);
  11. }
  12. type Local = Record<string, any> | undefined;
  13. export class MetaProxy {
  14. private local: Local;
  15. constructor(local: Local) {
  16. this.local = local;
  17. }
  18. get<T extends {}>(obj: T | Array<T>, prop: Extract<keyof T, string>, receiver: T) {
  19. // trap calls to `getMeta` to return meta object
  20. if (prop === GET_META) {
  21. return key => {
  22. if (this.local && this.local[key] && this.local[key]['']) {
  23. // TODO: Error checks
  24. const meta = this.local[key][''];
  25. return isAnnotated(meta) ? meta : undefined;
  26. }
  27. return undefined;
  28. };
  29. }
  30. // this is how we can determine if current `obj` is a proxy
  31. if (prop === IS_PROXY) {
  32. return true;
  33. }
  34. const value = Reflect.get(obj, prop, receiver);
  35. if (!Reflect.has(obj, prop) || typeof value !== 'object' || isNull(value)) {
  36. return value;
  37. }
  38. // This is so we don't create a new Proxy from an object that is
  39. // already a proxy. Otherwise we can get into very deep recursive calls
  40. if (Reflect.get(obj, IS_PROXY, receiver)) {
  41. return value;
  42. }
  43. // Make sure we apply proxy to all children (objects and arrays)
  44. // Do we need to check for annotated inside of objects?
  45. return new Proxy(value, new MetaProxy(this.local && this.local[prop]));
  46. }
  47. }
  48. export function withMeta<T>(event: T): T {
  49. if (!event) {
  50. return event;
  51. }
  52. // Return unproxied `event` if browser does not support `Proxy`
  53. if (typeof window.Proxy === 'undefined' || typeof window.Reflect === 'undefined') {
  54. return event;
  55. }
  56. // withMeta returns a type that is supposed to be 100% compatible with its
  57. // input type. Proxy typing on typescript is not really functional enough to
  58. // make this work without casting.
  59. //
  60. // https://github.com/microsoft/TypeScript/issues/20846
  61. return new Proxy(event, new MetaProxy((event as any)._meta)) as T;
  62. }
  63. export function getMeta<T extends {}>(
  64. obj: T | undefined,
  65. prop: Extract<keyof T, string>
  66. ): Meta | undefined {
  67. if (!obj || typeof obj[GET_META] !== 'function') {
  68. return undefined;
  69. }
  70. return obj[GET_META](prop);
  71. }