Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. export function Lazy(hostProperty?: string) {
  2. return (proto, prop) => {
  3. if (!hostProperty && !proto["__lazyHost"]) {
  4. throw new Error("@Lazy() decorator requires either a @LazyHost(), or a `hostProperty` argument that matches the name of the `@Element()` property.");
  5. } else if (!hostProperty) {
  6. hostProperty = proto["__lazyHost"];
  7. }
  8.  
  9. const { render } = proto;
  10. proto.render = function () {
  11. if ('IntersectionObserver' in window) {
  12. let io = new IntersectionObserver((data: any) => {
  13. if (data[0].isIntersecting) {
  14. this[prop].apply(this);
  15. io.disconnect();
  16. io = null;
  17. }
  18. });
  19. io.observe(this[hostProperty]);
  20. }
  21. else {
  22. // fall back to setTimeout for Safari and IE
  23. setTimeout(() => {
  24. this[prop].apply(this);
  25. }, 300);
  26. }
  27.  
  28. return render.apply(this);
  29. };
  30. };
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement