@legendapp/state 0.9.2-alpha.7 → 0.10.0-next.0

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.
Files changed (144) hide show
  1. package/esm/index.d.ts +4 -0
  2. package/esm/index.js +5 -0
  3. package/esm/index.js.map +1 -0
  4. package/esm/internal.d.ts +3 -0
  5. package/esm/internal.js +4 -0
  6. package/esm/internal.js.map +1 -0
  7. package/esm/local-storage.d.ts +1 -0
  8. package/esm/local-storage.js +2 -0
  9. package/esm/local-storage.js.map +1 -0
  10. package/esm/mmkv.d.ts +1 -0
  11. package/esm/mmkv.js +2 -0
  12. package/esm/mmkv.js.map +1 -0
  13. package/esm/persist.d.ts +1 -0
  14. package/esm/persist.js +2 -0
  15. package/esm/persist.js.map +1 -0
  16. package/esm/react.d.ts +3 -0
  17. package/esm/react.js +4 -0
  18. package/esm/react.js.map +1 -0
  19. package/esm/src/configureObservable.d.ts +10 -0
  20. package/esm/src/configureObservable.js +5 -0
  21. package/esm/src/configureObservable.js.map +1 -0
  22. package/esm/src/globals.d.ts +15 -0
  23. package/esm/src/globals.js +61 -0
  24. package/esm/src/globals.js.map +1 -0
  25. package/esm/src/helpers.d.ts +5 -0
  26. package/esm/src/helpers.js +38 -0
  27. package/esm/src/helpers.js.map +1 -0
  28. package/esm/src/is.d.ts +7 -0
  29. package/esm/src/is.js +23 -0
  30. package/esm/src/is.js.map +1 -0
  31. package/esm/src/observable.d.ts +6 -0
  32. package/esm/src/observable.js +365 -0
  33. package/esm/src/observable.js.map +1 -0
  34. package/esm/src/observableBatcher.d.ts +16 -0
  35. package/esm/src/observableBatcher.js +62 -0
  36. package/esm/src/observableBatcher.js.map +1 -0
  37. package/esm/src/observableComputed.d.ts +2 -0
  38. package/esm/src/observableComputed.js +22 -0
  39. package/esm/src/observableComputed.js.map +1 -0
  40. package/esm/src/observableEvent.d.ts +2 -0
  41. package/esm/src/observableEvent.js +14 -0
  42. package/esm/src/observableEvent.js.map +1 -0
  43. package/esm/src/observableInterfaces.d.ts +265 -0
  44. package/esm/src/observableInterfaces.js +2 -0
  45. package/esm/src/observableInterfaces.js.map +1 -0
  46. package/esm/src/on.d.ts +7 -0
  47. package/esm/src/on.js +67 -0
  48. package/esm/src/on.js.map +1 -0
  49. package/esm/src/persist/local-storage.d.ts +8 -0
  50. package/esm/src/persist/local-storage.js +34 -0
  51. package/esm/src/persist/local-storage.js.map +1 -0
  52. package/esm/src/persist/mmkv.d.ts +10 -0
  53. package/esm/src/persist/mmkv.js +47 -0
  54. package/esm/src/persist/mmkv.js.map +1 -0
  55. package/esm/src/persistHelpers.d.ts +3 -0
  56. package/esm/src/persistHelpers.js +37 -0
  57. package/esm/src/persistHelpers.js.map +1 -0
  58. package/esm/src/persistObservable.d.ts +3 -0
  59. package/esm/src/persistObservable.js +117 -0
  60. package/esm/src/persistObservable.js.map +1 -0
  61. package/esm/src/react/useNewObservable.d.ts +11 -0
  62. package/esm/src/react/useNewObservable.js +23 -0
  63. package/esm/src/react/useNewObservable.js.map +1 -0
  64. package/esm/src/react/useObservableArray.d.ts +2 -0
  65. package/esm/src/react/useObservableArray.js +18 -0
  66. package/esm/src/react/useObservableArray.js.map +1 -0
  67. package/esm/src/react/useObservables.d.ts +9 -0
  68. package/esm/src/react/useObservables.js +100 -0
  69. package/esm/src/react/useObservables.js.map +1 -0
  70. package/esm/src/state.d.ts +13 -0
  71. package/esm/src/state.js +15 -0
  72. package/esm/src/state.js.map +1 -0
  73. package/index.d.ts +2 -6
  74. package/index.js +5 -21
  75. package/index.js.map +1 -1
  76. package/internal.d.ts +2 -2
  77. package/internal.js +4 -5
  78. package/internal.js.map +1 -1
  79. package/package.json +27 -9
  80. package/persist.d.ts +1 -0
  81. package/persist.js +6 -0
  82. package/persist.js.map +1 -0
  83. package/react.d.ts +2 -1
  84. package/react.js +2 -1
  85. package/react.js.map +1 -1
  86. package/src/configureObservable.d.ts +0 -1
  87. package/src/configureObservable.js +1 -5
  88. package/src/configureObservable.js.map +1 -1
  89. package/src/globals.d.ts +13 -7
  90. package/src/globals.js +59 -62
  91. package/src/globals.js.map +1 -1
  92. package/src/helpers.d.ts +5 -0
  93. package/src/helpers.js +45 -0
  94. package/src/helpers.js.map +1 -0
  95. package/src/is.d.ts +7 -0
  96. package/src/is.js +33 -0
  97. package/src/is.js.map +1 -0
  98. package/src/observable.d.ts +6 -8
  99. package/src/observable.js +328 -296
  100. package/src/observable.js.map +1 -1
  101. package/src/observableBatcher.d.ts +11 -2
  102. package/src/observableBatcher.js +20 -15
  103. package/src/observableBatcher.js.map +1 -1
  104. package/src/observableComputed.d.ts +2 -2
  105. package/src/observableComputed.js +17 -19
  106. package/src/observableComputed.js.map +1 -1
  107. package/src/observableEvent.js +7 -17
  108. package/src/observableEvent.js.map +1 -1
  109. package/src/observableInterfaces.d.ts +154 -47
  110. package/src/observableInterfaces.js +1 -0
  111. package/src/observableInterfaces.js.map +1 -1
  112. package/src/on.d.ts +7 -0
  113. package/src/on.js +76 -0
  114. package/src/on.js.map +1 -0
  115. package/src/persistHelpers.d.ts +3 -0
  116. package/src/persistHelpers.js +43 -0
  117. package/src/persistHelpers.js.map +1 -0
  118. package/src/persistObservable.d.ts +2 -2
  119. package/src/persistObservable.js +13 -12
  120. package/src/persistObservable.js.map +1 -1
  121. package/src/react/useNewObservable.d.ts +2 -3
  122. package/src/react/useNewObservable.js +5 -11
  123. package/src/react/useNewObservable.js.map +1 -1
  124. package/src/react/useObservableArray.d.ts +2 -0
  125. package/src/react/useObservableArray.js +22 -0
  126. package/src/react/useObservableArray.js.map +1 -0
  127. package/src/react/useObservables.d.ts +2 -2
  128. package/src/react/useObservables.js +76 -86
  129. package/src/react/useObservables.js.map +1 -1
  130. package/src/state.d.ts +13 -0
  131. package/src/state.js +18 -0
  132. package/src/state.js.map +1 -0
  133. package/src/observableFns.d.ts +0 -36
  134. package/src/observableFns.js +0 -231
  135. package/src/observableFns.js.map +0 -1
  136. package/src/observableState.d.ts +0 -29
  137. package/src/observableState.js +0 -32
  138. package/src/observableState.js.map +0 -1
  139. package/src/primitivePrototypes.d.ts +0 -1
  140. package/src/primitivePrototypes.js +0 -22
  141. package/src/primitivePrototypes.js.map +0 -1
  142. package/src/react/usePrimitiveFunctions.d.ts +0 -4
  143. package/src/react/usePrimitiveFunctions.js +0 -21
  144. package/src/react/usePrimitiveFunctions.js.map +0 -1
package/esm/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { shouldRender, mergeIntoObservable, shallow } from './src/helpers';
2
+ export { observable } from './src/observable';
3
+ export { observableBatcher } from './src/observableBatcher';
4
+ export * from './src/observableInterfaces';
package/esm/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export { shouldRender, mergeIntoObservable, shallow } from './src/helpers';
2
+ export { observable } from './src/observable';
3
+ export { observableBatcher } from './src/observableBatcher';
4
+ export * from './src/observableInterfaces';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,cAAc,4BAA4B,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { observableConfiguration } from './src/configureObservable';
2
+ export { symbolShallow, symbolDateModified } from './src/globals';
3
+ export { getDateModifiedKey } from './src/persistHelpers';
@@ -0,0 +1,4 @@
1
+ export { observableConfiguration } from './src/configureObservable';
2
+ export { symbolShallow, symbolDateModified } from './src/globals';
3
+ export { getDateModifiedKey } from './src/persistHelpers';
4
+ //# sourceMappingURL=internal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal.js","sourceRoot":"","sources":["../../internal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './src/persist/local-storage';
@@ -0,0 +1,2 @@
1
+ export * from './src/persist/local-storage';
2
+ //# sourceMappingURL=local-storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local-storage.js","sourceRoot":"","sources":["../../local-storage.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC"}
package/esm/mmkv.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './src/persist/mmkv';
package/esm/mmkv.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './src/persist/mmkv';
2
+ //# sourceMappingURL=mmkv.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mmkv.js","sourceRoot":"","sources":["../../mmkv.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1 @@
1
+ export { persistObservable } from './src/persistObservable';
package/esm/persist.js ADDED
@@ -0,0 +1,2 @@
1
+ export { persistObservable } from './src/persistObservable';
2
+ //# sourceMappingURL=persist.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persist.js","sourceRoot":"","sources":["../../persist.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC"}
package/esm/react.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './src/react/useObservables';
2
+ export * from './src/react/useNewObservable';
3
+ export * from './src/react/useObservableArray';
package/esm/react.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './src/react/useObservables';
2
+ export * from './src/react/useNewObservable';
3
+ export * from './src/react/useObservableArray';
4
+ //# sourceMappingURL=react.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.js","sourceRoot":"","sources":["../../react.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gCAAgC,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { ClassConstructor, ObservablePersistLocal, ObservablePersistRemote } from './observableInterfaces';
2
+ interface Config {
3
+ persistLocal?: ClassConstructor<ObservablePersistLocal>;
4
+ persistRemote?: ClassConstructor<ObservablePersistRemote>;
5
+ saveTimeout?: number;
6
+ dateModifiedKey?: string;
7
+ }
8
+ export declare const observableConfiguration: Config;
9
+ export declare function configureObservable(options?: Config): void;
10
+ export {};
@@ -0,0 +1,5 @@
1
+ export const observableConfiguration = {};
2
+ export function configureObservable(options) {
3
+ Object.assign(observableConfiguration, options);
4
+ }
5
+ //# sourceMappingURL=configureObservable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"configureObservable.js","sourceRoot":"","sources":["../../../src/configureObservable.ts"],"names":[],"mappings":"AASA,MAAM,CAAC,MAAM,uBAAuB,GAAW,EAAE,CAAC;AAElD,MAAM,UAAU,mBAAmB,CAAC,OAAgB;IAChD,MAAM,CAAC,MAAM,CAAC,uBAAuB,EAAE,OAAO,CAAC,CAAC;AACpD,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { ObservableChecker, ProxyValue } from './observableInterfaces';
2
+ export declare const delim = "\uFEFF";
3
+ export declare const symbolDateModified: unique symbol;
4
+ export declare const symbolShallow: unique symbol;
5
+ export declare const symbolShouldRender: unique symbol;
6
+ export declare const symbolID: unique symbol;
7
+ export declare const symbolGet: unique symbol;
8
+ export declare const symbolIsObservable: unique symbol;
9
+ export declare function getNodeValue(node: ProxyValue): any;
10
+ export declare function getParentNode(node: ProxyValue): {
11
+ parent: ProxyValue;
12
+ key: string | number;
13
+ };
14
+ export declare function getChildNode(node: ProxyValue, key: string | number): ProxyValue;
15
+ export declare function getObservableRawValue<T>(obs: ObservableChecker<T>): T;
@@ -0,0 +1,61 @@
1
+ import { isPrimitive } from './is';
2
+ import { state } from './state';
3
+ export const delim = '\uFEFF';
4
+ export const symbolDateModified = Symbol('dateModified');
5
+ export const symbolShallow = Symbol('shallow');
6
+ export const symbolShouldRender = Symbol('shouldRender');
7
+ export const symbolID = Symbol('id');
8
+ export const symbolGet = Symbol('get');
9
+ export const symbolIsObservable = Symbol('isObservable');
10
+ export function getNodeValue(node) {
11
+ let child = node.root;
12
+ const arr = node.path.split(delim);
13
+ for (let i = 0; i < arr.length; i++) {
14
+ if (arr[i] !== undefined && child) {
15
+ child = child[arr[i]];
16
+ }
17
+ }
18
+ return child;
19
+ }
20
+ export function getParentNode(node) {
21
+ if (node.path === '_')
22
+ return { parent: node, key: undefined };
23
+ const parent = node.root.proxyValues.get(node.pathParent);
24
+ return { parent, key: node.key };
25
+ }
26
+ export function getChildNode(node, key) {
27
+ const path = node.path + delim + key;
28
+ let child = node.root.proxyValues.get(path);
29
+ if (!child) {
30
+ // console.log('creating child', node.path, key);
31
+ child = {
32
+ root: node.root,
33
+ path: node.path + delim + key,
34
+ // arr: node.arr.concat(key),
35
+ // arrParent: node.arr,
36
+ pathParent: node.path,
37
+ key,
38
+ };
39
+ node.root.proxyValues.set(path, child);
40
+ }
41
+ return child;
42
+ }
43
+ export function getObservableRawValue(obs) {
44
+ if (!obs || isPrimitive(obs))
45
+ return obs;
46
+ const eq = obs[symbolShouldRender];
47
+ if (eq) {
48
+ state.trackingShouldRender = eq.fn;
49
+ obs = eq.obs;
50
+ }
51
+ const shallow = obs[symbolShallow];
52
+ if (shallow) {
53
+ state.trackingShallow = true;
54
+ obs = shallow;
55
+ }
56
+ let ret = obs[symbolGet];
57
+ state.trackingShouldRender = undefined;
58
+ state.trackingShallow = false;
59
+ return ret;
60
+ }
61
+ //# sourceMappingURL=globals.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"globals.js","sourceRoot":"","sources":["../../../src/globals.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AAEnC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AA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@@ -0,0 +1,5 @@
1
+ import type { ShouldRender, Observable, Shallow } from './observableInterfaces';
2
+ export declare function shallow(obs: Observable): Shallow;
3
+ export declare function shouldRender<T>(obs: Observable<T>, fn: (value: T, prev: T) => any): ShouldRender;
4
+ export declare function isObservable(obs: any): boolean;
5
+ export declare function mergeIntoObservable(target: Observable, ...sources: any[]): any;
@@ -0,0 +1,38 @@
1
+ import { symbolDateModified, symbolShouldRender, symbolShallow, symbolIsObservable } from './globals';
2
+ import { isObject } from './is';
3
+ export function shallow(obs) {
4
+ return {
5
+ [symbolShallow]: obs,
6
+ };
7
+ }
8
+ export function shouldRender(obs, fn) {
9
+ return {
10
+ [symbolShouldRender]: { obs, fn },
11
+ };
12
+ }
13
+ export function isObservable(obs) {
14
+ return obs && !!obs[symbolIsObservable];
15
+ }
16
+ export function mergeIntoObservable(target, ...sources) {
17
+ if (!sources.length)
18
+ return target;
19
+ const source = sources.shift();
20
+ if (isObject(target) && isObject(source)) {
21
+ if (source[symbolDateModified]) {
22
+ target.setProp(symbolDateModified, source[symbolDateModified]);
23
+ }
24
+ for (const key in source) {
25
+ if (isObject(source[key])) {
26
+ if (!target[key] || !isObject(target[key])) {
27
+ target.setProp(key, {});
28
+ }
29
+ mergeIntoObservable(target[key], source[key]);
30
+ }
31
+ else {
32
+ target.setProp(key, source[key]);
33
+ }
34
+ }
35
+ }
36
+ return mergeIntoObservable(target, ...sources);
37
+ }
38
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AACtG,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAGhC,MAAM,UAAU,OAAO,CAAC,GAAe;IACnC,OAAO;QACH,CAAC,aAAa,CAAC,EAAE,GAAG;KACvB,CAAC;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isArray(obj: unknown): obj is Array<any>;
2
+ export declare function isString(obj: unknown): obj is string;
3
+ export declare function isObject(obj: unknown): obj is Record<any, any>;
4
+ export declare function isFunction(obj: unknown): obj is Function;
5
+ export declare function isPrimitive(arg: any): boolean;
6
+ export declare function isObjectEmpty(obj: object): boolean;
7
+ export declare function isSymbol(obj: unknown): obj is symbol;
package/esm/src/is.js ADDED
@@ -0,0 +1,23 @@
1
+ export function isArray(obj) {
2
+ return Array.isArray(obj);
3
+ }
4
+ export function isString(obj) {
5
+ return typeof obj === 'string';
6
+ }
7
+ export function isObject(obj) {
8
+ return typeof obj === 'object' && obj !== null && !isArray(obj);
9
+ }
10
+ export function isFunction(obj) {
11
+ return typeof obj === 'function';
12
+ }
13
+ export function isPrimitive(arg) {
14
+ var type = typeof arg;
15
+ return arg == null || (type != 'object' && type != 'function');
16
+ }
17
+ export function isObjectEmpty(obj) {
18
+ return obj && isObject(obj) && Object.keys(obj).length === 0;
19
+ }
20
+ export function isSymbol(obj) {
21
+ return typeof obj === 'symbol';
22
+ }
23
+ //# sourceMappingURL=is.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is.js","sourceRoot":"","sources":["../../../src/is.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,OAAO,CAAC,GAAY;IAChC,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAC9B,CAAC;AACD,MAAM,UAAU,QAAQ,CAAC,GAAY;IACjC,OAAO,OAAO,GAAG,KAAK,QAAQ,CAAC;AACnC,CAAC;AACD,MAAM,UAAU,QAAQ,CAAC,GAAY;IACjC,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AACpE,CAAC;AACD,MAAM,UAAU,UAAU,CAAC,GAAY;IACnC,OAAO,OAAO,GAAG,KAAK,UAAU,CAAC;AACrC,CAAC;AACD,MAAM,UAAU,WAAW,CAAC,GAAG;IAC3B,IAAI,IAAI,GAAG,OAAO,GAAG,CAAC;IACtB,OAAO,GAAG,IAAI,IAAI,IAAI,CAAC,IAAI,IAAI,QAAQ,IAAI,IAAI,IAAI,UAAU,CAAC,CAAC;AACnE,CAAC;AACD,MAAM,UAAU,aAAa,CAAC,GAAW;IACrC,OAAO,GAAG,IAAI,QAAQ,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;AACjE,CAAC;AACD,MAAM,UAAU,QAAQ,CAAC,GAAY;IACjC,OAAO,OAAO,GAAG,KAAK,QAAQ,CAAC;AACnC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { Observable, ObservablePrimitive } from './observableInterfaces';
2
+ export declare function observable<T extends object | Array<any>>(obj: T): Observable<T>;
3
+ export declare function observable<T extends boolean>(prim: T): ObservablePrimitive<boolean>;
4
+ export declare function observable<T extends string>(prim: T): ObservablePrimitive<string>;
5
+ export declare function observable<T extends number>(prim: T): ObservablePrimitive<number>;
6
+ export declare function observable<T extends boolean | string | number>(prim: T): ObservablePrimitive<T>;
@@ -0,0 +1,365 @@
1
+ import { delim, getChildNode, getNodeValue, getParentNode, symbolGet, symbolID, symbolIsObservable } from './globals';
2
+ import { isArray, isFunction, isPrimitive, isSymbol } from './is';
3
+ import { observableBatcher, observableBatcherNotify } from './observableBatcher';
4
+ import { onChange, onChangeShallow, onEquals, onHasValue, onTrue } from './on';
5
+ import { state } from './state';
6
+ let lastAccessedNode;
7
+ let lastAccessedPrimitive;
8
+ let inSetFn = false;
9
+ const ArrayModifiers = new Set([
10
+ 'copyWithin',
11
+ 'fill',
12
+ 'from',
13
+ 'pop',
14
+ 'push',
15
+ 'reverse',
16
+ 'shift',
17
+ 'sort',
18
+ 'splice',
19
+ 'unshift',
20
+ ]);
21
+ const ArrayLoopers = new Set(['every', 'some', 'filter', 'reduce', 'reduceRight', 'forEach', 'map']);
22
+ const objectFnsProxy = new Map([
23
+ ['set', set],
24
+ ['setProp', setProp],
25
+ ['onChange', onChange],
26
+ ['onChangeShallow', onChangeShallow],
27
+ ['onEquals', onEquals],
28
+ ['onHasValue', onHasValue],
29
+ ['onTrue', onTrue],
30
+ ['prop', prop],
31
+ ['assign', assign],
32
+ ['delete', deleteFn],
33
+ ]);
34
+ // Override primitives
35
+ const wrapFn = (fn) => function (...args) {
36
+ if (lastAccessedNode && lastAccessedPrimitive) {
37
+ const node = getChildNode(lastAccessedNode, lastAccessedPrimitive);
38
+ if (getNodeValue(node) === this) {
39
+ return fn(node, ...args);
40
+ }
41
+ }
42
+ };
43
+ const toOverride = [Number, Boolean, String];
44
+ objectFnsProxy.forEach((fn, key) => {
45
+ for (let i = 0; i < toOverride.length; i++) {
46
+ toOverride[i].prototype[key] = wrapFn(fn);
47
+ }
48
+ });
49
+ function collectionSetter(node, target, prop, ...args) {
50
+ const prevValue = (isArray(target) && target.slice()) || target;
51
+ const ret = target[prop].apply(target, args);
52
+ if (node) {
53
+ const { parent, key } = getParentNode(node);
54
+ if (parent) {
55
+ const parentValue = getNodeValue(parent);
56
+ // Set the object to the previous value first
57
+ parentValue[key] = prevValue;
58
+ // Then set with the new value so it notifies with the correct prevValue
59
+ setProp(parent, key, target);
60
+ }
61
+ }
62
+ // Return the original value
63
+ return ret;
64
+ }
65
+ function updateNodes(parent, obj, prevValue) {
66
+ const isArr = isArray(obj);
67
+ // If array it's faster to just use the array
68
+ const keys = isArr ? obj : Object.keys(obj);
69
+ const length = keys.length;
70
+ for (let i = 0; i < length; i++) {
71
+ const key = isArr ? i : keys[i];
72
+ const value = obj[key];
73
+ const prev = prevValue === null || prevValue === void 0 ? void 0 : prevValue[key];
74
+ if (!isArr && prevValue && value !== prev) {
75
+ const isObj = !isPrimitive(value);
76
+ const child = getChildNode(parent, key);
77
+ // If object iterate through its children
78
+ if (isObj) {
79
+ updateNodes(child, value, prev);
80
+ }
81
+ // Notify for this child if this element is different and it has listeners
82
+ // But do not notify child if the parent is an array - the array's listener will cover it
83
+ const doNotify = child.root.listenerMap.has(child.path);
84
+ if (doNotify) {
85
+ _notify(child, value, [], value, prev, 0);
86
+ }
87
+ }
88
+ }
89
+ }
90
+ function createProxy(node) {
91
+ const proxy = new Proxy(node, proxyHandler);
92
+ node.root.proxies.set(node.path, proxy);
93
+ return proxy;
94
+ }
95
+ const descriptorNotEnumerable = {
96
+ enumerable: false,
97
+ configurable: false,
98
+ };
99
+ const proxyHandler = {
100
+ get(target, p) {
101
+ // Return true is called by isObservable()
102
+ if (p === symbolIsObservable) {
103
+ return true;
104
+ }
105
+ const node = target;
106
+ const fn = objectFnsProxy.get(p);
107
+ // If this is an observable function, call it
108
+ if (fn) {
109
+ return (a, b, c) => fn(node, a, b, c);
110
+ }
111
+ let value = getNodeValue(node);
112
+ const vProp = value[p];
113
+ // The get() function as well as the internal obs[symbolGet]
114
+ if (p === symbolGet || p === 'get') {
115
+ // Primitives are { current } so return the current value
116
+ if (node.root.isPrimitive) {
117
+ value = value.current;
118
+ }
119
+ // Update that this node was accessed for useObservables and useComputed
120
+ if (state.isTracking) {
121
+ state.updateTracking(node, value);
122
+ }
123
+ return p === 'get' ? () => value : value;
124
+ }
125
+ // Accessing symbols passes straight through
126
+ if (isSymbol(p)) {
127
+ return vProp;
128
+ }
129
+ // Handle function calls
130
+ if (isFunction(vProp)) {
131
+ if (isArray(value)) {
132
+ if (ArrayModifiers.has(p)) {
133
+ // Call the wrapped modifier function
134
+ return (...args) => collectionSetter(node, value, p, ...args);
135
+ }
136
+ else if (ArrayLoopers.has(p)) {
137
+ // Bind this looping function to an array of proxies
138
+ const arr = [];
139
+ for (let i = 0; i < value.length; i++) {
140
+ arr.push(prop(node, i));
141
+ }
142
+ return vProp.bind(arr);
143
+ }
144
+ }
145
+ // Return the function bound to the value
146
+ return vProp.bind(value);
147
+ }
148
+ // Accessing primitives tracks and returns
149
+ if (isPrimitive(vProp)) {
150
+ // Accessing a primitive saves the last accessed so that the observable functions
151
+ // bound to primitives can know which node was accessed
152
+ lastAccessedNode = node;
153
+ lastAccessedPrimitive = p;
154
+ // Update that this node was accessed for useObservables and useComputed
155
+ if (state.isTracking) {
156
+ state.updateTracking(getChildNode(node, p), value);
157
+ }
158
+ return vProp;
159
+ }
160
+ // Create a proxy if not already cached and return it
161
+ const path = target.path + delim + p;
162
+ let proxy = node.root.proxies.get(path);
163
+ if (!proxy) {
164
+ proxy = createProxy(getChildNode(target, p));
165
+ }
166
+ return proxy;
167
+ },
168
+ // Forward all proxy properties to the target's value
169
+ getPrototypeOf(target) {
170
+ const value = getNodeValue(target);
171
+ return Reflect.getPrototypeOf(value);
172
+ },
173
+ ownKeys(target) {
174
+ const value = getNodeValue(target);
175
+ const keys = Reflect.ownKeys(value);
176
+ if (isArray(value)) {
177
+ if (keys[keys.length - 1] === 'length') {
178
+ keys.splice(keys.length - 1, 1);
179
+ }
180
+ }
181
+ return keys;
182
+ },
183
+ getOwnPropertyDescriptor(target, p) {
184
+ if (p === symbolID) {
185
+ return descriptorNotEnumerable;
186
+ }
187
+ const value = getNodeValue(target);
188
+ return Reflect.getOwnPropertyDescriptor(value, p);
189
+ },
190
+ set(target, prop, value) {
191
+ // If this assignment comes from within an observable function it's allowed
192
+ if (inSetFn) {
193
+ Reflect.set(target, prop, value);
194
+ return true;
195
+ }
196
+ else {
197
+ return false;
198
+ }
199
+ },
200
+ deleteProperty(target, prop) {
201
+ // If this delete comes from within an observable function it's allowed
202
+ if (inSetFn) {
203
+ Reflect.deleteProperty(target, prop);
204
+ return true;
205
+ }
206
+ else {
207
+ return false;
208
+ }
209
+ },
210
+ has(target, prop) {
211
+ const value = getNodeValue(target);
212
+ return Reflect.has(value, prop);
213
+ },
214
+ };
215
+ function set(node, newValue) {
216
+ if (node.root.isPrimitive) {
217
+ return setProp(node, 'current', newValue);
218
+ }
219
+ else {
220
+ const { parent, key } = getParentNode(node);
221
+ return setProp(parent, key, newValue);
222
+ }
223
+ }
224
+ function setProp(node, key, newValue) {
225
+ var _a;
226
+ newValue = (_a = newValue === null || newValue === void 0 ? void 0 : newValue[symbolGet]) !== null && _a !== void 0 ? _a : newValue;
227
+ const isPrim = isPrimitive(newValue);
228
+ if (!key && !node.path.includes(delim)) {
229
+ return assign(node, newValue);
230
+ }
231
+ inSetFn = true;
232
+ // Get the child node for updating and notifying
233
+ const childNode = getChildNode(node, key);
234
+ // Get the value of the parent
235
+ let parentValue = getNodeValue(node);
236
+ // Save the previous value first
237
+ const prevValue = parentValue[key];
238
+ // Save the new value
239
+ parentValue[key] = newValue;
240
+ // If new value is an object or array update notify down the tree
241
+ if (!isPrim) {
242
+ updateNodes(childNode, newValue, prevValue);
243
+ }
244
+ // Notify for this element if it's an object or it's changed
245
+ if (!isPrim || newValue !== prevValue) {
246
+ notify(node.root.isPrimitive ? node : childNode, newValue, prevValue, prevValue === undefined ? -1 : 0);
247
+ }
248
+ inSetFn = false;
249
+ return newValue;
250
+ }
251
+ function _notify(node, value, path, valueAtPath, prevAtPath, level) {
252
+ const listeners = node.root.listenerMap.get(node.path);
253
+ // Notify all listeners
254
+ if (listeners) {
255
+ let getPrevious;
256
+ for (let listener of listeners) {
257
+ // Notify if listener is not shallow or if this is the first level
258
+ if (!listener.shallow || level <= 0) {
259
+ // Create a function to get the previous data. Computing a clone of previous data can be expensive if doing
260
+ // it often, so leave it up to the user.
261
+ if (!getPrevious) {
262
+ getPrevious = createPreviousHandler(value, path, prevAtPath);
263
+ }
264
+ observableBatcherNotify({ cb: listener.callback, value, getPrevious, path, valueAtPath, prevAtPath });
265
+ }
266
+ }
267
+ }
268
+ }
269
+ function createPreviousHandler(value, path, prevAtPath) {
270
+ // Create a function that clones the current state and injects the previous data at the changed path
271
+ return function () {
272
+ let clone = value ? JSON.parse(JSON.stringify(value)) : path.length > 0 ? {} : value;
273
+ let o = clone;
274
+ if (path.length > 0) {
275
+ let i;
276
+ for (i = 0; i < path.length - 1; i++) {
277
+ o = o[path[i]];
278
+ }
279
+ o[path[i]] = prevAtPath;
280
+ }
281
+ else {
282
+ clone = prevAtPath;
283
+ }
284
+ return clone;
285
+ };
286
+ }
287
+ function _notifyParents(node, value, path, valueAtPath, prevAtPath, level) {
288
+ // Do the notify
289
+ _notify(node, value, path, valueAtPath, prevAtPath, level);
290
+ // If not root notify up through parents
291
+ if (node.path !== '_') {
292
+ const { parent, key } = getParentNode(node);
293
+ if (parent) {
294
+ const parentValue = getNodeValue(parent);
295
+ _notifyParents(parent, parentValue, [key].concat(path), valueAtPath, prevAtPath, level + 1);
296
+ }
297
+ }
298
+ }
299
+ function notify(node, value, prev, level) {
300
+ // Start notifying up through parents with the listenerInfo
301
+ _notifyParents(node, value, [], value, prev, level);
302
+ }
303
+ function prop(node, key) {
304
+ const child = getChildNode(node, key);
305
+ return createProxy(child);
306
+ }
307
+ function assign(node, value) {
308
+ observableBatcher.begin();
309
+ // Assign calls set with all assigned properties
310
+ const keys = Object.keys(value);
311
+ const length = keys.length;
312
+ for (let i = 0; i < length; i++) {
313
+ setProp(node, keys[i], value[keys[i]]);
314
+ }
315
+ const ret = getNodeValue(node);
316
+ observableBatcher.end();
317
+ return ret;
318
+ }
319
+ function deleteFn(node, key) {
320
+ // If called without a key, delete by key from the parent node
321
+ if (key !== undefined) {
322
+ return deleteFnByKey(node, key);
323
+ }
324
+ else {
325
+ const { parent, key } = getParentNode(node);
326
+ return deleteFnByKey(parent, key);
327
+ }
328
+ }
329
+ function deleteFnByKey(node, key) {
330
+ if (!node.root.isPrimitive) {
331
+ // delete sets to undefined first to cleanup children
332
+ setProp(node, key, undefined);
333
+ }
334
+ inSetFn = true;
335
+ // Then delete the key from the object
336
+ let child = getNodeValue(node);
337
+ delete child[key];
338
+ inSetFn = false;
339
+ }
340
+ export function observable(obj) {
341
+ const isPrim = isPrimitive(obj);
342
+ // Primitives wrap in current
343
+ if (isPrim) {
344
+ obj = { current: obj };
345
+ }
346
+ const obs = {
347
+ _: obj,
348
+ isPrimitive: isPrim,
349
+ listenerMap: new Map(),
350
+ proxies: new Map(),
351
+ proxyValues: new Map(),
352
+ };
353
+ const node = {
354
+ root: obs,
355
+ path: '_',
356
+ pathParent: '',
357
+ key: '_',
358
+ };
359
+ obs.proxyValues.set(node.path, node);
360
+ updateNodes(node, obs._);
361
+ const proxy = createProxy(node);
362
+ // @ts-ignore
363
+ return proxy;
364
+ }
365
+ //# sourceMappingURL=observable.js.map