mithril-pipeline 0.2.5 → 1.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.
@@ -0,0 +1,116 @@
1
+ "use strict"
2
+
3
+ var guid = 0, HALT = {}
4
+ function createStream() {
5
+ function stream() {
6
+ if (arguments.length > 0 && arguments[0] !== HALT) updateStream(stream, arguments[0])
7
+ return stream._state.value
8
+ }
9
+ initStream(stream)
10
+
11
+ if (arguments.length > 0 && arguments[0] !== HALT) updateStream(stream, arguments[0])
12
+
13
+ return stream
14
+ }
15
+ function initStream(stream) {
16
+ stream.constructor = createStream
17
+ stream._state = {id: guid++, value: undefined, state: 0, derive: undefined, recover: undefined, deps: {}, parents: [], endStream: undefined}
18
+ stream.map = stream["fantasy-land/map"] = map, stream["fantasy-land/ap"] = ap, stream["fantasy-land/of"] = createStream
19
+ stream.valueOf = valueOf, stream.toJSON = toJSON, stream.toString = valueOf
20
+
21
+ Object.defineProperties(stream, {
22
+ end: {get: function() {
23
+ if (!stream._state.endStream) {
24
+ var endStream = createStream()
25
+ endStream.map(function(value) {
26
+ if (value === true) unregisterStream(stream), unregisterStream(endStream)
27
+ return value
28
+ })
29
+ stream._state.endStream = endStream
30
+ }
31
+ return stream._state.endStream
32
+ }}
33
+ })
34
+ }
35
+ function updateStream(stream, value) {
36
+ updateState(stream, value)
37
+ for (var id in stream._state.deps) updateDependency(stream._state.deps[id], false)
38
+ finalize(stream)
39
+ }
40
+ function updateState(stream, value) {
41
+ stream._state.value = value
42
+ stream._state.changed = true
43
+ if (stream._state.state !== 2) stream._state.state = 1
44
+ }
45
+ function updateDependency(stream, mustSync) {
46
+ var state = stream._state, parents = state.parents
47
+ if (parents.length > 0 && parents.every(active) && (mustSync || parents.some(changed))) {
48
+ var value = stream._state.derive()
49
+ if (value === HALT) return false
50
+ updateState(stream, value)
51
+ }
52
+ }
53
+ function finalize(stream) {
54
+ stream._state.changed = false
55
+ for (var id in stream._state.deps) stream._state.deps[id]._state.changed = false
56
+ }
57
+
58
+ function combine(fn, streams) {
59
+ if (!streams.every(valid)) throw new Error("Ensure that each item passed to stream.combine/stream.merge is a stream")
60
+ return initDependency(createStream(), streams, function() {
61
+ return fn.apply(this, streams.concat([streams.filter(changed)]))
62
+ })
63
+ }
64
+
65
+ function initDependency(dep, streams, derive) {
66
+ var state = dep._state
67
+ state.derive = derive
68
+ state.parents = streams.filter(notEnded)
69
+
70
+ registerDependency(dep, state.parents)
71
+ updateDependency(dep, true)
72
+
73
+ return dep
74
+ }
75
+ function registerDependency(stream, parents) {
76
+ for (var i = 0; i < parents.length; i++) {
77
+ parents[i]._state.deps[stream._state.id] = stream
78
+ registerDependency(stream, parents[i]._state.parents)
79
+ }
80
+ }
81
+ function unregisterStream(stream) {
82
+ for (var i = 0; i < stream._state.parents.length; i++) {
83
+ var parent = stream._state.parents[i]
84
+ delete parent._state.deps[stream._state.id]
85
+ }
86
+ for (var id in stream._state.deps) {
87
+ var dependent = stream._state.deps[id]
88
+ var index = dependent._state.parents.indexOf(stream)
89
+ if (index > -1) dependent._state.parents.splice(index, 1)
90
+ }
91
+ stream._state.state = 2 //ended
92
+ stream._state.deps = {}
93
+ }
94
+
95
+ function map(fn) {return combine(function(stream) {return fn(stream())}, [this])}
96
+ function ap(stream) {return combine(function(s1, s2) {return s1()(s2())}, [stream, this])}
97
+ function valueOf() {return this._state.value}
98
+ function toJSON() {return this._state.value != null && typeof this._state.value.toJSON === "function" ? this._state.value.toJSON() : this._state.value}
99
+
100
+ function valid(stream) {return stream._state }
101
+ function active(stream) {return stream._state.state === 1}
102
+ function changed(stream) {return stream._state.changed}
103
+ function notEnded(stream) {return stream._state.state !== 2}
104
+
105
+ function merge(streams) {
106
+ return combine(function() {
107
+ return streams.map(function(s) {return s()})
108
+ }, streams)
109
+ }
110
+ createStream["fantasy-land/of"] = createStream
111
+ createStream.merge = merge
112
+ createStream.combine = combine
113
+ createStream.HALT = HALT
114
+
115
+ if (typeof module !== "undefined") module["exports"] = createStream
116
+ else window.stream = createStream
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mithril-pipeline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valery Mayatsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-16 00:00:00.000000000 Z
11
+ date: 2017-02-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Mithril.js for rails pipeline
14
14
  email: valerymayatsky@gmail.com
@@ -25,6 +25,7 @@ files:
25
25
  - lib/mithril-pipeline/version.rb
26
26
  - mithril-pipeline.gemspec
27
27
  - vendor/assets/javascripts/mithril.js
28
+ - vendor/assets/javascripts/mithril_stream.js
28
29
  homepage: https://github.com/Arugin/mithril-pipeline
29
30
  licenses:
30
31
  - MIT