yummies 5.7.0 → 5.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,26 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.lazyObserve = void 0;
4
4
  const mobx_1 = require("mobx");
5
+ /**
6
+ * When ONE OF the properties is becomes observed then `onStart` function is called.
7
+ * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.
8
+ *
9
+ * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.
10
+ */
5
11
  const lazyObserve = ({ context, property, onStart, onEnd, endDelay = false, }) => {
6
12
  let timeoutId;
7
13
  let metaData;
14
+ const observingProps = new Set();
8
15
  const properties = Array.isArray(property) ? property : [property];
9
- let isObserving = false;
10
- const start = () => {
11
- if (isObserving) {
12
- return;
13
- }
14
- isObserving = true;
15
- if (timeoutId) {
16
- clearTimeout(timeoutId);
17
- timeoutId = undefined;
18
- }
19
- metaData = onStart?.();
20
- };
21
16
  const cleanup = () => {
22
- if (!isObserving) {
23
- return;
24
- }
17
+ observingProps.clear();
25
18
  if (endDelay === false) {
26
19
  onEnd?.(metaData, cleanup);
27
20
  metaData = undefined;
@@ -37,14 +30,35 @@ const lazyObserve = ({ context, property, onStart, onEnd, endDelay = false, }) =
37
30
  metaData = undefined;
38
31
  }, endDelay);
39
32
  };
33
+ const start = (property) => {
34
+ const isAlreadyObserving = observingProps.size > 0;
35
+ observingProps.add(property);
36
+ if (isAlreadyObserving) {
37
+ return;
38
+ }
39
+ if (timeoutId) {
40
+ clearTimeout(timeoutId);
41
+ timeoutId = undefined;
42
+ }
43
+ metaData = onStart?.();
44
+ };
45
+ const stop = (property) => {
46
+ const isAlreadyNotObserving = !observingProps.size;
47
+ observingProps.delete(property);
48
+ const isObserving = observingProps.size > 0;
49
+ if (isAlreadyNotObserving || isObserving) {
50
+ return;
51
+ }
52
+ cleanup();
53
+ };
40
54
  properties.forEach((property) => {
41
55
  if (context) {
42
- (0, mobx_1.onBecomeObserved)(context, property, start);
43
- (0, mobx_1.onBecomeUnobserved)(context, property, cleanup);
56
+ (0, mobx_1.onBecomeObserved)(context, property, () => start(property));
57
+ (0, mobx_1.onBecomeUnobserved)(context, property, () => stop(property));
44
58
  }
45
59
  else {
46
- (0, mobx_1.onBecomeObserved)(property, start);
47
- (0, mobx_1.onBecomeUnobserved)(property, cleanup);
60
+ (0, mobx_1.onBecomeObserved)(property, () => start(property));
61
+ (0, mobx_1.onBecomeUnobserved)(property, () => stop(property));
48
62
  }
49
63
  });
50
64
  return cleanup;
@@ -1,3 +1,9 @@
1
+ /**
2
+ * When ONE OF the properties is becomes observed then `onStart` function is called.
3
+ * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.
4
+ *
5
+ * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.
6
+ */
1
7
  export declare const lazyObserve: <TMetaData = void>({ context, property, onStart, onEnd, endDelay, }: {
2
8
  context?: any;
3
9
  property: any | any[];
@@ -1 +1 @@
1
- {"version":3,"file":"lazy-observe.d.ts","sourceRoot":"","sources":["../../src/mobx/lazy-observe.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,WAAW,GAAI,SAAS,GAAG,IAAI,EAAE,kDAM3C;IACD,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,QAAQ,EAAE,GAAG,GAAG,GAAG,EAAE,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,SAAS,CAAC;IAC1B,KAAK,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,KAAK,IAAI,CAAC;IAC/D,QAAQ,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CAC3B,eAmDA,CAAC"}
1
+ {"version":3,"file":"lazy-observe.d.ts","sourceRoot":"","sources":["../../src/mobx/lazy-observe.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,SAAS,GAAG,IAAI,EAAE,kDAM3C;IACD,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,QAAQ,EAAE,GAAG,GAAG,GAAG,EAAE,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,SAAS,CAAC;IAC1B,KAAK,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,KAAK,IAAI,CAAC;IAC/D,QAAQ,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CAC3B,eAoEA,CAAC"}
@@ -1,3 +1,9 @@
1
+ /**
2
+ * When ONE OF the properties is becomes observed then `onStart` function is called.
3
+ * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.
4
+ *
5
+ * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.
6
+ */
1
7
  export declare const lazyObserve: <TMetaData = void>({ context, property, onStart, onEnd, endDelay, }: {
2
8
  context?: any;
3
9
  property: any | any[];
@@ -1 +1 @@
1
- {"version":3,"file":"lazy-observe.d.ts","sourceRoot":"","sources":["../../src/mobx/lazy-observe.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,WAAW,GAAI,SAAS,GAAG,IAAI,EAAE,kDAM3C;IACD,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,QAAQ,EAAE,GAAG,GAAG,GAAG,EAAE,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,SAAS,CAAC;IAC1B,KAAK,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,KAAK,IAAI,CAAC;IAC/D,QAAQ,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CAC3B,eAmDA,CAAC"}
1
+ {"version":3,"file":"lazy-observe.d.ts","sourceRoot":"","sources":["../../src/mobx/lazy-observe.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,SAAS,GAAG,IAAI,EAAE,kDAM3C;IACD,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,QAAQ,EAAE,GAAG,GAAG,GAAG,EAAE,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,SAAS,CAAC;IAC1B,KAAK,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,KAAK,IAAI,CAAC;IAC/D,QAAQ,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CAC3B,eAoEA,CAAC"}
@@ -1,24 +1,17 @@
1
1
  import { onBecomeObserved, onBecomeUnobserved } from 'mobx';
2
+ /**
3
+ * When ONE OF the properties is becomes observed then `onStart` function is called.
4
+ * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.
5
+ *
6
+ * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.
7
+ */
2
8
  export const lazyObserve = ({ context, property, onStart, onEnd, endDelay = false, }) => {
3
9
  let timeoutId;
4
10
  let metaData;
11
+ const observingProps = new Set();
5
12
  const properties = Array.isArray(property) ? property : [property];
6
- let isObserving = false;
7
- const start = () => {
8
- if (isObserving) {
9
- return;
10
- }
11
- isObserving = true;
12
- if (timeoutId) {
13
- clearTimeout(timeoutId);
14
- timeoutId = undefined;
15
- }
16
- metaData = onStart?.();
17
- };
18
13
  const cleanup = () => {
19
- if (!isObserving) {
20
- return;
21
- }
14
+ observingProps.clear();
22
15
  if (endDelay === false) {
23
16
  onEnd?.(metaData, cleanup);
24
17
  metaData = undefined;
@@ -34,14 +27,35 @@ export const lazyObserve = ({ context, property, onStart, onEnd, endDelay = fals
34
27
  metaData = undefined;
35
28
  }, endDelay);
36
29
  };
30
+ const start = (property) => {
31
+ const isAlreadyObserving = observingProps.size > 0;
32
+ observingProps.add(property);
33
+ if (isAlreadyObserving) {
34
+ return;
35
+ }
36
+ if (timeoutId) {
37
+ clearTimeout(timeoutId);
38
+ timeoutId = undefined;
39
+ }
40
+ metaData = onStart?.();
41
+ };
42
+ const stop = (property) => {
43
+ const isAlreadyNotObserving = !observingProps.size;
44
+ observingProps.delete(property);
45
+ const isObserving = observingProps.size > 0;
46
+ if (isAlreadyNotObserving || isObserving) {
47
+ return;
48
+ }
49
+ cleanup();
50
+ };
37
51
  properties.forEach((property) => {
38
52
  if (context) {
39
- onBecomeObserved(context, property, start);
40
- onBecomeUnobserved(context, property, cleanup);
53
+ onBecomeObserved(context, property, () => start(property));
54
+ onBecomeUnobserved(context, property, () => stop(property));
41
55
  }
42
56
  else {
43
- onBecomeObserved(property, start);
44
- onBecomeUnobserved(property, cleanup);
57
+ onBecomeObserved(property, () => start(property));
58
+ onBecomeUnobserved(property, () => stop(property));
45
59
  }
46
60
  });
47
61
  return cleanup;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yummies",
3
- "version": "5.7.0",
3
+ "version": "5.7.1",
4
4
  "keywords": [
5
5
  "javascript",
6
6
  "typescript",