@quenty/counter 1.0.1-canary.417.052e8ba.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## 1.0.1-canary.417.052e8ba.0 (2023-10-11)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * Add cleanup ([4d4be82](https://github.com/Quenty/NevermoreEngine/commit/4d4be825a9ba20ade8c7d0f4eea4c0029bbbb881))
12
+
13
+
14
+ ### Features
15
+
16
+ * Add Counter package ([0bb0d7c](https://github.com/Quenty/NevermoreEngine/commit/0bb0d7c891e46d588f4a4b180aa2ee2f9eed987c))
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2014-2023 James Onnen (Quenty)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ ## Counter
2
+
3
+ <div align="center">
4
+ <a href="http://quenty.github.io/NevermoreEngine/">
5
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
6
+ </a>
7
+ <a href="https://discord.gg/mhtGUS8">
8
+ <img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
9
+ </a>
10
+ <a href="https://github.com/Quenty/NevermoreEngine/actions">
11
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
12
+ </a>
13
+ </div>
14
+
15
+ Helps count a total value
16
+
17
+ <div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/CounterUtils">View docs →</a></div>
18
+
19
+ ## Installation
20
+
21
+ ```
22
+ npm install @quenty/counter --save
23
+ ```
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "counter",
3
+ "tree": {
4
+ "$path": "src"
5
+ }
6
+ }
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@quenty/counter",
3
+ "version": "1.0.1-canary.417.052e8ba.0",
4
+ "description": "Helps count a total value",
5
+ "keywords": [
6
+ "Roblox",
7
+ "Nevermore",
8
+ "Lua",
9
+ "counter"
10
+ ],
11
+ "bugs": {
12
+ "url": "https://github.com/Quenty/NevermoreEngine/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/Quenty/NevermoreEngine.git",
17
+ "directory": "src/counter/"
18
+ },
19
+ "funding": {
20
+ "type": "patreon",
21
+ "url": "https://www.patreon.com/quenty"
22
+ },
23
+ "license": "MIT",
24
+ "contributors": [
25
+ "Quenty"
26
+ ],
27
+ "dependencies": {
28
+ "@quenty/loader": "6.3.1-canary.417.052e8ba.0",
29
+ "@quenty/maid": "2.6.0",
30
+ "@quenty/rx": "7.16.1-canary.417.052e8ba.0",
31
+ "@quenty/valueobject": "7.24.1-canary.417.052e8ba.0"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "gitHead": "052e8ba33437f71398c4c08c8dbcbf076022d388"
37
+ }
@@ -0,0 +1,105 @@
1
+ --[=[
2
+ @class Counter
3
+ ]=]
4
+
5
+ local require = require(script.Parent.loader).load(script)
6
+
7
+ local BaseObject = require("BaseObject")
8
+ local Maid = require("Maid")
9
+ local Observable = require("Observable")
10
+ local ValueObject = require("ValueObject")
11
+
12
+ local Counter = setmetatable({}, BaseObject)
13
+ Counter.ClassName = "Counter"
14
+ Counter.__index = Counter
15
+
16
+ --[=[
17
+ Creates a new counter
18
+
19
+ @return Counter
20
+ ]=]
21
+ function Counter.new()
22
+ local self = setmetatable(BaseObject.new(), Counter)
23
+
24
+ self._count = self._maid:Add(ValueObject.new(0, "number"))
25
+
26
+ self.Changed = assert(self._count.Changed, "Bad .Changed")
27
+
28
+ return self
29
+ end
30
+
31
+
32
+ --[=[
33
+ Returns the current count
34
+
35
+ @return number
36
+ ]=]
37
+ function Counter:GetValue()
38
+ return self._count.Value
39
+ end
40
+
41
+ --[=[
42
+ Observes the current count
43
+
44
+ @return number
45
+ ]=]
46
+ function Counter:Observe()
47
+ return self._count:Observe()
48
+ end
49
+
50
+
51
+ --[=[
52
+ Adds an amount to the counter.
53
+
54
+ @param amount number | Observable<number>
55
+ @return MaidTask
56
+ ]=]
57
+ function Counter:Add(amount)
58
+ if type(amount) == "number" then
59
+ self._count.Value = self._count.Value + amount
60
+
61
+ return function()
62
+ self._count.Value = self._count.Value - amount
63
+ end
64
+ elseif Observable.isObservable(amount) then
65
+ return self:_addObservable(amount)
66
+ else
67
+ error("Bad amount")
68
+ end
69
+ end
70
+
71
+ function Counter:_addObservable(observeAmount)
72
+ assert(Observable.isObservable(observeAmount), "Bad observeAmount")
73
+
74
+ local lastCount = 0
75
+
76
+ local maid = Maid.new()
77
+ maid:GiveTask(observeAmount:Subscribe(function(count)
78
+ assert(type(count) == "number", "Bad count")
79
+ local delta = count - lastCount
80
+ lastCount = count
81
+
82
+ self._count.Value = self._count.Value + delta
83
+ end))
84
+
85
+ maid:GiveTask(function()
86
+ if not self._count.Destroy then
87
+ return
88
+ end
89
+
90
+ local delta = lastCount
91
+ lastCount = 0
92
+ self._count.Value = self._count.Value - delta
93
+ end)
94
+
95
+ self._maid[maid] = maid
96
+ maid:GiveTask(function()
97
+ self._maid[maid] = nil
98
+ end)
99
+
100
+ return function()
101
+ self._maid[maid] = nil
102
+ end
103
+ end
104
+
105
+ return Counter
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "node_modules",
3
+ "globIgnorePaths": [ "**/.package-lock.json" ],
4
+ "tree": {
5
+ "$path": { "optional": "../node_modules" }
6
+ }
7
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "CounterTest",
3
+ "tree": {
4
+ "$className": "DataModel",
5
+ "ServerScriptService": {
6
+ "counter": {
7
+ "$path": ".."
8
+ }
9
+ }
10
+ }
11
+ }