as-model 0.1.20 → 0.1.21

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/dist/index.js CHANGED
@@ -604,7 +604,10 @@
604
604
  updater.mutate(function(u, effect) {
605
605
  var _args_model;
606
606
  var model2 = (_args_model = args.model) !== null && _args_model !== void 0 ? _args_model : u.model;
607
- var state = "state" in args ? args.state : u.state;
607
+ var hasState = Object.prototype.hasOwnProperty.call(args, "state");
608
+ var hasInitialState = Object.prototype.hasOwnProperty.call(args, "initialState");
609
+ var state = hasState ? args.state : u.state;
610
+ var isInitialize = !u.initialized || hasInitialState;
608
611
  var token = createToken();
609
612
  if (u.controlled) {
610
613
  var instance = model2(state);
@@ -620,8 +623,9 @@
620
623
  if (!u.initialized && !("state" in args)) {
621
624
  throw new Error("The updater has not been initialized, it should be updated with a state for initializing.");
622
625
  }
623
- if (!u.initialized) {
624
- var instance1 = model2(state);
626
+ if (isInitialize) {
627
+ var initialState = hasInitialState ? args.initialState : state;
628
+ var instance1 = model2(initialState);
625
629
  var initializedUpdater = createInitializedUpdater(u, middleWare);
626
630
  return _object_spread(_object_spread_props(_object_spread({}, u), {
627
631
  model: model2,
@@ -1194,7 +1198,7 @@
1194
1198
  getInstance,
1195
1199
  update: function update(args) {
1196
1200
  var updateArgs = args !== null && args !== void 0 ? args : {};
1197
- if ("key" in updateArgs && updateArgs.key) {
1201
+ if (updateArgs.key) {
1198
1202
  var updatingKey = updateArgs.key, updatingModel = updateArgs.model, rest = _object_without_properties(updateArgs, [
1199
1203
  "key",
1200
1204
  "model"
@@ -1205,7 +1209,7 @@
1205
1209
  store.key = updatingKey;
1206
1210
  return;
1207
1211
  }
1208
- if ("model" in updateArgs && updateArgs.model) {
1212
+ if (updateArgs.model) {
1209
1213
  var updatingKey1 = updateArgs.key, updatingModel1 = updateArgs.model, rest1 = _object_without_properties(updateArgs, [
1210
1214
  "key",
1211
1215
  "model"
@@ -103,13 +103,13 @@ function createStore(modelLike, config = {}) {
103
103
  getInstance,
104
104
  update(args) {
105
105
  const updateArgs = args != null ? args : {};
106
- if ("key" in updateArgs && updateArgs.key) {
106
+ if (updateArgs.key) {
107
107
  const _a = updateArgs, { key: updatingKey, model: updatingModel } = _a, rest = __objRest(_a, ["key", "model"]);
108
108
  updater.update(__spreadProps(__spreadValues({}, rest), { model: updatingKey.source }));
109
109
  store.key = updatingKey;
110
110
  return;
111
111
  }
112
- if ("model" in updateArgs && updateArgs.model) {
112
+ if (updateArgs.model) {
113
113
  const _b = updateArgs, { key: updatingKey, model: updatingModel } = _b, rest = __objRest(_b, ["key", "model"]);
114
114
  updater.update(__spreadProps(__spreadValues({}, rest), { model: updatingModel }));
115
115
  store.key = createPrimaryKey(updatingModel, config);
@@ -37,7 +37,13 @@ function createUpdateFn(updater, middleWare) {
37
37
  updater.mutate((u, effect) => {
38
38
  var _a;
39
39
  const model = (_a = args.model) != null ? _a : u.model;
40
- const state = "state" in args ? args.state : u.state;
40
+ const hasState = Object.prototype.hasOwnProperty.call(args, "state");
41
+ const hasInitialState = Object.prototype.hasOwnProperty.call(
42
+ args,
43
+ "initialState"
44
+ );
45
+ const state = hasState ? args.state : u.state;
46
+ const isInitialize = !u.initialized || hasInitialState;
41
47
  const token = createToken();
42
48
  if (u.controlled) {
43
49
  const instance = model(state);
@@ -51,8 +57,9 @@ function createUpdateFn(updater, middleWare) {
51
57
  "The updater has not been initialized, it should be updated with a state for initializing."
52
58
  );
53
59
  }
54
- if (!u.initialized) {
55
- const instance = model(state);
60
+ if (isInitialize) {
61
+ const initialState = hasInitialState ? args.initialState : state;
62
+ const instance = model(initialState);
56
63
  const initializedUpdater = createInitializedUpdater(u, middleWare);
57
64
  return __spreadValues(__spreadProps(__spreadValues({}, u), {
58
65
  model,
package/index.d.ts CHANGED
@@ -103,6 +103,7 @@ export declare interface Store<
103
103
  update: (args?: {
104
104
  model?: Model<S, T>;
105
105
  key?: Key<S, T, R>;
106
+ initialState?: S;
106
107
  state?: S;
107
108
  }) => void;
108
109
  destroy: () => void;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "as-model",
4
- "version": "0.1.20",
4
+ "version": "0.1.21",
5
5
  "description": "This is a model state management tool",
6
6
  "license": "MIT",
7
7
  "author": "Jimmy.Harding",