Skip to content

Commit b2ef0f2

Browse files
committed
add store.umd.js - fixes #967
1 parent d07c334 commit b2ef0f2

File tree

3 files changed

+242
-1
lines changed

3 files changed

+242
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"codecov": "codecov",
1919
"precodecov": "npm run coverage",
2020
"lint": "eslint src test/*.js",
21-
"build": "node src/shared/_build.js && rollup -c",
21+
"build": "node src/shared/_build.js && rollup -c && rollup -c rollup.store.config.js",
2222
"prepare": "npm run build",
2323
"dev": "node src/shared/_build.js && rollup -c -w",
2424
"pretest": "npm run build",

rollup.store.config.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
input: 'store.js',
3+
output: {
4+
file: 'store.umd.js',
5+
format: 'umd',
6+
name: 'svelte',
7+
extend: true
8+
}
9+
};

store.umd.js

+232
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
(function (global, factory) {
2+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
4+
(factory((global.svelte = global.svelte || {})));
5+
}(this, (function (exports) { 'use strict';
6+
7+
function assign(target) {
8+
var k,
9+
source,
10+
i = 1,
11+
len = arguments.length;
12+
for (; i < len; i++) {
13+
source = arguments[i];
14+
for (k in source) target[k] = source[k];
15+
}
16+
17+
return target;
18+
}
19+
20+
function blankObject() {
21+
return Object.create(null);
22+
}
23+
24+
function differs(a, b) {
25+
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
26+
}
27+
28+
function dispatchObservers(component, group, changed, newState, oldState) {
29+
for (var key in group) {
30+
if (!changed[key]) continue;
31+
32+
var newValue = newState[key];
33+
var oldValue = oldState[key];
34+
35+
var callbacks = group[key];
36+
if (!callbacks) continue;
37+
38+
for (var i = 0; i < callbacks.length; i += 1) {
39+
var callback = callbacks[i];
40+
if (callback.__calling) continue;
41+
42+
callback.__calling = true;
43+
callback.call(component, newValue, oldValue);
44+
callback.__calling = false;
45+
}
46+
}
47+
}
48+
49+
function get(key) {
50+
return key ? this._state[key] : this._state;
51+
}
52+
53+
function observe(key, callback, options) {
54+
var group = options && options.defer
55+
? this._observers.post
56+
: this._observers.pre;
57+
58+
(group[key] || (group[key] = [])).push(callback);
59+
60+
if (!options || options.init !== false) {
61+
callback.__calling = true;
62+
callback.call(this, this._state[key]);
63+
callback.__calling = false;
64+
}
65+
66+
return {
67+
cancel: function() {
68+
var index = group[key].indexOf(callback);
69+
if (~index) group[key].splice(index, 1);
70+
}
71+
};
72+
}
73+
74+
function Store(state) {
75+
this._observers = { pre: blankObject(), post: blankObject() };
76+
this._changeHandlers = [];
77+
this._dependents = [];
78+
79+
this._computed = blankObject();
80+
this._sortedComputedProperties = [];
81+
82+
this._state = assign({}, state);
83+
}
84+
85+
assign(Store.prototype, {
86+
_add: function(component, props) {
87+
this._dependents.push({
88+
component: component,
89+
props: props
90+
});
91+
},
92+
93+
_init: function(props) {
94+
var state = {};
95+
for (var i = 0; i < props.length; i += 1) {
96+
var prop = props[i];
97+
state['$' + prop] = this._state[prop];
98+
}
99+
return state;
100+
},
101+
102+
_remove: function(component) {
103+
var i = this._dependents.length;
104+
while (i--) {
105+
if (this._dependents[i].component === component) {
106+
this._dependents.splice(i, 1);
107+
return;
108+
}
109+
}
110+
},
111+
112+
_sortComputedProperties: function() {
113+
var computed = this._computed;
114+
var sorted = this._sortedComputedProperties = [];
115+
var cycles;
116+
var visited = blankObject();
117+
118+
function visit(key) {
119+
if (cycles[key]) {
120+
throw new Error('Cyclical dependency detected');
121+
}
122+
123+
if (visited[key]) return;
124+
visited[key] = true;
125+
126+
var c = computed[key];
127+
128+
if (c) {
129+
cycles[key] = true;
130+
c.deps.forEach(visit);
131+
sorted.push(c);
132+
}
133+
}
134+
135+
for (var key in this._computed) {
136+
cycles = blankObject();
137+
visit(key);
138+
}
139+
},
140+
141+
compute: function(key, deps, fn) {
142+
var store = this;
143+
var value;
144+
145+
var c = {
146+
deps: deps,
147+
update: function(state, changed, dirty) {
148+
var values = deps.map(function(dep) {
149+
if (dep in changed) dirty = true;
150+
return state[dep];
151+
});
152+
153+
if (dirty) {
154+
var newValue = fn.apply(null, values);
155+
if (differs(newValue, value)) {
156+
value = newValue;
157+
changed[key] = true;
158+
state[key] = value;
159+
}
160+
}
161+
}
162+
};
163+
164+
c.update(this._state, {}, true);
165+
166+
this._computed[key] = c;
167+
this._sortComputedProperties();
168+
},
169+
170+
get: get,
171+
172+
observe: observe,
173+
174+
onchange: function(callback) {
175+
this._changeHandlers.push(callback);
176+
return {
177+
cancel: function() {
178+
var index = this._changeHandlers.indexOf(callback);
179+
if (~index) this._changeHandlers.splice(index, 1);
180+
}
181+
};
182+
},
183+
184+
set: function(newState) {
185+
var oldState = this._state,
186+
changed = this._changed = {},
187+
dirty = false;
188+
189+
for (var key in newState) {
190+
if (this._computed[key]) throw new Error("'" + key + "' is a read-only property");
191+
if (differs(newState[key], oldState[key])) changed[key] = dirty = true;
192+
}
193+
if (!dirty) return;
194+
195+
this._state = assign({}, oldState, newState);
196+
197+
for (var i = 0; i < this._sortedComputedProperties.length; i += 1) {
198+
this._sortedComputedProperties[i].update(this._state, changed);
199+
}
200+
201+
for (var i = 0; i < this._changeHandlers.length; i += 1) {
202+
this._changeHandlers[i](this._state, changed);
203+
}
204+
205+
dispatchObservers(this, this._observers.pre, changed, this._state, oldState);
206+
207+
var dependents = this._dependents.slice(); // guard against mutations
208+
for (var i = 0; i < dependents.length; i += 1) {
209+
var dependent = dependents[i];
210+
var componentState = {};
211+
dirty = false;
212+
213+
for (var j = 0; j < dependent.props.length; j += 1) {
214+
var prop = dependent.props[j];
215+
if (prop in changed) {
216+
componentState['$' + prop] = this._state[prop];
217+
dirty = true;
218+
}
219+
}
220+
221+
if (dirty) dependent.component.set(componentState);
222+
}
223+
224+
dispatchObservers(this, this._observers.post, changed, this._state, oldState);
225+
}
226+
});
227+
228+
exports.Store = Store;
229+
230+
Object.defineProperty(exports, '__esModule', { value: true });
231+
232+
})));

0 commit comments

Comments
 (0)