as-model 0.1.0 → 0.1.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.
@@ -3,14 +3,14 @@ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
3
  import { createNoStateModel } from "../validation";
4
4
  import { generateNotifier } from "./notifier";
5
5
  import { generateTunnelCreator, createUnInitializedUpdater, destroy } from "./tunnel";
6
- function createInitializedUpdater(updater) {
6
+ function createInitializedUpdater(updater, middleWare) {
7
7
  var createTunnel = generateTunnelCreator(updater);
8
8
  return {
9
- notify: generateNotifier(updater),
9
+ notify: generateNotifier(updater, middleWare),
10
10
  createTunnel
11
11
  };
12
12
  }
13
- function createUpdateFn(updater) {
13
+ function createUpdateFn(updater, middleWare) {
14
14
  return function update() {
15
15
  var args = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
16
16
  updater.mutate(function(u, effect) {
@@ -34,7 +34,7 @@ function createUpdateFn(updater) {
34
34
  }
35
35
  if (!u.initialized) {
36
36
  var instance1 = model(initialState);
37
- var initializedUpdater = createInitializedUpdater(u);
37
+ var initializedUpdater = createInitializedUpdater(u, middleWare);
38
38
  return _object_spread(_object_spread_props(_object_spread({}, u), {
39
39
  model,
40
40
  state: initialState,
@@ -66,8 +66,8 @@ function createUpdateFn(updater) {
66
66
  };
67
67
  }
68
68
  var lazyModel = createNoStateModel();
69
- function createUpdater(model) {
70
- var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
69
+ function createUpdater(model, middleWare) {
70
+ var config = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
71
71
  var hasDefaultState = "state" in config;
72
72
  var controlled = config.controlled, defaultState = config.state;
73
73
  var defaultInstance = hasDefaultState ? model(defaultState) : lazyModel(void 0);
@@ -118,9 +118,9 @@ function createUpdater(model) {
118
118
  destroy(updater);
119
119
  }
120
120
  }, unInitializedUpdater);
121
- var initialized = createInitializedUpdater(updater);
121
+ var initialized = createInitializedUpdater(updater, middleWare);
122
122
  Object.assign(updater, initialized, {
123
- update: createUpdateFn(updater)
123
+ update: createUpdateFn(updater, middleWare)
124
124
  });
125
125
  return updater;
126
126
  }
@@ -6,7 +6,7 @@ function defaultNotifyImplement(dispatches, action) {
6
6
  callback(action);
7
7
  });
8
8
  }
9
- function generateNotifier(updater) {
9
+ function generateNotifier(updater, middleWare) {
10
10
  function pendAction(value) {
11
11
  updater.mutate(function(u) {
12
12
  var dispatching = u.dispatching;
@@ -75,9 +75,37 @@ function generateNotifier(updater) {
75
75
  call(initializedAction);
76
76
  });
77
77
  }
78
- var config = updater.config;
78
+ var config = updater.config, model = updater.model;
79
+ var dispatch = function dispatch2(action) {
80
+ var dispatches = updater.dispatches, controlled = updater.controlled;
81
+ var dispatchCallbacks = _to_consumable_array(dispatches);
82
+ var state = action.state;
83
+ if (!controlled) {
84
+ updater.mutate(function(u) {
85
+ return _object_spread_props(_object_spread({}, u), {
86
+ state,
87
+ instance: model(state),
88
+ version: u.version + 1
89
+ });
90
+ });
91
+ }
92
+ try {
93
+ if (typeof config.batchNotify === "function") {
94
+ config.batchNotify(dispatchCallbacks, action);
95
+ } else {
96
+ defaultNotifyImplement(dispatchCallbacks, action);
97
+ }
98
+ } catch (e) {
99
+ updater.mutate(function(u) {
100
+ return _object_spread_props(_object_spread({}, u), {
101
+ dispatching: void 0
102
+ });
103
+ });
104
+ throw e;
105
+ }
106
+ };
79
107
  return function notify(action) {
80
- if (action == null) {
108
+ if (action == null || updater.isDestroyed) {
81
109
  return;
82
110
  }
83
111
  var dispatching = updater.dispatching;
@@ -88,22 +116,15 @@ function generateNotifier(updater) {
88
116
  while (updater.dispatching) {
89
117
  var wrap = updater.dispatching;
90
118
  if (wrap) {
91
- var dispatches = updater.dispatches;
92
- var dispatchCallbacks = _to_consumable_array(dispatches);
93
- try {
94
- if (typeof config.batchNotify === "function" && dispatchCallbacks.length) {
95
- config.batchNotify(dispatchCallbacks, wrap.value);
96
- } else {
97
- defaultNotifyImplement(dispatchCallbacks, wrap.value);
98
- }
99
- } catch (e) {
100
- updater.mutate(function(u) {
101
- return _object_spread_props(_object_spread({}, u), {
102
- dispatching: void 0
103
- });
104
- });
105
- throw e;
106
- }
119
+ middleWare({
120
+ getState: function getState() {
121
+ return {
122
+ state: updater.state,
123
+ instance: updater.instance
124
+ };
125
+ },
126
+ dispatch: notify
127
+ })(dispatch)(wrap.value);
107
128
  unshiftAction();
108
129
  } else {
109
130
  updater.mutate(function(u) {
@@ -1,5 +1,6 @@
1
1
  import { _ as _define_property } from "@swc/helpers/_/_define_property";
2
2
  import { _ as _type_of } from "@swc/helpers/_/_type_of";
3
+ import { modelKeyIdentifier, modelUsageIdentifier } from "../identifiers";
3
4
  var noStateAModelKey = "no-state-a-model-key";
4
5
  function createNoStateModel() {
5
6
  return function noStateModel(state) {
@@ -19,7 +20,21 @@ function isInstanceFromNoStateModel(instance) {
19
20
  var validations = {
20
21
  isInstanceFromNoStateModel
21
22
  };
23
+ function isModelKey(data) {
24
+ if (!data) {
25
+ return false;
26
+ }
27
+ return data.modelKeyIdentifier === modelKeyIdentifier && data.modelKeyIdentifier();
28
+ }
29
+ function isModelUsage(data) {
30
+ if (!data) {
31
+ return false;
32
+ }
33
+ return data.modelUsageIdentifier === modelUsageIdentifier && data.modelUsageIdentifier();
34
+ }
22
35
  export {
23
36
  createNoStateModel,
37
+ isModelKey,
38
+ isModelUsage,
24
39
  validations
25
40
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "as-model",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "description": "This is a model state management tool",
6
6
  "license": "MIT",
7
7
  "author": "Jimmy.Harding",