yummies 5.12.1 → 5.14.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.
- package/mobx/create-ref.d.ts +22 -0
- package/mobx/create-ref.d.ts.map +1 -0
- package/mobx/index.d.ts +1 -0
- package/mobx/index.d.ts.map +1 -1
- package/mobx.cjs +1 -1
- package/mobx.cjs.map +1 -1
- package/mobx.js +30 -0
- package/mobx.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { AnyObject, Maybe } from '../utils/types.ts';
|
|
2
|
+
export type RefChangeListener<T> = (value: T | null) => void;
|
|
3
|
+
/**
|
|
4
|
+
* Alternative to React.createRef but works in MobX world.
|
|
5
|
+
* Typically it the should be the same React.LegacyRef (fn style)
|
|
6
|
+
*/
|
|
7
|
+
export type Ref<T = any, TMeta = AnyObject> = ((element: Maybe<T>) => void) & {
|
|
8
|
+
listeners: RefChangeListener<T>[];
|
|
9
|
+
current: T | null;
|
|
10
|
+
meta: TMeta;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Creates ref thing to attach HTMLElements in React and all other
|
|
14
|
+
*/
|
|
15
|
+
export declare const createRef: <T = HTMLElement, TMeta = AnyObject>(cfg?: {
|
|
16
|
+
onSet?: (node: T) => void;
|
|
17
|
+
onUnset?: () => void;
|
|
18
|
+
onChange?: RefChangeListener<T>;
|
|
19
|
+
meta?: TMeta;
|
|
20
|
+
}) => Ref<T, TMeta>;
|
|
21
|
+
export declare const isRef: <T, TMeta = AnyObject>(value: any) => value is Ref<T, TMeta>;
|
|
22
|
+
//# 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,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,KAAK,IAAI,CAAC;AAE7D;;;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,SAAS,EAAE,iBAAiB,CAAC,CAAC,CAAC,EAAE,CAAC;IAClC,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,QAAQ,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC;IAChC,IAAI,CAAC,EAAE,KAAK,CAAC;CACd,KAAG,GAAG,CAAC,CAAC,EAAE,KAAK,CA8Bf,CAAC;AAEF,eAAO,MAAM,KAAK,GAAI,CAAC,EAAE,KAAK,GAAG,SAAS,EACxC,OAAO,GAAG,KACT,KAAK,IAAI,GAAG,CAAC,CAAC,EAAE,KAAK,CAEvB,CAAC"}
|
package/mobx/index.d.ts
CHANGED
package/mobx/index.d.ts.map
CHANGED
|
@@ -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
|
|
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,n,a]=r[o],c=a[e],i=n[e];if(o++,e in a)if(t.typeGuard.isObject(c)&&t.typeGuard.isObject(i)){const t=Object.keys(c);Object.keys(i).forEach(t=>{t in c||delete n[e][t]}),t.forEach(t=>{const o=r.push([t,n[e],c]);s=o})}else c!==i&&(n[e]=c);else delete n[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 n=e.createAtom(t,r&&(()=>r(n)),o&&(()=>o(n)));return n.meta=s??{},n.reportChanged=n.reportChanged.bind(n),n.reportObserved=n.reportObserved.bind(n),n},exports.createRef=t=>{const r=e.action(e=>{r.current=e??null,r.listeners.forEach(e=>{e(r.current)})});return r.listeners=t?.onChange?[t.onChange]:[],(t?.onSet||t?.onUnset)&&r.listeners.push(e=>{e?t.onSet?.(e):t.onUnset?.()}),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.isRef=e=>"function"==typeof e&&"current"in e,exports.lazyObserve=({context:t,property:r,onStart:o,onEnd:s,endDelay:n=!1})=>{let a,c;const i=new Set,b=Array.isArray(r)?r:[r],d=()=>{if(i.clear(),!1===n)return s?.(c,d),void(c=void 0);a&&(clearTimeout(a),a=void 0),a=setTimeout(()=>{s?.(c,d),a=void 0,c=void 0},n)},l=e=>{const t=i.size>0;i.add(e),t||(a&&(clearTimeout(a),a=void 0),c=o?.())},u=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,()=>u(r))):(e.onBecomeObserved(r,()=>l(r)),e.onBecomeUnobserved(r,()=>u(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,EAAaC,QA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,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\nexport type RefChangeListener<T> = (value: T | null) => void;\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 listeners: RefChangeListener<T>[];\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 onChange?: RefChangeListener<T>;\n meta?: TMeta;\n}): Ref<T, TMeta> => {\n const actionFn = action((value: Maybe<T>) => {\n actionFn.current = value ?? null;\n\n actionFn.listeners.forEach((listener) => {\n listener(actionFn.current);\n });\n }) as Ref<T, TMeta>;\n\n actionFn.listeners = cfg?.onChange ? [cfg.onChange] : [];\n\n if (cfg?.onSet || cfg?.onUnset) {\n actionFn.listeners.push((value) => {\n if (value) {\n cfg.onSet?.(value);\n } else {\n cfg.onUnset?.();\n }\n });\n }\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\nexport const isRef = <T, TMeta = AnyObject>(\n value: any,\n): value is Ref<T, TMeta> => {\n return typeof value === 'function' && 'current' in value;\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","listeners","listener","onChange","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,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AAMf,IAChDwB,EAAAA,mBAAmBlD,EAAS0B,EAAU,IAAMmB,EAAKnB,MAEjDuB,EAAAA,iBAAiBvB,EAAU,IAAMe,EAAMf,IACvCwB,EAAAA,mBAAmBxB,EAAU,IAAMmB,EAAKnB,OAIrCW"}
|
package/mobx.js
CHANGED
|
@@ -21,6 +21,34 @@ 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 ?? null;
|
|
27
|
+
actionFn.listeners.forEach((listener) => {
|
|
28
|
+
listener(actionFn.current);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
actionFn.listeners = cfg?.onChange ? [cfg.onChange] : [];
|
|
32
|
+
if (cfg?.onSet || cfg?.onUnset) {
|
|
33
|
+
actionFn.listeners.push((value) => {
|
|
34
|
+
if (value) {
|
|
35
|
+
cfg.onSet?.(value);
|
|
36
|
+
} else {
|
|
37
|
+
cfg.onUnset?.();
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
actionFn.current = null;
|
|
42
|
+
actionFn.meta = cfg?.meta ?? {};
|
|
43
|
+
makeObservable(actionFn, {
|
|
44
|
+
current: observable.ref,
|
|
45
|
+
meta: observable
|
|
46
|
+
});
|
|
47
|
+
return actionFn;
|
|
48
|
+
};
|
|
49
|
+
const isRef = (value) => {
|
|
50
|
+
return typeof value === "function" && "current" in value;
|
|
51
|
+
};
|
|
24
52
|
class DeepObservableStruct {
|
|
25
53
|
data;
|
|
26
54
|
constructor(data) {
|
|
@@ -138,7 +166,9 @@ export {
|
|
|
138
166
|
DeepObservableStruct,
|
|
139
167
|
applyObservable,
|
|
140
168
|
createEnhancedAtom,
|
|
169
|
+
createRef,
|
|
141
170
|
getMobxAdministration,
|
|
171
|
+
isRef,
|
|
142
172
|
lazyObserve
|
|
143
173
|
};
|
|
144
174
|
//# sourceMappingURL=mobx.js.map
|
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,SAAS,OAAO,YAAY,gBAAgB,CAAC;AAAA,EAC9D;AACF;ACPO,MAAM,qBAAqB,CAChC,MACA,yBACA,2BACA,SACyB;AACzB,QAAM,OAAO;AAAA,IACX;AAA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A,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\nexport type RefChangeListener<T> = (value: T | null) => void;\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 listeners: RefChangeListener<T>[];\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 onChange?: RefChangeListener<T>;\n meta?: TMeta;\n}): Ref<T, TMeta> => {\n const actionFn = action((value: Maybe<T>) => {\n actionFn.current = value ?? null;\n\n actionFn.listeners.forEach((listener) => {\n listener(actionFn.current);\n });\n }) as Ref<T, TMeta>;\n\n actionFn.listeners = cfg?.onChange ? [cfg.onChange] : [];\n\n if (cfg?.onSet || cfg?.onUnset) {\n actionFn.listeners.push((value) => {\n if (value) {\n cfg.onSet?.(value);\n } else {\n cfg.onUnset?.();\n }\n });\n }\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\nexport const isRef = <T, TMeta = AnyObject>(\n value: any,\n): value is Ref<T, TMeta> => {\n return typeof value === 'function' && 'current' in value;\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,CAA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