yummies 5.12.1 → 5.13.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.
@@ -0,0 +1,18 @@
1
+ import { AnyObject, Maybe } from '../utils/types.ts';
2
+ /**
3
+ * Alternative to React.createRef but works in MobX world.
4
+ * Typically it the should be the same React.LegacyRef (fn style)
5
+ */
6
+ export type Ref<T = any, TMeta = AnyObject> = ((element: Maybe<T>) => void) & {
7
+ current: T | null;
8
+ meta: TMeta;
9
+ };
10
+ /**
11
+ * Creates ref thing to attach HTMLElements in React and all other
12
+ */
13
+ export declare const createRef: <T = HTMLElement, TMeta = AnyObject>(cfg?: {
14
+ onSet?: (node: T) => void;
15
+ onUnset?: () => void;
16
+ meta?: TMeta;
17
+ }) => Ref<T, TMeta>;
18
+ //# sourceMappingURL=create-ref.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-ref.d.ts","sourceRoot":"","sources":["../../src/mobx/create-ref.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAE5D;;;GAGG;AACH,MAAM,MAAM,GAAG,CAAC,CAAC,GAAG,GAAG,EAAE,KAAK,GAAG,SAAS,IAAI,CAAC,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,GAAG;IAC5E,OAAO,EAAE,CAAC,GAAG,IAAI,CAAC;IAClB,IAAI,EAAE,KAAK,CAAC;CACb,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,GAAG,WAAW,EAAE,KAAK,GAAG,SAAS,EAAE,MAAM;IAClE,KAAK,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC;IAC1B,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,IAAI,CAAC,EAAE,KAAK,CAAC;CACd,KAAG,GAAG,CAAC,CAAC,EAAE,KAAK,CAmBf,CAAC"}
package/mobx/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './apply-observable.js';
2
2
  export * from './create-enhanced-atom.js';
3
+ export * from './create-ref.js';
3
4
  export * from './deep-observable-struct.js';
4
5
  export * from './get-mobx-administration.js';
5
6
  export * from './lazy-observe.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mobx/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mobx/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC;AAChC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,mBAAmB,CAAC"}
package/mobx.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("mobx"),t=require("yummies/type-guard");exports.DeepObservableStruct=class{data;constructor(t){this.data=t,e.makeObservable(this,{data:e.observable.deep,set:e.action})}set(e){const r=Object.keys(this.data).map(t=>[t,this.data,e]);let o=0,s=r.length;for(;o<s;){const[e,a,n]=r[o],c=n[e],i=a[e];if(o++,e in n)if(t.typeGuard.isObject(c)&&t.typeGuard.isObject(i)){const t=Object.keys(c);Object.keys(i).forEach(t=>{t in c||delete a[e][t]}),t.forEach(t=>{const o=r.push([t,a[e],c]);s=o})}else c!==i&&(a[e]=c);else delete a[e]}Object.keys(e).forEach(t=>{this.data[t]||(this.data[t]=e[t])})}},exports.applyObservable=(t,r,o)=>{o?(r.forEach(([e,r])=>{r(t,e)}),e.makeObservable(t)):e.makeObservable(t,Object.fromEntries(r))},exports.createEnhancedAtom=(t,r,o,s)=>{const a=e.createAtom(t,r&&(()=>r(a)),o&&(()=>o(a)));return a.meta=s??{},a.reportChanged=a.reportChanged.bind(a),a.reportObserved=a.reportObserved.bind(a),a},exports.getMobxAdministration=t=>t[e.$mobx],exports.lazyObserve=({context:t,property:r,onStart:o,onEnd:s,endDelay:a=!1})=>{let n,c;const i=new Set,d=Array.isArray(r)?r:[r],b=()=>{if(i.clear(),!1===a)return s?.(c,b),void(c=void 0);n&&(clearTimeout(n),n=void 0),n=setTimeout(()=>{s?.(c,b),n=void 0,c=void 0},a)},l=e=>{const t=i.size>0;i.add(e),t||(n&&(clearTimeout(n),n=void 0),c=o?.())},p=e=>{const t=!i.size;i.delete(e);const r=i.size>0;t||r||b()};return d.forEach(r=>{t?(e.onBecomeObserved(t,r,()=>l(r)),e.onBecomeUnobserved(t,r,()=>p(r))):(e.onBecomeObserved(r,()=>l(r)),e.onBecomeUnobserved(r,()=>p(r)))}),b};
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("mobx"),t=require("yummies/type-guard");exports.DeepObservableStruct=class{data;constructor(t){this.data=t,e.makeObservable(this,{data:e.observable.deep,set:e.action})}set(e){const r=Object.keys(this.data).map(t=>[t,this.data,e]);let o=0,s=r.length;for(;o<s;){const[e,a,n]=r[o],c=n[e],i=a[e];if(o++,e in n)if(t.typeGuard.isObject(c)&&t.typeGuard.isObject(i)){const t=Object.keys(c);Object.keys(i).forEach(t=>{t in c||delete a[e][t]}),t.forEach(t=>{const o=r.push([t,a[e],c]);s=o})}else c!==i&&(a[e]=c);else delete a[e]}Object.keys(e).forEach(t=>{this.data[t]||(this.data[t]=e[t])})}},exports.applyObservable=(t,r,o)=>{o?(r.forEach(([e,r])=>{r(t,e)}),e.makeObservable(t)):e.makeObservable(t,Object.fromEntries(r))},exports.createEnhancedAtom=(t,r,o,s)=>{const a=e.createAtom(t,r&&(()=>r(a)),o&&(()=>o(a)));return a.meta=s??{},a.reportChanged=a.reportChanged.bind(a),a.reportObserved=a.reportObserved.bind(a),a},exports.createRef=t=>{const r=e.action(e=>{r.current=e,r.current?t?.onSet?.(r.current):t?.onUnset?.()});return r.current=null,r.meta=t?.meta??{},e.makeObservable(r,{current:e.observable.ref,meta:e.observable}),r},exports.getMobxAdministration=t=>t[e.$mobx],exports.lazyObserve=({context:t,property:r,onStart:o,onEnd:s,endDelay:a=!1})=>{let n,c;const i=new Set,b=Array.isArray(r)?r:[r],d=()=>{if(i.clear(),!1===a)return s?.(c,d),void(c=void 0);n&&(clearTimeout(n),n=void 0),n=setTimeout(()=>{s?.(c,d),n=void 0,c=void 0},a)},l=e=>{const t=i.size>0;i.add(e),t||(n&&(clearTimeout(n),n=void 0),c=o?.())},m=e=>{const t=!i.size;i.delete(e);const r=i.size>0;t||r||d()};return b.forEach(r=>{t?(e.onBecomeObserved(t,r,()=>l(r)),e.onBecomeUnobserved(t,r,()=>m(r))):(e.onBecomeObserved(r,()=>l(r)),e.onBecomeUnobserved(r,()=>m(r)))}),d};
2
2
  //# sourceMappingURL=mobx.cjs.map
package/mobx.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mobx.cjs","sources":["../src/mobx/deep-observable-struct.ts","../src/mobx/apply-observable.ts","../src/mobx/create-enhanced-atom.ts","../src/mobx/get-mobx-administration.ts","../src/mobx/lazy-observe.ts"],"sourcesContent":["import { action, makeObservable, observable } from 'mobx';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport class DeepObservableStruct<TData extends AnyObject> {\n data: TData;\n\n constructor(data: TData) {\n this.data = data;\n\n makeObservable(this, {\n data: observable.deep,\n set: action,\n });\n }\n\n set(newData: Partial<TData>) {\n type StackItem = [key: string, currObservable: AnyObject, new: AnyObject];\n\n const stack: StackItem[] = Object.keys(this.data).map((key) => [\n key,\n this.data,\n newData,\n ]);\n\n let currentIndex = 0;\n let stackLength = stack.length;\n\n while (currentIndex < stackLength) {\n const [key, currObservableData, newData] = stack[currentIndex];\n const newValue = newData[key];\n const currValue = currObservableData[key];\n\n currentIndex++;\n\n if (key in newData) {\n if (typeGuard.isObject(newValue) && typeGuard.isObject(currValue)) {\n const newValueKeys = Object.keys(newValue);\n\n Object.keys(currValue).forEach((childKey) => {\n if (!(childKey in newValue)) {\n delete currObservableData[key][childKey];\n }\n });\n\n newValueKeys.forEach((childKey) => {\n const length = stack.push([\n childKey,\n currObservableData[key],\n newValue,\n ]);\n stackLength = length;\n });\n } else if (newValue !== currValue) {\n currObservableData[key] = newValue;\n }\n } else {\n delete currObservableData[key];\n }\n }\n\n Object.keys(newData).forEach((newDataKey) => {\n if (!this.data[newDataKey]) {\n // @ts-expect-error\n this.data[newDataKey] = newData[newDataKey];\n }\n });\n }\n}\n","import { makeObservable } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport type ObservableAnnotationsArray = [string, any][];\n\nexport const applyObservable = (\n context: AnyObject,\n annotationsArray: ObservableAnnotationsArray,\n useDecorators?: boolean,\n) => {\n if (useDecorators) {\n annotationsArray.forEach(([field, annotation]) => {\n annotation(context, field);\n });\n\n makeObservable(context);\n } else {\n makeObservable(context, Object.fromEntries(annotationsArray));\n }\n};\n","import { createAtom, type IAtom } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport interface IEnhancedAtom<TMeta extends AnyObject = AnyObject>\n extends IAtom {\n meta: TMeta;\n}\n\n/**\n * Creates an enhanced atom with meta data\n * And bind `reportChanged` and `reportObserved` method to the atom\n */\nexport const createEnhancedAtom = <TMeta extends AnyObject>(\n name: string,\n onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n meta?: TMeta,\n): IEnhancedAtom<TMeta> => {\n const atom = createAtom(\n name,\n onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),\n onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom)),\n ) as IEnhancedAtom<TMeta>;\n atom.meta = meta ?? ({} as TMeta);\n atom.reportChanged = atom.reportChanged.bind(atom);\n atom.reportObserved = atom.reportObserved.bind(atom);\n return atom;\n};\n","import { $mobx, type AnnotationMapEntry } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\ntype ObservableObjectAdministration = Parameters<\n Exclude<AnnotationMapEntry, boolean>['make_']\n>[0];\n\nexport const getMobxAdministration = (\n context: AnyObject,\n): ObservableObjectAdministration => context[$mobx];\n","import { onBecomeObserved, onBecomeUnobserved } from 'mobx';\n\n/**\n * When ONE OF the properties is becomes observed then `onStart` function is called.\n * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.\n *\n * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.\n */\nexport const lazyObserve = <TMetaData = void>({\n context,\n property,\n onStart,\n onEnd,\n endDelay = false,\n}: {\n context?: any;\n property: any | any[];\n onStart?: () => TMetaData;\n onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;\n endDelay?: number | false;\n}) => {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n let metaData: TMetaData | undefined;\n const observingProps = new Set<string>();\n const properties = Array.isArray(property) ? property : [property];\n\n const cleanup = () => {\n observingProps.clear();\n\n if (endDelay === false) {\n onEnd?.(metaData!, cleanup);\n metaData = undefined;\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n timeoutId = setTimeout(() => {\n onEnd?.(metaData!, cleanup);\n timeoutId = undefined;\n metaData = undefined;\n }, endDelay);\n };\n\n const start = (property: string) => {\n const isAlreadyObserving = observingProps.size > 0;\n observingProps.add(property);\n\n if (isAlreadyObserving) {\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n metaData = onStart?.();\n };\n\n const stop = (property: string) => {\n const isAlreadyNotObserving = !observingProps.size;\n\n observingProps.delete(property);\n\n const isObserving = observingProps.size > 0;\n\n if (isAlreadyNotObserving || isObserving) {\n return;\n }\n\n cleanup();\n };\n\n properties.forEach((property) => {\n if (context) {\n onBecomeObserved(context, property, () => start(property));\n onBecomeUnobserved(context, property, () => stop(property));\n } else {\n onBecomeObserved(property, () => start(property));\n onBecomeUnobserved(property, () => stop(property));\n }\n });\n\n return cleanup;\n};\n"],"names":["data","constructor","this","makeObservable","observable","deep","set","action","newData","stack","Object","keys","map","key","currentIndex","stackLength","length","currObservableData","newValue","currValue","typeGuard","isObject","newValueKeys","forEach","childKey","push","newDataKey","context","annotationsArray","useDecorators","field","annotation","fromEntries","name","onBecomeObservedHandler","onBecomeUnobservedHandler","meta","atom","createAtom","reportChanged","bind","reportObserved","$mobx","property","onStart","onEnd","endDelay","timeoutId","metaData","observingProps","Set","properties","Array","isArray","cleanup","clear","clearTimeout","setTimeout","start","isAlreadyObserving","size","add","stop","isAlreadyNotObserving","delete","isObserving","onBecomeObserved","onBecomeUnobserved"],"mappings":"qKAIO,MACLA,KAEA,WAAAC,CAAYD,GACVE,KAAKF,KAAOA,EAEZG,EAAAA,eAAeD,KAAM,CACnBF,KAAMI,EAAAA,WAAWC,KACjBC,IAAKC,EAAAA,QAET,CAEA,GAAAD,CAAIE,GAGF,MAAMC,EAAqBC,OAAOC,KAAKT,KAAKF,MAAMY,IAAKC,GAAQ,CAC7DA,EACAX,KAAKF,KACLQ,IAGF,IAAIM,EAAe,EACfC,EAAcN,EAAMO,OAExB,KAAOF,EAAeC,GAAa,CACjC,MAAOF,EAAKI,EAAoBT,GAAWC,EAAMK,GAC3CI,EAAWV,EAAQK,GACnBM,EAAYF,EAAmBJ,GAIrC,GAFAC,IAEID,KAAOL,EACT,GAAIY,EAAAA,UAAUC,SAASH,IAAaE,EAAAA,UAAUC,SAASF,GAAY,CACjE,MAAMG,EAAeZ,OAAOC,KAAKO,GAEjCR,OAAOC,KAAKQ,GAAWI,QAASC,IACxBA,KAAYN,UACTD,EAAmBJ,GAAKW,KAInCF,EAA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AE9CX,EAAee,OAAOrB,GAEtB,MAAMsB,EAAchB,EAAeW,KAAO,EAEtCG,GAAyBE,GAI7BX,KAaF,OAVAH,EAAW5B,QAASoB,IACdhB,GACFuC,EAAAA,iBAAiBvC,EAASgB,EAAU,IAAMe,EAAMf,IAChDwB,EAAAA,mBAAmBxC,EAASgB,EAAU,IAAMmB,EAAKnB,MAEjDuB,EAAAA,iBAAiBvB,EAAU,IAAMe,EAAMf,IACvCwB,EAAAA,mBAAmBxB,EAAU,IAAMmB,EAAKnB,OAIrCW"}
1
+ {"version":3,"file":"mobx.cjs","sources":["../src/mobx/deep-observable-struct.ts","../src/mobx/apply-observable.ts","../src/mobx/create-enhanced-atom.ts","../src/mobx/create-ref.ts","../src/mobx/get-mobx-administration.ts","../src/mobx/lazy-observe.ts"],"sourcesContent":["import { action, makeObservable, observable } from 'mobx';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport class DeepObservableStruct<TData extends AnyObject> {\n data: TData;\n\n constructor(data: TData) {\n this.data = data;\n\n makeObservable(this, {\n data: observable.deep,\n set: action,\n });\n }\n\n set(newData: Partial<TData>) {\n type StackItem = [key: string, currObservable: AnyObject, new: AnyObject];\n\n const stack: StackItem[] = Object.keys(this.data).map((key) => [\n key,\n this.data,\n newData,\n ]);\n\n let currentIndex = 0;\n let stackLength = stack.length;\n\n while (currentIndex < stackLength) {\n const [key, currObservableData, newData] = stack[currentIndex];\n const newValue = newData[key];\n const currValue = currObservableData[key];\n\n currentIndex++;\n\n if (key in newData) {\n if (typeGuard.isObject(newValue) && typeGuard.isObject(currValue)) {\n const newValueKeys = Object.keys(newValue);\n\n Object.keys(currValue).forEach((childKey) => {\n if (!(childKey in newValue)) {\n delete currObservableData[key][childKey];\n }\n });\n\n newValueKeys.forEach((childKey) => {\n const length = stack.push([\n childKey,\n currObservableData[key],\n newValue,\n ]);\n stackLength = length;\n });\n } else if (newValue !== currValue) {\n currObservableData[key] = newValue;\n }\n } else {\n delete currObservableData[key];\n }\n }\n\n Object.keys(newData).forEach((newDataKey) => {\n if (!this.data[newDataKey]) {\n // @ts-expect-error\n this.data[newDataKey] = newData[newDataKey];\n }\n });\n }\n}\n","import { makeObservable } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport type ObservableAnnotationsArray = [string, any][];\n\nexport const applyObservable = (\n context: AnyObject,\n annotationsArray: ObservableAnnotationsArray,\n useDecorators?: boolean,\n) => {\n if (useDecorators) {\n annotationsArray.forEach(([field, annotation]) => {\n annotation(context, field);\n });\n\n makeObservable(context);\n } else {\n makeObservable(context, Object.fromEntries(annotationsArray));\n }\n};\n","import { createAtom, type IAtom } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport interface IEnhancedAtom<TMeta extends AnyObject = AnyObject>\n extends IAtom {\n meta: TMeta;\n}\n\n/**\n * Creates an enhanced atom with meta data\n * And bind `reportChanged` and `reportObserved` method to the atom\n */\nexport const createEnhancedAtom = <TMeta extends AnyObject>(\n name: string,\n onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n meta?: TMeta,\n): IEnhancedAtom<TMeta> => {\n const atom = createAtom(\n name,\n onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),\n onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom)),\n ) as IEnhancedAtom<TMeta>;\n atom.meta = meta ?? ({} as TMeta);\n atom.reportChanged = atom.reportChanged.bind(atom);\n atom.reportObserved = atom.reportObserved.bind(atom);\n return atom;\n};\n","import { action, makeObservable, observable } from 'mobx';\nimport type { AnyObject, Maybe } from 'yummies/utils/types';\n\n/**\n * Alternative to React.createRef but works in MobX world.\n * Typically it the should be the same React.LegacyRef (fn style)\n */\nexport type Ref<T = any, TMeta = AnyObject> = ((element: Maybe<T>) => void) & {\n current: T | null;\n meta: TMeta;\n};\n\n/**\n * Creates ref thing to attach HTMLElements in React and all other\n */\nexport const createRef = <T = HTMLElement, TMeta = AnyObject>(cfg?: {\n onSet?: (node: T) => void;\n onUnset?: () => void;\n meta?: TMeta;\n}): Ref<T, TMeta> => {\n const actionFn = action((value: T | null) => {\n actionFn.current = value;\n if (actionFn.current) {\n cfg?.onSet?.(actionFn.current);\n } else {\n cfg?.onUnset?.();\n }\n }) as Ref<T, TMeta>;\n\n actionFn.current = null;\n actionFn.meta = cfg?.meta ?? ({} as TMeta);\n\n makeObservable(actionFn, {\n current: observable.ref,\n meta: observable,\n });\n\n return actionFn;\n};\n","import { $mobx, type AnnotationMapEntry } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\ntype ObservableObjectAdministration = Parameters<\n Exclude<AnnotationMapEntry, boolean>['make_']\n>[0];\n\nexport const getMobxAdministration = (\n context: AnyObject,\n): ObservableObjectAdministration => context[$mobx];\n","import { onBecomeObserved, onBecomeUnobserved } from 'mobx';\n\n/**\n * When ONE OF the properties is becomes observed then `onStart` function is called.\n * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.\n *\n * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.\n */\nexport const lazyObserve = <TMetaData = void>({\n context,\n property,\n onStart,\n onEnd,\n endDelay = false,\n}: {\n context?: any;\n property: any | any[];\n onStart?: () => TMetaData;\n onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;\n endDelay?: number | false;\n}) => {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n let metaData: TMetaData | undefined;\n const observingProps = new Set<string>();\n const properties = Array.isArray(property) ? property : [property];\n\n const cleanup = () => {\n observingProps.clear();\n\n if (endDelay === false) {\n onEnd?.(metaData!, cleanup);\n metaData = undefined;\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n timeoutId = setTimeout(() => {\n onEnd?.(metaData!, cleanup);\n timeoutId = undefined;\n metaData = undefined;\n }, endDelay);\n };\n\n const start = (property: string) => {\n const isAlreadyObserving = observingProps.size > 0;\n observingProps.add(property);\n\n if (isAlreadyObserving) {\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n metaData = onStart?.();\n };\n\n const stop = (property: string) => {\n const isAlreadyNotObserving = !observingProps.size;\n\n observingProps.delete(property);\n\n const isObserving = observingProps.size > 0;\n\n if (isAlreadyNotObserving || isObserving) {\n return;\n }\n\n cleanup();\n };\n\n properties.forEach((property) => {\n if (context) {\n onBecomeObserved(context, property, () => start(property));\n onBecomeUnobserved(context, property, () => stop(property));\n } else {\n onBecomeObserved(property, () => start(property));\n onBecomeUnobserved(property, () => stop(property));\n }\n });\n\n return cleanup;\n};\n"],"names":["data","constructor","this","makeObservable","observable","deep","set","action","newData","stack","Object","keys","map","key","currentIndex","stackLength","length","currObservableData","newValue","currValue","typeGuard","isObject","newValueKeys","forEach","childKey","push","newDataKey","context","annotationsArray","useDecorators","field","annotation","fromEntries","name","onBecomeObservedHandler","onBecomeUnobservedHandler","meta","atom","createAtom","reportChanged","bind","reportObserved","cfg","actionFn","value","current","onSet","onUnset","ref","$mobx","property","onStart","onEnd","endDelay","timeoutId","metaData","observingProps","Set","properties","Array","isArray","cleanup","clear","clearTimeout","setTimeout","start","isAlreadyObserving","size","add","stop","isAlreadyNotObserving","delete","isObserving","onBecomeObserved","onBecomeUnobserved"],"mappings":"qKAIO,MACLA,KAEA,WAAAC,CAAYD,GACVE,KAAKF,KAAOA,EAEZG,EAAAA,eAAeD,KAAM,CACnBF,KAAMI,EAAAA,WAAWC,KACjBC,IAAKC,EAAAA,QAET,CAEA,GAAAD,CAAIE,GAGF,MAAMC,EAAqBC,OAAOC,KAAKT,KAAKF,MAAMY,IAAKC,GAAQ,CAC7DA,EACAX,KAAKF,KACLQ,IAGF,IAAIM,EAAe,EACfC,EAAcN,EAAMO,OAExB,KAAOF,EAAeC,GAAa,CACjC,MAAOF,EAAKI,EAAoBT,GAAWC,EAAMK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CC7BPhB,GACmCA,EAAQsB,EAAAA,2BCDlB,EACzBtB,UACAuB,WACAC,UACAC,QACAC,YAAW,MAQX,IAAIC,EACAC,EACJ,MAAMC,MAAqBC,IACrBC,EAAaC,MAAMC,QAAQV,GAAYA,EAAW,CAACA,GAEnDW,EAAU,KAGd,GAFAL,EAAeM,SAEE,IAAbT,EAGF,OAFAD,IAAQG,EAAWM,QACnBN,OAAW,GAITD,IACFS,aAAaT,GACbA,OAAY,GAGdA,EAAYU,WAAW,KACrBZ,IAAQG,EAAWM,GACnBP,OAAY,EACZC,OAAW,GACVF,IAGCY,EAASf,IACb,MAAMgB,EAAqBV,EAAeW,KAAO,EACjDX,EAAeY,IAAIlB,GAEfgB,IAIAZ,IACFS,aAAaT,GACbA,OAAY,GAGdC,EAAWJ,QAGPkB,EAAQnB,IACZ,MAAMoB,GAAyBd,EAAeW,KAE9CX,EAAee,OAAOrB,GAEtB,MAAMsB,EAAchB,EAAeW,KAAO,EAEtCG,GAAyBE,GAI7BX,KAaF,OAVAH,EAAWnC,QAAS2B,IACdvB,GACF8C,EAAAA,iBAAiB9C,EAASuB,EAAU,IAAMe,EAAMf,IAChDwB,EAAAA,mBAAmB/C,EAASuB,EAAU,IAAMmB,EAAKnB,MAEjDuB,EAAAA,iBAAiBvB,EAAU,IAAMe,EAAMf,IACvCwB,EAAAA,mBAAmBxB,EAAU,IAAMmB,EAAKnB,OAIrCW"}
package/mobx.js CHANGED
@@ -21,6 +21,23 @@ const createEnhancedAtom = (name, onBecomeObservedHandler, onBecomeUnobservedHan
21
21
  atom.reportObserved = atom.reportObserved.bind(atom);
22
22
  return atom;
23
23
  };
24
+ const createRef = (cfg) => {
25
+ const actionFn = action((value) => {
26
+ actionFn.current = value;
27
+ if (actionFn.current) {
28
+ cfg?.onSet?.(actionFn.current);
29
+ } else {
30
+ cfg?.onUnset?.();
31
+ }
32
+ });
33
+ actionFn.current = null;
34
+ actionFn.meta = cfg?.meta ?? {};
35
+ makeObservable(actionFn, {
36
+ current: observable.ref,
37
+ meta: observable
38
+ });
39
+ return actionFn;
40
+ };
24
41
  class DeepObservableStruct {
25
42
  data;
26
43
  constructor(data) {
@@ -138,6 +155,7 @@ export {
138
155
  DeepObservableStruct,
139
156
  applyObservable,
140
157
  createEnhancedAtom,
158
+ createRef,
141
159
  getMobxAdministration,
142
160
  lazyObserve
143
161
  };
package/mobx.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mobx.js","sources":["../src/mobx/apply-observable.ts","../src/mobx/create-enhanced-atom.ts","../src/mobx/deep-observable-struct.ts","../src/mobx/get-mobx-administration.ts","../src/mobx/lazy-observe.ts"],"sourcesContent":["import { makeObservable } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport type ObservableAnnotationsArray = [string, any][];\n\nexport const applyObservable = (\n context: AnyObject,\n annotationsArray: ObservableAnnotationsArray,\n useDecorators?: boolean,\n) => {\n if (useDecorators) {\n annotationsArray.forEach(([field, annotation]) => {\n annotation(context, field);\n });\n\n makeObservable(context);\n } else {\n makeObservable(context, Object.fromEntries(annotationsArray));\n }\n};\n","import { createAtom, type IAtom } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport interface IEnhancedAtom<TMeta extends AnyObject = AnyObject>\n extends IAtom {\n meta: TMeta;\n}\n\n/**\n * Creates an enhanced atom with meta data\n * And bind `reportChanged` and `reportObserved` method to the atom\n */\nexport const createEnhancedAtom = <TMeta extends AnyObject>(\n name: string,\n onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n meta?: TMeta,\n): IEnhancedAtom<TMeta> => {\n const atom = createAtom(\n name,\n onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),\n onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom)),\n ) as IEnhancedAtom<TMeta>;\n atom.meta = meta ?? ({} as TMeta);\n atom.reportChanged = atom.reportChanged.bind(atom);\n atom.reportObserved = atom.reportObserved.bind(atom);\n return atom;\n};\n","import { action, makeObservable, observable } from 'mobx';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport class DeepObservableStruct<TData extends AnyObject> {\n data: TData;\n\n constructor(data: TData) {\n this.data = data;\n\n makeObservable(this, {\n data: observable.deep,\n set: action,\n });\n }\n\n set(newData: Partial<TData>) {\n type StackItem = [key: string, currObservable: AnyObject, new: AnyObject];\n\n const stack: StackItem[] = Object.keys(this.data).map((key) => [\n key,\n this.data,\n newData,\n ]);\n\n let currentIndex = 0;\n let stackLength = stack.length;\n\n while (currentIndex < stackLength) {\n const [key, currObservableData, newData] = stack[currentIndex];\n const newValue = newData[key];\n const currValue = currObservableData[key];\n\n currentIndex++;\n\n if (key in newData) {\n if (typeGuard.isObject(newValue) && typeGuard.isObject(currValue)) {\n const newValueKeys = Object.keys(newValue);\n\n Object.keys(currValue).forEach((childKey) => {\n if (!(childKey in newValue)) {\n delete currObservableData[key][childKey];\n }\n });\n\n newValueKeys.forEach((childKey) => {\n const length = stack.push([\n childKey,\n currObservableData[key],\n newValue,\n ]);\n stackLength = length;\n });\n } else if (newValue !== currValue) {\n currObservableData[key] = newValue;\n }\n } else {\n delete currObservableData[key];\n }\n }\n\n Object.keys(newData).forEach((newDataKey) => {\n if (!this.data[newDataKey]) {\n // @ts-expect-error\n this.data[newDataKey] = newData[newDataKey];\n }\n });\n }\n}\n","import { $mobx, type AnnotationMapEntry } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\ntype ObservableObjectAdministration = Parameters<\n Exclude<AnnotationMapEntry, boolean>['make_']\n>[0];\n\nexport const getMobxAdministration = (\n context: AnyObject,\n): ObservableObjectAdministration => context[$mobx];\n","import { onBecomeObserved, onBecomeUnobserved } from 'mobx';\n\n/**\n * When ONE OF the properties is becomes observed then `onStart` function is called.\n * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.\n *\n * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.\n */\nexport const lazyObserve = <TMetaData = void>({\n context,\n property,\n onStart,\n onEnd,\n endDelay = false,\n}: {\n context?: any;\n property: any | any[];\n onStart?: () => TMetaData;\n onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;\n endDelay?: number | false;\n}) => {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n let metaData: TMetaData | undefined;\n const observingProps = new Set<string>();\n const properties = Array.isArray(property) ? property : [property];\n\n const cleanup = () => {\n observingProps.clear();\n\n if (endDelay === false) {\n onEnd?.(metaData!, cleanup);\n metaData = undefined;\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n timeoutId = setTimeout(() => {\n onEnd?.(metaData!, cleanup);\n timeoutId = undefined;\n metaData = undefined;\n }, endDelay);\n };\n\n const start = (property: string) => {\n const isAlreadyObserving = observingProps.size > 0;\n observingProps.add(property);\n\n if (isAlreadyObserving) {\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n metaData = onStart?.();\n };\n\n const stop = (property: string) => {\n const isAlreadyNotObserving = !observingProps.size;\n\n observingProps.delete(property);\n\n const isObserving = observingProps.size > 0;\n\n if (isAlreadyNotObserving || isObserving) {\n return;\n }\n\n cleanup();\n };\n\n properties.forEach((property) => {\n if (context) {\n onBecomeObserved(context, property, () => start(property));\n onBecomeUnobserved(context, property, () => stop(property));\n } else {\n onBecomeObserved(property, () => start(property));\n onBecomeUnobserved(property, () => stop(property));\n }\n });\n\n return cleanup;\n};\n"],"names":["newData","property"],"mappings":";;AAKO,MAAM,kBAAkB,CAC7B,SACA,kBACA,kBACG;AACH,MAAI,eAAe;AACjB,qBAAiB,QAAQ,CAAC,CAAC,OAAO,UAAU,MAAM;AAChD,iBAAW,SAAS,KAAK;AAAA,IAC3B,CAAC;AAED,mBAAe,OAAO;AAAA,EACxB,OAAO;AACL,mBAAe,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CAAC,eAAe;AAE9C,mBAAe,OAAOA,SAAQ;AAE9B,UAAM,cAAc,eAAe,OAAO;AAE1C,QAAI,yBAAyB,aAAa;AACxC;AAAA,IACF;AAEA,YAAA;AAAA,EACF;AAEA,aAAW,QAAQ,CAACA,cAAa;AAC/B,QAAI,SAAS;AACX,uBAAiB,SAASA,WAAU,MAAM,MAAMA,SAAQ,CAAC;AACzD,yBAAmB,SAASA,WAAU,MAAM,KAAKA,SAAQ,CAAC;AAAA,IAC5D,OAAO;AACL,uBAAiBA,WAAU,MAAM,MAAMA,SAAQ,CAAC;AAChD,yBAAmBA,WAAU,MAAM,KAAKA,SAAQ,CAAC;AAAA,IACnD;AAAA,EACF,CAAC;AAED,SAAO;AACT;"}
1
+ {"version":3,"file":"mobx.js","sources":["../src/mobx/apply-observable.ts","../src/mobx/create-enhanced-atom.ts","../src/mobx/create-ref.ts","../src/mobx/deep-observable-struct.ts","../src/mobx/get-mobx-administration.ts","../src/mobx/lazy-observe.ts"],"sourcesContent":["import { makeObservable } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport type ObservableAnnotationsArray = [string, any][];\n\nexport const applyObservable = (\n context: AnyObject,\n annotationsArray: ObservableAnnotationsArray,\n useDecorators?: boolean,\n) => {\n if (useDecorators) {\n annotationsArray.forEach(([field, annotation]) => {\n annotation(context, field);\n });\n\n makeObservable(context);\n } else {\n makeObservable(context, Object.fromEntries(annotationsArray));\n }\n};\n","import { createAtom, type IAtom } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport interface IEnhancedAtom<TMeta extends AnyObject = AnyObject>\n extends IAtom {\n meta: TMeta;\n}\n\n/**\n * Creates an enhanced atom with meta data\n * And bind `reportChanged` and `reportObserved` method to the atom\n */\nexport const createEnhancedAtom = <TMeta extends AnyObject>(\n name: string,\n onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n meta?: TMeta,\n): IEnhancedAtom<TMeta> => {\n const atom = createAtom(\n name,\n onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),\n onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom)),\n ) as IEnhancedAtom<TMeta>;\n atom.meta = meta ?? ({} as TMeta);\n atom.reportChanged = atom.reportChanged.bind(atom);\n atom.reportObserved = atom.reportObserved.bind(atom);\n return atom;\n};\n","import { action, makeObservable, observable } from 'mobx';\nimport type { AnyObject, Maybe } from 'yummies/utils/types';\n\n/**\n * Alternative to React.createRef but works in MobX world.\n * Typically it the should be the same React.LegacyRef (fn style)\n */\nexport type Ref<T = any, TMeta = AnyObject> = ((element: Maybe<T>) => void) & {\n current: T | null;\n meta: TMeta;\n};\n\n/**\n * Creates ref thing to attach HTMLElements in React and all other\n */\nexport const createRef = <T = HTMLElement, TMeta = AnyObject>(cfg?: {\n onSet?: (node: T) => void;\n onUnset?: () => void;\n meta?: TMeta;\n}): Ref<T, TMeta> => {\n const actionFn = action((value: T | null) => {\n actionFn.current = value;\n if (actionFn.current) {\n cfg?.onSet?.(actionFn.current);\n } else {\n cfg?.onUnset?.();\n }\n }) as Ref<T, TMeta>;\n\n actionFn.current = null;\n actionFn.meta = cfg?.meta ?? ({} as TMeta);\n\n makeObservable(actionFn, {\n current: observable.ref,\n meta: observable,\n });\n\n return actionFn;\n};\n","import { action, makeObservable, observable } from 'mobx';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport class DeepObservableStruct<TData extends AnyObject> {\n data: TData;\n\n constructor(data: TData) {\n this.data = data;\n\n makeObservable(this, {\n data: observable.deep,\n set: action,\n });\n }\n\n set(newData: Partial<TData>) {\n type StackItem = [key: string, currObservable: AnyObject, new: AnyObject];\n\n const stack: StackItem[] = Object.keys(this.data).map((key) => [\n key,\n this.data,\n newData,\n ]);\n\n let currentIndex = 0;\n let stackLength = stack.length;\n\n while (currentIndex < stackLength) {\n const [key, currObservableData, newData] = stack[currentIndex];\n const newValue = newData[key];\n const currValue = currObservableData[key];\n\n currentIndex++;\n\n if (key in newData) {\n if (typeGuard.isObject(newValue) && typeGuard.isObject(currValue)) {\n const newValueKeys = Object.keys(newValue);\n\n Object.keys(currValue).forEach((childKey) => {\n if (!(childKey in newValue)) {\n delete currObservableData[key][childKey];\n }\n });\n\n newValueKeys.forEach((childKey) => {\n const length = stack.push([\n childKey,\n currObservableData[key],\n newValue,\n ]);\n stackLength = length;\n });\n } else if (newValue !== currValue) {\n currObservableData[key] = newValue;\n }\n } else {\n delete currObservableData[key];\n }\n }\n\n Object.keys(newData).forEach((newDataKey) => {\n if (!this.data[newDataKey]) {\n // @ts-expect-error\n this.data[newDataKey] = newData[newDataKey];\n }\n });\n }\n}\n","import { $mobx, type AnnotationMapEntry } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\ntype ObservableObjectAdministration = Parameters<\n Exclude<AnnotationMapEntry, boolean>['make_']\n>[0];\n\nexport const getMobxAdministration = (\n context: AnyObject,\n): ObservableObjectAdministration => context[$mobx];\n","import { onBecomeObserved, onBecomeUnobserved } from 'mobx';\n\n/**\n * When ONE OF the properties is becomes observed then `onStart` function is called.\n * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.\n *\n * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.\n */\nexport const lazyObserve = <TMetaData = void>({\n context,\n property,\n onStart,\n onEnd,\n endDelay = false,\n}: {\n context?: any;\n property: any | any[];\n onStart?: () => TMetaData;\n onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;\n endDelay?: number | false;\n}) => {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n let metaData: TMetaData | undefined;\n const observingProps = new Set<string>();\n const properties = Array.isArray(property) ? property : [property];\n\n const cleanup = () => {\n observingProps.clear();\n\n if (endDelay === false) {\n onEnd?.(metaData!, cleanup);\n metaData = undefined;\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n timeoutId = setTimeout(() => {\n onEnd?.(metaData!, cleanup);\n timeoutId = undefined;\n metaData = undefined;\n }, endDelay);\n };\n\n const start = (property: string) => {\n const isAlreadyObserving = observingProps.size > 0;\n observingProps.add(property);\n\n if (isAlreadyObserving) {\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n metaData = onStart?.();\n };\n\n const stop = (property: string) => {\n const isAlreadyNotObserving = !observingProps.size;\n\n observingProps.delete(property);\n\n const isObserving = observingProps.size > 0;\n\n if (isAlreadyNotObserving || isObserving) {\n return;\n }\n\n cleanup();\n };\n\n properties.forEach((property) => {\n if (context) {\n onBecomeObserved(context, property, () => start(property));\n onBecomeUnobserved(context, property, () => stop(property));\n } else {\n onBecomeObserved(property, () => start(property));\n onBecomeUnobserved(property, () => stop(property));\n }\n });\n\n return cleanup;\n};\n"],"names":["newData","property"],"mappings":";;AAKO,MAAM,kBAAkB,CAC7B,SACA,kBACA,kBACG;AACH,MAAI,eAAe;AACjB,qBAAiB,QAAQ,CAAC,CAAC,OAAO,UAAU,MAAM;AAChD,iBAAW,SAAS,KAAK;AAAA,IAC3B,CAAC;AAED,mBAAe,OAAO;AAAA,EACxB,OAAO;AACL,mBAAe,SAAS,OAAO,YAAY,gBAAgB,CAAC;AAAA,EAC9D;AACF;ACPO,MAAM,qBAAqB,CAChC,MACA,yBACA,2BACA,SACyB;AACzB,QAAM,OAAO;AAAA,IACX;AAAA,IACA,4BAA4B,MAAM,wBAAwB,IAAI;AAAA,IAC9D,8BAA8B,MAAM,0BAA0B,IAAI;AAAA,EAAA;AAEpE,OAAK,OAAO,QAAS,CAAA;AACrB,OAAK,gBAAgB,KAAK,cAAc,KAAK,IAAI;AACjD,OAAK,iBAAiB,KAAK,eAAe,KAAK,IAAI;AACnD,SAAO;AACT;ACZO,MAAM,YAAY,CAAqC,QAIzC;AACnB,QAAM,WAAW,OAAO,CAAC,UAAoB;AA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