@madronejs/core 1.0.16 → 1.0.18

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/README.md CHANGED
@@ -1,92 +1,4 @@
1
1
  # madrone (mah droh nuh)
2
2
  🌳
3
3
 
4
- Madrone is an easy way to make reactive objects in JS.
5
-
6
- ## Installation
7
- ```javascript
8
- // npm
9
- npm install madronejs --save
10
-
11
- // pnpm
12
- pnpm install madronejs
13
-
14
- // yarn
15
- yarn add madronejs
16
- ```
17
-
18
- ## Quick start
19
-
20
- ### Reactivity
21
-
22
- ```javascript
23
- import Madrone, { MadroneState } from '@madronejs/core'
24
-
25
- // add reactivity integration
26
- // MadroneVue2, and MadroneVue3 are also available
27
- Madrone.use(MadroneState);
28
-
29
- const PersonFactory = ({ name } = {}) => Madrone.auto({
30
- // When using reactivity integration, getters become cached computeds
31
- // that only get recomputed when a reactive data property changes.
32
- // In this case, `name` is a reactive data property.
33
- name,
34
- get greeting() {
35
- return `Hi, I'm ${this.name}`
36
- },
37
- });
38
-
39
- const person = PersonFactory({ name: 'Greg' });
40
- const newVals = [];
41
- const oldVals = [];
42
-
43
- // Watch a reactive property when it changes. Any reactive property accessed
44
- // in the first argument will cause the watcher callback to trigger. Anything
45
- // returned from the first argument will define what the newVal/oldVal is.
46
- Madrone.watch(() => person.greeting, (newVal, oldVal) => {
47
- newVals.push(newVal);
48
- oldVals.push(oldVal);
49
- });
50
-
51
- person.name; // Greg
52
- person.greeting; // Hi, I'm Greg
53
-
54
- person.name = 'Not Greg';
55
- person.greeting; // Hi, I'm Not Greg
56
-
57
- // watcher is async...
58
- console.log('New Vals:', newVals); // ["Hi, I'm Not Greg"]
59
- console.log('Old Vals:', oldVals); // ["Hi, I'm Greg"]
60
- ```
61
-
62
- ### Decorator support
63
-
64
- ```javascript
65
-
66
- import Madrone, { MadroneState, computed, reactive } from '@madronejs/core'
67
-
68
- Madrone.use(MadroneState);
69
-
70
- class Person {
71
- @reactive name: string;
72
- @reactive age;
73
-
74
- @computed get greeting() {
75
- return `Hi, I'm ${this.name}`;
76
- }
77
-
78
- constructor(options) {
79
- this.name = options?.name;
80
- this.age = options?.age;
81
- }
82
- }
83
-
84
- const person = new Person({ name: 'Greg' });
85
-
86
-
87
- person.name; // Greg
88
- person.greeting; // Hi, I'm Greg
89
-
90
- person.name = 'Not Greg';
91
- person.greeting; // Hi, I'm Not Greg
92
- ```
4
+ Please follow the docs at https://madronejs.github.io/docs/core/
package/dist/core.js ADDED
@@ -0,0 +1,528 @@
1
+ const A = /* @__PURE__ */ new Set();
2
+ let x;
3
+ function he() {
4
+ return [...A];
5
+ }
6
+ function pe() {
7
+ return he().at(-1);
8
+ }
9
+ function Y() {
10
+ x = pe();
11
+ }
12
+ function $(e) {
13
+ e && (A.add(e), Y());
14
+ }
15
+ function ge(e) {
16
+ A.delete(e), Y();
17
+ }
18
+ function m() {
19
+ return x;
20
+ }
21
+ const K = /* @__PURE__ */ new WeakMap();
22
+ function U(e) {
23
+ return (m()?.toRaw ?? (() => e))(e);
24
+ }
25
+ function w(e) {
26
+ K.set(U(e), Date.now());
27
+ }
28
+ function me(e) {
29
+ return K.get(U(e));
30
+ }
31
+ function y(e, t, n) {
32
+ const r = m();
33
+ if (!r)
34
+ throw new Error("No integration specified");
35
+ typeof n.get == "function" && r?.defineComputed ? r.defineComputed(e, t, {
36
+ get: n.get.bind(e),
37
+ set: n.set?.bind(e),
38
+ enumerable: n.enumerable,
39
+ configurable: n.configurable,
40
+ cache: n.cache ?? !0
41
+ }) : !n.get && r?.defineProperty && r.defineProperty(e, t, {
42
+ value: n.value,
43
+ enumerable: n.enumerable,
44
+ configurable: n.configurable,
45
+ deep: n.deep
46
+ });
47
+ }
48
+ function be(e, t) {
49
+ const n = Object.getOwnPropertyDescriptors(e), r = (o, c) => t?.[o]?.[c];
50
+ for (const [o, c] of Object.entries(n))
51
+ y(e, o, {
52
+ get: c.get?.bind(e),
53
+ set: c.set?.bind(e),
54
+ value: r(o, "value") ?? c.value,
55
+ enumerable: r(o, "enumerable") ?? c.enumerable,
56
+ configurable: r(o, "configurable") ?? c.configurable,
57
+ cache: r(o, "cache") ?? !0,
58
+ deep: r(o, "deep") ?? !0
59
+ });
60
+ return e;
61
+ }
62
+ function ve(e, t, n) {
63
+ return m()?.watch?.(e, t, n);
64
+ }
65
+ const D = Symbol("keys"), R = Symbol("computed"), H = /* @__PURE__ */ new WeakMap(), M = /* @__PURE__ */ new WeakMap(), h = /* @__PURE__ */ new WeakMap(), p = /* @__PURE__ */ new WeakMap();
66
+ let I = [], S = null;
67
+ const we = (e) => H.has(e), X = (e) => M.has(e), G = (e) => H.get(e), Oe = (e) => M.get(e), ye = (e) => X(e) ? Oe(e) : e, Ce = (e, t) => {
68
+ H.set(e, t), M.set(t, e);
69
+ }, Re = () => {
70
+ S === null && (S = Symbol("scheduler"), setTimeout(() => {
71
+ const e = I;
72
+ for (I = []; e.length > 0; )
73
+ e.shift()();
74
+ S = null;
75
+ }));
76
+ }, Se = (e) => {
77
+ I.push(e), Re();
78
+ }, z = (e, t) => {
79
+ const n = p.get(e), r = n?.get(t);
80
+ if (r) {
81
+ for (const o of r)
82
+ h.get(o).delete(e);
83
+ r.clear(), n.delete(t), n.size === 0 && p.delete(e);
84
+ }
85
+ }, k = (e, t) => {
86
+ const n = Te();
87
+ if (!n) return;
88
+ p.has(n) || p.set(n, /* @__PURE__ */ new Map()), h.has(e) || h.set(e, /* @__PURE__ */ new Map());
89
+ const r = h.get(e), o = p.get(n);
90
+ r.has(t) || r.set(t, /* @__PURE__ */ new Set()), o.has(t) || o.set(t, /* @__PURE__ */ new Set());
91
+ const c = r.get(t), i = o.get(t);
92
+ c.add(n), i.add(e);
93
+ }, q = (e, t) => {
94
+ const n = G(e);
95
+ n && k(n, t);
96
+ }, F = (e, t) => {
97
+ const n = h.get(e);
98
+ if (n?.get(t))
99
+ for (const r of n.get(t))
100
+ r.setDirty(), z(r, t);
101
+ }, g = (e, t) => {
102
+ F(G(e), t);
103
+ }, _ = [];
104
+ function Te() {
105
+ return _.at(-1);
106
+ }
107
+ class N {
108
+ static create(...t) {
109
+ return new N(...t);
110
+ }
111
+ constructor(t) {
112
+ this.name = t.name, this.get = t.get, this.set = t.set, this.cache = !!(t.cache ?? !0), this.alive = !0, this.dirty = !0, this.cachedVal = void 0, this.hooks = {
113
+ onGet: t.onGet,
114
+ onSet: t.onSet,
115
+ onChange: t.onChange,
116
+ onImmediateChange: t.onImmediateChange
117
+ };
118
+ }
119
+ callHook(t) {
120
+ typeof this.hooks[t] == "function" && this.hooks[t](this);
121
+ }
122
+ /**
123
+ * Stop observing and dispose of the observer
124
+ * @returns {void}
125
+ */
126
+ dispose() {
127
+ z(this, R), this.alive = !1, this.dirty = !1, this.cachedVal = void 0, this.prev = void 0;
128
+ }
129
+ wrap(t) {
130
+ _.push(this);
131
+ const n = t();
132
+ return _.pop(), n;
133
+ }
134
+ setDirty() {
135
+ this.alive && !this.dirty && (this.dirty = !0, F(this, R), this.prev = this.cachedVal, this.cachedVal = void 0, this.callHook(
136
+ "onImmediateChange"
137
+ /* onImmediateChange */
138
+ ), Se(() => this.notifyChange()));
139
+ }
140
+ notifyChange() {
141
+ this.callHook(
142
+ "onChange"
143
+ /* onChange */
144
+ ), this.prev = void 0;
145
+ }
146
+ run() {
147
+ if (!this.alive) return;
148
+ const t = this.wrap(() => ((this.cache && this.dirty || !this.cache) && (this.cachedVal = this.get(), this.dirty = !1), this.cachedVal));
149
+ return k(this, R), this.callHook(
150
+ "onGet"
151
+ /* onGet */
152
+ ), t;
153
+ }
154
+ /** The value of the observer */
155
+ get value() {
156
+ return this.run();
157
+ }
158
+ set value(t) {
159
+ if (typeof this.set == "function")
160
+ this.set(t), this.callHook(
161
+ "onSet"
162
+ /* onSet */
163
+ );
164
+ else
165
+ throw new TypeError(`No setter defined for "${this.name}"`);
166
+ }
167
+ }
168
+ function Q(...e) {
169
+ return N.create(...e);
170
+ }
171
+ function Pe(e) {
172
+ return Q(e);
173
+ }
174
+ const C = (e) => {
175
+ const {
176
+ name: t,
177
+ target: n,
178
+ key: r,
179
+ receiver: o,
180
+ value: c,
181
+ keysChanged: i = !1,
182
+ valueChanged: f = !1
183
+ } = e;
184
+ return {
185
+ name: t,
186
+ target: n,
187
+ key: r,
188
+ receiver: o,
189
+ value: c,
190
+ keysChanged: i,
191
+ valueChanged: f
192
+ };
193
+ }, Ie = (e, t, n, r) => {
194
+ q(t, n), e?.onGet?.(
195
+ C({
196
+ name: e.name,
197
+ target: t,
198
+ key: n,
199
+ receiver: r
200
+ })
201
+ );
202
+ }, _e = (e, t, n, r) => {
203
+ const o = t[n], c = Array.isArray(t);
204
+ let i = !1, f = !1;
205
+ n in t || (g(t, D), f = !0, c && g(t, "length")), (o !== r || c) && (g(t, n), i = !0), (f || i) && e?.onSet?.(
206
+ C({
207
+ name: e.name,
208
+ target: t,
209
+ key: n,
210
+ value: r,
211
+ keysChanged: f,
212
+ valueChanged: i
213
+ })
214
+ );
215
+ }, Ee = (e, t, n) => {
216
+ g(t, n), g(t, D), e?.onDelete?.(
217
+ C({
218
+ name: e.name,
219
+ target: t,
220
+ key: n,
221
+ keysChanged: !0
222
+ })
223
+ );
224
+ }, B = (e, t, n) => {
225
+ q(t, D), e?.onHas?.(
226
+ C({
227
+ name: e.name,
228
+ target: t,
229
+ key: n
230
+ })
231
+ );
232
+ };
233
+ function J(e) {
234
+ const t = e.needsProxy || (() => !0);
235
+ return {
236
+ get: (n, r, o) => {
237
+ Ie(e, n, r, o);
238
+ const c = Reflect.get(n, r, o);
239
+ return t({ target: n, key: r, value: c }) && e?.deep && Object.getOwnPropertyDescriptor(n, r)?.configurable ? d(c, e) : c;
240
+ },
241
+ set: (n, r, o) => (_e(e, n, r, o), Reflect.set(n, r, o)),
242
+ deleteProperty: (...n) => (Ee(e, ...n), Reflect.deleteProperty(...n)),
243
+ has: (n, r) => (B(e, n), Reflect.has(n, r)),
244
+ ownKeys: (n) => (B(e, n), Reflect.ownKeys(n)),
245
+ getPrototypeOf: (n) => Object.getPrototypeOf(n)
246
+ };
247
+ }
248
+ const Ae = (e) => ({
249
+ ...J(e)
250
+ }), De = (e) => ({
251
+ ...J(e)
252
+ }), Z = Object.freeze({
253
+ object: Ae,
254
+ array: De
255
+ // set: setHandler,
256
+ // map: mapHandler,
257
+ // weakset: weaksetHandler,
258
+ // weakmap: weakmapHandler,
259
+ });
260
+ function d(e, t) {
261
+ if (we(e)) return G(e);
262
+ if (X(e)) return e;
263
+ const n = t || {}, r = { ...n, deep: n?.deep ?? !0 }, o = d.getStringType(e);
264
+ if (!d.hasHandler(o)) return e;
265
+ const c = new Proxy(e, d.typeHandler(o, r));
266
+ return Ce(e, c), c;
267
+ }
268
+ d.getStringType = (e) => Object.prototype.toString.call(e).slice(8, -1).toLowerCase();
269
+ d.hasHandler = (e) => !!Z[e];
270
+ d.typeHandler = (e, t) => Z[e]?.(t);
271
+ function ee(e, t, n) {
272
+ const r = Q({
273
+ get: e,
274
+ onChange: ({ value: c, prev: i }) => t(c, i)
275
+ }), o = r.run();
276
+ return n?.immediate && t(o), () => r.dispose();
277
+ }
278
+ function V(e, t, n) {
279
+ let r, o;
280
+ if (t.cache) {
281
+ const c = Pe({
282
+ ...t,
283
+ get: t.get,
284
+ name: e,
285
+ onImmediateChange: n?.computed?.onImmediateChange,
286
+ onChange: n?.computed?.onChange,
287
+ onGet: n?.computed?.onGet,
288
+ onSet: n?.computed?.onSet
289
+ });
290
+ r = function() {
291
+ return w(this), c.value;
292
+ }, o = function(f) {
293
+ c.value = f;
294
+ };
295
+ } else
296
+ r = function() {
297
+ return w(this), t.get.call(this);
298
+ }, o = function(...i) {
299
+ t.set.call(this, ...i);
300
+ };
301
+ return {
302
+ enumerable: t.enumerable,
303
+ configurable: t.configurable,
304
+ get: r,
305
+ set: o
306
+ };
307
+ }
308
+ function j(e, t, n) {
309
+ const r = { value: t.value }, o = d(r, {
310
+ name: e,
311
+ onGet: n?.reactive?.onGet,
312
+ onHas: n?.reactive?.onHas,
313
+ onSet: n?.reactive?.onSet,
314
+ onDelete: n?.reactive?.onDelete,
315
+ needsProxy: n?.reactive?.needsProxy,
316
+ deep: t.deep ?? n?.reactive?.deep
317
+ });
318
+ return {
319
+ configurable: t.configurable,
320
+ enumerable: t.enumerable,
321
+ get: function() {
322
+ w(this);
323
+ const { value: i } = o;
324
+ return Array.isArray(i) && Reflect.get(i, "length"), i;
325
+ },
326
+ set: function(i) {
327
+ o.value = i;
328
+ }
329
+ };
330
+ }
331
+ function te(e, t, n, r) {
332
+ Object.defineProperty(e, t, V(t, n, r));
333
+ }
334
+ function ne(e, t, n, r) {
335
+ Object.defineProperty(e, t, j(t, n, r));
336
+ }
337
+ const re = {
338
+ toRaw: ye,
339
+ watch: ee,
340
+ describeProperty: j,
341
+ defineProperty: ne,
342
+ describeComputed: V,
343
+ defineComputed: te
344
+ }, T = /* @__PURE__ */ new Set(["__proto__", "__ob__"]), E = "value", He = (e) => {
345
+ e[E] += 1;
346
+ };
347
+ function Le({ reactive: e, toRaw: t } = {}) {
348
+ const n = t ?? ((s) => s), r = /* @__PURE__ */ new WeakMap(), o = (s, a) => {
349
+ const u = n(s);
350
+ let l = r.get(u);
351
+ l || (l = /* @__PURE__ */ new Map(), r.set(u, l));
352
+ let v = l.get(a);
353
+ return v || (v = e({ [E]: 0 }), l.set(a, v)), v;
354
+ }, c = (s, a) => {
355
+ T.has(a) || Reflect.get(o(s, a), E);
356
+ }, i = (s, a) => {
357
+ T.has(a) || He(o(s, a));
358
+ }, f = (s, a) => {
359
+ const u = n(s), l = r.get(u);
360
+ l && i(l, a), r.delete(u);
361
+ }, b = {
362
+ computed: {
363
+ onGet: (s) => {
364
+ c(s, s.name);
365
+ },
366
+ onImmediateChange: (s) => {
367
+ i(s, s.name);
368
+ }
369
+ },
370
+ reactive: {
371
+ onGet: ({ target: s, key: a }) => {
372
+ w(s), c(s, a);
373
+ },
374
+ onHas: ({ target: s, key: a }) => {
375
+ c(s, a);
376
+ },
377
+ onDelete: ({ target: s, key: a }) => {
378
+ f(s, a);
379
+ },
380
+ onSet: ({ target: s, key: a, keysChanged: u }) => {
381
+ i(s, a), u && i(s);
382
+ },
383
+ needsProxy: ({ key: s }) => !T.has(s)
384
+ }
385
+ };
386
+ function ue(s, a) {
387
+ return V(s, a, b);
388
+ }
389
+ function fe(s, a) {
390
+ return j(s, a, b);
391
+ }
392
+ function le(s, a, u) {
393
+ return te(s, a, u, b);
394
+ }
395
+ function de(s, a, u) {
396
+ return ne(s, a, u, b);
397
+ }
398
+ return {
399
+ toRaw: re.toRaw,
400
+ watch: ee,
401
+ describeProperty: fe,
402
+ defineProperty: de,
403
+ describeComputed: ue,
404
+ defineComputed: le
405
+ };
406
+ }
407
+ function Me(...e) {
408
+ const t = {}, n = {};
409
+ for (const r of e) {
410
+ const o = typeof r == "function" ? r() : r;
411
+ Object.assign(t, Object.getOwnPropertyDescriptors(o ?? r ?? {}));
412
+ }
413
+ return Object.defineProperties(n, t), n;
414
+ }
415
+ function Ge(e, t) {
416
+ Object.defineProperties(
417
+ e.prototype,
418
+ Object.getOwnPropertyDescriptors(Me(...[...t, e].map((n) => n.prototype)))
419
+ );
420
+ }
421
+ const O = /* @__PURE__ */ new WeakMap();
422
+ function Be(...e) {
423
+ return (t) => {
424
+ e?.length && Ge(t, e);
425
+ };
426
+ }
427
+ function oe(e) {
428
+ O.has(e) || O.set(e, /* @__PURE__ */ new Set());
429
+ }
430
+ function ce(e, t) {
431
+ return oe(e), O.get(e).has(t);
432
+ }
433
+ function se(e, t) {
434
+ oe(e), O.get(e).add(t);
435
+ }
436
+ function W(e, t, n, r) {
437
+ return m() && !ce(e, t) ? (y(e, t, {
438
+ ...n,
439
+ get: n.get.bind(e),
440
+ set: n.set?.bind(e),
441
+ enumerable: !0,
442
+ ...r?.descriptors,
443
+ cache: !0
444
+ }), se(e, t), !0) : !1;
445
+ }
446
+ function ie(e, t, n, r) {
447
+ if (typeof n.get == "function") {
448
+ const o = {
449
+ ...n,
450
+ enumerable: !0,
451
+ configurable: !0
452
+ };
453
+ return o.get = function() {
454
+ return W(this, t, n, r), this[t];
455
+ }, o.set = function(i) {
456
+ W(this, t, n, r), this[t] = i;
457
+ }, o;
458
+ }
459
+ return n;
460
+ }
461
+ function Ne(e, t, n) {
462
+ return ie(e, t, n);
463
+ }
464
+ Ne.configure = function(t) {
465
+ return (n, r, o) => ie(
466
+ n,
467
+ r,
468
+ o,
469
+ { descriptors: t }
470
+ );
471
+ };
472
+ function P(e, t, n) {
473
+ return m() && !ce(e, t) ? (se(e, t), y(e, t, {
474
+ ...Object.getOwnPropertyDescriptor(e, t),
475
+ enumerable: !0,
476
+ ...n?.descriptors
477
+ }), !0) : !1;
478
+ }
479
+ function L(e, t, n) {
480
+ typeof e == "function" ? P(e, t) : Object.defineProperty(e, t, {
481
+ configurable: !0,
482
+ enumerable: !0,
483
+ get() {
484
+ if (P(this, t, n))
485
+ return this[t];
486
+ },
487
+ set(r) {
488
+ P(this, t, n) && (this[t] = r);
489
+ }
490
+ });
491
+ }
492
+ function ae(e, t) {
493
+ return L(e, t);
494
+ }
495
+ ae.shallow = function(t, n) {
496
+ return L(t, n, { descriptors: { deep: !1 } });
497
+ };
498
+ ae.configure = function(t) {
499
+ return (n, r) => L(n, r, { descriptors: t });
500
+ };
501
+ $(re);
502
+ const We = {
503
+ /** Configure a global plugin */
504
+ use: $,
505
+ /** Remove a global plugin */
506
+ unuse: ge,
507
+ /** Create reactive objects */
508
+ auto: be,
509
+ /** Define properties on objects */
510
+ define: y,
511
+ /** Watch reactive objects */
512
+ watch: ve,
513
+ /** Get the last time any reactive property was touched on a given object */
514
+ lastAccessed: me
515
+ };
516
+ export {
517
+ re as MadroneState,
518
+ Le as MadroneVue3,
519
+ Ge as applyClassMixins,
520
+ be as auto,
521
+ Be as classMixin,
522
+ Ne as computed,
523
+ We as default,
524
+ Me as merge,
525
+ ae as reactive,
526
+ U as toRaw,
527
+ ve as watch
528
+ };