GunNode.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* globals Gun */
  2. import * as R from 'ramda';
  3. import { GunNodeType } from './types';
  4. declare const Gun: any;
  5. const soul = R.pathOr('', ['_', '#']) as (node: GunNodeType) => string;
  6. const state = R.pathOr({}, ['_', '>']) as (node: GunNodeType) => { [key: string]: number };
  7. const latest = R.compose(
  8. R.last as (res: number[]) => number,
  9. R.sortBy(R.identity),
  10. R.values as (state: any) => number[],
  11. state
  12. );
  13. const edges = R.compose(R.map(R.propOr('', '#')), R.values as any);
  14. const diff = (existing: GunNodeType, updated: GunNodeType) => {
  15. const changedKeys = R.without(['_'], R.keysIn(updated)).filter(k => {
  16. const newVal = updated[k];
  17. const oldVal = R.prop(k, existing);
  18. return !R.equals(newVal, oldVal) && `${newVal}` !== `${oldVal}`;
  19. });
  20. return R.pick(changedKeys, updated);
  21. };
  22. function decodeSEA(rawData: GunNodeType) {
  23. const data = rawData ? { ...rawData } : rawData;
  24. const soul = R.pathOr('' as string, ['_', '#'], data);
  25. if (!soul || !Gun.SEA || soul.indexOf('~') === -1) return rawData;
  26. R.without(['_'], R.keys(data)).forEach(key => {
  27. Gun.SEA.verify(
  28. Gun.SEA.opt.pack(rawData[key], key, rawData, soul),
  29. false,
  30. (res: any) => (data[key] = Gun.SEA.opt.unpack(res, key, rawData))
  31. );
  32. });
  33. return data;
  34. }
  35. export const GunNode = { soul, state, diff, latest, edges, decodeSEA };