as-model 0.0.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/esm/index.js ADDED
@@ -0,0 +1,49 @@
1
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
+ import { createStore as cs } from "./store";
4
+ import { createKey as ck, createStores as css } from "./key";
5
+ import { configModel } from "./model";
6
+ function config() {
7
+ var configuration = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
8
+ var createStore2 = function createStore3(modelLike, state) {
9
+ return cs(modelLike, arguments.length > 1 ? _object_spread_props(_object_spread({}, configuration), {
10
+ state
11
+ }) : configuration);
12
+ };
13
+ var createKey2 = function createKey3(model3, state) {
14
+ var isKeySetState = arguments.length > 1;
15
+ var key = ck(model3, isKeySetState ? _object_spread_props(_object_spread({}, configuration), {
16
+ state
17
+ }) : configuration);
18
+ key.createStore = function createKeyStore(s) {
19
+ return arguments.length > 0 ? createStore2(key, s) : createStore2(key);
20
+ };
21
+ return key;
22
+ };
23
+ createKey2.isModelKey = ck.isModelKey;
24
+ var createStores2 = function createStores3() {
25
+ for (var _len = arguments.length, modelKeys = new Array(_len), _key = 0; _key < _len; _key++) {
26
+ modelKeys[_key] = arguments[_key];
27
+ }
28
+ return css(modelKeys, configuration);
29
+ };
30
+ var model2 = configModel(configuration);
31
+ return {
32
+ createStore: createStore2,
33
+ createKey: createKey2,
34
+ createStores: createStores2,
35
+ model: model2
36
+ };
37
+ }
38
+ var _config = config(), createStore = _config.createStore, createKey = _config.createKey, createStores = _config.createStores, model = _config.model;
39
+ import { createSignal } from "./store";
40
+ import { validations } from "./validation";
41
+ export {
42
+ config,
43
+ createKey,
44
+ createSignal,
45
+ createStore,
46
+ createStores,
47
+ model,
48
+ validations
49
+ };
@@ -0,0 +1,73 @@
1
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
+ import { createStore, createPrimaryKey, isModelKey } from "../store";
4
+ function createKey(model) {
5
+ var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
6
+ var hasDefaultState = "state" in config;
7
+ var wrapModel = hasDefaultState ? createPrimaryKey(model, config.state) : createPrimaryKey(model);
8
+ wrapModel.createStore = function createKeyStore() {
9
+ var storeConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
10
+ return createStore(wrapModel, _object_spread({}, config, storeConfig));
11
+ };
12
+ return wrapModel;
13
+ }
14
+ createKey.isModelKey = isModelKey;
15
+ function createStores(modelKeys) {
16
+ var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
17
+ var state = {
18
+ destroyed: false
19
+ };
20
+ var storeUnits = modelKeys.map(function(modelKey) {
21
+ if (typeof modelKey === "function") {
22
+ return modelKey.createStore();
23
+ }
24
+ var k = modelKey.key;
25
+ return createStore(k, "defaultState" in k ? _object_spread_props(_object_spread({}, config), {
26
+ state: k.defaultState
27
+ }) : config);
28
+ });
29
+ return {
30
+ find: function find(key) {
31
+ var found = storeUnits.find(function(c) {
32
+ if (typeof key === "function") {
33
+ return c.key === key;
34
+ }
35
+ return c.key === key.key;
36
+ });
37
+ if (!found) {
38
+ return null;
39
+ }
40
+ return found;
41
+ },
42
+ update: function update() {
43
+ for (var _len = arguments.length, keys = new Array(_len), _key = 0; _key < _len; _key++) {
44
+ keys[_key] = arguments[_key];
45
+ }
46
+ if (state.destroyed) {
47
+ return;
48
+ }
49
+ storeUnits.forEach(function(un, i) {
50
+ var key = keys[i];
51
+ if (!key) {
52
+ return;
53
+ }
54
+ Object.assign(un, {
55
+ key
56
+ });
57
+ un.update({
58
+ model: typeof key === "function" ? key.source : key.key.source
59
+ });
60
+ });
61
+ },
62
+ destroy: function destroy() {
63
+ storeUnits.forEach(function(unit) {
64
+ unit.destroy();
65
+ });
66
+ state.destroyed = true;
67
+ }
68
+ };
69
+ }
70
+ export {
71
+ createKey,
72
+ createStores
73
+ };
File without changes
@@ -0,0 +1,28 @@
1
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
+ import { createStore, createField, createMethod } from "../store";
4
+ import { createKey } from "../key";
5
+ function configModel(config) {
6
+ var model = function model2(modelFn) {
7
+ var modelWrapper = function modelWrapper2(state) {
8
+ return modelFn(state);
9
+ };
10
+ modelWrapper.createKey = function createModelKey(state) {
11
+ return createKey(modelWrapper, arguments.length ? _object_spread_props(_object_spread({}, config), {
12
+ state
13
+ }) : config);
14
+ };
15
+ modelWrapper.createStore = function createModelStore(state) {
16
+ return createStore(modelWrapper, arguments.length ? _object_spread_props(_object_spread({}, config), {
17
+ state
18
+ }) : config);
19
+ };
20
+ return modelWrapper;
21
+ };
22
+ model.createField = createField;
23
+ model.createMethod = createMethod;
24
+ return model;
25
+ }
26
+ export {
27
+ configModel
28
+ };
File without changes
@@ -0,0 +1,153 @@
1
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
+ import { createUpdater } from "../updater";
4
+ import { shallowEqual } from "../tools";
5
+ import { cacheIdentify, extractInstance, createField as createInstanceField, createMethod as createInstanceMethod } from "./instance";
6
+ function modelKeyIdentifier() {
7
+ return true;
8
+ }
9
+ function isModelKey(data) {
10
+ if (!data) {
11
+ return false;
12
+ }
13
+ return data.modelKeyIdentifier === modelKeyIdentifier;
14
+ }
15
+ function createPrimaryKey(modelFn, defaultState) {
16
+ var model = isModelKey(modelFn) ? modelFn.source : modelFn;
17
+ var wrapModel = function wrapModel2(state) {
18
+ return model(state);
19
+ };
20
+ wrapModel.source = model;
21
+ wrapModel.modelKeyIdentifier = modelKeyIdentifier;
22
+ if (arguments.length > 1) {
23
+ wrapModel.defaultState = defaultState;
24
+ }
25
+ return wrapModel;
26
+ }
27
+ function createStore(modelLike) {
28
+ var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
29
+ var model = isModelKey(modelLike) ? modelLike.source : modelLike;
30
+ var modelKey = isModelKey(modelLike) ? modelLike : void 0;
31
+ var conf = function computeConfig() {
32
+ var hasConfigState = "state" in config;
33
+ var hasKeyState = !!modelKey && "defaultState" in modelKey;
34
+ if (hasConfigState) {
35
+ return config;
36
+ }
37
+ if (hasKeyState) {
38
+ return _object_spread_props(_object_spread({}, config), {
39
+ state: modelKey === null || modelKey === void 0 ? void 0 : modelKey.defaultState
40
+ });
41
+ }
42
+ return config;
43
+ }();
44
+ var updater = createUpdater(model, conf);
45
+ var store = {
46
+ key: modelKey !== null && modelKey !== void 0 ? modelKey : "state" in config ? createPrimaryKey(model, config.state) : createPrimaryKey(model),
47
+ subscribe: function subscribe(dispatcher) {
48
+ var _updater_createTunnel = updater.createTunnel(dispatcher), connect = _updater_createTunnel.connect, disconnect = _updater_createTunnel.disconnect;
49
+ connect();
50
+ return disconnect;
51
+ },
52
+ getInstance: function getInstance() {
53
+ return extractInstance(updater);
54
+ },
55
+ update: function update(args) {
56
+ updater.update(args);
57
+ },
58
+ destroy: function destroy() {
59
+ updater.destroy();
60
+ },
61
+ payload: function payload(callback) {
62
+ return updater.payload(callback);
63
+ },
64
+ isDestroyed: function isDestroyed() {
65
+ return updater.isDestroyed;
66
+ },
67
+ updater
68
+ };
69
+ return store;
70
+ }
71
+ function createSignal(store) {
72
+ var signalStore = {
73
+ collection: null,
74
+ started: false,
75
+ enabled: false
76
+ };
77
+ var middleWare = function(dispatcher) {
78
+ return function(action) {
79
+ if (!signalStore.enabled) {
80
+ dispatcher(action);
81
+ return;
82
+ }
83
+ var collection = signalStore.collection;
84
+ if (collection == null) {
85
+ dispatcher(action);
86
+ return;
87
+ }
88
+ var current = extractInstance(store.updater);
89
+ var keys = Object.keys(collection);
90
+ var currentCollectionEntries = keys.map(function(key) {
91
+ var field = current[key];
92
+ if (cacheIdentify.field(field)) {
93
+ return [
94
+ key,
95
+ field.get()
96
+ ];
97
+ }
98
+ return [
99
+ key,
100
+ field
101
+ ];
102
+ });
103
+ var currentCollection = Object.fromEntries(currentCollectionEntries);
104
+ if (!shallowEqual(collection, currentCollection)) {
105
+ dispatcher(action);
106
+ }
107
+ };
108
+ };
109
+ return {
110
+ key: store.key,
111
+ subscribe: function subscribe(dispatcher) {
112
+ return store.subscribe(middleWare(dispatcher));
113
+ },
114
+ getSignal: function getSignal() {
115
+ var collectUsedFields = function collectUsedFields2(key, val) {
116
+ if (!signalStore.started) {
117
+ return;
118
+ }
119
+ signalStore.collection = signalStore.collection || {};
120
+ signalStore.collection[key] = val;
121
+ };
122
+ var signal = function signal2() {
123
+ return extractInstance(store.updater, collectUsedFields);
124
+ };
125
+ signal.startStatistics = function startStatistics() {
126
+ signalStore.started = true;
127
+ };
128
+ signal.stopStatistics = function stopStatistics() {
129
+ signalStore.started = false;
130
+ };
131
+ signal.subscribe = function subscribe(dispatchCallback) {
132
+ return store.subscribe(dispatchCallback);
133
+ };
134
+ signal.payload = function payload(callback) {
135
+ return store.payload(callback);
136
+ };
137
+ signalStore.enabled = true;
138
+ signalStore.started = true;
139
+ return signal;
140
+ }
141
+ };
142
+ }
143
+ var createField = createInstanceField;
144
+ var createMethod = createInstanceMethod;
145
+ export {
146
+ createField,
147
+ createMethod,
148
+ createPrimaryKey,
149
+ createSignal,
150
+ createStore,
151
+ isModelKey,
152
+ modelKeyIdentifier
153
+ };
@@ -0,0 +1,197 @@
1
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
4
+ import { _ as _type_of } from "@swc/helpers/_/_type_of";
5
+ import { createProxy, shallowEqual } from "../tools";
6
+ var cacheIdentify = {
7
+ field: function field(d) {
8
+ if (!d) {
9
+ return false;
10
+ }
11
+ var f = d;
12
+ return f.identifier === cacheIdentify.field;
13
+ },
14
+ method: function method(d) {
15
+ if (typeof d !== "function") {
16
+ return false;
17
+ }
18
+ var m = d;
19
+ return m.identifier === cacheIdentify.method;
20
+ }
21
+ };
22
+ function createField(callback, deps) {
23
+ var currentDeps = function computeDeps() {
24
+ if (deps == null) {
25
+ return deps;
26
+ }
27
+ if (deps.some(function(d) {
28
+ return cacheIdentify.field(d) && d.deps == null;
29
+ })) {
30
+ return void 0;
31
+ }
32
+ return deps.flatMap(function(d) {
33
+ if (cacheIdentify.field(d)) {
34
+ return d.deps;
35
+ }
36
+ return d;
37
+ });
38
+ }();
39
+ var value = callback();
40
+ return {
41
+ callback,
42
+ deps: currentDeps,
43
+ identifier: cacheIdentify.field,
44
+ value,
45
+ get: function get() {
46
+ return value;
47
+ }
48
+ };
49
+ }
50
+ function createMethod(method2) {
51
+ var replace = function replace2() {
52
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
53
+ args[_key] = arguments[_key];
54
+ }
55
+ return method2.apply(void 0, _to_consumable_array(args));
56
+ };
57
+ Object.assign(replace, method2());
58
+ replace.identifier = cacheIdentify.method;
59
+ return replace;
60
+ }
61
+ function wrapToActionMethod(updater, methodName) {
62
+ var cacheMethods = updater.cacheMethods;
63
+ var cachedMethod = cacheMethods[methodName];
64
+ if (typeof cachedMethod === "function") {
65
+ return cachedMethod;
66
+ }
67
+ var actionMethod = function actionMethod2() {
68
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
69
+ args[_key] = arguments[_key];
70
+ }
71
+ var instance = updater.instance, isDestroyed = updater.isDestroyed, controlled = updater.controlled, model = updater.model;
72
+ var method2 = instance[methodName];
73
+ if (typeof method2 !== "function") {
74
+ throw new Error("Can not change methods in runtime.");
75
+ }
76
+ if (cacheIdentify.method(method2)) {
77
+ return method2.apply(void 0, _to_consumable_array(args));
78
+ }
79
+ var state = method2.apply(void 0, _to_consumable_array(args));
80
+ if (isDestroyed) {
81
+ return state;
82
+ }
83
+ var action = {
84
+ type: methodName,
85
+ state,
86
+ prevState: updater.state,
87
+ instance: updater.instance,
88
+ prevInstance: updater.instance,
89
+ method: actionMethod2
90
+ };
91
+ if (controlled) {
92
+ updater.notify(action);
93
+ return state;
94
+ }
95
+ var prevState = updater.state;
96
+ var prevInstance = updater.instance;
97
+ var newestInstance = model(state);
98
+ updater.mutate(function(up, effect) {
99
+ effect(function(u) {
100
+ u.notify({
101
+ type: methodName,
102
+ state,
103
+ prevState,
104
+ instance: newestInstance,
105
+ prevInstance,
106
+ method: actionMethod2
107
+ });
108
+ });
109
+ return _object_spread_props(_object_spread({}, up), {
110
+ instance: newestInstance,
111
+ state,
112
+ version: up.version + 1
113
+ });
114
+ });
115
+ return state;
116
+ };
117
+ cacheMethods[methodName] = actionMethod;
118
+ return actionMethod;
119
+ }
120
+ function wrapToField(updater, propertyName, value, onGot) {
121
+ var cacheFields = updater.cacheFields;
122
+ if (!cacheIdentify.field(value)) {
123
+ if (onGot) {
124
+ onGot(propertyName, value);
125
+ }
126
+ return value;
127
+ }
128
+ var field2 = value;
129
+ var cachedField = cacheFields[propertyName];
130
+ if (cachedField) {
131
+ return cachedField.getter;
132
+ }
133
+ var getter = {
134
+ get: function get() {
135
+ var currentField = updater.instance[propertyName];
136
+ if (!cacheIdentify.field(currentField)) {
137
+ throw new Error("Field should always be field.");
138
+ }
139
+ var current = currentField.get();
140
+ var fieldInCache = updater.cacheFields[propertyName];
141
+ if (!currentField.deps || fieldInCache == null) {
142
+ cacheFields[propertyName] = {
143
+ getter,
144
+ value: current,
145
+ deps: currentField.deps
146
+ };
147
+ return current;
148
+ }
149
+ if (shallowEqual(currentField.deps, fieldInCache.deps)) {
150
+ return fieldInCache.value;
151
+ }
152
+ cacheFields[propertyName] = {
153
+ getter,
154
+ value: current,
155
+ deps: currentField.deps
156
+ };
157
+ return current;
158
+ }
159
+ };
160
+ cacheFields[propertyName] = {
161
+ getter,
162
+ value: field2.value,
163
+ deps: field2.deps
164
+ };
165
+ return getter;
166
+ }
167
+ function extractInstance(updater, onGet) {
168
+ var instance = updater.instance;
169
+ if ((typeof instance === "undefined" ? "undefined" : _type_of(instance)) !== "object" || !instance) {
170
+ throw new Error("The instance should be an object or array.");
171
+ }
172
+ var properties = Object.getOwnPropertyNames(instance);
173
+ var handleGetter = function handleGetter2(key, value) {
174
+ if (!onGet) {
175
+ return;
176
+ }
177
+ onGet(key, value);
178
+ };
179
+ return createProxy(instance, {
180
+ get: function get(target, p, receiver) {
181
+ var value = target[p];
182
+ if (typeof value === "function" && properties.indexOf(p) >= 0) {
183
+ var actionMethod = wrapToActionMethod(updater, p);
184
+ Object.assign(actionMethod, value);
185
+ handleGetter(p, actionMethod);
186
+ return actionMethod;
187
+ }
188
+ return wrapToField(updater, p, value, handleGetter);
189
+ }
190
+ });
191
+ }
192
+ export {
193
+ cacheIdentify,
194
+ createField,
195
+ createMethod,
196
+ extractInstance
197
+ };
File without changes
package/esm/tools.js ADDED
@@ -0,0 +1,78 @@
1
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
+ import { _ as _type_of } from "@swc/helpers/_/_type_of";
3
+ function getDescriptors(target, receiver, ownOrPrototype, handler) {
4
+ var it = Object.keys(ownOrPrototype);
5
+ var result = {};
6
+ it.forEach(function(key) {
7
+ result[key] = {
8
+ get: function() {
9
+ if (!handler.get) {
10
+ return target[key];
11
+ }
12
+ return handler.get(target, key, receiver);
13
+ },
14
+ set: function(v) {
15
+ if (!handler.set) {
16
+ target[key] = v;
17
+ return;
18
+ }
19
+ var valid = handler.set(target, key, v, receiver);
20
+ if (!valid) {
21
+ throw new Error("".concat(key, " in proxy target is not mutable"));
22
+ }
23
+ }
24
+ };
25
+ });
26
+ return result;
27
+ }
28
+ var createSimpleProxy = function(target, handler) {
29
+ var proxy = {};
30
+ var own = getDescriptors(target, proxy, target, handler);
31
+ var prototype = getDescriptors(target, proxy, Object.getPrototypeOf(target), handler);
32
+ Object.defineProperties(proxy, _object_spread({}, prototype, own));
33
+ return proxy;
34
+ };
35
+ var createProxy = function(target, handler) {
36
+ if (typeof Proxy !== "function") {
37
+ return createSimpleProxy(target, handler);
38
+ }
39
+ return new Proxy(target, handler);
40
+ };
41
+ function isObject(data) {
42
+ return data && (typeof data === "undefined" ? "undefined" : _type_of(data)) === "object";
43
+ }
44
+ function shallowEqual(prev, current) {
45
+ if (Object.is(prev, current)) {
46
+ return true;
47
+ }
48
+ if (!isObject(prev) || !isObject(current)) {
49
+ return false;
50
+ }
51
+ var prevKeys = Object.keys(prev);
52
+ var currentKeys = Object.keys(current);
53
+ if (prevKeys.length !== currentKeys.length) {
54
+ return false;
55
+ }
56
+ var pre = prev;
57
+ var curr = current;
58
+ var hasDiffKey = prevKeys.some(function(key) {
59
+ return !Object.prototype.hasOwnProperty.call(curr, key);
60
+ });
61
+ if (hasDiffKey) {
62
+ return false;
63
+ }
64
+ var hasDiffValue = currentKeys.some(function(key) {
65
+ var currentValue = curr[key];
66
+ var prevValue = pre[key];
67
+ return !Object.is(currentValue, prevValue);
68
+ });
69
+ return !hasDiffValue;
70
+ }
71
+ function noop() {
72
+ }
73
+ export {
74
+ createProxy,
75
+ createSimpleProxy,
76
+ noop,
77
+ shallowEqual
78
+ };