@expressive/mvc 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +37 -7
- package/lib/index.esm.js +102 -107
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +102 -107
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -112,8 +112,16 @@ interface Model extends Stateful {
|
|
|
112
112
|
* Circular reference to `this` controller.
|
|
113
113
|
*
|
|
114
114
|
* Useful to obtain full reference where one has already destructured.
|
|
115
|
+
*
|
|
116
|
+
* @deprecated will be removed in favor of `is`
|
|
115
117
|
*/
|
|
116
118
|
get: this;
|
|
119
|
+
/**
|
|
120
|
+
* Circular reference to `this` controller.
|
|
121
|
+
*
|
|
122
|
+
* Useful to obtain full reference where one has already destructured.
|
|
123
|
+
*/
|
|
124
|
+
is: this;
|
|
117
125
|
/**
|
|
118
126
|
* Setter utility
|
|
119
127
|
*
|
|
@@ -356,6 +364,18 @@ declare function set<T>(factory: set.Factory<T>, required?: boolean): T;
|
|
|
356
364
|
declare function set<T, S>(factory: set.Factory<T, S>, required?: boolean): T;
|
|
357
365
|
declare function set<T>(value: set.Factory<T>, onUpdate: set.Callback<T>): T;
|
|
358
366
|
declare function set<T, S>(value: set.Factory<T, S>, onUpdate: set.Callback<T, S>): T;
|
|
367
|
+
/**
|
|
368
|
+
* Assign a property with result of a promise.
|
|
369
|
+
*/
|
|
370
|
+
declare function set<T>(factory: Promise<T>, required: false): T | undefined;
|
|
371
|
+
declare function set<T>(factory: Promise<T>, required?: boolean): T;
|
|
372
|
+
declare function set<T>(value: Promise<T>, onUpdate: set.Callback<T>): T;
|
|
373
|
+
/**
|
|
374
|
+
* Assign a property.
|
|
375
|
+
*/
|
|
376
|
+
declare function set<T>(factory: T, required: false): T | undefined;
|
|
377
|
+
declare function set<T>(factory: T, required?: boolean): T;
|
|
378
|
+
declare function set<T>(value: T, onUpdate: set.Callback<T>): T;
|
|
359
379
|
|
|
360
380
|
/**
|
|
361
381
|
* Create a placeholder for specified Model type.
|
|
@@ -464,8 +484,10 @@ declare class MVC extends Model {
|
|
|
464
484
|
|
|
465
485
|
declare namespace Consumer {
|
|
466
486
|
type HasProps<E extends Class> = {
|
|
487
|
+
/** @deprecated - use for instead */
|
|
488
|
+
of?: never;
|
|
467
489
|
/** Type of controller to fetch from context. */
|
|
468
|
-
|
|
490
|
+
for: E;
|
|
469
491
|
/**
|
|
470
492
|
* Getter function. Is called on every natural render of this component.
|
|
471
493
|
* Will throw if usable instance cannot be found in context.
|
|
@@ -473,14 +495,18 @@ declare namespace Consumer {
|
|
|
473
495
|
has: (value: InstanceType<E>) => void;
|
|
474
496
|
};
|
|
475
497
|
type GetProps<E extends Class> = {
|
|
498
|
+
/** @deprecated - use for instead */
|
|
499
|
+
of?: never;
|
|
476
500
|
/** Type of controller to fetch from context. */
|
|
477
|
-
|
|
501
|
+
for: E;
|
|
478
502
|
/** Getter function. Is called on every natural render of this component. */
|
|
479
503
|
get: (value: InstanceType<E> | undefined) => void;
|
|
480
504
|
};
|
|
481
505
|
type RenderProps<E extends Class> = {
|
|
506
|
+
/** @deprecated - use for instead */
|
|
507
|
+
of?: never;
|
|
482
508
|
/** Type of controller to fetch from context. */
|
|
483
|
-
|
|
509
|
+
for: E;
|
|
484
510
|
/**
|
|
485
511
|
* Render function, will receive instance of desired controller.
|
|
486
512
|
*
|
|
@@ -513,11 +539,15 @@ declare namespace Provider {
|
|
|
513
539
|
};
|
|
514
540
|
type Existent<E> = E extends Class ? InstanceType<E> : E extends Model ? E : never;
|
|
515
541
|
type NormalProps<E, I = Existent<E>> = {
|
|
516
|
-
|
|
542
|
+
/** @deprecated use for instead. */
|
|
543
|
+
of?: never;
|
|
544
|
+
for: E;
|
|
517
545
|
children: React.ReactNode | ((instance: I) => React.ReactNode);
|
|
518
546
|
} & Model.Compat<I>;
|
|
519
547
|
type MultipleProps<T extends Item> = {
|
|
520
|
-
|
|
548
|
+
/** @deprecated use for instead. */
|
|
549
|
+
of?: never;
|
|
550
|
+
for: Collection<T>;
|
|
521
551
|
children?: React.ReactNode | (() => React.ReactNode);
|
|
522
552
|
} & Model.Compat<Existent<T>>;
|
|
523
553
|
type Props<T extends Item> = MultipleProps<T> | NormalProps<T>;
|
|
@@ -581,7 +611,7 @@ declare function tap<T extends Class>(Type: T, required?: boolean): InstanceOf<T
|
|
|
581
611
|
declare function useFrom<T extends Stateful, R>(source: (() => T) | T, compute: (this: T, from: T) => R, expect: true): Exclude<R, undefined>;
|
|
582
612
|
declare function useFrom<T extends Stateful, R>(source: (() => T) | T, compute: (this: T, from: T) => R, expect?: boolean): R;
|
|
583
613
|
|
|
584
|
-
declare function
|
|
614
|
+
declare function useLocal<T extends typeof Model>(Type: T, arg?: boolean | string): any;
|
|
585
615
|
|
|
586
616
|
declare function useModel<T extends Class, I extends InstanceOf<T>>(source: T, watch: Model.Field<I>[], callback?: (instance: I) => void): I;
|
|
587
617
|
declare function useModel<T extends Class, I extends InstanceOf<T>>(source: T, callback?: (instance: I) => void): I;
|
|
@@ -603,4 +633,4 @@ declare function useTap<T extends Stateful, K extends Model.Field<T>>(source: T
|
|
|
603
633
|
declare function useTap<T extends Stateful, R>(source: T | (() => T) | useTap.Tappable, compute: (this: T, from: T) => R, expect: true): Exclude<R, undefined>;
|
|
604
634
|
declare function useTap<T extends Stateful, R>(source: T | (() => T) | useTap.Tappable, compute: (this: T, from: T) => R, expect?: boolean): R;
|
|
605
635
|
|
|
606
|
-
export { CONTROL, Consumer, Global, LOCAL, MVC as Model, Provider, STATE, Stateful, WHY, act, apply, array, MVC as default, from, parent, ref, set, tap, use, useFrom,
|
|
636
|
+
export { CONTROL, Consumer, Global, LOCAL, MVC as Model, Provider, STATE, Stateful, WHY, act, apply, array, MVC as default, from, parent, ref, set, tap, use, useFrom, useLocal, useModel, useTap };
|
package/lib/index.esm.js
CHANGED
|
@@ -43,10 +43,10 @@ function issues(register) {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
const Oops$c = issues({
|
|
46
|
-
|
|
46
|
+
NotReady: (model, key) => `Value ${model}.${key} value is not yet available.`,
|
|
47
47
|
});
|
|
48
48
|
function suspend(source, key) {
|
|
49
|
-
const error = Oops$c.
|
|
49
|
+
const error = Oops$c.NotReady(source.subject, key);
|
|
50
50
|
const promise = new Promise(resolve => {
|
|
51
51
|
const release = source.addListener(forKey => {
|
|
52
52
|
if (forKey == key)
|
|
@@ -87,7 +87,7 @@ function mayRetry(fn) {
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
const Oops$b = issues({
|
|
90
|
-
|
|
90
|
+
BadCallback: () => `Callback for effect-callback may only return a function.`
|
|
91
91
|
});
|
|
92
92
|
const { assign, create, defineProperty, entries, getPrototypeOf, getOwnPropertyNames, getOwnPropertyDescriptor, getOwnPropertySymbols, values } = Object;
|
|
93
93
|
function define(target, key, value) {
|
|
@@ -112,7 +112,7 @@ function createEffect(callback) {
|
|
|
112
112
|
if (unSet instanceof Promise)
|
|
113
113
|
unSet = undefined;
|
|
114
114
|
if (unSet && typeof unSet !== "function")
|
|
115
|
-
throw Oops$b.
|
|
115
|
+
throw Oops$b.BadCallback();
|
|
116
116
|
};
|
|
117
117
|
}
|
|
118
118
|
function createValueEffect(callback) {
|
|
@@ -128,7 +128,7 @@ function createValueEffect(callback) {
|
|
|
128
128
|
if (typeof out == "function")
|
|
129
129
|
unSet = out;
|
|
130
130
|
else
|
|
131
|
-
throw Oops$b.
|
|
131
|
+
throw Oops$b.BadCallback();
|
|
132
132
|
};
|
|
133
133
|
}
|
|
134
134
|
|
|
@@ -148,16 +148,16 @@ const LOCAL = Symbol("LOCAL");
|
|
|
148
148
|
const STATE = Symbol("STATE");
|
|
149
149
|
class Model {
|
|
150
150
|
constructor() {
|
|
151
|
-
|
|
152
|
-
define(this, CONTROL, control);
|
|
151
|
+
define(this, CONTROL, new Controller(this));
|
|
153
152
|
define(this, "get", this);
|
|
153
|
+
define(this, "is", this);
|
|
154
154
|
defineLazy(this, "set", () => {
|
|
155
|
+
const controller = control(this);
|
|
155
156
|
const assign = (key, value) => {
|
|
156
|
-
|
|
157
|
+
controller.update(key, value);
|
|
157
158
|
};
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
define(assign, key, control.ref(key));
|
|
159
|
+
for (const key of getOwnPropertyNames(controller.proxy))
|
|
160
|
+
define(assign, key, controller.ref(key));
|
|
161
161
|
return assign;
|
|
162
162
|
});
|
|
163
163
|
}
|
|
@@ -345,10 +345,10 @@ class Subscriber {
|
|
|
345
345
|
}
|
|
346
346
|
|
|
347
347
|
const Oops$a = issues({
|
|
348
|
-
|
|
348
|
+
BadSource: (model, property, got) => `Bad from-instruction provided to ${model}.${property}. Expects an arrow-function or a Model as source. Got ${got}.`,
|
|
349
349
|
PeerNotAllowed: (model, property) => `Attempted to use an instruction result (probably use or tap) as computed source for ${model}.${property}. This is not possible.`,
|
|
350
|
-
|
|
351
|
-
|
|
350
|
+
Failed: (parent, property, initial) => `An exception was thrown while ${initial ? "initializing" : "refreshing"} [${parent}.${property}].`,
|
|
351
|
+
Early: (property) => `Note: Computed values don't run until accessed, except when subscribed to. '${property}' getter may have run earlier than intended.`
|
|
352
352
|
});
|
|
353
353
|
const INIT = new WeakSet();
|
|
354
354
|
const INFO = new WeakMap();
|
|
@@ -375,7 +375,7 @@ function from(source, arg1, arg2) {
|
|
|
375
375
|
}
|
|
376
376
|
// Regular function is too ambiguous so not allowed.
|
|
377
377
|
else
|
|
378
|
-
throw Oops$a.
|
|
378
|
+
throw Oops$a.BadSource(subject, key, source);
|
|
379
379
|
let sub;
|
|
380
380
|
const setter = arg1;
|
|
381
381
|
const info = { key, parent, priority: 1 };
|
|
@@ -390,7 +390,7 @@ function from(source, arg1, arg2) {
|
|
|
390
390
|
return setter.call(sub.proxy, sub.proxy);
|
|
391
391
|
}
|
|
392
392
|
catch (err) {
|
|
393
|
-
Oops$a.
|
|
393
|
+
Oops$a.Failed(subject, key, !!initial).warn();
|
|
394
394
|
throw err;
|
|
395
395
|
}
|
|
396
396
|
}
|
|
@@ -431,7 +431,7 @@ function from(source, arg1, arg2) {
|
|
|
431
431
|
}
|
|
432
432
|
catch (e) {
|
|
433
433
|
if (early)
|
|
434
|
-
Oops$a.
|
|
434
|
+
Oops$a.Early(key).warn();
|
|
435
435
|
throw e;
|
|
436
436
|
}
|
|
437
437
|
finally {
|
|
@@ -615,9 +615,6 @@ function control(subject, cb) {
|
|
|
615
615
|
control = new Controller(subject);
|
|
616
616
|
define(subject, CONTROL, control);
|
|
617
617
|
}
|
|
618
|
-
return ready(control, cb);
|
|
619
|
-
}
|
|
620
|
-
function ready(control, cb) {
|
|
621
618
|
const ready = READY.has(control);
|
|
622
619
|
if (!ready) {
|
|
623
620
|
if (cb) {
|
|
@@ -715,14 +712,14 @@ function forSubscriber(fn) {
|
|
|
715
712
|
}
|
|
716
713
|
|
|
717
714
|
const Oops$8 = issues({
|
|
718
|
-
|
|
715
|
+
DuplicatePending: (key) => `Invoked action ${key} but one is already active.`
|
|
719
716
|
});
|
|
720
717
|
function act(task) {
|
|
721
718
|
return apply(function act(key) {
|
|
722
719
|
let pending = false;
|
|
723
720
|
const invoke = (...args) => __awaiter(this, void 0, void 0, function* () {
|
|
724
721
|
if (pending)
|
|
725
|
-
return Promise.reject(Oops$8.
|
|
722
|
+
return Promise.reject(Oops$8.DuplicatePending(key));
|
|
726
723
|
pending = true;
|
|
727
724
|
this.update(key);
|
|
728
725
|
try {
|
|
@@ -827,28 +824,74 @@ function createRef(key, cb) {
|
|
|
827
824
|
|
|
828
825
|
const Oops$7 = issues({
|
|
829
826
|
NonOptional: (Parent, key) => `Property ${Parent}.${key} is marked as required.`,
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
FactoryFailed: (model, key) => `Generating initial value for ${model}.${key} failed.`
|
|
827
|
+
NotReady: (model, key) => `Value ${model}.${key} value is not yet available.`,
|
|
828
|
+
Failed: (model, key) => `Generating initial value for ${model}.${key} failed.`
|
|
833
829
|
});
|
|
834
830
|
function set(factory, argument) {
|
|
835
831
|
return apply(function set(key) {
|
|
832
|
+
const { subject, state } = this;
|
|
833
|
+
const required = argument === true || argument === undefined;
|
|
836
834
|
let set;
|
|
837
835
|
let get;
|
|
838
836
|
let suspense;
|
|
839
|
-
const required = argument === true || argument === undefined;
|
|
840
837
|
if (factory === undefined)
|
|
841
838
|
suspense = true;
|
|
842
|
-
else
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
839
|
+
else {
|
|
840
|
+
let pending;
|
|
841
|
+
let error;
|
|
842
|
+
const init = () => {
|
|
843
|
+
const output = typeof factory == "function" ?
|
|
844
|
+
mayRetry(() => factory.call(subject, key, subject)) :
|
|
845
|
+
factory;
|
|
846
|
+
if (output instanceof Promise) {
|
|
847
|
+
pending = output
|
|
848
|
+
.catch(err => error = err)
|
|
849
|
+
.then(val => state[key] = val)
|
|
850
|
+
.finally(() => {
|
|
851
|
+
pending = undefined;
|
|
852
|
+
this.update(key);
|
|
853
|
+
});
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
return state[key] = output;
|
|
857
|
+
};
|
|
858
|
+
const suspend = () => {
|
|
859
|
+
if (required === false)
|
|
860
|
+
return undefined;
|
|
861
|
+
const issue = Oops$7.NotReady(subject, key);
|
|
862
|
+
Object.assign(pending, {
|
|
863
|
+
message: issue.message,
|
|
864
|
+
stack: issue.stack
|
|
865
|
+
});
|
|
866
|
+
throw pending;
|
|
867
|
+
};
|
|
868
|
+
if (required)
|
|
869
|
+
try {
|
|
870
|
+
init();
|
|
871
|
+
}
|
|
872
|
+
catch (err) {
|
|
873
|
+
Oops$7.Failed(subject, key).warn();
|
|
874
|
+
throw err;
|
|
875
|
+
}
|
|
876
|
+
get = () => {
|
|
877
|
+
if (pending)
|
|
878
|
+
return suspend();
|
|
879
|
+
if (error)
|
|
880
|
+
throw error;
|
|
881
|
+
if (key in state)
|
|
882
|
+
return state[key];
|
|
883
|
+
let output = init();
|
|
884
|
+
return pending
|
|
885
|
+
? suspend()
|
|
886
|
+
: output;
|
|
887
|
+
};
|
|
888
|
+
}
|
|
846
889
|
if (typeof argument == "function")
|
|
847
890
|
set = createValueEffect(argument);
|
|
848
891
|
else
|
|
849
892
|
set = (value) => {
|
|
850
893
|
if (value === undefined && required)
|
|
851
|
-
throw Oops$7.NonOptional(
|
|
894
|
+
throw Oops$7.NonOptional(subject, key);
|
|
852
895
|
};
|
|
853
896
|
return {
|
|
854
897
|
set,
|
|
@@ -856,63 +899,12 @@ function set(factory, argument) {
|
|
|
856
899
|
suspense
|
|
857
900
|
};
|
|
858
901
|
});
|
|
859
|
-
}
|
|
860
|
-
function pendingFactory(parent, key, fn, required) {
|
|
861
|
-
const { subject, state } = parent;
|
|
862
|
-
let pending;
|
|
863
|
-
let error;
|
|
864
|
-
const init = () => {
|
|
865
|
-
const output = mayRetry(() => {
|
|
866
|
-
return fn.call(subject, key, subject);
|
|
867
|
-
});
|
|
868
|
-
if (output instanceof Promise) {
|
|
869
|
-
pending = output
|
|
870
|
-
.catch(err => error = err)
|
|
871
|
-
.then(val => state[key] = val)
|
|
872
|
-
.finally(() => {
|
|
873
|
-
pending = undefined;
|
|
874
|
-
parent.update(key);
|
|
875
|
-
});
|
|
876
|
-
return;
|
|
877
|
-
}
|
|
878
|
-
return state[key] = output;
|
|
879
|
-
};
|
|
880
|
-
const suspend = () => {
|
|
881
|
-
if (required === false)
|
|
882
|
-
return undefined;
|
|
883
|
-
const issue = Oops$7.ValueNotReady(subject, key);
|
|
884
|
-
Object.assign(pending, {
|
|
885
|
-
message: issue.message,
|
|
886
|
-
stack: issue.stack
|
|
887
|
-
});
|
|
888
|
-
throw pending;
|
|
889
|
-
};
|
|
890
|
-
if (required)
|
|
891
|
-
try {
|
|
892
|
-
init();
|
|
893
|
-
}
|
|
894
|
-
catch (err) {
|
|
895
|
-
Oops$7.FactoryFailed(subject, key).warn();
|
|
896
|
-
throw err;
|
|
897
|
-
}
|
|
898
|
-
return () => {
|
|
899
|
-
if (pending)
|
|
900
|
-
return suspend();
|
|
901
|
-
if (error)
|
|
902
|
-
throw error;
|
|
903
|
-
if (key in state)
|
|
904
|
-
return state[key];
|
|
905
|
-
let output = init();
|
|
906
|
-
return pending
|
|
907
|
-
? suspend()
|
|
908
|
-
: output;
|
|
909
|
-
};
|
|
910
902
|
}
|
|
911
903
|
|
|
912
904
|
const Parent = new WeakMap();
|
|
913
905
|
const Oops$6 = issues({
|
|
914
|
-
|
|
915
|
-
|
|
906
|
+
Required: (expects, child) => `New ${child} created standalone but requires parent of type ${expects}. Did you remember to create via use(${child})?`,
|
|
907
|
+
Unexpected: (expects, child, got) => `New ${child} created as child of ${got}, but must be instanceof ${expects}.`,
|
|
916
908
|
});
|
|
917
909
|
function parent(Expects, required) {
|
|
918
910
|
return apply(function parent() {
|
|
@@ -921,10 +913,10 @@ function parent(Expects, required) {
|
|
|
921
913
|
const expected = Expects.name;
|
|
922
914
|
if (!value) {
|
|
923
915
|
if (required !== false)
|
|
924
|
-
throw Oops$6.
|
|
916
|
+
throw Oops$6.Required(expected, child);
|
|
925
917
|
}
|
|
926
918
|
else if (!(value instanceof Expects))
|
|
927
|
-
throw Oops$6.
|
|
919
|
+
throw Oops$6.Unexpected(expected, child, value);
|
|
928
920
|
return { value };
|
|
929
921
|
});
|
|
930
922
|
}
|
|
@@ -1029,19 +1021,19 @@ class Lookup {
|
|
|
1029
1021
|
const LookupContext = React.createContext(new Lookup());
|
|
1030
1022
|
const useLookup = () => React.useContext(LookupContext);
|
|
1031
1023
|
const Oops$4 = issues({
|
|
1032
|
-
|
|
1024
|
+
NotFound: (name) => `Couldn't find ${name} in context; did you forget to use a Provider?`
|
|
1033
1025
|
});
|
|
1034
|
-
function
|
|
1026
|
+
function useLocal(Type, arg) {
|
|
1035
1027
|
const instance = useLookup().get(Type);
|
|
1036
1028
|
if (!instance && arg !== false)
|
|
1037
|
-
throw Oops$4.
|
|
1029
|
+
throw Oops$4.NotFound(Type.name);
|
|
1038
1030
|
return typeof arg == "string" ?
|
|
1039
1031
|
instance[arg] :
|
|
1040
1032
|
instance;
|
|
1041
1033
|
}
|
|
1042
1034
|
|
|
1043
1035
|
const Oops$3 = issues({
|
|
1044
|
-
|
|
1036
|
+
NotAllowed: (parent, child) => `Global '${parent}' attempted to attach '${child}' but it is not also a singleton.`,
|
|
1045
1037
|
AmbientRequired: (requested, requester, key) => `Attempted to find an instance of ${requested} in context. It is required for [${requester}.${key}], but one could not be found.`
|
|
1046
1038
|
});
|
|
1047
1039
|
const PendingContext = new WeakMap();
|
|
@@ -1055,7 +1047,7 @@ function tap(type, argument) {
|
|
|
1055
1047
|
};
|
|
1056
1048
|
const { subject } = this;
|
|
1057
1049
|
if ("set" in subject.constructor)
|
|
1058
|
-
throw Oops$3.
|
|
1050
|
+
throw Oops$3.NotAllowed(subject, type.name);
|
|
1059
1051
|
getPending(subject).push(context => {
|
|
1060
1052
|
let instance = context.get(type);
|
|
1061
1053
|
if (typeof argument == "function") {
|
|
@@ -1199,7 +1191,7 @@ function useTap(source, path, expect) {
|
|
|
1199
1191
|
if ("get" in source)
|
|
1200
1192
|
return () => source.get();
|
|
1201
1193
|
if ("prototype" in source)
|
|
1202
|
-
return () =>
|
|
1194
|
+
return () => useLocal(source);
|
|
1203
1195
|
else
|
|
1204
1196
|
return source;
|
|
1205
1197
|
}, [])();
|
|
@@ -1234,7 +1226,7 @@ class MVC extends Model {
|
|
|
1234
1226
|
return useModel(this, arg, callback);
|
|
1235
1227
|
}
|
|
1236
1228
|
static get(required) {
|
|
1237
|
-
return
|
|
1229
|
+
return useLocal(this, required);
|
|
1238
1230
|
}
|
|
1239
1231
|
static tap(key, expect) {
|
|
1240
1232
|
return useTap(this, key, expect);
|
|
@@ -1280,25 +1272,28 @@ class MVC extends Model {
|
|
|
1280
1272
|
}
|
|
1281
1273
|
|
|
1282
1274
|
const Oops$2 = issues({
|
|
1283
|
-
|
|
1275
|
+
BadProps: () => `Provider expects either a render function, 'get' or 'has' props.`
|
|
1284
1276
|
});
|
|
1285
1277
|
function Consumer(props) {
|
|
1286
|
-
const { get, has, children,
|
|
1278
|
+
const { get, has, children, for: type } = props;
|
|
1287
1279
|
if (typeof children == "function")
|
|
1288
|
-
return children(useTap(
|
|
1280
|
+
return children(useTap(type));
|
|
1289
1281
|
const callback = has || get;
|
|
1290
1282
|
if (typeof callback == "function")
|
|
1291
|
-
callback(
|
|
1283
|
+
callback(useLocal(type, !!has));
|
|
1292
1284
|
else
|
|
1293
|
-
throw Oops$2.
|
|
1285
|
+
throw Oops$2.BadProps();
|
|
1294
1286
|
return null;
|
|
1295
1287
|
}
|
|
1296
1288
|
|
|
1297
1289
|
const Oops$1 = issues({
|
|
1298
|
-
|
|
1290
|
+
NoType: () => "Provider 'of' prop must be Model, typeof Model or a collection of them.",
|
|
1291
|
+
PropDeprecated: () => "Provider `of` prop is deprecated, use `for` instead."
|
|
1299
1292
|
});
|
|
1300
1293
|
function Provider(props) {
|
|
1301
|
-
|
|
1294
|
+
if ("of" in props)
|
|
1295
|
+
throw Oops$1.PropDeprecated();
|
|
1296
|
+
const context = useNewContext(props.for);
|
|
1302
1297
|
const render = props.children;
|
|
1303
1298
|
useAppliedProps(context, props);
|
|
1304
1299
|
React.useLayoutEffect(() => () => context.pop(), []);
|
|
@@ -1310,7 +1305,7 @@ function useNewContext(inject) {
|
|
|
1310
1305
|
const from = useLookup();
|
|
1311
1306
|
return React.useMemo(() => {
|
|
1312
1307
|
if (!inject)
|
|
1313
|
-
throw Oops$1.
|
|
1308
|
+
throw Oops$1.NoType();
|
|
1314
1309
|
const context = from.push(inject);
|
|
1315
1310
|
for (const instance of context.local)
|
|
1316
1311
|
if (instance)
|
|
@@ -1324,7 +1319,7 @@ function useAppliedProps(within, props) {
|
|
|
1324
1319
|
const targets = within.local;
|
|
1325
1320
|
return function integrate(props) {
|
|
1326
1321
|
for (const [key, value] of entries(props))
|
|
1327
|
-
if (
|
|
1322
|
+
if (["of", "for", "children"].indexOf(key) < 0)
|
|
1328
1323
|
for (const into of targets)
|
|
1329
1324
|
if (into && key in into)
|
|
1330
1325
|
into[key] = value;
|
|
@@ -1345,15 +1340,15 @@ function RenderFunction(props) {
|
|
|
1345
1340
|
}
|
|
1346
1341
|
|
|
1347
1342
|
const Oops = issues({
|
|
1348
|
-
|
|
1349
|
-
|
|
1343
|
+
DoesNotExist: (name) => `Tried to access singleton ${name}, but none exist! Did you forget to initialize?\nCall ${name}.create() before attempting to access, or consider using ${name}.use() instead.`,
|
|
1344
|
+
AlreadyExists: (name) => `Shared instance of ${name} already exists! Consider unmounting existing, or use ${name}.reset() to force-delete it.`
|
|
1350
1345
|
});
|
|
1351
1346
|
const Active = new WeakMap();
|
|
1352
1347
|
class Global extends MVC {
|
|
1353
1348
|
static create(...args) {
|
|
1354
1349
|
const Type = this;
|
|
1355
1350
|
if (Active.has(Type))
|
|
1356
|
-
throw Oops.
|
|
1351
|
+
throw Oops.AlreadyExists(this.name);
|
|
1357
1352
|
const instance = super.create(...args);
|
|
1358
1353
|
Active.set(Type, instance);
|
|
1359
1354
|
return instance;
|
|
@@ -1371,7 +1366,7 @@ class Global extends MVC {
|
|
|
1371
1366
|
static get(arg) {
|
|
1372
1367
|
const instance = Active.get(this);
|
|
1373
1368
|
if (!instance && arg !== false)
|
|
1374
|
-
throw Oops.
|
|
1369
|
+
throw Oops.DoesNotExist(this.name);
|
|
1375
1370
|
return (typeof arg == "string" ?
|
|
1376
1371
|
instance[arg] :
|
|
1377
1372
|
instance);
|
|
@@ -1388,5 +1383,5 @@ class Global extends MVC {
|
|
|
1388
1383
|
}
|
|
1389
1384
|
}
|
|
1390
1385
|
|
|
1391
|
-
export { CONTROL, Consumer, Global, LOCAL, MVC as Model, Provider, STATE, WHY, act, apply, array, MVC as default, from, parent, ref, set, tap, use$1 as use, useFrom,
|
|
1386
|
+
export { CONTROL, Consumer, Global, LOCAL, MVC as Model, Provider, STATE, WHY, act, apply, array, MVC as default, from, parent, ref, set, tap, use$1 as use, useFrom, useLocal, useModel, useTap };
|
|
1392
1387
|
//# sourceMappingURL=index.esm.js.map
|