native-document 1.0.221 → 1.0.223
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "native-document",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.223",
|
|
4
4
|
"description": "A reactive JavaScript framework that preserves native DOM simplicity without sacrificing modern features",
|
|
5
5
|
"author": "AfroCodeur <https://github.com/afrocodeur>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -184,16 +184,16 @@ Observable.computed = function(_callback, _dependencies = []) {
|
|
|
184
184
|
};
|
|
185
185
|
ObservableItem.computed = Observable.computed;
|
|
186
186
|
|
|
187
|
-
Observable.auto = (value) => {
|
|
187
|
+
Observable.auto = (value, options = null) => {
|
|
188
188
|
if(value == null) {
|
|
189
|
-
return new ObservableItem(value);
|
|
189
|
+
return new ObservableItem(value, options);
|
|
190
190
|
}
|
|
191
191
|
if(Validator.isArray(value)) {
|
|
192
|
-
return new ObservableArray(value);
|
|
192
|
+
return new ObservableArray(value, options);
|
|
193
193
|
} else if(typeof value === 'object') {
|
|
194
|
-
return new ObservableObject(value);
|
|
194
|
+
return new ObservableObject(value, options);
|
|
195
195
|
}
|
|
196
|
-
return new ObservableItem(value);
|
|
196
|
+
return new ObservableItem(value, options);
|
|
197
197
|
};
|
|
198
198
|
ObservableItem.auto = Observable.auto;
|
|
199
199
|
|
|
@@ -5,8 +5,8 @@ import PluginsManager from '../utils/plugins-manager.js';
|
|
|
5
5
|
import NativeDocumentError from '../errors/NativeDocumentError.js';
|
|
6
6
|
import {nextTick} from '../utils/helpers';
|
|
7
7
|
|
|
8
|
-
const mutationMethods = ['push', 'pop', 'shift', 'unshift', 'reverse', 'sort', 'splice'];
|
|
9
|
-
const noMutationMethods = ['map', 'forEach', 'filter', 'reduce', 'some', 'every', 'find', 'findIndex', 'concat', 'includes', 'indexOf'];
|
|
8
|
+
export const mutationMethods = ['push', 'pop', 'shift', 'unshift', 'reverse', 'sort', 'splice'];
|
|
9
|
+
export const noMutationMethods = ['map', 'forEach', 'filter', 'reduce', 'some', 'every', 'find', 'findIndex', 'concat', 'includes', 'indexOf'];
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Reactive array container extending ObservableItem.
|
package/src/core/data/Store.js
CHANGED
|
@@ -2,7 +2,7 @@ import { Observable } from './Observable';
|
|
|
2
2
|
import NativeDocumentError from '../errors/NativeDocumentError';
|
|
3
3
|
import DebugManager from '../utils/debug-manager';
|
|
4
4
|
import {$getFromStorage, $saveToStorage, LocalStorage} from '../utils/localstorage';
|
|
5
|
-
import
|
|
5
|
+
import {mutationMethods} from './ObservableArray';
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
const WRITE_METHODS = [
|
|
@@ -44,18 +44,44 @@ export const StoreFactory = function() {
|
|
|
44
44
|
observer.reset = readOnlyError('reset');
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
-
const $
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
const $synchroProtectedFollower = (from, follower, name, context) => {
|
|
48
|
+
const originalSet = follower.set.bind(follower);
|
|
49
|
+
if(!from.__$isObservableArray) {
|
|
50
|
+
from.subscribe(originalSet);
|
|
51
|
+
$applyReadOnly(follower, name, 'follow');
|
|
52
|
+
return originalSet;
|
|
50
53
|
}
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
const methods = {};
|
|
55
|
+
const readOnlyError = (method) => {
|
|
56
|
+
return () => {
|
|
57
|
+
DebugManager.error('Store', `Store.${context}('${name}') is read-only. '${method}()' is not allowed.`);
|
|
58
|
+
throw new NativeDocumentError(
|
|
59
|
+
`Store.${context}('${name}') is read-only.`,
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
for(const method of mutationMethods) {
|
|
65
|
+
methods[method] = follower[method].bind(follower);
|
|
66
|
+
follower[method] = readOnlyError(method);
|
|
53
67
|
}
|
|
54
|
-
|
|
68
|
+
|
|
69
|
+
from.subscribe((data, _, operations) => {
|
|
70
|
+
const callback = methods[operations.action];
|
|
71
|
+
if(!callback) {
|
|
72
|
+
originalSet(data);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
callback(...(operations.args||[]));
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const $createObservable = (value, options = {}) => {
|
|
80
|
+
return Observable.auto(value, options);
|
|
55
81
|
};
|
|
56
82
|
|
|
57
83
|
const $api = {
|
|
58
|
-
/**
|
|
84
|
+
/**a
|
|
59
85
|
* Create a new state and return the observer.
|
|
60
86
|
* Throws if a store with the same name already exists.
|
|
61
87
|
*
|
|
@@ -246,12 +272,7 @@ export const StoreFactory = function() {
|
|
|
246
272
|
const { observer: originalObserver, subscribers } = $getStoreOrThrow('follow', name);
|
|
247
273
|
const observerFollower = $createObservable(originalObserver.val());
|
|
248
274
|
|
|
249
|
-
const
|
|
250
|
-
const onStoreChange = value => originalSet(value);
|
|
251
|
-
|
|
252
|
-
originalObserver.subscribe(onStoreChange);
|
|
253
|
-
|
|
254
|
-
$applyReadOnly(observerFollower, name, 'follow');
|
|
275
|
+
const onStoreChange = $synchroProtectedFollower(originalObserver, observerFollower, name,'follow');
|
|
255
276
|
|
|
256
277
|
observerFollower.destroy = () => {
|
|
257
278
|
originalObserver.unsubscribe(onStoreChange);
|