You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.3 KiB

1 year ago
  1. // mimic the linkEvent function from inferno for easier migration
  2. // helpers from inferno-shared
  3. // https://github.com/infernojs/inferno/blob/master/packages/inferno-shared/src/index.ts
  4. // @ts-ignore
  5. // eslint-disable-next-line @typescript-eslint/ban-types
  6. export function isFunction(o: any): o is Function {
  7. return typeof o === 'function';
  8. }
  9. export function isNull(o: any): o is null {
  10. return o === null;
  11. }
  12. export interface LinkedEvent<T, E extends Event> {
  13. data: T;
  14. event: (data: T, event: E) => void;
  15. }
  16. /**
  17. * Links given data to event as first parameter
  18. * @param {*} data data to be linked, it will be available in function as first parameter
  19. * @param {Function} event Function to be called when event occurs
  20. * @returns {{data: *, event: Function}}
  21. */
  22. export function linkEvent<T, E extends Event>(
  23. data: T,
  24. event: (data: T, event: E) => void
  25. // @TODO: Properly type this at some point, but i have no idea how to do it with curried functions like this
  26. ): any {
  27. if (isFunction(event)) {
  28. return evt => event(data, evt);
  29. }
  30. return null; // Return null when event is invalid, to avoid creating unnecessary event handlers
  31. }
  32. // object.event should always be function, otherwise its badly created object.
  33. export function isLinkEventObject(o): o is LinkedEvent<any, any> {
  34. return !isNull(o) && typeof o === 'object';
  35. }