@expressive/mvc 0.2.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 CHANGED
@@ -364,6 +364,18 @@ declare function set<T>(factory: set.Factory<T>, required?: boolean): T;
364
364
  declare function set<T, S>(factory: set.Factory<T, S>, required?: boolean): T;
365
365
  declare function set<T>(value: set.Factory<T>, onUpdate: set.Callback<T>): T;
366
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;
367
379
 
368
380
  /**
369
381
  * Create a placeholder for specified Model type.
package/lib/index.esm.js CHANGED
@@ -824,28 +824,74 @@ function createRef(key, cb) {
824
824
 
825
825
  const Oops$7 = issues({
826
826
  NonOptional: (Parent, key) => `Property ${Parent}.${key} is marked as required.`,
827
- BadFactory: () => `Set instruction can only accept a factory or undefined.`,
828
827
  NotReady: (model, key) => `Value ${model}.${key} value is not yet available.`,
829
828
  Failed: (model, key) => `Generating initial value for ${model}.${key} failed.`
830
829
  });
831
830
  function set(factory, argument) {
832
831
  return apply(function set(key) {
832
+ const { subject, state } = this;
833
+ const required = argument === true || argument === undefined;
833
834
  let set;
834
835
  let get;
835
836
  let suspense;
836
- const required = argument === true || argument === undefined;
837
837
  if (factory === undefined)
838
838
  suspense = true;
839
- else if (typeof factory === "function")
840
- get = pendingFactory(this, key, factory, required);
841
- else
842
- throw Oops$7.BadFactory();
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
+ }
843
889
  if (typeof argument == "function")
844
890
  set = createValueEffect(argument);
845
891
  else
846
892
  set = (value) => {
847
893
  if (value === undefined && required)
848
- throw Oops$7.NonOptional(this.subject, key);
894
+ throw Oops$7.NonOptional(subject, key);
849
895
  };
850
896
  return {
851
897
  set,
@@ -853,57 +899,6 @@ function set(factory, argument) {
853
899
  suspense
854
900
  };
855
901
  });
856
- }
857
- function pendingFactory(parent, key, fn, required) {
858
- const { subject, state } = parent;
859
- let pending;
860
- let error;
861
- const init = () => {
862
- const output = mayRetry(() => {
863
- return fn.call(subject, key, subject);
864
- });
865
- if (output instanceof Promise) {
866
- pending = output
867
- .catch(err => error = err)
868
- .then(val => state[key] = val)
869
- .finally(() => {
870
- pending = undefined;
871
- parent.update(key);
872
- });
873
- return;
874
- }
875
- return state[key] = output;
876
- };
877
- const suspend = () => {
878
- if (required === false)
879
- return undefined;
880
- const issue = Oops$7.NotReady(subject, key);
881
- Object.assign(pending, {
882
- message: issue.message,
883
- stack: issue.stack
884
- });
885
- throw pending;
886
- };
887
- if (required)
888
- try {
889
- init();
890
- }
891
- catch (err) {
892
- Oops$7.Failed(subject, key).warn();
893
- throw err;
894
- }
895
- return () => {
896
- if (pending)
897
- return suspend();
898
- if (error)
899
- throw error;
900
- if (key in state)
901
- return state[key];
902
- let output = init();
903
- return pending
904
- ? suspend()
905
- : output;
906
- };
907
902
  }
908
903
 
909
904
  const Parent = new WeakMap();