@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.js CHANGED
@@ -832,28 +832,74 @@ function createRef(key, cb) {
832
832
 
833
833
  const Oops$7 = issues({
834
834
  NonOptional: (Parent, key) => `Property ${Parent}.${key} is marked as required.`,
835
- BadFactory: () => `Set instruction can only accept a factory or undefined.`,
836
835
  NotReady: (model, key) => `Value ${model}.${key} value is not yet available.`,
837
836
  Failed: (model, key) => `Generating initial value for ${model}.${key} failed.`
838
837
  });
839
838
  function set(factory, argument) {
840
839
  return apply(function set(key) {
840
+ const { subject, state } = this;
841
+ const required = argument === true || argument === undefined;
841
842
  let set;
842
843
  let get;
843
844
  let suspense;
844
- const required = argument === true || argument === undefined;
845
845
  if (factory === undefined)
846
846
  suspense = true;
847
- else if (typeof factory === "function")
848
- get = pendingFactory(this, key, factory, required);
849
- else
850
- throw Oops$7.BadFactory();
847
+ else {
848
+ let pending;
849
+ let error;
850
+ const init = () => {
851
+ const output = typeof factory == "function" ?
852
+ mayRetry(() => factory.call(subject, key, subject)) :
853
+ factory;
854
+ if (output instanceof Promise) {
855
+ pending = output
856
+ .catch(err => error = err)
857
+ .then(val => state[key] = val)
858
+ .finally(() => {
859
+ pending = undefined;
860
+ this.update(key);
861
+ });
862
+ return;
863
+ }
864
+ return state[key] = output;
865
+ };
866
+ const suspend = () => {
867
+ if (required === false)
868
+ return undefined;
869
+ const issue = Oops$7.NotReady(subject, key);
870
+ Object.assign(pending, {
871
+ message: issue.message,
872
+ stack: issue.stack
873
+ });
874
+ throw pending;
875
+ };
876
+ if (required)
877
+ try {
878
+ init();
879
+ }
880
+ catch (err) {
881
+ Oops$7.Failed(subject, key).warn();
882
+ throw err;
883
+ }
884
+ get = () => {
885
+ if (pending)
886
+ return suspend();
887
+ if (error)
888
+ throw error;
889
+ if (key in state)
890
+ return state[key];
891
+ let output = init();
892
+ return pending
893
+ ? suspend()
894
+ : output;
895
+ };
896
+ }
851
897
  if (typeof argument == "function")
852
898
  set = createValueEffect(argument);
853
899
  else
854
900
  set = (value) => {
855
901
  if (value === undefined && required)
856
- throw Oops$7.NonOptional(this.subject, key);
902
+ throw Oops$7.NonOptional(subject, key);
857
903
  };
858
904
  return {
859
905
  set,
@@ -861,57 +907,6 @@ function set(factory, argument) {
861
907
  suspense
862
908
  };
863
909
  });
864
- }
865
- function pendingFactory(parent, key, fn, required) {
866
- const { subject, state } = parent;
867
- let pending;
868
- let error;
869
- const init = () => {
870
- const output = mayRetry(() => {
871
- return fn.call(subject, key, subject);
872
- });
873
- if (output instanceof Promise) {
874
- pending = output
875
- .catch(err => error = err)
876
- .then(val => state[key] = val)
877
- .finally(() => {
878
- pending = undefined;
879
- parent.update(key);
880
- });
881
- return;
882
- }
883
- return state[key] = output;
884
- };
885
- const suspend = () => {
886
- if (required === false)
887
- return undefined;
888
- const issue = Oops$7.NotReady(subject, key);
889
- Object.assign(pending, {
890
- message: issue.message,
891
- stack: issue.stack
892
- });
893
- throw pending;
894
- };
895
- if (required)
896
- try {
897
- init();
898
- }
899
- catch (err) {
900
- Oops$7.Failed(subject, key).warn();
901
- throw err;
902
- }
903
- return () => {
904
- if (pending)
905
- return suspend();
906
- if (error)
907
- throw error;
908
- if (key in state)
909
- return state[key];
910
- let output = init();
911
- return pending
912
- ? suspend()
913
- : output;
914
- };
915
910
  }
916
911
 
917
912
  const Parent = new WeakMap();